blob: f07040445ad3b6d3875105bc972824294a2d8710 [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");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 Wouters49fd7fa2006-04-21 10:40:58 +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 Wouters49fd7fa2006-04-21 10:40:58 +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;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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 *
Neal Norwitz4886cc32006-08-21 17:06:07 +0000583int_floor_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:
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000593 return PyLong_Type.tp_as_number->nb_floor_divide((PyObject *)x,
594 (PyObject *)y);
Guido van Rossum393661d2001-08-31 17:40:15 +0000595 default:
596 return NULL;
597 }
598}
599
600static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000601int_true_divide(PyObject *v, PyObject *w)
602{
Tim Peterse2a60002001-09-04 06:17:36 +0000603 /* If they aren't both ints, give someone else a chance. In
604 particular, this lets int/long get handled by longs, which
605 underflows to 0 gracefully if the long is too big to convert
606 to float. */
607 if (PyInt_Check(v) && PyInt_Check(w))
608 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
609 Py_INCREF(Py_NotImplemented);
610 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000611}
612
613static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000614int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000615{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000616 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000617 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000618 CONVERT_TO_LONG(x, xi);
619 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000620 switch (i_divmod(xi, yi, &d, &m)) {
621 case DIVMOD_OK:
622 return PyInt_FromLong(m);
623 case DIVMOD_OVERFLOW:
624 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
625 (PyObject *)y);
626 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000627 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000628 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000629}
630
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000632int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000633{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000634 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000635 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000636 CONVERT_TO_LONG(x, xi);
637 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000638 switch (i_divmod(xi, yi, &d, &m)) {
639 case DIVMOD_OK:
640 return Py_BuildValue("(ll)", d, m);
641 case DIVMOD_OVERFLOW:
642 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
643 (PyObject *)y);
644 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000645 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000646 }
Guido van Rossum00466951991-05-05 20:08:27 +0000647}
648
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000650int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000651{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000652 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000653 CONVERT_TO_LONG(v, iv);
654 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000655 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000656 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000657 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
658 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000659 return NULL;
660 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000661 /* Return a float. This works because we know that
662 this calls float_pow() which converts its
663 arguments to double. */
664 return PyFloat_Type.tp_as_number->nb_power(
665 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000666 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000668 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000669 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000671 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000672 return NULL;
673 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000674 }
675 /*
676 * XXX: The original exponentiation code stopped looping
677 * when temp hit zero; this code will continue onwards
678 * unnecessarily, but at least it won't cause any errors.
679 * Hopefully the speed improvement from the fast exponentiation
680 * will compensate for the slight inefficiency.
681 * XXX: Better handling of overflows is desperately needed.
682 */
683 temp = iv;
684 ix = 1;
685 while (iw > 0) {
686 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000687 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000688 ix = ix*temp;
689 if (temp == 0)
690 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000691 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000692 return PyLong_Type.tp_as_number->nb_power(
693 (PyObject *)v,
694 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000695 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000696 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000697 }
698 iw >>= 1; /* Shift exponent down by 1 bit */
699 if (iw==0) break;
700 prev = temp;
701 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000702 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000703 return PyLong_Type.tp_as_number->nb_power(
704 (PyObject *)v, (PyObject *)w, (PyObject *)z);
705 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000706 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000707 /* If we did a multiplication, perform a modulo */
708 ix = ix % iz;
709 temp = temp % iz;
710 }
711 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000712 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000713 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000714 switch (i_divmod(ix, iz, &div, &mod)) {
715 case DIVMOD_OK:
716 ix = mod;
717 break;
718 case DIVMOD_OVERFLOW:
719 return PyLong_Type.tp_as_number->nb_power(
720 (PyObject *)v, (PyObject *)w, (PyObject *)z);
721 default:
722 return NULL;
723 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000724 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000725 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000726}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000728static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000729int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000730{
731 register long a, x;
732 a = v->ob_ival;
733 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000734 if (a < 0 && x < 0) {
Tim Petersc8854432004-08-25 02:14:08 +0000735 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000736 if (o != NULL) {
737 PyObject *result = PyNumber_Negative(o);
738 Py_DECREF(o);
739 return result;
740 }
741 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000742 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000743 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000744}
745
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000746static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000747int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000748{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000749 if (PyInt_CheckExact(v)) {
750 Py_INCREF(v);
751 return (PyObject *)v;
752 }
753 else
754 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755}
756
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000758int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000759{
760 if (v->ob_ival >= 0)
761 return int_pos(v);
762 else
763 return int_neg(v);
764}
765
Guido van Rossum0bff0151991-05-14 12:05:32 +0000766static int
Fred Drakea2f55112000-07-09 15:16:51 +0000767int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000768{
769 return v->ob_ival != 0;
770}
771
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000772static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000773int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000774{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000776}
777
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000778static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000779int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000780{
Guido van Rossum078151d2002-08-11 04:24:12 +0000781 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000782 PyObject *vv, *ww, *result;
783
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000784 CONVERT_TO_LONG(v, a);
785 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000786 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000788 return NULL;
789 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000790 if (a == 0 || b == 0)
791 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000792 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000793 vv = PyLong_FromLong(PyInt_AS_LONG(v));
794 if (vv == NULL)
795 return NULL;
796 ww = PyLong_FromLong(PyInt_AS_LONG(w));
797 if (ww == NULL) {
798 Py_DECREF(vv);
799 return NULL;
800 }
801 result = PyNumber_Lshift(vv, ww);
802 Py_DECREF(vv);
803 Py_DECREF(ww);
804 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000805 }
Tim Petersda1a2212002-08-11 17:54:42 +0000806 c = a << b;
807 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000808 vv = PyLong_FromLong(PyInt_AS_LONG(v));
809 if (vv == NULL)
810 return NULL;
811 ww = PyLong_FromLong(PyInt_AS_LONG(w));
812 if (ww == NULL) {
813 Py_DECREF(vv);
814 return NULL;
815 }
816 result = PyNumber_Lshift(vv, ww);
817 Py_DECREF(vv);
818 Py_DECREF(ww);
819 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000820 }
821 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000822}
823
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000825int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000826{
827 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000828 CONVERT_TO_LONG(v, a);
829 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000830 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000831 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000832 return NULL;
833 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000834 if (a == 0 || b == 0)
835 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000836 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000837 if (a < 0)
838 a = -1;
839 else
840 a = 0;
841 }
842 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000843 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000844 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000846}
847
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000848static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000849int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000850{
851 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000852 CONVERT_TO_LONG(v, a);
853 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000854 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000855}
856
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000857static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000858int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000859{
860 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000861 CONVERT_TO_LONG(v, a);
862 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000863 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000864}
865
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000866static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000867int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000868{
869 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000870 CONVERT_TO_LONG(v, a);
871 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000872 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000873}
874
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000876int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000877{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000878 if (PyInt_CheckExact(v))
879 Py_INCREF(v);
880 else
881 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000882 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000883}
884
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000885static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000886int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000887{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000889}
890
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000891static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000892int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000893{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000894 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000895}
896
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000898int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000899{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000900 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000901 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000902 if (x < 0)
903 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
904 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000905 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000906 else
Barry Warsaw61975092001-11-28 20:55:34 +0000907 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000909}
910
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000911static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000912int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000913{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000914 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000915 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000916 if (x < 0)
917 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
918 else
919 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000920 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000921}
922
Jeremy Hylton938ace62002-07-17 16:30:39 +0000923static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000924int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
925
Tim Peters6d6c1a32001-08-02 04:15:00 +0000926static PyObject *
927int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
928{
929 PyObject *x = NULL;
930 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000931 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000932
Guido van Rossumbef14172001-08-29 15:47:46 +0000933 if (type != &PyInt_Type)
934 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000935 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
936 &x, &base))
937 return NULL;
938 if (x == NULL)
939 return PyInt_FromLong(0L);
940 if (base == -909)
941 return PyNumber_Int(x);
942 if (PyString_Check(x))
943 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000944#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000945 if (PyUnicode_Check(x))
946 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
947 PyUnicode_GET_SIZE(x),
948 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000949#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950 PyErr_SetString(PyExc_TypeError,
951 "int() can't convert non-string with explicit base");
952 return NULL;
953}
954
Guido van Rossumbef14172001-08-29 15:47:46 +0000955/* Wimpy, slow approach to tp_new calls for subtypes of int:
956 first create a regular int from whatever arguments we got,
957 then allocate a subtype instance and initialize its ob_ival
958 from the regular int. The regular int is then thrown away.
959*/
960static PyObject *
961int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
962{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000963 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000964 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000965
966 assert(PyType_IsSubtype(type, &PyInt_Type));
967 tmp = int_new(&PyInt_Type, args, kwds);
968 if (tmp == NULL)
969 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000970 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000971 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000972 if (ival == -1 && PyErr_Occurred()) {
973 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000974 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +0000975 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +0000976 } else {
977 ival = ((PyIntObject *)tmp)->ob_ival;
978 }
979
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000980 newobj = type->tp_alloc(type, 0);
981 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +0000982 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +0000983 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +0000984 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000985 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +0000986 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000987 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +0000988}
989
Guido van Rossum5d9113d2003-01-29 17:58:45 +0000990static PyObject *
991int_getnewargs(PyIntObject *v)
992{
993 return Py_BuildValue("(l)", v->ob_ival);
994}
995
996static PyMethodDef int_methods[] = {
997 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
998 {NULL, NULL} /* sentinel */
999};
1000
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001001PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001002"int(x[, base]) -> integer\n\
1003\n\
1004Convert a string or number to an integer, if possible. A floating point\n\
1005argument will be truncated towards zero (this does not include a string\n\
1006representation of a floating point number!) When converting a string, use\n\
1007the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +00001008non-string. If the argument is outside the integer range a long object\n\
1009will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001010
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001011static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001012 (binaryfunc)int_add, /*nb_add*/
1013 (binaryfunc)int_sub, /*nb_subtract*/
1014 (binaryfunc)int_mul, /*nb_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001015 (binaryfunc)int_mod, /*nb_remainder*/
1016 (binaryfunc)int_divmod, /*nb_divmod*/
1017 (ternaryfunc)int_pow, /*nb_power*/
1018 (unaryfunc)int_neg, /*nb_negative*/
1019 (unaryfunc)int_pos, /*nb_positive*/
1020 (unaryfunc)int_abs, /*nb_absolute*/
1021 (inquiry)int_nonzero, /*nb_nonzero*/
1022 (unaryfunc)int_invert, /*nb_invert*/
1023 (binaryfunc)int_lshift, /*nb_lshift*/
1024 (binaryfunc)int_rshift, /*nb_rshift*/
1025 (binaryfunc)int_and, /*nb_and*/
1026 (binaryfunc)int_xor, /*nb_xor*/
1027 (binaryfunc)int_or, /*nb_or*/
Neal Norwitz4886cc32006-08-21 17:06:07 +00001028 0, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001029 (unaryfunc)int_int, /*nb_int*/
1030 (unaryfunc)int_long, /*nb_long*/
1031 (unaryfunc)int_float, /*nb_float*/
1032 (unaryfunc)int_oct, /*nb_oct*/
1033 (unaryfunc)int_hex, /*nb_hex*/
1034 0, /*nb_inplace_add*/
1035 0, /*nb_inplace_subtract*/
1036 0, /*nb_inplace_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001037 0, /*nb_inplace_remainder*/
1038 0, /*nb_inplace_power*/
1039 0, /*nb_inplace_lshift*/
1040 0, /*nb_inplace_rshift*/
1041 0, /*nb_inplace_and*/
1042 0, /*nb_inplace_xor*/
1043 0, /*nb_inplace_or*/
Neal Norwitz4886cc32006-08-21 17:06:07 +00001044 (binaryfunc)int_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00001045 int_true_divide, /* nb_true_divide */
1046 0, /* nb_inplace_floor_divide */
1047 0, /* nb_inplace_true_divide */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001048 PyInt_AsSsize_t, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001049};
1050
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001051PyTypeObject PyInt_Type = {
1052 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001053 0,
1054 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001055 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001056 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001057 (destructor)int_dealloc, /* tp_dealloc */
1058 (printfunc)int_print, /* tp_print */
1059 0, /* tp_getattr */
1060 0, /* tp_setattr */
1061 (cmpfunc)int_compare, /* tp_compare */
1062 (reprfunc)int_repr, /* tp_repr */
1063 &int_as_number, /* tp_as_number */
1064 0, /* tp_as_sequence */
1065 0, /* tp_as_mapping */
1066 (hashfunc)int_hash, /* tp_hash */
1067 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001068 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001069 PyObject_GenericGetAttr, /* tp_getattro */
1070 0, /* tp_setattro */
1071 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001072 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001073 int_doc, /* tp_doc */
1074 0, /* tp_traverse */
1075 0, /* tp_clear */
1076 0, /* tp_richcompare */
1077 0, /* tp_weaklistoffset */
1078 0, /* tp_iter */
1079 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001080 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001081 0, /* tp_members */
1082 0, /* tp_getset */
1083 0, /* tp_base */
1084 0, /* tp_dict */
1085 0, /* tp_descr_get */
1086 0, /* tp_descr_set */
1087 0, /* tp_dictoffset */
1088 0, /* tp_init */
1089 0, /* tp_alloc */
1090 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001091 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001092};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001093
Neal Norwitzc91ed402002-12-30 22:29:22 +00001094int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001095_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001096{
1097 PyIntObject *v;
1098 int ival;
1099#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1100 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001101 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001102 return 0;
1103 /* PyObject_New is inlined */
1104 v = free_list;
1105 free_list = (PyIntObject *)v->ob_type;
1106 PyObject_INIT(v, &PyInt_Type);
1107 v->ob_ival = ival;
1108 small_ints[ival + NSMALLNEGINTS] = v;
1109 }
1110#endif
1111 return 1;
1112}
1113
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001114void
Fred Drakea2f55112000-07-09 15:16:51 +00001115PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001116{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001117 PyIntObject *p;
1118 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001119 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001120 unsigned int ctr;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001121 int bc, bf; /* block count, number of freed blocks */
1122 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001123
Guido van Rossumda084ed1999-03-10 22:55:24 +00001124#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1125 PyIntObject **q;
1126
1127 i = NSMALLNEGINTS + NSMALLPOSINTS;
1128 q = small_ints;
1129 while (--i >= 0) {
1130 Py_XDECREF(*q);
1131 *q++ = NULL;
1132 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001133#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001134 bc = 0;
1135 bf = 0;
1136 isum = 0;
1137 list = block_list;
1138 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001139 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001140 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001141 bc++;
1142 irem = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143 for (ctr = 0, p = &list->objects[0];
1144 ctr < N_INTOBJECTS;
1145 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001146 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001147 irem++;
1148 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001149 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001150 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001151 list->next = block_list;
1152 block_list = list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001153 for (ctr = 0, p = &list->objects[0];
1154 ctr < N_INTOBJECTS;
1155 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001156 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001157 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001158 p->ob_type = (struct _typeobject *)
1159 free_list;
1160 free_list = p;
1161 }
1162#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1163 else if (-NSMALLNEGINTS <= p->ob_ival &&
1164 p->ob_ival < NSMALLPOSINTS &&
1165 small_ints[p->ob_ival +
1166 NSMALLNEGINTS] == NULL) {
1167 Py_INCREF(p);
1168 small_ints[p->ob_ival +
1169 NSMALLNEGINTS] = p;
1170 }
1171#endif
1172 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001173 }
1174 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001175 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001176 bf++;
1177 }
1178 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001179 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001180 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001181 if (!Py_VerboseFlag)
1182 return;
1183 fprintf(stderr, "# cleanup ints");
1184 if (!isum) {
1185 fprintf(stderr, "\n");
1186 }
1187 else {
1188 fprintf(stderr,
1189 ": %d unfreed int%s in %d out of %d block%s\n",
1190 isum, isum == 1 ? "" : "s",
1191 bc - bf, bc, bc == 1 ? "" : "s");
1192 }
1193 if (Py_VerboseFlag > 1) {
1194 list = block_list;
1195 while (list != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001196 for (ctr = 0, p = &list->objects[0];
1197 ctr < N_INTOBJECTS;
1198 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001199 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001200 /* XXX(twouters) cast refcount to
1201 long until %zd is universally
1202 available
1203 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001204 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001205 "# <int at %p, refcnt=%ld, val=%ld>\n",
1206 p, (long)p->ob_refcnt,
1207 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001208 }
1209 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001210 }
1211 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001212}