blob: ad60a49b2d9685aa58cf2921b320780e67b670b1 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Integer object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Barry Warsaw226ae6c1999-10-12 19:54:53 +00005#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum2e1d4331993-12-24 10:22:45 +00007long
Fred Drakea2f55112000-07-09 15:16:51 +00008PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +00009{
10 return LONG_MAX; /* To initialize sys.maxint */
11}
12
Guido van Rossumddefaf32007-01-14 03:31:43 +000013#if 0
Guido van Rossum3f5da241990-12-20 15:06:42 +000014/* Integers are quite normal objects, to make object handling uniform.
15 (Using odd pointers to represent integers would save much space
16 but require extra checks for this special case throughout the code.)
Tim Peters29c0afc2002-04-28 16:57:34 +000017 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000018 and deallocating integers, these operations should be very fast.
19 Therefore we use a dedicated allocation scheme with a much lower
20 overhead (in space and time) than straight malloc(): a simple
21 dedicated free list, filled when necessary with memory from malloc().
Tim Peters29c0afc2002-04-28 16:57:34 +000022
23 block_list is a singly-linked list of all PyIntBlocks ever allocated,
24 linked via their next members. PyIntBlocks are never returned to the
25 system before shutdown (PyInt_Fini).
26
27 free_list is a singly-linked list of available PyIntObjects, linked
28 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000029*/
30
31#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000032#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
33#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000034
Guido van Rossum3fce8831999-03-12 19:43:17 +000035struct _intblock {
36 struct _intblock *next;
37 PyIntObject objects[N_INTOBJECTS];
38};
39
40typedef struct _intblock PyIntBlock;
41
42static PyIntBlock *block_list = NULL;
43static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000044
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000046fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000047{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000049 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000050 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000051 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000052 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000053 ((PyIntBlock *)p)->next = block_list;
54 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000055 /* Link the int objects together, from rear to front, then return
56 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000057 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000058 q = p + N_INTOBJECTS;
59 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000060 q->ob_type = (struct _typeobject *)(q-1);
61 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000062 return p + N_INTOBJECTS - 1;
63}
64
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000065#ifndef NSMALLPOSINTS
Georg Brandl418a1ef2006-02-22 11:30:06 +000066#define NSMALLPOSINTS 257
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000067#endif
68#ifndef NSMALLNEGINTS
Neal Norwitzc91ed402002-12-30 22:29:22 +000069#define NSMALLNEGINTS 5
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000070#endif
71#if NSMALLNEGINTS + NSMALLPOSINTS > 0
72/* References to small integers are saved in this array so that they
73 can be shared.
74 The integers that are saved are those in the range
75 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
76*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000078#endif
79#ifdef COUNT_ALLOCS
80int quick_int_allocs, quick_neg_int_allocs;
81#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000082
Guido van Rossumc0b618a1997-05-02 03:12:38 +000083PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000084PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000087#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Neal Norwitzc91ed402002-12-30 22:29:22 +000088 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
89 v = small_ints[ival + NSMALLNEGINTS];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000091#ifdef COUNT_ALLOCS
92 if (ival >= 0)
93 quick_int_allocs++;
94 else
95 quick_neg_int_allocs++;
96#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000098 }
99#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100 if (free_list == NULL) {
101 if ((free_list = fill_free_list()) == NULL)
102 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000104 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000106 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000107 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110}
111
Martin v. Löwis18e16552006-02-15 17:27:45 +0000112PyObject *
113PyInt_FromSize_t(size_t ival)
114{
115 if (ival <= LONG_MAX)
116 return PyInt_FromLong((long)ival);
117 return _PyLong_FromSize_t(ival);
118}
119
120PyObject *
121PyInt_FromSsize_t(Py_ssize_t ival)
122{
123 if (ival >= LONG_MIN && ival <= LONG_MAX)
124 return PyInt_FromLong((long)ival);
125 return _PyLong_FromSsize_t(ival);
126}
127
Guido van Rossum3f5da241990-12-20 15:06:42 +0000128static void
Fred Drakea2f55112000-07-09 15:16:51 +0000129int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000130{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000131 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000132 v->ob_type = (struct _typeobject *)free_list;
133 free_list = v;
134 }
135 else
136 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137}
138
Guido van Rossum93646982002-04-26 00:53:34 +0000139static void
140int_free(PyIntObject *v)
141{
142 v->ob_type = (struct _typeobject *)free_list;
143 free_list = v;
144}
145
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146long
Fred Drakea2f55112000-07-09 15:16:51 +0000147PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 PyNumberMethods *nb;
150 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000151 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000152
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000153 if (op && PyInt_Check(op))
154 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000155
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000156 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
157 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000158 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159 return -1;
160 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000161
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000163 if (io == NULL)
164 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000166 if (PyLong_Check(io)) {
167 /* got a long? => retry int conversion */
168 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000169 Py_DECREF(io);
170 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000171 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000172 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000173 }
174 else
175 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000176 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000177 PyErr_SetString(PyExc_TypeError,
178 "nb_int should return int object");
179 return -1;
180 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000181 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000182
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 val = PyInt_AS_LONG(io);
184 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000185
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000186 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187}
188
Martin v. Löwis18e16552006-02-15 17:27:45 +0000189Py_ssize_t
190PyInt_AsSsize_t(register PyObject *op)
191{
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000192#if SIZEOF_SIZE_T != SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000193 PyNumberMethods *nb;
194 PyIntObject *io;
195 Py_ssize_t val;
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000196#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000197
198 if (op == NULL) {
199 PyErr_SetString(PyExc_TypeError, "an integer is required");
200 return -1;
201 }
202
203 if (PyInt_Check(op))
204 return PyInt_AS_LONG((PyIntObject*) op);
205 if (PyLong_Check(op))
Martin v. Löwis18e16552006-02-15 17:27:45 +0000206 return _PyLong_AsSsize_t(op);
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000207#if SIZEOF_SIZE_T == SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000208 return PyInt_AsLong(op);
209#else
210
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000211 if ((nb = op->ob_type->tp_as_number) == NULL ||
Martin v. Löwis18e16552006-02-15 17:27:45 +0000212 (nb->nb_int == NULL && nb->nb_long == 0)) {
213 PyErr_SetString(PyExc_TypeError, "an integer is required");
214 return -1;
215 }
216
Guido van Rossum360e4b82007-05-14 22:51:27 +0000217 if (nb->nb_long != 0)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000218 io = (PyIntObject*) (*nb->nb_long) (op);
Guido van Rossum360e4b82007-05-14 22:51:27 +0000219 else
Martin v. Löwis18e16552006-02-15 17:27:45 +0000220 io = (PyIntObject*) (*nb->nb_int) (op);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000221 if (io == NULL)
222 return -1;
223 if (!PyInt_Check(io)) {
224 if (PyLong_Check(io)) {
225 /* got a long? => retry int conversion */
226 val = _PyLong_AsSsize_t((PyObject *)io);
227 Py_DECREF(io);
228 if ((val == -1) && PyErr_Occurred())
229 return -1;
230 return val;
231 }
232 else
233 {
234 Py_DECREF(io);
235 PyErr_SetString(PyExc_TypeError,
236 "nb_int should return int object");
237 return -1;
238 }
239 }
240
241 val = PyInt_AS_LONG(io);
242 Py_DECREF(io);
243
244 return val;
245#endif
246}
247
Thomas Hellera4ea6032003-04-17 18:55:45 +0000248unsigned long
249PyInt_AsUnsignedLongMask(register PyObject *op)
250{
251 PyNumberMethods *nb;
252 PyIntObject *io;
253 unsigned long val;
254
255 if (op && PyInt_Check(op))
256 return PyInt_AS_LONG((PyIntObject*) op);
257 if (op && PyLong_Check(op))
258 return PyLong_AsUnsignedLongMask(op);
259
260 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
261 nb->nb_int == NULL) {
262 PyErr_SetString(PyExc_TypeError, "an integer is required");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000263 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000264 }
265
266 io = (PyIntObject*) (*nb->nb_int) (op);
267 if (io == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000268 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000269 if (!PyInt_Check(io)) {
270 if (PyLong_Check(io)) {
271 val = PyLong_AsUnsignedLongMask((PyObject *)io);
272 Py_DECREF(io);
273 if (PyErr_Occurred())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000275 return val;
276 }
277 else
278 {
279 Py_DECREF(io);
280 PyErr_SetString(PyExc_TypeError,
281 "nb_int should return int object");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000282 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000283 }
284 }
285
286 val = PyInt_AS_LONG(io);
287 Py_DECREF(io);
288
289 return val;
290}
291
292#ifdef HAVE_LONG_LONG
293unsigned PY_LONG_LONG
294PyInt_AsUnsignedLongLongMask(register PyObject *op)
295{
296 PyNumberMethods *nb;
297 PyIntObject *io;
298 unsigned PY_LONG_LONG val;
299
300 if (op && PyInt_Check(op))
301 return PyInt_AS_LONG((PyIntObject*) op);
302 if (op && PyLong_Check(op))
303 return PyLong_AsUnsignedLongLongMask(op);
304
305 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
306 nb->nb_int == NULL) {
307 PyErr_SetString(PyExc_TypeError, "an integer is required");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000308 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000309 }
310
311 io = (PyIntObject*) (*nb->nb_int) (op);
312 if (io == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000314 if (!PyInt_Check(io)) {
315 if (PyLong_Check(io)) {
316 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
317 Py_DECREF(io);
318 if (PyErr_Occurred())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000320 return val;
321 }
322 else
323 {
324 Py_DECREF(io);
325 PyErr_SetString(PyExc_TypeError,
326 "nb_int should return int object");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000328 }
329 }
330
331 val = PyInt_AS_LONG(io);
332 Py_DECREF(io);
333
334 return val;
335}
336#endif
337
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000338PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000339PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000340{
341 char *end;
342 long x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343 Py_ssize_t slen;
344 PyObject *sobj, *srepr;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000345
346 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000347 PyErr_SetString(PyExc_ValueError,
348 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000349 return NULL;
350 }
351
352 while (*s && isspace(Py_CHARMASK(*s)))
353 s++;
354 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000355 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000356 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000357 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000358 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000359 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000360 else
361 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000362 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000363 goto bad;
364 while (*end && isspace(Py_CHARMASK(*end)))
365 end++;
366 if (*end != '\0') {
367 bad:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 slen = strlen(s) < 200 ? strlen(s) : 200;
369 sobj = PyString_FromStringAndSize(s, slen);
370 if (sobj == NULL)
371 return NULL;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000372 srepr = PyObject_ReprStr8(sobj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000373 Py_DECREF(sobj);
374 if (srepr == NULL)
375 return NULL;
376 PyErr_Format(PyExc_ValueError,
377 "invalid literal for int() with base %d: %s",
378 base, PyString_AS_STRING(srepr));
379 Py_DECREF(srepr);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000380 return NULL;
381 }
Tim Petersc8854432004-08-25 02:14:08 +0000382 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000383 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000384 if (pend)
385 *pend = end;
386 return PyInt_FromLong(x);
387}
388
Guido van Rossum9e896b32000-04-05 20:11:21 +0000389PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000390PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000391{
Walter Dörwald07e14762002-11-06 16:15:14 +0000392 PyObject *result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000393 char *buffer = (char *)PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000394
Walter Dörwald07e14762002-11-06 16:15:14 +0000395 if (buffer == NULL)
Guido van Rossuma28c2912007-05-15 20:39:12 +0000396 return PyErr_NoMemory();
Walter Dörwald07e14762002-11-06 16:15:14 +0000397
398 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
399 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000400 return NULL;
401 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000402 result = PyInt_FromString(buffer, NULL, base);
403 PyMem_FREE(buffer);
404 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000405}
406
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407/* Methods */
408
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000409/* Integers are seen as the "smallest" of all numeric types and thus
410 don't have any knowledge about conversion of other types to
411 integers. */
412
413#define CONVERT_TO_LONG(obj, lng) \
414 if (PyInt_Check(obj)) { \
415 lng = PyInt_AS_LONG(obj); \
416 } \
417 else { \
418 Py_INCREF(Py_NotImplemented); \
419 return Py_NotImplemented; \
420 }
421
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000422/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000423static int
Fred Drakea2f55112000-07-09 15:16:51 +0000424int_print(PyIntObject *v, FILE *fp, int flags)
425 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426{
427 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000428 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429}
430
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000432int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000433{
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000434 return PyUnicode_FromFormat("%ld", v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435}
436
437static int
Fred Drakea2f55112000-07-09 15:16:51 +0000438int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439{
440 register long i = v->ob_ival;
441 register long j = w->ob_ival;
442 return (i < j) ? -1 : (i > j) ? 1 : 0;
443}
444
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000445static PyObject *
446int_richcompare(PyObject *self, PyObject *other, int op)
447{
448 if (!PyInt_Check(self) || !PyInt_Check(other)) {
449 Py_INCREF(Py_NotImplemented);
450 return Py_NotImplemented;
451 }
452 return Py_CmpToRich(op, int_compare((PyIntObject *)self,
453 (PyIntObject *)other));
454}
455
Guido van Rossum9bfef441993-03-29 10:43:31 +0000456static long
Fred Drakea2f55112000-07-09 15:16:51 +0000457int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000458{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000459 /* XXX If this is changed, you also need to change the way
460 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000461 long x = v -> ob_ival;
462 if (x == -1)
463 x = -2;
464 return x;
465}
466
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000467static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000468int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469{
470 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000471 CONVERT_TO_LONG(v, a);
472 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000474 if ((x^a) >= 0 || (x^b) >= 0)
475 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000476 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477}
478
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000479static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000480int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481{
482 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000483 CONVERT_TO_LONG(v, a);
484 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000486 if ((x^a) >= 0 || (x^~b) >= 0)
487 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000488 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
489 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490}
491
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000492/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000493Integer overflow checking for * is painful: Python tried a couple ways, but
494they didn't work on all platforms, or failed in endcases (a product of
495-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000496
Tim Petersa3c01ce2001-12-04 23:05:10 +0000497Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000498
Tim Petersa3c01ce2001-12-04 23:05:10 +0000499The native long product x*y is either exactly right or *way* off, being
500just the last n bits of the true product, where n is the number of bits
501in a long (the delivered product is the true product plus i*2**n for
502some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000503
Tim Petersa3c01ce2001-12-04 23:05:10 +0000504The native double product (double)x * (double)y is subject to three
505rounding errors: on a sizeof(long)==8 box, each cast to double can lose
506info, and even on a sizeof(long)==4 box, the multiplication can lose info.
507But, unlike the native long product, it's not in *range* trouble: even
508if sizeof(long)==32 (256-bit longs), the product easily fits in the
509dynamic range of a double. So the leading 50 (or so) bits of the double
510product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000511
Tim Petersa3c01ce2001-12-04 23:05:10 +0000512We check these two ways against each other, and declare victory if they're
513approximately the same. Else, because the native long product is the only
514one that can lose catastrophic amounts of information, it's the native long
515product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000516*/
517
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000518static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000519int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000521 long a, b;
522 long longprod; /* a*b in native long arithmetic */
523 double doubled_longprod; /* (double)longprod */
524 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000525
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000526 CONVERT_TO_LONG(v, a);
527 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000528 longprod = a * b;
529 doubleprod = (double)a * (double)b;
530 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000531
Tim Petersa3c01ce2001-12-04 23:05:10 +0000532 /* Fast path for normal case: small multiplicands, and no info
533 is lost in either method. */
534 if (doubled_longprod == doubleprod)
535 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000536
Tim Petersa3c01ce2001-12-04 23:05:10 +0000537 /* Somebody somewhere lost info. Close enough, or way off? Note
538 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
539 The difference either is or isn't significant compared to the
540 true value (of which doubleprod is a good approximation).
541 */
542 {
543 const double diff = doubled_longprod - doubleprod;
544 const double absdiff = diff >= 0.0 ? diff : -diff;
545 const double absprod = doubleprod >= 0.0 ? doubleprod :
546 -doubleprod;
547 /* absdiff/absprod <= 1/32 iff
548 32 * absdiff <= absprod -- 5 good bits is "close enough" */
549 if (32.0 * absdiff <= absprod)
550 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000551 else
552 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000553 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554}
555
Thomas Wouters89f507f2006-12-13 04:49:30 +0000556/* Integer overflow checking for unary negation: on a 2's-complement
557 * box, -x overflows iff x is the most negative long. In this case we
558 * get -x == x. However, -x is undefined (by C) if x /is/ the most
559 * negative long (it's a signed overflow case), and some compilers care.
560 * So we cast x to unsigned long first. However, then other compilers
561 * warn about applying unary minus to an unsigned operand. Hence the
562 * weird "0-".
563 */
564#define UNARY_NEG_WOULD_OVERFLOW(x) \
565 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
566
Guido van Rossume27f7952001-08-23 02:59:04 +0000567/* Return type of i_divmod */
568enum divmod_result {
569 DIVMOD_OK, /* Correct result */
570 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
571 DIVMOD_ERROR /* Exception raised */
572};
573
574static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000575i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000576 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000577{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000578 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000579
Tim Peters1dad6a82001-06-18 19:21:11 +0000580 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000582 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000583 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000585 /* (-sys.maxint-1)/-1 is the only overflow case. */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000586 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
Guido van Rossume27f7952001-08-23 02:59:04 +0000587 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000588 xdivy = x / y;
589 xmody = x - xdivy * y;
590 /* If the signs of x and y differ, and the remainder is non-0,
591 * C89 doesn't define whether xdivy is now the floor or the
592 * ceiling of the infinitely precise quotient. We want the floor,
593 * and we have it iff the remainder's sign matches y's.
594 */
595 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
596 xmody += y;
597 --xdivy;
598 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000599 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000600 *p_xdivy = xdivy;
601 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000602 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000603}
604
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605static PyObject *
Neal Norwitz4886cc32006-08-21 17:06:07 +0000606int_floor_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000607{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000608 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000609 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000610 CONVERT_TO_LONG(x, xi);
611 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000612 switch (i_divmod(xi, yi, &d, &m)) {
613 case DIVMOD_OK:
614 return PyInt_FromLong(d);
615 case DIVMOD_OVERFLOW:
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000616 return PyLong_Type.tp_as_number->nb_floor_divide((PyObject *)x,
617 (PyObject *)y);
Guido van Rossum393661d2001-08-31 17:40:15 +0000618 default:
619 return NULL;
620 }
621}
622
623static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000624int_true_divide(PyObject *v, PyObject *w)
625{
Tim Peterse2a60002001-09-04 06:17:36 +0000626 /* If they aren't both ints, give someone else a chance. In
627 particular, this lets int/long get handled by longs, which
628 underflows to 0 gracefully if the long is too big to convert
629 to float. */
630 if (PyInt_Check(v) && PyInt_Check(w))
631 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
632 Py_INCREF(Py_NotImplemented);
633 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000634}
635
636static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000637int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000638{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000639 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000640 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000641 CONVERT_TO_LONG(x, xi);
642 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000643 switch (i_divmod(xi, yi, &d, &m)) {
644 case DIVMOD_OK:
645 return PyInt_FromLong(m);
646 case DIVMOD_OVERFLOW:
647 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
648 (PyObject *)y);
649 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000650 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000651 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000652}
653
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000655int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000656{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000657 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000658 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000659 CONVERT_TO_LONG(x, xi);
660 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000661 switch (i_divmod(xi, yi, &d, &m)) {
662 case DIVMOD_OK:
663 return Py_BuildValue("(ll)", d, m);
664 case DIVMOD_OVERFLOW:
665 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
666 (PyObject *)y);
667 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000668 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000669 }
Guido van Rossum00466951991-05-05 20:08:27 +0000670}
671
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000673int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000675 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000676 CONVERT_TO_LONG(v, iv);
677 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000678 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000679 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000680 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
681 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000682 return NULL;
683 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000684 /* Return a float. This works because we know that
685 this calls float_pow() which converts its
686 arguments to double. */
687 return PyFloat_Type.tp_as_number->nb_power(
688 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000689 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000691 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000692 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000694 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000695 return NULL;
696 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000697 }
698 /*
699 * XXX: The original exponentiation code stopped looping
700 * when temp hit zero; this code will continue onwards
701 * unnecessarily, but at least it won't cause any errors.
702 * Hopefully the speed improvement from the fast exponentiation
703 * will compensate for the slight inefficiency.
704 * XXX: Better handling of overflows is desperately needed.
705 */
706 temp = iv;
707 ix = 1;
708 while (iw > 0) {
709 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000710 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000711 ix = ix*temp;
712 if (temp == 0)
713 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000714 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000715 return PyLong_Type.tp_as_number->nb_power(
716 (PyObject *)v,
717 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000718 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000719 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000720 }
721 iw >>= 1; /* Shift exponent down by 1 bit */
722 if (iw==0) break;
723 prev = temp;
724 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000725 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000726 return PyLong_Type.tp_as_number->nb_power(
727 (PyObject *)v, (PyObject *)w, (PyObject *)z);
728 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000729 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000730 /* If we did a multiplication, perform a modulo */
731 ix = ix % iz;
732 temp = temp % iz;
733 }
734 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000735 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000736 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000737 switch (i_divmod(ix, iz, &div, &mod)) {
738 case DIVMOD_OK:
739 ix = mod;
740 break;
741 case DIVMOD_OVERFLOW:
742 return PyLong_Type.tp_as_number->nb_power(
743 (PyObject *)v, (PyObject *)w, (PyObject *)z);
744 default:
745 return NULL;
746 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000747 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000749}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000750
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000752int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000754 register long a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755 a = v->ob_ival;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000756 /* check for overflow */
757 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
Tim Petersc8854432004-08-25 02:14:08 +0000758 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000759 if (o != NULL) {
760 PyObject *result = PyNumber_Negative(o);
761 Py_DECREF(o);
762 return result;
763 }
764 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000765 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000766 return PyInt_FromLong(-a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000767}
768
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000769static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000770int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000772 if (PyInt_CheckExact(v)) {
773 Py_INCREF(v);
774 return (PyObject *)v;
775 }
776 else
777 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778}
779
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000780static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000781int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000782{
783 if (v->ob_ival >= 0)
784 return int_pos(v);
785 else
786 return int_neg(v);
787}
788
Guido van Rossum0bff0151991-05-14 12:05:32 +0000789static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000790int_bool(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000791{
792 return v->ob_ival != 0;
793}
794
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000796int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000797{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000799}
800
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000801static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000802int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000803{
Guido van Rossum078151d2002-08-11 04:24:12 +0000804 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000805 PyObject *vv, *ww, *result;
806
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000807 CONVERT_TO_LONG(v, a);
808 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000809 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000810 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000811 return NULL;
812 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000813 if (a == 0 || b == 0)
814 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000815 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000816 vv = PyLong_FromLong(PyInt_AS_LONG(v));
817 if (vv == NULL)
818 return NULL;
819 ww = PyLong_FromLong(PyInt_AS_LONG(w));
820 if (ww == NULL) {
821 Py_DECREF(vv);
822 return NULL;
823 }
824 result = PyNumber_Lshift(vv, ww);
825 Py_DECREF(vv);
826 Py_DECREF(ww);
827 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000828 }
Tim Petersda1a2212002-08-11 17:54:42 +0000829 c = a << b;
830 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000831 vv = PyLong_FromLong(PyInt_AS_LONG(v));
832 if (vv == NULL)
833 return NULL;
834 ww = PyLong_FromLong(PyInt_AS_LONG(w));
835 if (ww == NULL) {
836 Py_DECREF(vv);
837 return NULL;
838 }
839 result = PyNumber_Lshift(vv, ww);
840 Py_DECREF(vv);
841 Py_DECREF(ww);
842 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000843 }
844 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000845}
846
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000848int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000849{
850 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000851 CONVERT_TO_LONG(v, a);
852 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000853 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000854 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000855 return NULL;
856 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000857 if (a == 0 || b == 0)
858 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000859 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000860 if (a < 0)
861 a = -1;
862 else
863 a = 0;
864 }
865 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000866 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000867 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000869}
870
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000872int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000873{
874 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000875 CONVERT_TO_LONG(v, a);
876 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000877 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000878}
879
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000880static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000881int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000882{
883 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000884 CONVERT_TO_LONG(v, a);
885 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000886 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000887}
888
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000889static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000890int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000891{
892 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000893 CONVERT_TO_LONG(v, a);
894 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000895 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000896}
897
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000899int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000900{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000901 if (PyInt_CheckExact(v))
902 Py_INCREF(v);
903 else
904 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000905 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000906}
907
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000909int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000910{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000911 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000912}
913
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000914static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000915int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000916{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000917 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000918}
919
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000920static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000921int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000922{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000923 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000924 if (x < 0)
Walter Dörwald233ccf22007-06-05 19:50:53 +0000925 return PyUnicode_FromFormat("-0%lo", -x);
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000926 else if (x == 0)
Walter Dörwald233ccf22007-06-05 19:50:53 +0000927 return PyUnicode_FromString("0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000928 else
Walter Dörwald233ccf22007-06-05 19:50:53 +0000929 return PyUnicode_FromFormat("0%lo", x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000930}
931
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000932static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000933int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000934{
Guido van Rossum9bfef441993-03-29 10:43:31 +0000935 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000936 if (x < 0)
Walter Dörwald233ccf22007-06-05 19:50:53 +0000937 return PyUnicode_FromFormat("-0x%lx", -x);
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000938 else
Walter Dörwald233ccf22007-06-05 19:50:53 +0000939 return PyUnicode_FromFormat("0x%lx", x);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000940}
941
Jeremy Hylton938ace62002-07-17 16:30:39 +0000942static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000943int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
944
Tim Peters6d6c1a32001-08-02 04:15:00 +0000945static PyObject *
946int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
947{
948 PyObject *x = NULL;
949 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000950 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000951
Guido van Rossumbef14172001-08-29 15:47:46 +0000952 if (type != &PyInt_Type)
953 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000954 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
955 &x, &base))
956 return NULL;
957 if (x == NULL)
958 return PyInt_FromLong(0L);
959 if (base == -909)
960 return PyNumber_Int(x);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000961 if (PyString_Check(x)) {
962 /* Since PyInt_FromString doesn't have a length parameter,
963 * check here for possible NULs in the string. */
964 char *string = PyString_AS_STRING(x);
965 if (strlen(string) != PyString_Size(x)) {
966 /* create a repr() of the input string,
967 * just like PyInt_FromString does */
968 PyObject *srepr;
Walter Dörwald1ab83302007-05-18 17:15:44 +0000969 srepr = PyObject_ReprStr8(x);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000970 if (srepr == NULL)
971 return NULL;
972 PyErr_Format(PyExc_ValueError,
973 "invalid literal for int() with base %d: %s",
974 base, PyString_AS_STRING(srepr));
975 Py_DECREF(srepr);
976 return NULL;
977 }
978 return PyInt_FromString(string, NULL, base);
979 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000980 if (PyUnicode_Check(x))
981 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
982 PyUnicode_GET_SIZE(x),
983 base);
984 PyErr_SetString(PyExc_TypeError,
985 "int() can't convert non-string with explicit base");
986 return NULL;
987}
988
Guido van Rossumbef14172001-08-29 15:47:46 +0000989/* Wimpy, slow approach to tp_new calls for subtypes of int:
990 first create a regular int from whatever arguments we got,
991 then allocate a subtype instance and initialize its ob_ival
992 from the regular int. The regular int is then thrown away.
993*/
994static PyObject *
995int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
996{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000997 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000998 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000999
1000 assert(PyType_IsSubtype(type, &PyInt_Type));
1001 tmp = int_new(&PyInt_Type, args, kwds);
1002 if (tmp == NULL)
1003 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001004 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001005 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001006 if (ival == -1 && PyErr_Occurred()) {
1007 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001008 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001009 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001010 } else {
1011 ival = ((PyIntObject *)tmp)->ob_ival;
1012 }
1013
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001014 newobj = type->tp_alloc(type, 0);
1015 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001016 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001017 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001018 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001019 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001020 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001021 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001022}
1023
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001024static PyObject *
1025int_getnewargs(PyIntObject *v)
1026{
1027 return Py_BuildValue("(l)", v->ob_ival);
1028}
1029
1030static PyMethodDef int_methods[] = {
1031 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
1032 {NULL, NULL} /* sentinel */
1033};
1034
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001035PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001036"int(x[, base]) -> integer\n\
1037\n\
1038Convert a string or number to an integer, if possible. A floating point\n\
1039argument will be truncated towards zero (this does not include a string\n\
1040representation of a floating point number!) When converting a string, use\n\
1041the optional base. It is an error to supply a base when converting a\n\
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001042non-string. If base is zero, the proper base is guessed based on the\n\
1043string content. If the argument is outside the integer range a\n\
1044long object will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001045
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001046static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001047 (binaryfunc)int_add, /*nb_add*/
1048 (binaryfunc)int_sub, /*nb_subtract*/
1049 (binaryfunc)int_mul, /*nb_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001050 (binaryfunc)int_mod, /*nb_remainder*/
1051 (binaryfunc)int_divmod, /*nb_divmod*/
1052 (ternaryfunc)int_pow, /*nb_power*/
1053 (unaryfunc)int_neg, /*nb_negative*/
1054 (unaryfunc)int_pos, /*nb_positive*/
1055 (unaryfunc)int_abs, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001056 (inquiry)int_bool, /*nb_bool*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001057 (unaryfunc)int_invert, /*nb_invert*/
1058 (binaryfunc)int_lshift, /*nb_lshift*/
1059 (binaryfunc)int_rshift, /*nb_rshift*/
1060 (binaryfunc)int_and, /*nb_and*/
1061 (binaryfunc)int_xor, /*nb_xor*/
1062 (binaryfunc)int_or, /*nb_or*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001063 0, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001064 (unaryfunc)int_int, /*nb_int*/
1065 (unaryfunc)int_long, /*nb_long*/
1066 (unaryfunc)int_float, /*nb_float*/
1067 (unaryfunc)int_oct, /*nb_oct*/
1068 (unaryfunc)int_hex, /*nb_hex*/
1069 0, /*nb_inplace_add*/
1070 0, /*nb_inplace_subtract*/
1071 0, /*nb_inplace_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001072 0, /*nb_inplace_remainder*/
1073 0, /*nb_inplace_power*/
1074 0, /*nb_inplace_lshift*/
1075 0, /*nb_inplace_rshift*/
1076 0, /*nb_inplace_and*/
1077 0, /*nb_inplace_xor*/
1078 0, /*nb_inplace_or*/
Neal Norwitz4886cc32006-08-21 17:06:07 +00001079 (binaryfunc)int_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00001080 int_true_divide, /* nb_true_divide */
1081 0, /* nb_inplace_floor_divide */
1082 0, /* nb_inplace_true_divide */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001083 (unaryfunc)int_int, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001084};
1085
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001086PyTypeObject PyInt_Type = {
1087 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001088 0,
1089 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001090 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001091 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001092 (destructor)int_dealloc, /* tp_dealloc */
1093 (printfunc)int_print, /* tp_print */
1094 0, /* tp_getattr */
1095 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001096 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001097 (reprfunc)int_repr, /* tp_repr */
1098 &int_as_number, /* tp_as_number */
1099 0, /* tp_as_sequence */
1100 0, /* tp_as_mapping */
1101 (hashfunc)int_hash, /* tp_hash */
1102 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001103 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001104 PyObject_GenericGetAttr, /* tp_getattro */
1105 0, /* tp_setattro */
1106 0, /* tp_as_buffer */
Thomas Wouters27d517b2007-02-25 20:39:11 +00001107 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
1108 Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001109 int_doc, /* tp_doc */
1110 0, /* tp_traverse */
1111 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001112 int_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001113 0, /* tp_weaklistoffset */
1114 0, /* tp_iter */
1115 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001116 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001117 0, /* tp_members */
1118 0, /* tp_getset */
1119 0, /* tp_base */
1120 0, /* tp_dict */
1121 0, /* tp_descr_get */
1122 0, /* tp_descr_set */
1123 0, /* tp_dictoffset */
1124 0, /* tp_init */
1125 0, /* tp_alloc */
1126 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001127 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001128};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001129
Neal Norwitzc91ed402002-12-30 22:29:22 +00001130int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001131_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001132{
1133 PyIntObject *v;
1134 int ival;
1135#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1136 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001137 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001138 return 0;
1139 /* PyObject_New is inlined */
1140 v = free_list;
1141 free_list = (PyIntObject *)v->ob_type;
1142 PyObject_INIT(v, &PyInt_Type);
1143 v->ob_ival = ival;
1144 small_ints[ival + NSMALLNEGINTS] = v;
1145 }
1146#endif
1147 return 1;
1148}
1149
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001150void
Fred Drakea2f55112000-07-09 15:16:51 +00001151PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001152{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001153 PyIntObject *p;
1154 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001155 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001156 unsigned int ctr;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001157 int bc, bf; /* block count, number of freed blocks */
1158 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001159
Guido van Rossumda084ed1999-03-10 22:55:24 +00001160#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1161 PyIntObject **q;
1162
1163 i = NSMALLNEGINTS + NSMALLPOSINTS;
1164 q = small_ints;
1165 while (--i >= 0) {
1166 Py_XDECREF(*q);
1167 *q++ = NULL;
1168 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001169#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001170 bc = 0;
1171 bf = 0;
1172 isum = 0;
1173 list = block_list;
1174 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001175 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001176 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001177 bc++;
1178 irem = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001179 for (ctr = 0, p = &list->objects[0];
1180 ctr < N_INTOBJECTS;
1181 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001182 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001183 irem++;
1184 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001185 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001186 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001187 list->next = block_list;
1188 block_list = list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001189 for (ctr = 0, p = &list->objects[0];
1190 ctr < N_INTOBJECTS;
1191 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001192 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001193 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001194 p->ob_type = (struct _typeobject *)
1195 free_list;
1196 free_list = p;
1197 }
1198#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1199 else if (-NSMALLNEGINTS <= p->ob_ival &&
1200 p->ob_ival < NSMALLPOSINTS &&
1201 small_ints[p->ob_ival +
1202 NSMALLNEGINTS] == NULL) {
1203 Py_INCREF(p);
1204 small_ints[p->ob_ival +
1205 NSMALLNEGINTS] = p;
1206 }
1207#endif
1208 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001209 }
1210 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001211 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001212 bf++;
1213 }
1214 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001215 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001216 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001217 if (!Py_VerboseFlag)
1218 return;
1219 fprintf(stderr, "# cleanup ints");
1220 if (!isum) {
1221 fprintf(stderr, "\n");
1222 }
1223 else {
1224 fprintf(stderr,
1225 ": %d unfreed int%s in %d out of %d block%s\n",
1226 isum, isum == 1 ? "" : "s",
1227 bc - bf, bc, bc == 1 ? "" : "s");
1228 }
1229 if (Py_VerboseFlag > 1) {
1230 list = block_list;
1231 while (list != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001232 for (ctr = 0, p = &list->objects[0];
1233 ctr < N_INTOBJECTS;
1234 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001235 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001236 /* XXX(twouters) cast refcount to
1237 long until %zd is universally
1238 available
1239 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001240 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001241 "# <int at %p, refcnt=%ld, val=%ld>\n",
1242 p, (long)p->ob_refcnt,
1243 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001244 }
1245 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001246 }
1247 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001248}
Guido van Rossumddefaf32007-01-14 03:31:43 +00001249#endif /* if 0 */