blob: 2062bee9de18d4940987634e9f07b6debb5fb46b [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 Rossum3f5da241990-12-20 15:06:42 +000013/* Integers are quite normal objects, to make object handling uniform.
14 (Using odd pointers to represent integers would save much space
15 but require extra checks for this special case throughout the code.)
Tim Peters29c0afc2002-04-28 16:57:34 +000016 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000017 and deallocating integers, these operations should be very fast.
18 Therefore we use a dedicated allocation scheme with a much lower
19 overhead (in space and time) than straight malloc(): a simple
20 dedicated free list, filled when necessary with memory from malloc().
Tim Peters29c0afc2002-04-28 16:57:34 +000021
22 block_list is a singly-linked list of all PyIntBlocks ever allocated,
23 linked via their next members. PyIntBlocks are never returned to the
24 system before shutdown (PyInt_Fini).
25
26 free_list is a singly-linked list of available PyIntObjects, linked
27 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000028*/
29
30#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000031#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
32#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000033
Guido van Rossum3fce8831999-03-12 19:43:17 +000034struct _intblock {
35 struct _intblock *next;
36 PyIntObject objects[N_INTOBJECTS];
37};
38
39typedef struct _intblock PyIntBlock;
40
41static PyIntBlock *block_list = NULL;
42static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000043
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000045fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000046{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000048 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000049 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000050 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000051 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000052 ((PyIntBlock *)p)->next = block_list;
53 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000054 /* Link the int objects together, from rear to front, then return
55 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000056 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000057 q = p + N_INTOBJECTS;
58 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000059 q->ob_type = (struct _typeobject *)(q-1);
60 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000061 return p + N_INTOBJECTS - 1;
62}
63
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000064#ifndef NSMALLPOSINTS
Georg Brandl418a1ef2006-02-22 11:30:06 +000065#define NSMALLPOSINTS 257
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000066#endif
67#ifndef NSMALLNEGINTS
Neal Norwitzc91ed402002-12-30 22:29:22 +000068#define NSMALLNEGINTS 5
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000069#endif
70#if NSMALLNEGINTS + NSMALLPOSINTS > 0
71/* References to small integers are saved in this array so that they
72 can be shared.
73 The integers that are saved are those in the range
74 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
75*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000077#endif
78#ifdef COUNT_ALLOCS
79int quick_int_allocs, quick_neg_int_allocs;
80#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000081
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000083PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000086#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Neal Norwitzc91ed402002-12-30 22:29:22 +000087 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
88 v = small_ints[ival + NSMALLNEGINTS];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#ifdef COUNT_ALLOCS
91 if (ival >= 0)
92 quick_int_allocs++;
93 else
94 quick_neg_int_allocs++;
95#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000097 }
98#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000099 if (free_list == NULL) {
100 if ((free_list = fill_free_list()) == NULL)
101 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000103 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000104 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000105 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000108 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000109}
110
Martin v. Löwis18e16552006-02-15 17:27:45 +0000111PyObject *
112PyInt_FromSize_t(size_t ival)
113{
114 if (ival <= LONG_MAX)
115 return PyInt_FromLong((long)ival);
116 return _PyLong_FromSize_t(ival);
117}
118
119PyObject *
120PyInt_FromSsize_t(Py_ssize_t ival)
121{
122 if (ival >= LONG_MIN && ival <= LONG_MAX)
123 return PyInt_FromLong((long)ival);
124 return _PyLong_FromSsize_t(ival);
125}
126
Guido van Rossum3f5da241990-12-20 15:06:42 +0000127static void
Fred Drakea2f55112000-07-09 15:16:51 +0000128int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000130 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000131 v->ob_type = (struct _typeobject *)free_list;
132 free_list = v;
133 }
134 else
135 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136}
137
Guido van Rossum93646982002-04-26 00:53:34 +0000138static void
139int_free(PyIntObject *v)
140{
141 v->ob_type = (struct _typeobject *)free_list;
142 free_list = v;
143}
144
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145long
Fred Drakea2f55112000-07-09 15:16:51 +0000146PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 PyNumberMethods *nb;
149 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000150 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000151
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 if (op && PyInt_Check(op))
153 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000154
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000155 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
156 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000157 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158 return -1;
159 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000160
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000162 if (io == NULL)
163 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000165 if (PyLong_Check(io)) {
166 /* got a long? => retry int conversion */
167 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000168 Py_DECREF(io);
169 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000170 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000171 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000172 }
173 else
174 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000175 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000176 PyErr_SetString(PyExc_TypeError,
177 "nb_int should return int object");
178 return -1;
179 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000180 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000181
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 val = PyInt_AS_LONG(io);
183 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000184
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000185 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186}
187
Martin v. Löwis18e16552006-02-15 17:27:45 +0000188Py_ssize_t
189PyInt_AsSsize_t(register PyObject *op)
190{
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000191#if SIZEOF_SIZE_T != SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000192 PyNumberMethods *nb;
193 PyIntObject *io;
194 Py_ssize_t val;
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000195#endif
Martin v. Löwis18e16552006-02-15 17:27:45 +0000196 if (op && !PyInt_CheckExact(op) && PyLong_Check(op))
197 return _PyLong_AsSsize_t(op);
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000198#if SIZEOF_SIZE_T == SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000199 return PyInt_AsLong(op);
200#else
201
202 if (op && PyInt_Check(op))
203 return PyInt_AS_LONG((PyIntObject*) op);
204
205 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
206 (nb->nb_int == NULL && nb->nb_long == 0)) {
207 PyErr_SetString(PyExc_TypeError, "an integer is required");
208 return -1;
209 }
210
211 if (nb->nb_long != 0) {
212 io = (PyIntObject*) (*nb->nb_long) (op);
213 } else {
214 io = (PyIntObject*) (*nb->nb_int) (op);
215 }
216 if (io == NULL)
217 return -1;
218 if (!PyInt_Check(io)) {
219 if (PyLong_Check(io)) {
220 /* got a long? => retry int conversion */
221 val = _PyLong_AsSsize_t((PyObject *)io);
222 Py_DECREF(io);
223 if ((val == -1) && PyErr_Occurred())
224 return -1;
225 return val;
226 }
227 else
228 {
229 Py_DECREF(io);
230 PyErr_SetString(PyExc_TypeError,
231 "nb_int should return int object");
232 return -1;
233 }
234 }
235
236 val = PyInt_AS_LONG(io);
237 Py_DECREF(io);
238
239 return val;
240#endif
241}
242
Thomas Hellera4ea6032003-04-17 18:55:45 +0000243unsigned long
244PyInt_AsUnsignedLongMask(register PyObject *op)
245{
246 PyNumberMethods *nb;
247 PyIntObject *io;
248 unsigned long val;
249
250 if (op && PyInt_Check(op))
251 return PyInt_AS_LONG((PyIntObject*) op);
252 if (op && PyLong_Check(op))
253 return PyLong_AsUnsignedLongMask(op);
254
255 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
256 nb->nb_int == NULL) {
257 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000258 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000259 }
260
261 io = (PyIntObject*) (*nb->nb_int) (op);
262 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000263 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000264 if (!PyInt_Check(io)) {
265 if (PyLong_Check(io)) {
266 val = PyLong_AsUnsignedLongMask((PyObject *)io);
267 Py_DECREF(io);
268 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000269 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000270 return val;
271 }
272 else
273 {
274 Py_DECREF(io);
275 PyErr_SetString(PyExc_TypeError,
276 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000277 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000278 }
279 }
280
281 val = PyInt_AS_LONG(io);
282 Py_DECREF(io);
283
284 return val;
285}
286
287#ifdef HAVE_LONG_LONG
288unsigned PY_LONG_LONG
289PyInt_AsUnsignedLongLongMask(register PyObject *op)
290{
291 PyNumberMethods *nb;
292 PyIntObject *io;
293 unsigned PY_LONG_LONG val;
294
295 if (op && PyInt_Check(op))
296 return PyInt_AS_LONG((PyIntObject*) op);
297 if (op && PyLong_Check(op))
298 return PyLong_AsUnsignedLongLongMask(op);
299
300 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
301 nb->nb_int == NULL) {
302 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000303 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000304 }
305
306 io = (PyIntObject*) (*nb->nb_int) (op);
307 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000308 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000309 if (!PyInt_Check(io)) {
310 if (PyLong_Check(io)) {
311 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
312 Py_DECREF(io);
313 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000314 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000315 return val;
316 }
317 else
318 {
319 Py_DECREF(io);
320 PyErr_SetString(PyExc_TypeError,
321 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000322 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000323 }
324 }
325
326 val = PyInt_AS_LONG(io);
327 Py_DECREF(io);
328
329 return val;
330}
331#endif
332
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000333PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000334PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000335{
336 char *end;
337 long x;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000338 Py_ssize_t slen;
339 PyObject *sobj, *srepr;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000340
341 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000342 PyErr_SetString(PyExc_ValueError,
343 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000344 return NULL;
345 }
346
347 while (*s && isspace(Py_CHARMASK(*s)))
348 s++;
349 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000350 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000351 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000352 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000353 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000354 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000355 else
356 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000357 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000358 goto bad;
359 while (*end && isspace(Py_CHARMASK(*end)))
360 end++;
361 if (*end != '\0') {
362 bad:
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000363 slen = strlen(s) < 200 ? strlen(s) : 200;
364 sobj = PyString_FromStringAndSize(s, slen);
365 if (sobj == NULL)
366 return NULL;
367 srepr = PyObject_Repr(sobj);
368 Py_DECREF(sobj);
369 if (srepr == NULL)
370 return NULL;
371 PyErr_Format(PyExc_ValueError,
372 "invalid literal for int() with base %d: %s",
373 base, PyString_AS_STRING(srepr));
374 Py_DECREF(srepr);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000375 return NULL;
376 }
Tim Petersc8854432004-08-25 02:14:08 +0000377 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000378 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000379 if (pend)
380 *pend = end;
381 return PyInt_FromLong(x);
382}
383
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000384#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000385PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000386PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000387{
Walter Dörwald07e14762002-11-06 16:15:14 +0000388 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +0000389 char *buffer = (char *)PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000390
Walter Dörwald07e14762002-11-06 16:15:14 +0000391 if (buffer == NULL)
392 return NULL;
393
394 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
395 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000396 return NULL;
397 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000398 result = PyInt_FromString(buffer, NULL, base);
399 PyMem_FREE(buffer);
400 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000401}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000402#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000403
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000404/* Methods */
405
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000406/* Integers are seen as the "smallest" of all numeric types and thus
407 don't have any knowledge about conversion of other types to
408 integers. */
409
410#define CONVERT_TO_LONG(obj, lng) \
411 if (PyInt_Check(obj)) { \
412 lng = PyInt_AS_LONG(obj); \
413 } \
414 else { \
415 Py_INCREF(Py_NotImplemented); \
416 return Py_NotImplemented; \
417 }
418
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000419/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000420static int
Fred Drakea2f55112000-07-09 15:16:51 +0000421int_print(PyIntObject *v, FILE *fp, int flags)
422 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423{
424 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000425 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426}
427
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000428static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000429int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430{
Tim Peters42221042001-12-01 02:52:56 +0000431 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000432 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434}
435
436static int
Fred Drakea2f55112000-07-09 15:16:51 +0000437int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438{
439 register long i = v->ob_ival;
440 register long j = w->ob_ival;
441 return (i < j) ? -1 : (i > j) ? 1 : 0;
442}
443
Guido van Rossum9bfef441993-03-29 10:43:31 +0000444static long
Fred Drakea2f55112000-07-09 15:16:51 +0000445int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000446{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000447 /* XXX If this is changed, you also need to change the way
448 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000449 long x = v -> ob_ival;
450 if (x == -1)
451 x = -2;
452 return x;
453}
454
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000456int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457{
458 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000459 CONVERT_TO_LONG(v, a);
460 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000462 if ((x^a) >= 0 || (x^b) >= 0)
463 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000464 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465}
466
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000467static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000468int_sub(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_subtract((PyObject *)v,
477 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478}
479
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000480/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000481Integer overflow checking for * is painful: Python tried a couple ways, but
482they didn't work on all platforms, or failed in endcases (a product of
483-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000484
Tim Petersa3c01ce2001-12-04 23:05:10 +0000485Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000486
Tim Petersa3c01ce2001-12-04 23:05:10 +0000487The native long product x*y is either exactly right or *way* off, being
488just the last n bits of the true product, where n is the number of bits
489in a long (the delivered product is the true product plus i*2**n for
490some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000491
Tim Petersa3c01ce2001-12-04 23:05:10 +0000492The native double product (double)x * (double)y is subject to three
493rounding errors: on a sizeof(long)==8 box, each cast to double can lose
494info, and even on a sizeof(long)==4 box, the multiplication can lose info.
495But, unlike the native long product, it's not in *range* trouble: even
496if sizeof(long)==32 (256-bit longs), the product easily fits in the
497dynamic range of a double. So the leading 50 (or so) bits of the double
498product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000499
Tim Petersa3c01ce2001-12-04 23:05:10 +0000500We check these two ways against each other, and declare victory if they're
501approximately the same. Else, because the native long product is the only
502one that can lose catastrophic amounts of information, it's the native long
503product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000504*/
505
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000507int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000508{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000509 long a, b;
510 long longprod; /* a*b in native long arithmetic */
511 double doubled_longprod; /* (double)longprod */
512 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000513
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000514 CONVERT_TO_LONG(v, a);
515 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000516 longprod = a * b;
517 doubleprod = (double)a * (double)b;
518 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000519
Tim Petersa3c01ce2001-12-04 23:05:10 +0000520 /* Fast path for normal case: small multiplicands, and no info
521 is lost in either method. */
522 if (doubled_longprod == doubleprod)
523 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000524
Tim Petersa3c01ce2001-12-04 23:05:10 +0000525 /* Somebody somewhere lost info. Close enough, or way off? Note
526 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
527 The difference either is or isn't significant compared to the
528 true value (of which doubleprod is a good approximation).
529 */
530 {
531 const double diff = doubled_longprod - doubleprod;
532 const double absdiff = diff >= 0.0 ? diff : -diff;
533 const double absprod = doubleprod >= 0.0 ? doubleprod :
534 -doubleprod;
535 /* absdiff/absprod <= 1/32 iff
536 32 * absdiff <= absprod -- 5 good bits is "close enough" */
537 if (32.0 * absdiff <= absprod)
538 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000539 else
540 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000541 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000542}
543
Guido van Rossume27f7952001-08-23 02:59:04 +0000544/* Return type of i_divmod */
545enum divmod_result {
546 DIVMOD_OK, /* Correct result */
547 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
548 DIVMOD_ERROR /* Exception raised */
549};
550
551static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000552i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000553 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000554{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000555 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000556
Tim Peters1dad6a82001-06-18 19:21:11 +0000557 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000559 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000560 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000562 /* (-sys.maxint-1)/-1 is the only overflow case. */
Tim Petersc8854432004-08-25 02:14:08 +0000563 if (y == -1 && x < 0 && x == -x)
Guido van Rossume27f7952001-08-23 02:59:04 +0000564 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000565 xdivy = x / y;
566 xmody = x - xdivy * y;
567 /* If the signs of x and y differ, and the remainder is non-0,
568 * C89 doesn't define whether xdivy is now the floor or the
569 * ceiling of the infinitely precise quotient. We want the floor,
570 * and we have it iff the remainder's sign matches y's.
571 */
572 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
573 xmody += y;
574 --xdivy;
575 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000576 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000577 *p_xdivy = xdivy;
578 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000579 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000580}
581
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000583int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000584{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000585 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000586 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000587 CONVERT_TO_LONG(x, xi);
588 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000589 switch (i_divmod(xi, yi, &d, &m)) {
590 case DIVMOD_OK:
591 return PyInt_FromLong(d);
592 case DIVMOD_OVERFLOW:
593 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
594 (PyObject *)y);
595 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000596 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000597 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000598}
599
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000600static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000601int_classic_div(PyIntObject *x, PyIntObject *y)
602{
603 long xi, yi;
604 long d, m;
605 CONVERT_TO_LONG(x, xi);
606 CONVERT_TO_LONG(y, yi);
607 if (Py_DivisionWarningFlag &&
608 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
609 return NULL;
610 switch (i_divmod(xi, yi, &d, &m)) {
611 case DIVMOD_OK:
612 return PyInt_FromLong(d);
613 case DIVMOD_OVERFLOW:
614 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
615 (PyObject *)y);
616 default:
617 return NULL;
618 }
619}
620
621static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000622int_true_divide(PyObject *v, PyObject *w)
623{
Tim Peterse2a60002001-09-04 06:17:36 +0000624 /* If they aren't both ints, give someone else a chance. In
625 particular, this lets int/long get handled by longs, which
626 underflows to 0 gracefully if the long is too big to convert
627 to float. */
628 if (PyInt_Check(v) && PyInt_Check(w))
629 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
630 Py_INCREF(Py_NotImplemented);
631 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000632}
633
634static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000635int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000636{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000637 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000638 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000639 CONVERT_TO_LONG(x, xi);
640 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000641 switch (i_divmod(xi, yi, &d, &m)) {
642 case DIVMOD_OK:
643 return PyInt_FromLong(m);
644 case DIVMOD_OVERFLOW:
645 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
646 (PyObject *)y);
647 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000648 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000649 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000650}
651
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000653int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000654{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000655 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000656 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000657 CONVERT_TO_LONG(x, xi);
658 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000659 switch (i_divmod(xi, yi, &d, &m)) {
660 case DIVMOD_OK:
661 return Py_BuildValue("(ll)", d, m);
662 case DIVMOD_OVERFLOW:
663 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
664 (PyObject *)y);
665 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000666 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000667 }
Guido van Rossum00466951991-05-05 20:08:27 +0000668}
669
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000671int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000673 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000674 CONVERT_TO_LONG(v, iv);
675 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000676 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000677 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000678 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
679 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000680 return NULL;
681 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000682 /* Return a float. This works because we know that
683 this calls float_pow() which converts its
684 arguments to double. */
685 return PyFloat_Type.tp_as_number->nb_power(
686 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000687 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000689 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000690 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000691 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000692 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000693 return NULL;
694 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000695 }
696 /*
697 * XXX: The original exponentiation code stopped looping
698 * when temp hit zero; this code will continue onwards
699 * unnecessarily, but at least it won't cause any errors.
700 * Hopefully the speed improvement from the fast exponentiation
701 * will compensate for the slight inefficiency.
702 * XXX: Better handling of overflows is desperately needed.
703 */
704 temp = iv;
705 ix = 1;
706 while (iw > 0) {
707 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000708 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000709 ix = ix*temp;
710 if (temp == 0)
711 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000712 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000713 return PyLong_Type.tp_as_number->nb_power(
714 (PyObject *)v,
715 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000716 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000717 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000718 }
719 iw >>= 1; /* Shift exponent down by 1 bit */
720 if (iw==0) break;
721 prev = temp;
722 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000723 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000724 return PyLong_Type.tp_as_number->nb_power(
725 (PyObject *)v, (PyObject *)w, (PyObject *)z);
726 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000727 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000728 /* If we did a multiplication, perform a modulo */
729 ix = ix % iz;
730 temp = temp % iz;
731 }
732 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000733 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000734 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000735 switch (i_divmod(ix, iz, &div, &mod)) {
736 case DIVMOD_OK:
737 ix = mod;
738 break;
739 case DIVMOD_OVERFLOW:
740 return PyLong_Type.tp_as_number->nb_power(
741 (PyObject *)v, (PyObject *)w, (PyObject *)z);
742 default:
743 return NULL;
744 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000745 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000746 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000747}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000748
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000749static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000750int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000751{
752 register long a, x;
753 a = v->ob_ival;
754 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000755 if (a < 0 && x < 0) {
Tim Petersc8854432004-08-25 02:14:08 +0000756 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000757 if (o != NULL) {
758 PyObject *result = PyNumber_Negative(o);
759 Py_DECREF(o);
760 return result;
761 }
762 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000763 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765}
766
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000767static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000768int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000770 if (PyInt_CheckExact(v)) {
771 Py_INCREF(v);
772 return (PyObject *)v;
773 }
774 else
775 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000776}
777
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000778static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000779int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000780{
781 if (v->ob_ival >= 0)
782 return int_pos(v);
783 else
784 return int_neg(v);
785}
786
Guido van Rossum0bff0151991-05-14 12:05:32 +0000787static int
Fred Drakea2f55112000-07-09 15:16:51 +0000788int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000789{
790 return v->ob_ival != 0;
791}
792
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000793static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000794int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000795{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000796 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000797}
798
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000799static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000800int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000801{
Guido van Rossum078151d2002-08-11 04:24:12 +0000802 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000803 PyObject *vv, *ww, *result;
804
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000805 CONVERT_TO_LONG(v, a);
806 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000807 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000808 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000809 return NULL;
810 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000811 if (a == 0 || b == 0)
812 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000813 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000814 vv = PyLong_FromLong(PyInt_AS_LONG(v));
815 if (vv == NULL)
816 return NULL;
817 ww = PyLong_FromLong(PyInt_AS_LONG(w));
818 if (ww == NULL) {
819 Py_DECREF(vv);
820 return NULL;
821 }
822 result = PyNumber_Lshift(vv, ww);
823 Py_DECREF(vv);
824 Py_DECREF(ww);
825 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000826 }
Tim Petersda1a2212002-08-11 17:54:42 +0000827 c = a << b;
828 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000829 vv = PyLong_FromLong(PyInt_AS_LONG(v));
830 if (vv == NULL)
831 return NULL;
832 ww = PyLong_FromLong(PyInt_AS_LONG(w));
833 if (ww == NULL) {
834 Py_DECREF(vv);
835 return NULL;
836 }
837 result = PyNumber_Lshift(vv, ww);
838 Py_DECREF(vv);
839 Py_DECREF(ww);
840 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000841 }
842 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000843}
844
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000846int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000847{
848 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000849 CONVERT_TO_LONG(v, a);
850 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000851 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000852 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000853 return NULL;
854 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000855 if (a == 0 || b == 0)
856 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000857 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000858 if (a < 0)
859 a = -1;
860 else
861 a = 0;
862 }
863 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000864 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000865 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000866 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000867}
868
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000869static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000870int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000871{
872 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000873 CONVERT_TO_LONG(v, a);
874 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000876}
877
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000878static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000879int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000880{
881 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000882 CONVERT_TO_LONG(v, a);
883 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000884 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000885}
886
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000887static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000888int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000889{
890 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000891 CONVERT_TO_LONG(v, a);
892 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000893 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000894}
895
Guido van Rossum1952e382001-09-19 01:25:16 +0000896static int
897int_coerce(PyObject **pv, PyObject **pw)
898{
899 if (PyInt_Check(*pw)) {
900 Py_INCREF(*pv);
901 Py_INCREF(*pw);
902 return 0;
903 }
904 return 1; /* Can't do it */
905}
906
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000907static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000908int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000909{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000910 if (PyInt_CheckExact(v))
911 Py_INCREF(v);
912 else
913 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000914 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000915}
916
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000917static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000918int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000919{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000920 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000921}
922
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000923static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000924int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000925{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000926 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000927}
928
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000929static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000930int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000931{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000932 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000933 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000934 if (x < 0)
935 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
936 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000937 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000938 else
Barry Warsaw61975092001-11-28 20:55:34 +0000939 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000940 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000941}
942
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000943static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000944int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000945{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000946 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000947 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000948 if (x < 0)
949 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
950 else
951 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000952 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000953}
954
Jeremy Hylton938ace62002-07-17 16:30:39 +0000955static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000956int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
957
Tim Peters6d6c1a32001-08-02 04:15:00 +0000958static PyObject *
959int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
960{
961 PyObject *x = NULL;
962 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000963 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000964
Guido van Rossumbef14172001-08-29 15:47:46 +0000965 if (type != &PyInt_Type)
966 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000967 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
968 &x, &base))
969 return NULL;
970 if (x == NULL)
971 return PyInt_FromLong(0L);
972 if (base == -909)
973 return PyNumber_Int(x);
974 if (PyString_Check(x))
975 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000976#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000977 if (PyUnicode_Check(x))
978 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
979 PyUnicode_GET_SIZE(x),
980 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000981#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000982 PyErr_SetString(PyExc_TypeError,
983 "int() can't convert non-string with explicit base");
984 return NULL;
985}
986
Guido van Rossumbef14172001-08-29 15:47:46 +0000987/* Wimpy, slow approach to tp_new calls for subtypes of int:
988 first create a regular int from whatever arguments we got,
989 then allocate a subtype instance and initialize its ob_ival
990 from the regular int. The regular int is then thrown away.
991*/
992static PyObject *
993int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
994{
Anthony Baxter377be112006-04-11 06:54:30 +0000995 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000996 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000997
998 assert(PyType_IsSubtype(type, &PyInt_Type));
999 tmp = int_new(&PyInt_Type, args, kwds);
1000 if (tmp == NULL)
1001 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001002 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001003 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001004 if (ival == -1 && PyErr_Occurred()) {
1005 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001006 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001007 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001008 } else {
1009 ival = ((PyIntObject *)tmp)->ob_ival;
1010 }
1011
Anthony Baxter377be112006-04-11 06:54:30 +00001012 newobj = type->tp_alloc(type, 0);
1013 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001014 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001015 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001016 }
Anthony Baxter377be112006-04-11 06:54:30 +00001017 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001018 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00001019 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001020}
1021
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001022static PyObject *
1023int_getnewargs(PyIntObject *v)
1024{
1025 return Py_BuildValue("(l)", v->ob_ival);
1026}
1027
1028static PyMethodDef int_methods[] = {
1029 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
1030 {NULL, NULL} /* sentinel */
1031};
1032
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001033PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034"int(x[, base]) -> integer\n\
1035\n\
1036Convert a string or number to an integer, if possible. A floating point\n\
1037argument will be truncated towards zero (this does not include a string\n\
1038representation of a floating point number!) When converting a string, use\n\
1039the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +00001040non-string. If the argument is outside the integer range a long object\n\
1041will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001042
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001043static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001044 (binaryfunc)int_add, /*nb_add*/
1045 (binaryfunc)int_sub, /*nb_subtract*/
1046 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00001047 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001048 (binaryfunc)int_mod, /*nb_remainder*/
1049 (binaryfunc)int_divmod, /*nb_divmod*/
1050 (ternaryfunc)int_pow, /*nb_power*/
1051 (unaryfunc)int_neg, /*nb_negative*/
1052 (unaryfunc)int_pos, /*nb_positive*/
1053 (unaryfunc)int_abs, /*nb_absolute*/
1054 (inquiry)int_nonzero, /*nb_nonzero*/
1055 (unaryfunc)int_invert, /*nb_invert*/
1056 (binaryfunc)int_lshift, /*nb_lshift*/
1057 (binaryfunc)int_rshift, /*nb_rshift*/
1058 (binaryfunc)int_and, /*nb_and*/
1059 (binaryfunc)int_xor, /*nb_xor*/
1060 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +00001061 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001062 (unaryfunc)int_int, /*nb_int*/
1063 (unaryfunc)int_long, /*nb_long*/
1064 (unaryfunc)int_float, /*nb_float*/
1065 (unaryfunc)int_oct, /*nb_oct*/
1066 (unaryfunc)int_hex, /*nb_hex*/
1067 0, /*nb_inplace_add*/
1068 0, /*nb_inplace_subtract*/
1069 0, /*nb_inplace_multiply*/
1070 0, /*nb_inplace_divide*/
1071 0, /*nb_inplace_remainder*/
1072 0, /*nb_inplace_power*/
1073 0, /*nb_inplace_lshift*/
1074 0, /*nb_inplace_rshift*/
1075 0, /*nb_inplace_and*/
1076 0, /*nb_inplace_xor*/
1077 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001078 (binaryfunc)int_div, /* nb_floor_divide */
1079 int_true_divide, /* nb_true_divide */
1080 0, /* nb_inplace_floor_divide */
1081 0, /* nb_inplace_true_divide */
Georg Brandl347b3002006-03-30 11:57:00 +00001082 PyInt_AsSsize_t, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001083};
1084
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001085PyTypeObject PyInt_Type = {
1086 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001087 0,
1088 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001089 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001090 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001091 (destructor)int_dealloc, /* tp_dealloc */
1092 (printfunc)int_print, /* tp_print */
1093 0, /* tp_getattr */
1094 0, /* tp_setattr */
1095 (cmpfunc)int_compare, /* tp_compare */
1096 (reprfunc)int_repr, /* tp_repr */
1097 &int_as_number, /* tp_as_number */
1098 0, /* tp_as_sequence */
1099 0, /* tp_as_mapping */
1100 (hashfunc)int_hash, /* tp_hash */
1101 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001102 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001103 PyObject_GenericGetAttr, /* tp_getattro */
1104 0, /* tp_setattro */
1105 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001106 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
1107 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001108 int_doc, /* tp_doc */
1109 0, /* tp_traverse */
1110 0, /* tp_clear */
1111 0, /* tp_richcompare */
1112 0, /* tp_weaklistoffset */
1113 0, /* tp_iter */
1114 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001115 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001116 0, /* tp_members */
1117 0, /* tp_getset */
1118 0, /* tp_base */
1119 0, /* tp_dict */
1120 0, /* tp_descr_get */
1121 0, /* tp_descr_set */
1122 0, /* tp_dictoffset */
1123 0, /* tp_init */
1124 0, /* tp_alloc */
1125 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001126 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001127};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001128
Neal Norwitzc91ed402002-12-30 22:29:22 +00001129int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001130_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001131{
1132 PyIntObject *v;
1133 int ival;
1134#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1135 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001136 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001137 return 0;
1138 /* PyObject_New is inlined */
1139 v = free_list;
1140 free_list = (PyIntObject *)v->ob_type;
1141 PyObject_INIT(v, &PyInt_Type);
1142 v->ob_ival = ival;
1143 small_ints[ival + NSMALLNEGINTS] = v;
1144 }
1145#endif
1146 return 1;
1147}
1148
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001149void
Fred Drakea2f55112000-07-09 15:16:51 +00001150PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001151{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001152 PyIntObject *p;
1153 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001154 int i;
Skip Montanaro429433b2006-04-18 00:35:43 +00001155 unsigned int ctr;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001156 int bc, bf; /* block count, number of freed blocks */
1157 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001158
Guido van Rossumda084ed1999-03-10 22:55:24 +00001159#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1160 PyIntObject **q;
1161
1162 i = NSMALLNEGINTS + NSMALLPOSINTS;
1163 q = small_ints;
1164 while (--i >= 0) {
1165 Py_XDECREF(*q);
1166 *q++ = NULL;
1167 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001168#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001169 bc = 0;
1170 bf = 0;
1171 isum = 0;
1172 list = block_list;
1173 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001174 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001175 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001176 bc++;
1177 irem = 0;
Skip Montanaro429433b2006-04-18 00:35:43 +00001178 for (ctr = 0, p = &list->objects[0];
1179 ctr < N_INTOBJECTS;
1180 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001181 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001182 irem++;
1183 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001184 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001185 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001186 list->next = block_list;
1187 block_list = list;
Skip Montanaro429433b2006-04-18 00:35:43 +00001188 for (ctr = 0, p = &list->objects[0];
1189 ctr < N_INTOBJECTS;
1190 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001191 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001192 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001193 p->ob_type = (struct _typeobject *)
1194 free_list;
1195 free_list = p;
1196 }
1197#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1198 else if (-NSMALLNEGINTS <= p->ob_ival &&
1199 p->ob_ival < NSMALLPOSINTS &&
1200 small_ints[p->ob_ival +
1201 NSMALLNEGINTS] == NULL) {
1202 Py_INCREF(p);
1203 small_ints[p->ob_ival +
1204 NSMALLNEGINTS] = p;
1205 }
1206#endif
1207 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001208 }
1209 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001210 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001211 bf++;
1212 }
1213 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001214 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001215 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001216 if (!Py_VerboseFlag)
1217 return;
1218 fprintf(stderr, "# cleanup ints");
1219 if (!isum) {
1220 fprintf(stderr, "\n");
1221 }
1222 else {
1223 fprintf(stderr,
1224 ": %d unfreed int%s in %d out of %d block%s\n",
1225 isum, isum == 1 ? "" : "s",
1226 bc - bf, bc, bc == 1 ? "" : "s");
1227 }
1228 if (Py_VerboseFlag > 1) {
1229 list = block_list;
1230 while (list != NULL) {
Skip Montanaro429433b2006-04-18 00:35:43 +00001231 for (ctr = 0, p = &list->objects[0];
1232 ctr < N_INTOBJECTS;
1233 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001234 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001235 /* XXX(twouters) cast refcount to
1236 long until %zd is universally
1237 available
1238 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001239 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001240 "# <int at %p, refcnt=%ld, val=%ld>\n",
1241 p, (long)p->ob_refcnt,
1242 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001243 }
1244 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001245 }
1246 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001247}