blob: 7f56acff5d220e177a8f1321e6f5daa49e3da6d9 [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;
372 srepr = PyObject_Repr(sobj);
373 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)
396 return NULL;
397
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{
Tim Peters42221042001-12-01 02:52:56 +0000434 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000435 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000436 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
439static int
Fred Drakea2f55112000-07-09 15:16:51 +0000440int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
442 register long i = v->ob_ival;
443 register long j = w->ob_ival;
444 return (i < j) ? -1 : (i > j) ? 1 : 0;
445}
446
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000447static PyObject *
448int_richcompare(PyObject *self, PyObject *other, int op)
449{
450 if (!PyInt_Check(self) || !PyInt_Check(other)) {
451 Py_INCREF(Py_NotImplemented);
452 return Py_NotImplemented;
453 }
454 return Py_CmpToRich(op, int_compare((PyIntObject *)self,
455 (PyIntObject *)other));
456}
457
Guido van Rossum9bfef441993-03-29 10:43:31 +0000458static long
Fred Drakea2f55112000-07-09 15:16:51 +0000459int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000460{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000461 /* XXX If this is changed, you also need to change the way
462 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000463 long x = v -> ob_ival;
464 if (x == -1)
465 x = -2;
466 return x;
467}
468
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000470int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471{
472 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000473 CONVERT_TO_LONG(v, a);
474 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000475 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000476 if ((x^a) >= 0 || (x^b) >= 0)
477 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000478 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000479}
480
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000482int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483{
484 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000485 CONVERT_TO_LONG(v, a);
486 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000488 if ((x^a) >= 0 || (x^~b) >= 0)
489 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000490 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
491 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000492}
493
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000494/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000495Integer overflow checking for * is painful: Python tried a couple ways, but
496they didn't work on all platforms, or failed in endcases (a product of
497-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000498
Tim Petersa3c01ce2001-12-04 23:05:10 +0000499Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000500
Tim Petersa3c01ce2001-12-04 23:05:10 +0000501The native long product x*y is either exactly right or *way* off, being
502just the last n bits of the true product, where n is the number of bits
503in a long (the delivered product is the true product plus i*2**n for
504some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000505
Tim Petersa3c01ce2001-12-04 23:05:10 +0000506The native double product (double)x * (double)y is subject to three
507rounding errors: on a sizeof(long)==8 box, each cast to double can lose
508info, and even on a sizeof(long)==4 box, the multiplication can lose info.
509But, unlike the native long product, it's not in *range* trouble: even
510if sizeof(long)==32 (256-bit longs), the product easily fits in the
511dynamic range of a double. So the leading 50 (or so) bits of the double
512product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000513
Tim Petersa3c01ce2001-12-04 23:05:10 +0000514We check these two ways against each other, and declare victory if they're
515approximately the same. Else, because the native long product is the only
516one that can lose catastrophic amounts of information, it's the native long
517product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000518*/
519
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000521int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000522{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000523 long a, b;
524 long longprod; /* a*b in native long arithmetic */
525 double doubled_longprod; /* (double)longprod */
526 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000527
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000528 CONVERT_TO_LONG(v, a);
529 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000530 longprod = a * b;
531 doubleprod = (double)a * (double)b;
532 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000533
Tim Petersa3c01ce2001-12-04 23:05:10 +0000534 /* Fast path for normal case: small multiplicands, and no info
535 is lost in either method. */
536 if (doubled_longprod == doubleprod)
537 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000538
Tim Petersa3c01ce2001-12-04 23:05:10 +0000539 /* Somebody somewhere lost info. Close enough, or way off? Note
540 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
541 The difference either is or isn't significant compared to the
542 true value (of which doubleprod is a good approximation).
543 */
544 {
545 const double diff = doubled_longprod - doubleprod;
546 const double absdiff = diff >= 0.0 ? diff : -diff;
547 const double absprod = doubleprod >= 0.0 ? doubleprod :
548 -doubleprod;
549 /* absdiff/absprod <= 1/32 iff
550 32 * absdiff <= absprod -- 5 good bits is "close enough" */
551 if (32.0 * absdiff <= absprod)
552 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000553 else
554 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000555 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556}
557
Thomas Wouters89f507f2006-12-13 04:49:30 +0000558/* Integer overflow checking for unary negation: on a 2's-complement
559 * box, -x overflows iff x is the most negative long. In this case we
560 * get -x == x. However, -x is undefined (by C) if x /is/ the most
561 * negative long (it's a signed overflow case), and some compilers care.
562 * So we cast x to unsigned long first. However, then other compilers
563 * warn about applying unary minus to an unsigned operand. Hence the
564 * weird "0-".
565 */
566#define UNARY_NEG_WOULD_OVERFLOW(x) \
567 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
568
Guido van Rossume27f7952001-08-23 02:59:04 +0000569/* Return type of i_divmod */
570enum divmod_result {
571 DIVMOD_OK, /* Correct result */
572 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
573 DIVMOD_ERROR /* Exception raised */
574};
575
576static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000577i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000578 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000579{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000580 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000581
Tim Peters1dad6a82001-06-18 19:21:11 +0000582 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000584 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000585 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000586 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000587 /* (-sys.maxint-1)/-1 is the only overflow case. */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000588 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
Guido van Rossume27f7952001-08-23 02:59:04 +0000589 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000590 xdivy = x / y;
591 xmody = x - xdivy * y;
592 /* If the signs of x and y differ, and the remainder is non-0,
593 * C89 doesn't define whether xdivy is now the floor or the
594 * ceiling of the infinitely precise quotient. We want the floor,
595 * and we have it iff the remainder's sign matches y's.
596 */
597 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
598 xmody += y;
599 --xdivy;
600 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000601 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000602 *p_xdivy = xdivy;
603 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000604 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000605}
606
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607static PyObject *
Neal Norwitz4886cc32006-08-21 17:06:07 +0000608int_floor_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000609{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000610 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000611 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000612 CONVERT_TO_LONG(x, xi);
613 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000614 switch (i_divmod(xi, yi, &d, &m)) {
615 case DIVMOD_OK:
616 return PyInt_FromLong(d);
617 case DIVMOD_OVERFLOW:
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000618 return PyLong_Type.tp_as_number->nb_floor_divide((PyObject *)x,
619 (PyObject *)y);
Guido van Rossum393661d2001-08-31 17:40:15 +0000620 default:
621 return NULL;
622 }
623}
624
625static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000626int_true_divide(PyObject *v, PyObject *w)
627{
Tim Peterse2a60002001-09-04 06:17:36 +0000628 /* If they aren't both ints, give someone else a chance. In
629 particular, this lets int/long get handled by longs, which
630 underflows to 0 gracefully if the long is too big to convert
631 to float. */
632 if (PyInt_Check(v) && PyInt_Check(w))
633 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
634 Py_INCREF(Py_NotImplemented);
635 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000636}
637
638static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000639int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000640{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000641 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000642 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000643 CONVERT_TO_LONG(x, xi);
644 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000645 switch (i_divmod(xi, yi, &d, &m)) {
646 case DIVMOD_OK:
647 return PyInt_FromLong(m);
648 case DIVMOD_OVERFLOW:
649 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
650 (PyObject *)y);
651 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000652 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000653 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000654}
655
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000656static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000657int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000658{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000659 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000660 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000661 CONVERT_TO_LONG(x, xi);
662 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000663 switch (i_divmod(xi, yi, &d, &m)) {
664 case DIVMOD_OK:
665 return Py_BuildValue("(ll)", d, m);
666 case DIVMOD_OVERFLOW:
667 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
668 (PyObject *)y);
669 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000670 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000671 }
Guido van Rossum00466951991-05-05 20:08:27 +0000672}
673
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000675int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000676{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000677 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000678 CONVERT_TO_LONG(v, iv);
679 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000680 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000681 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000682 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
683 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000684 return NULL;
685 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000686 /* Return a float. This works because we know that
687 this calls float_pow() which converts its
688 arguments to double. */
689 return PyFloat_Type.tp_as_number->nb_power(
690 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000691 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000693 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000694 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000696 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000697 return NULL;
698 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000699 }
700 /*
701 * XXX: The original exponentiation code stopped looping
702 * when temp hit zero; this code will continue onwards
703 * unnecessarily, but at least it won't cause any errors.
704 * Hopefully the speed improvement from the fast exponentiation
705 * will compensate for the slight inefficiency.
706 * XXX: Better handling of overflows is desperately needed.
707 */
708 temp = iv;
709 ix = 1;
710 while (iw > 0) {
711 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000712 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000713 ix = ix*temp;
714 if (temp == 0)
715 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000716 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000717 return PyLong_Type.tp_as_number->nb_power(
718 (PyObject *)v,
719 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000720 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000721 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000722 }
723 iw >>= 1; /* Shift exponent down by 1 bit */
724 if (iw==0) break;
725 prev = temp;
726 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000727 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000728 return PyLong_Type.tp_as_number->nb_power(
729 (PyObject *)v, (PyObject *)w, (PyObject *)z);
730 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000731 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000732 /* If we did a multiplication, perform a modulo */
733 ix = ix % iz;
734 temp = temp % iz;
735 }
736 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000737 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000738 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000739 switch (i_divmod(ix, iz, &div, &mod)) {
740 case DIVMOD_OK:
741 ix = mod;
742 break;
743 case DIVMOD_OVERFLOW:
744 return PyLong_Type.tp_as_number->nb_power(
745 (PyObject *)v, (PyObject *)w, (PyObject *)z);
746 default:
747 return NULL;
748 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000749 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000751}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000752
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000754int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000756 register long a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000757 a = v->ob_ival;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000758 /* check for overflow */
759 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
Tim Petersc8854432004-08-25 02:14:08 +0000760 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000761 if (o != NULL) {
762 PyObject *result = PyNumber_Negative(o);
763 Py_DECREF(o);
764 return result;
765 }
766 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000767 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000768 return PyInt_FromLong(-a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769}
770
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000771static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000772int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000773{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000774 if (PyInt_CheckExact(v)) {
775 Py_INCREF(v);
776 return (PyObject *)v;
777 }
778 else
779 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780}
781
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000782static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000783int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000784{
785 if (v->ob_ival >= 0)
786 return int_pos(v);
787 else
788 return int_neg(v);
789}
790
Guido van Rossum0bff0151991-05-14 12:05:32 +0000791static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000792int_bool(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000793{
794 return v->ob_ival != 0;
795}
796
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000798int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000799{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000800 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000801}
802
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000804int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000805{
Guido van Rossum078151d2002-08-11 04:24:12 +0000806 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000807 PyObject *vv, *ww, *result;
808
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000809 CONVERT_TO_LONG(v, a);
810 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000811 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000812 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000813 return NULL;
814 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000815 if (a == 0 || b == 0)
816 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000817 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000818 vv = PyLong_FromLong(PyInt_AS_LONG(v));
819 if (vv == NULL)
820 return NULL;
821 ww = PyLong_FromLong(PyInt_AS_LONG(w));
822 if (ww == NULL) {
823 Py_DECREF(vv);
824 return NULL;
825 }
826 result = PyNumber_Lshift(vv, ww);
827 Py_DECREF(vv);
828 Py_DECREF(ww);
829 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000830 }
Tim Petersda1a2212002-08-11 17:54:42 +0000831 c = a << b;
832 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000833 vv = PyLong_FromLong(PyInt_AS_LONG(v));
834 if (vv == NULL)
835 return NULL;
836 ww = PyLong_FromLong(PyInt_AS_LONG(w));
837 if (ww == NULL) {
838 Py_DECREF(vv);
839 return NULL;
840 }
841 result = PyNumber_Lshift(vv, ww);
842 Py_DECREF(vv);
843 Py_DECREF(ww);
844 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000845 }
846 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000847}
848
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000849static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000850int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000851{
852 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000853 CONVERT_TO_LONG(v, a);
854 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000855 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000857 return NULL;
858 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000859 if (a == 0 || b == 0)
860 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000861 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000862 if (a < 0)
863 a = -1;
864 else
865 a = 0;
866 }
867 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000868 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000869 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000870 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000871}
872
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000874int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000875{
876 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000877 CONVERT_TO_LONG(v, a);
878 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000880}
881
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000882static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000883int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000884{
885 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000886 CONVERT_TO_LONG(v, a);
887 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000889}
890
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000891static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000892int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000893{
894 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000895 CONVERT_TO_LONG(v, a);
896 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000898}
899
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000900static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000901int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000902{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000903 if (PyInt_CheckExact(v))
904 Py_INCREF(v);
905 else
906 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000907 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000908}
909
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000911int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000912{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000913 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000914}
915
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000916static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000917int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000918{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000919 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000920}
921
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000922static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000923int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000924{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000925 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000926 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000927 if (x < 0)
928 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
929 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000930 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000931 else
Barry Warsaw61975092001-11-28 20:55:34 +0000932 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000933 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000934}
935
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000936static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000937int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000938{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000939 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000940 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000941 if (x < 0)
942 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
943 else
944 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000945 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000946}
947
Jeremy Hylton938ace62002-07-17 16:30:39 +0000948static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000949int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
950
Tim Peters6d6c1a32001-08-02 04:15:00 +0000951static PyObject *
952int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
953{
954 PyObject *x = NULL;
955 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000956 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000957
Guido van Rossumbef14172001-08-29 15:47:46 +0000958 if (type != &PyInt_Type)
959 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000960 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
961 &x, &base))
962 return NULL;
963 if (x == NULL)
964 return PyInt_FromLong(0L);
965 if (base == -909)
966 return PyNumber_Int(x);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000967 if (PyString_Check(x)) {
968 /* Since PyInt_FromString doesn't have a length parameter,
969 * check here for possible NULs in the string. */
970 char *string = PyString_AS_STRING(x);
971 if (strlen(string) != PyString_Size(x)) {
972 /* create a repr() of the input string,
973 * just like PyInt_FromString does */
974 PyObject *srepr;
975 srepr = PyObject_Repr(x);
976 if (srepr == NULL)
977 return NULL;
978 PyErr_Format(PyExc_ValueError,
979 "invalid literal for int() with base %d: %s",
980 base, PyString_AS_STRING(srepr));
981 Py_DECREF(srepr);
982 return NULL;
983 }
984 return PyInt_FromString(string, NULL, base);
985 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000986 if (PyUnicode_Check(x))
987 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
988 PyUnicode_GET_SIZE(x),
989 base);
990 PyErr_SetString(PyExc_TypeError,
991 "int() can't convert non-string with explicit base");
992 return NULL;
993}
994
Guido van Rossumbef14172001-08-29 15:47:46 +0000995/* Wimpy, slow approach to tp_new calls for subtypes of int:
996 first create a regular int from whatever arguments we got,
997 then allocate a subtype instance and initialize its ob_ival
998 from the regular int. The regular int is then thrown away.
999*/
1000static PyObject *
1001int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1002{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001003 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001004 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001005
1006 assert(PyType_IsSubtype(type, &PyInt_Type));
1007 tmp = int_new(&PyInt_Type, args, kwds);
1008 if (tmp == NULL)
1009 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001010 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001011 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001012 if (ival == -1 && PyErr_Occurred()) {
1013 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001014 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001015 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001016 } else {
1017 ival = ((PyIntObject *)tmp)->ob_ival;
1018 }
1019
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020 newobj = type->tp_alloc(type, 0);
1021 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001022 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001023 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001024 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001025 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001026 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001027 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001028}
1029
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001030static PyObject *
1031int_getnewargs(PyIntObject *v)
1032{
1033 return Py_BuildValue("(l)", v->ob_ival);
1034}
1035
1036static PyMethodDef int_methods[] = {
1037 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
1038 {NULL, NULL} /* sentinel */
1039};
1040
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001041PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001042"int(x[, base]) -> integer\n\
1043\n\
1044Convert a string or number to an integer, if possible. A floating point\n\
1045argument will be truncated towards zero (this does not include a string\n\
1046representation of a floating point number!) When converting a string, use\n\
1047the optional base. It is an error to supply a base when converting a\n\
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001048non-string. If base is zero, the proper base is guessed based on the\n\
1049string content. If the argument is outside the integer range a\n\
1050long object will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001051
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001052static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001053 (binaryfunc)int_add, /*nb_add*/
1054 (binaryfunc)int_sub, /*nb_subtract*/
1055 (binaryfunc)int_mul, /*nb_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001056 (binaryfunc)int_mod, /*nb_remainder*/
1057 (binaryfunc)int_divmod, /*nb_divmod*/
1058 (ternaryfunc)int_pow, /*nb_power*/
1059 (unaryfunc)int_neg, /*nb_negative*/
1060 (unaryfunc)int_pos, /*nb_positive*/
1061 (unaryfunc)int_abs, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001062 (inquiry)int_bool, /*nb_bool*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001063 (unaryfunc)int_invert, /*nb_invert*/
1064 (binaryfunc)int_lshift, /*nb_lshift*/
1065 (binaryfunc)int_rshift, /*nb_rshift*/
1066 (binaryfunc)int_and, /*nb_and*/
1067 (binaryfunc)int_xor, /*nb_xor*/
1068 (binaryfunc)int_or, /*nb_or*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001069 0, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001070 (unaryfunc)int_int, /*nb_int*/
1071 (unaryfunc)int_long, /*nb_long*/
1072 (unaryfunc)int_float, /*nb_float*/
1073 (unaryfunc)int_oct, /*nb_oct*/
1074 (unaryfunc)int_hex, /*nb_hex*/
1075 0, /*nb_inplace_add*/
1076 0, /*nb_inplace_subtract*/
1077 0, /*nb_inplace_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001078 0, /*nb_inplace_remainder*/
1079 0, /*nb_inplace_power*/
1080 0, /*nb_inplace_lshift*/
1081 0, /*nb_inplace_rshift*/
1082 0, /*nb_inplace_and*/
1083 0, /*nb_inplace_xor*/
1084 0, /*nb_inplace_or*/
Neal Norwitz4886cc32006-08-21 17:06:07 +00001085 (binaryfunc)int_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00001086 int_true_divide, /* nb_true_divide */
1087 0, /* nb_inplace_floor_divide */
1088 0, /* nb_inplace_true_divide */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001089 (unaryfunc)int_int, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001090};
1091
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001092PyTypeObject PyInt_Type = {
1093 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001094 0,
1095 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001096 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001097 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001098 (destructor)int_dealloc, /* tp_dealloc */
1099 (printfunc)int_print, /* tp_print */
1100 0, /* tp_getattr */
1101 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001102 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001103 (reprfunc)int_repr, /* tp_repr */
1104 &int_as_number, /* tp_as_number */
1105 0, /* tp_as_sequence */
1106 0, /* tp_as_mapping */
1107 (hashfunc)int_hash, /* tp_hash */
1108 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001109 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001110 PyObject_GenericGetAttr, /* tp_getattro */
1111 0, /* tp_setattro */
1112 0, /* tp_as_buffer */
Thomas Wouters27d517b2007-02-25 20:39:11 +00001113 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
1114 Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001115 int_doc, /* tp_doc */
1116 0, /* tp_traverse */
1117 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001118 int_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001119 0, /* tp_weaklistoffset */
1120 0, /* tp_iter */
1121 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001122 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001123 0, /* tp_members */
1124 0, /* tp_getset */
1125 0, /* tp_base */
1126 0, /* tp_dict */
1127 0, /* tp_descr_get */
1128 0, /* tp_descr_set */
1129 0, /* tp_dictoffset */
1130 0, /* tp_init */
1131 0, /* tp_alloc */
1132 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001133 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001134};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001135
Neal Norwitzc91ed402002-12-30 22:29:22 +00001136int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001137_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001138{
1139 PyIntObject *v;
1140 int ival;
1141#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1142 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001143 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001144 return 0;
1145 /* PyObject_New is inlined */
1146 v = free_list;
1147 free_list = (PyIntObject *)v->ob_type;
1148 PyObject_INIT(v, &PyInt_Type);
1149 v->ob_ival = ival;
1150 small_ints[ival + NSMALLNEGINTS] = v;
1151 }
1152#endif
1153 return 1;
1154}
1155
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001156void
Fred Drakea2f55112000-07-09 15:16:51 +00001157PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001158{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001159 PyIntObject *p;
1160 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001161 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162 unsigned int ctr;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001163 int bc, bf; /* block count, number of freed blocks */
1164 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001165
Guido van Rossumda084ed1999-03-10 22:55:24 +00001166#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1167 PyIntObject **q;
1168
1169 i = NSMALLNEGINTS + NSMALLPOSINTS;
1170 q = small_ints;
1171 while (--i >= 0) {
1172 Py_XDECREF(*q);
1173 *q++ = NULL;
1174 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001175#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001176 bc = 0;
1177 bf = 0;
1178 isum = 0;
1179 list = block_list;
1180 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001181 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001182 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001183 bc++;
1184 irem = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001185 for (ctr = 0, p = &list->objects[0];
1186 ctr < N_INTOBJECTS;
1187 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001188 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001189 irem++;
1190 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001191 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001192 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001193 list->next = block_list;
1194 block_list = list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001195 for (ctr = 0, p = &list->objects[0];
1196 ctr < N_INTOBJECTS;
1197 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001198 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001199 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001200 p->ob_type = (struct _typeobject *)
1201 free_list;
1202 free_list = p;
1203 }
1204#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1205 else if (-NSMALLNEGINTS <= p->ob_ival &&
1206 p->ob_ival < NSMALLPOSINTS &&
1207 small_ints[p->ob_ival +
1208 NSMALLNEGINTS] == NULL) {
1209 Py_INCREF(p);
1210 small_ints[p->ob_ival +
1211 NSMALLNEGINTS] = p;
1212 }
1213#endif
1214 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001215 }
1216 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001217 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001218 bf++;
1219 }
1220 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001221 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001222 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001223 if (!Py_VerboseFlag)
1224 return;
1225 fprintf(stderr, "# cleanup ints");
1226 if (!isum) {
1227 fprintf(stderr, "\n");
1228 }
1229 else {
1230 fprintf(stderr,
1231 ": %d unfreed int%s in %d out of %d block%s\n",
1232 isum, isum == 1 ? "" : "s",
1233 bc - bf, bc, bc == 1 ? "" : "s");
1234 }
1235 if (Py_VerboseFlag > 1) {
1236 list = block_list;
1237 while (list != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001238 for (ctr = 0, p = &list->objects[0];
1239 ctr < N_INTOBJECTS;
1240 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001241 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001242 /* XXX(twouters) cast refcount to
1243 long until %zd is universally
1244 available
1245 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001246 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001247 "# <int at %p, refcnt=%ld, val=%ld>\n",
1248 p, (long)p->ob_refcnt,
1249 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001250 }
1251 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001252 }
1253 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001254}
Guido van Rossumddefaf32007-01-14 03:31:43 +00001255#endif /* if 0 */