blob: 31d8130e60b085253e1f996543855c54b7921bc1 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Integer object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Barry Warsaw226ae6c1999-10-12 19:54:53 +00005#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum2e1d4331993-12-24 10:22:45 +00007long
Fred Drakea2f55112000-07-09 15:16:51 +00008PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +00009{
10 return LONG_MAX; /* To initialize sys.maxint */
11}
12
Guido van Rossumddefaf32007-01-14 03:31:43 +000013#if 0
Guido van Rossum3f5da241990-12-20 15:06:42 +000014/* Integers are quite normal objects, to make object handling uniform.
15 (Using odd pointers to represent integers would save much space
16 but require extra checks for this special case throughout the code.)
Tim Peters29c0afc2002-04-28 16:57:34 +000017 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000018 and deallocating integers, these operations should be very fast.
19 Therefore we use a dedicated allocation scheme with a much lower
20 overhead (in space and time) than straight malloc(): a simple
21 dedicated free list, filled when necessary with memory from malloc().
Tim Peters29c0afc2002-04-28 16:57:34 +000022
23 block_list is a singly-linked list of all PyIntBlocks ever allocated,
24 linked via their next members. PyIntBlocks are never returned to the
25 system before shutdown (PyInt_Fini).
26
27 free_list is a singly-linked list of available PyIntObjects, linked
28 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000029*/
30
31#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000032#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
33#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000034
Guido van Rossum3fce8831999-03-12 19:43:17 +000035struct _intblock {
36 struct _intblock *next;
37 PyIntObject objects[N_INTOBJECTS];
38};
39
40typedef struct _intblock PyIntBlock;
41
42static PyIntBlock *block_list = NULL;
43static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000044
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000046fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000047{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000049 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000050 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000051 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000052 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000053 ((PyIntBlock *)p)->next = block_list;
54 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000055 /* Link the int objects together, from rear to front, then return
56 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000057 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000058 q = p + N_INTOBJECTS;
59 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000060 q->ob_type = (struct _typeobject *)(q-1);
61 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000062 return p + N_INTOBJECTS - 1;
63}
64
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000065#ifndef NSMALLPOSINTS
Georg Brandl418a1ef2006-02-22 11:30:06 +000066#define NSMALLPOSINTS 257
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000067#endif
68#ifndef NSMALLNEGINTS
Neal Norwitzc91ed402002-12-30 22:29:22 +000069#define NSMALLNEGINTS 5
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000070#endif
71#if NSMALLNEGINTS + NSMALLPOSINTS > 0
72/* References to small integers are saved in this array so that they
73 can be shared.
74 The integers that are saved are those in the range
75 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
76*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000078#endif
79#ifdef COUNT_ALLOCS
80int quick_int_allocs, quick_neg_int_allocs;
81#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000082
Guido van Rossumc0b618a1997-05-02 03:12:38 +000083PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000084PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000087#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Neal Norwitzc91ed402002-12-30 22:29:22 +000088 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
89 v = small_ints[ival + NSMALLNEGINTS];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000091#ifdef COUNT_ALLOCS
92 if (ival >= 0)
93 quick_int_allocs++;
94 else
95 quick_neg_int_allocs++;
96#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000098 }
99#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000100 if (free_list == NULL) {
101 if ((free_list = fill_free_list()) == NULL)
102 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000104 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000105 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000106 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000107 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110}
111
Martin v. Löwis18e16552006-02-15 17:27:45 +0000112PyObject *
113PyInt_FromSize_t(size_t ival)
114{
115 if (ival <= LONG_MAX)
116 return PyInt_FromLong((long)ival);
117 return _PyLong_FromSize_t(ival);
118}
119
120PyObject *
121PyInt_FromSsize_t(Py_ssize_t ival)
122{
123 if (ival >= LONG_MIN && ival <= LONG_MAX)
124 return PyInt_FromLong((long)ival);
125 return _PyLong_FromSsize_t(ival);
126}
127
Guido van Rossum3f5da241990-12-20 15:06:42 +0000128static void
Fred Drakea2f55112000-07-09 15:16:51 +0000129int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000130{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000131 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000132 v->ob_type = (struct _typeobject *)free_list;
133 free_list = v;
134 }
135 else
136 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137}
138
Guido van Rossum93646982002-04-26 00:53:34 +0000139static void
140int_free(PyIntObject *v)
141{
142 v->ob_type = (struct _typeobject *)free_list;
143 free_list = v;
144}
145
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146long
Fred Drakea2f55112000-07-09 15:16:51 +0000147PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 PyNumberMethods *nb;
150 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000151 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000152
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000153 if (op && PyInt_Check(op))
154 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000155
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000156 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
157 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000158 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000159 return -1;
160 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000161
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000163 if (io == NULL)
164 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000166 if (PyLong_Check(io)) {
167 /* got a long? => retry int conversion */
168 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000169 Py_DECREF(io);
170 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000171 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000172 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000173 }
174 else
175 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000176 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000177 PyErr_SetString(PyExc_TypeError,
178 "nb_int should return int object");
179 return -1;
180 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000181 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000182
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000183 val = PyInt_AS_LONG(io);
184 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000185
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000186 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000187}
188
Martin v. Löwis18e16552006-02-15 17:27:45 +0000189Py_ssize_t
190PyInt_AsSsize_t(register PyObject *op)
191{
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000192#if SIZEOF_SIZE_T != SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000193 PyNumberMethods *nb;
194 PyIntObject *io;
195 Py_ssize_t val;
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000196#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000197
198 if (op == NULL) {
199 PyErr_SetString(PyExc_TypeError, "an integer is required");
200 return -1;
201 }
202
203 if (PyInt_Check(op))
204 return PyInt_AS_LONG((PyIntObject*) op);
205 if (PyLong_Check(op))
Martin v. Löwis18e16552006-02-15 17:27:45 +0000206 return _PyLong_AsSsize_t(op);
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000207#if SIZEOF_SIZE_T == SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000208 return PyInt_AsLong(op);
209#else
210
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000211 if ((nb = op->ob_type->tp_as_number) == NULL ||
Martin v. Löwis18e16552006-02-15 17:27:45 +0000212 (nb->nb_int == NULL && nb->nb_long == 0)) {
213 PyErr_SetString(PyExc_TypeError, "an integer is required");
214 return -1;
215 }
216
217 if (nb->nb_long != 0) {
218 io = (PyIntObject*) (*nb->nb_long) (op);
219 } else {
220 io = (PyIntObject*) (*nb->nb_int) (op);
221 }
222 if (io == NULL)
223 return -1;
224 if (!PyInt_Check(io)) {
225 if (PyLong_Check(io)) {
226 /* got a long? => retry int conversion */
227 val = _PyLong_AsSsize_t((PyObject *)io);
228 Py_DECREF(io);
229 if ((val == -1) && PyErr_Occurred())
230 return -1;
231 return val;
232 }
233 else
234 {
235 Py_DECREF(io);
236 PyErr_SetString(PyExc_TypeError,
237 "nb_int should return int object");
238 return -1;
239 }
240 }
241
242 val = PyInt_AS_LONG(io);
243 Py_DECREF(io);
244
245 return val;
246#endif
247}
248
Thomas Hellera4ea6032003-04-17 18:55:45 +0000249unsigned long
250PyInt_AsUnsignedLongMask(register PyObject *op)
251{
252 PyNumberMethods *nb;
253 PyIntObject *io;
254 unsigned long val;
255
256 if (op && PyInt_Check(op))
257 return PyInt_AS_LONG((PyIntObject*) op);
258 if (op && PyLong_Check(op))
259 return PyLong_AsUnsignedLongMask(op);
260
261 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
262 nb->nb_int == NULL) {
263 PyErr_SetString(PyExc_TypeError, "an integer is required");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000264 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000265 }
266
267 io = (PyIntObject*) (*nb->nb_int) (op);
268 if (io == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000270 if (!PyInt_Check(io)) {
271 if (PyLong_Check(io)) {
272 val = PyLong_AsUnsignedLongMask((PyObject *)io);
273 Py_DECREF(io);
274 if (PyErr_Occurred())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000275 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000276 return val;
277 }
278 else
279 {
280 Py_DECREF(io);
281 PyErr_SetString(PyExc_TypeError,
282 "nb_int should return int object");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000284 }
285 }
286
287 val = PyInt_AS_LONG(io);
288 Py_DECREF(io);
289
290 return val;
291}
292
293#ifdef HAVE_LONG_LONG
294unsigned PY_LONG_LONG
295PyInt_AsUnsignedLongLongMask(register PyObject *op)
296{
297 PyNumberMethods *nb;
298 PyIntObject *io;
299 unsigned PY_LONG_LONG val;
300
301 if (op && PyInt_Check(op))
302 return PyInt_AS_LONG((PyIntObject*) op);
303 if (op && PyLong_Check(op))
304 return PyLong_AsUnsignedLongLongMask(op);
305
306 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
307 nb->nb_int == NULL) {
308 PyErr_SetString(PyExc_TypeError, "an integer is required");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000309 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000310 }
311
312 io = (PyIntObject*) (*nb->nb_int) (op);
313 if (io == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000314 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000315 if (!PyInt_Check(io)) {
316 if (PyLong_Check(io)) {
317 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
318 Py_DECREF(io);
319 if (PyErr_Occurred())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000320 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000321 return val;
322 }
323 else
324 {
325 Py_DECREF(io);
326 PyErr_SetString(PyExc_TypeError,
327 "nb_int should return int object");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000328 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000329 }
330 }
331
332 val = PyInt_AS_LONG(io);
333 Py_DECREF(io);
334
335 return val;
336}
337#endif
338
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000339PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000340PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000341{
342 char *end;
343 long x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000344 Py_ssize_t slen;
345 PyObject *sobj, *srepr;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000346
347 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000348 PyErr_SetString(PyExc_ValueError,
349 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000350 return NULL;
351 }
352
353 while (*s && isspace(Py_CHARMASK(*s)))
354 s++;
355 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000356 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000357 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000358 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000359 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000360 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000361 else
362 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000363 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000364 goto bad;
365 while (*end && isspace(Py_CHARMASK(*end)))
366 end++;
367 if (*end != '\0') {
368 bad:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369 slen = strlen(s) < 200 ? strlen(s) : 200;
370 sobj = PyString_FromStringAndSize(s, slen);
371 if (sobj == NULL)
372 return NULL;
373 srepr = PyObject_Repr(sobj);
374 Py_DECREF(sobj);
375 if (srepr == NULL)
376 return NULL;
377 PyErr_Format(PyExc_ValueError,
378 "invalid literal for int() with base %d: %s",
379 base, PyString_AS_STRING(srepr));
380 Py_DECREF(srepr);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000381 return NULL;
382 }
Tim Petersc8854432004-08-25 02:14:08 +0000383 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000384 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000385 if (pend)
386 *pend = end;
387 return PyInt_FromLong(x);
388}
389
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000390#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000391PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000392PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000393{
Walter Dörwald07e14762002-11-06 16:15:14 +0000394 PyObject *result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000395 char *buffer = (char *)PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000396
Walter Dörwald07e14762002-11-06 16:15:14 +0000397 if (buffer == NULL)
398 return NULL;
399
400 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
401 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000402 return NULL;
403 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000404 result = PyInt_FromString(buffer, NULL, base);
405 PyMem_FREE(buffer);
406 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000407}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000408#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000409
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410/* Methods */
411
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000412/* Integers are seen as the "smallest" of all numeric types and thus
413 don't have any knowledge about conversion of other types to
414 integers. */
415
416#define CONVERT_TO_LONG(obj, lng) \
417 if (PyInt_Check(obj)) { \
418 lng = PyInt_AS_LONG(obj); \
419 } \
420 else { \
421 Py_INCREF(Py_NotImplemented); \
422 return Py_NotImplemented; \
423 }
424
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000425/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000426static int
Fred Drakea2f55112000-07-09 15:16:51 +0000427int_print(PyIntObject *v, FILE *fp, int flags)
428 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429{
430 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000431 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432}
433
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000434static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000435int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436{
Tim Peters42221042001-12-01 02:52:56 +0000437 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000438 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440}
441
442static int
Fred Drakea2f55112000-07-09 15:16:51 +0000443int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
445 register long i = v->ob_ival;
446 register long j = w->ob_ival;
447 return (i < j) ? -1 : (i > j) ? 1 : 0;
448}
449
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000450static PyObject *
451int_richcompare(PyObject *self, PyObject *other, int op)
452{
453 if (!PyInt_Check(self) || !PyInt_Check(other)) {
454 Py_INCREF(Py_NotImplemented);
455 return Py_NotImplemented;
456 }
457 return Py_CmpToRich(op, int_compare((PyIntObject *)self,
458 (PyIntObject *)other));
459}
460
Guido van Rossum9bfef441993-03-29 10:43:31 +0000461static long
Fred Drakea2f55112000-07-09 15:16:51 +0000462int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000463{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000464 /* XXX If this is changed, you also need to change the way
465 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000466 long x = v -> ob_ival;
467 if (x == -1)
468 x = -2;
469 return x;
470}
471
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000473int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474{
475 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000476 CONVERT_TO_LONG(v, a);
477 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000479 if ((x^a) >= 0 || (x^b) >= 0)
480 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000481 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482}
483
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000485int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486{
487 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000488 CONVERT_TO_LONG(v, a);
489 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000491 if ((x^a) >= 0 || (x^~b) >= 0)
492 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000493 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
494 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000495}
496
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000497/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000498Integer overflow checking for * is painful: Python tried a couple ways, but
499they didn't work on all platforms, or failed in endcases (a product of
500-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000501
Tim Petersa3c01ce2001-12-04 23:05:10 +0000502Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000503
Tim Petersa3c01ce2001-12-04 23:05:10 +0000504The native long product x*y is either exactly right or *way* off, being
505just the last n bits of the true product, where n is the number of bits
506in a long (the delivered product is the true product plus i*2**n for
507some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000508
Tim Petersa3c01ce2001-12-04 23:05:10 +0000509The native double product (double)x * (double)y is subject to three
510rounding errors: on a sizeof(long)==8 box, each cast to double can lose
511info, and even on a sizeof(long)==4 box, the multiplication can lose info.
512But, unlike the native long product, it's not in *range* trouble: even
513if sizeof(long)==32 (256-bit longs), the product easily fits in the
514dynamic range of a double. So the leading 50 (or so) bits of the double
515product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000516
Tim Petersa3c01ce2001-12-04 23:05:10 +0000517We check these two ways against each other, and declare victory if they're
518approximately the same. Else, because the native long product is the only
519one that can lose catastrophic amounts of information, it's the native long
520product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000521*/
522
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000524int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000525{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000526 long a, b;
527 long longprod; /* a*b in native long arithmetic */
528 double doubled_longprod; /* (double)longprod */
529 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000530
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000531 CONVERT_TO_LONG(v, a);
532 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000533 longprod = a * b;
534 doubleprod = (double)a * (double)b;
535 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000536
Tim Petersa3c01ce2001-12-04 23:05:10 +0000537 /* Fast path for normal case: small multiplicands, and no info
538 is lost in either method. */
539 if (doubled_longprod == doubleprod)
540 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000541
Tim Petersa3c01ce2001-12-04 23:05:10 +0000542 /* Somebody somewhere lost info. Close enough, or way off? Note
543 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
544 The difference either is or isn't significant compared to the
545 true value (of which doubleprod is a good approximation).
546 */
547 {
548 const double diff = doubled_longprod - doubleprod;
549 const double absdiff = diff >= 0.0 ? diff : -diff;
550 const double absprod = doubleprod >= 0.0 ? doubleprod :
551 -doubleprod;
552 /* absdiff/absprod <= 1/32 iff
553 32 * absdiff <= absprod -- 5 good bits is "close enough" */
554 if (32.0 * absdiff <= absprod)
555 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000556 else
557 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000558 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000559}
560
Thomas Wouters89f507f2006-12-13 04:49:30 +0000561/* Integer overflow checking for unary negation: on a 2's-complement
562 * box, -x overflows iff x is the most negative long. In this case we
563 * get -x == x. However, -x is undefined (by C) if x /is/ the most
564 * negative long (it's a signed overflow case), and some compilers care.
565 * So we cast x to unsigned long first. However, then other compilers
566 * warn about applying unary minus to an unsigned operand. Hence the
567 * weird "0-".
568 */
569#define UNARY_NEG_WOULD_OVERFLOW(x) \
570 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
571
Guido van Rossume27f7952001-08-23 02:59:04 +0000572/* Return type of i_divmod */
573enum divmod_result {
574 DIVMOD_OK, /* Correct result */
575 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
576 DIVMOD_ERROR /* Exception raised */
577};
578
579static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000580i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000581 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000582{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000583 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000584
Tim Peters1dad6a82001-06-18 19:21:11 +0000585 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000587 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000588 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000590 /* (-sys.maxint-1)/-1 is the only overflow case. */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000591 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
Guido van Rossume27f7952001-08-23 02:59:04 +0000592 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000593 xdivy = x / y;
594 xmody = x - xdivy * y;
595 /* If the signs of x and y differ, and the remainder is non-0,
596 * C89 doesn't define whether xdivy is now the floor or the
597 * ceiling of the infinitely precise quotient. We want the floor,
598 * and we have it iff the remainder's sign matches y's.
599 */
600 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
601 xmody += y;
602 --xdivy;
603 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000604 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000605 *p_xdivy = xdivy;
606 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000607 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000608}
609
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610static PyObject *
Neal Norwitz4886cc32006-08-21 17:06:07 +0000611int_floor_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000612{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000613 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000614 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000615 CONVERT_TO_LONG(x, xi);
616 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000617 switch (i_divmod(xi, yi, &d, &m)) {
618 case DIVMOD_OK:
619 return PyInt_FromLong(d);
620 case DIVMOD_OVERFLOW:
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000621 return PyLong_Type.tp_as_number->nb_floor_divide((PyObject *)x,
622 (PyObject *)y);
Guido van Rossum393661d2001-08-31 17:40:15 +0000623 default:
624 return NULL;
625 }
626}
627
628static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000629int_true_divide(PyObject *v, PyObject *w)
630{
Tim Peterse2a60002001-09-04 06:17:36 +0000631 /* If they aren't both ints, give someone else a chance. In
632 particular, this lets int/long get handled by longs, which
633 underflows to 0 gracefully if the long is too big to convert
634 to float. */
635 if (PyInt_Check(v) && PyInt_Check(w))
636 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
637 Py_INCREF(Py_NotImplemented);
638 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000639}
640
641static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000642int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000643{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000644 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000645 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000646 CONVERT_TO_LONG(x, xi);
647 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000648 switch (i_divmod(xi, yi, &d, &m)) {
649 case DIVMOD_OK:
650 return PyInt_FromLong(m);
651 case DIVMOD_OVERFLOW:
652 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
653 (PyObject *)y);
654 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000655 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000656 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000657}
658
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000660int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000661{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000662 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000663 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000664 CONVERT_TO_LONG(x, xi);
665 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000666 switch (i_divmod(xi, yi, &d, &m)) {
667 case DIVMOD_OK:
668 return Py_BuildValue("(ll)", d, m);
669 case DIVMOD_OVERFLOW:
670 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
671 (PyObject *)y);
672 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000673 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000674 }
Guido van Rossum00466951991-05-05 20:08:27 +0000675}
676
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000678int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000679{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000680 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000681 CONVERT_TO_LONG(v, iv);
682 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000683 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000684 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000685 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
686 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000687 return NULL;
688 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000689 /* Return a float. This works because we know that
690 this calls float_pow() which converts its
691 arguments to double. */
692 return PyFloat_Type.tp_as_number->nb_power(
693 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000694 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000696 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000697 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000699 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000700 return NULL;
701 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000702 }
703 /*
704 * XXX: The original exponentiation code stopped looping
705 * when temp hit zero; this code will continue onwards
706 * unnecessarily, but at least it won't cause any errors.
707 * Hopefully the speed improvement from the fast exponentiation
708 * will compensate for the slight inefficiency.
709 * XXX: Better handling of overflows is desperately needed.
710 */
711 temp = iv;
712 ix = 1;
713 while (iw > 0) {
714 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000715 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000716 ix = ix*temp;
717 if (temp == 0)
718 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000719 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000720 return PyLong_Type.tp_as_number->nb_power(
721 (PyObject *)v,
722 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000723 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000724 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000725 }
726 iw >>= 1; /* Shift exponent down by 1 bit */
727 if (iw==0) break;
728 prev = temp;
729 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000730 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000731 return PyLong_Type.tp_as_number->nb_power(
732 (PyObject *)v, (PyObject *)w, (PyObject *)z);
733 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000734 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000735 /* If we did a multiplication, perform a modulo */
736 ix = ix % iz;
737 temp = temp % iz;
738 }
739 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000740 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000741 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000742 switch (i_divmod(ix, iz, &div, &mod)) {
743 case DIVMOD_OK:
744 ix = mod;
745 break;
746 case DIVMOD_OVERFLOW:
747 return PyLong_Type.tp_as_number->nb_power(
748 (PyObject *)v, (PyObject *)w, (PyObject *)z);
749 default:
750 return NULL;
751 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000752 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000754}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000757int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000759 register long a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000760 a = v->ob_ival;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000761 /* check for overflow */
762 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
Tim Petersc8854432004-08-25 02:14:08 +0000763 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000764 if (o != NULL) {
765 PyObject *result = PyNumber_Negative(o);
766 Py_DECREF(o);
767 return result;
768 }
769 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000770 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000771 return PyInt_FromLong(-a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772}
773
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000775int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000776{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000777 if (PyInt_CheckExact(v)) {
778 Py_INCREF(v);
779 return (PyObject *)v;
780 }
781 else
782 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000783}
784
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000785static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000786int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000787{
788 if (v->ob_ival >= 0)
789 return int_pos(v);
790 else
791 return int_neg(v);
792}
793
Guido van Rossum0bff0151991-05-14 12:05:32 +0000794static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000795int_bool(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000796{
797 return v->ob_ival != 0;
798}
799
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000800static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000801int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000802{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000804}
805
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000806static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000807int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000808{
Guido van Rossum078151d2002-08-11 04:24:12 +0000809 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000810 PyObject *vv, *ww, *result;
811
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000812 CONVERT_TO_LONG(v, a);
813 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000814 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000816 return NULL;
817 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000818 if (a == 0 || b == 0)
819 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000820 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000821 vv = PyLong_FromLong(PyInt_AS_LONG(v));
822 if (vv == NULL)
823 return NULL;
824 ww = PyLong_FromLong(PyInt_AS_LONG(w));
825 if (ww == NULL) {
826 Py_DECREF(vv);
827 return NULL;
828 }
829 result = PyNumber_Lshift(vv, ww);
830 Py_DECREF(vv);
831 Py_DECREF(ww);
832 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000833 }
Tim Petersda1a2212002-08-11 17:54:42 +0000834 c = a << b;
835 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000836 vv = PyLong_FromLong(PyInt_AS_LONG(v));
837 if (vv == NULL)
838 return NULL;
839 ww = PyLong_FromLong(PyInt_AS_LONG(w));
840 if (ww == NULL) {
841 Py_DECREF(vv);
842 return NULL;
843 }
844 result = PyNumber_Lshift(vv, ww);
845 Py_DECREF(vv);
846 Py_DECREF(ww);
847 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000848 }
849 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000850}
851
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000852static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000853int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000854{
855 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000856 CONVERT_TO_LONG(v, a);
857 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000858 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000859 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000860 return NULL;
861 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000862 if (a == 0 || b == 0)
863 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000864 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000865 if (a < 0)
866 a = -1;
867 else
868 a = 0;
869 }
870 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000871 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000872 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000874}
875
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000876static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000877int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000878{
879 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000880 CONVERT_TO_LONG(v, a);
881 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000882 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000883}
884
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000885static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000886int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000887{
888 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000889 CONVERT_TO_LONG(v, a);
890 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000891 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000892}
893
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000894static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000895int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000896{
897 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000898 CONVERT_TO_LONG(v, a);
899 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000900 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000901}
902
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000903static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000904int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000905{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000906 if (PyInt_CheckExact(v))
907 Py_INCREF(v);
908 else
909 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000911}
912
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000913static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000914int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000915{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000916 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000917}
918
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000919static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000920int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000921{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000922 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000923}
924
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000925static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000926int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000927{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000928 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000929 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000930 if (x < 0)
931 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
932 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000933 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000934 else
Barry Warsaw61975092001-11-28 20:55:34 +0000935 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000936 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000937}
938
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000939static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000940int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000941{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000942 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000943 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000944 if (x < 0)
945 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
946 else
947 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000948 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000949}
950
Jeremy Hylton938ace62002-07-17 16:30:39 +0000951static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000952int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
953
Tim Peters6d6c1a32001-08-02 04:15:00 +0000954static PyObject *
955int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
956{
957 PyObject *x = NULL;
958 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000959 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000960
Guido van Rossumbef14172001-08-29 15:47:46 +0000961 if (type != &PyInt_Type)
962 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000963 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
964 &x, &base))
965 return NULL;
966 if (x == NULL)
967 return PyInt_FromLong(0L);
968 if (base == -909)
969 return PyNumber_Int(x);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000970 if (PyString_Check(x)) {
971 /* Since PyInt_FromString doesn't have a length parameter,
972 * check here for possible NULs in the string. */
973 char *string = PyString_AS_STRING(x);
974 if (strlen(string) != PyString_Size(x)) {
975 /* create a repr() of the input string,
976 * just like PyInt_FromString does */
977 PyObject *srepr;
978 srepr = PyObject_Repr(x);
979 if (srepr == NULL)
980 return NULL;
981 PyErr_Format(PyExc_ValueError,
982 "invalid literal for int() with base %d: %s",
983 base, PyString_AS_STRING(srepr));
984 Py_DECREF(srepr);
985 return NULL;
986 }
987 return PyInt_FromString(string, NULL, base);
988 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000989#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990 if (PyUnicode_Check(x))
991 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
992 PyUnicode_GET_SIZE(x),
993 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000994#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000995 PyErr_SetString(PyExc_TypeError,
996 "int() can't convert non-string with explicit base");
997 return NULL;
998}
999
Guido van Rossumbef14172001-08-29 15:47:46 +00001000/* Wimpy, slow approach to tp_new calls for subtypes of int:
1001 first create a regular int from whatever arguments we got,
1002 then allocate a subtype instance and initialize its ob_ival
1003 from the regular int. The regular int is then thrown away.
1004*/
1005static PyObject *
1006int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1007{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001008 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001009 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001010
1011 assert(PyType_IsSubtype(type, &PyInt_Type));
1012 tmp = int_new(&PyInt_Type, args, kwds);
1013 if (tmp == NULL)
1014 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001015 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001016 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001017 if (ival == -1 && PyErr_Occurred()) {
1018 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001019 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001020 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001021 } else {
1022 ival = ((PyIntObject *)tmp)->ob_ival;
1023 }
1024
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001025 newobj = type->tp_alloc(type, 0);
1026 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001027 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001028 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001029 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001030 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001031 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001032 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001033}
1034
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001035static PyObject *
1036int_getnewargs(PyIntObject *v)
1037{
1038 return Py_BuildValue("(l)", v->ob_ival);
1039}
1040
1041static PyMethodDef int_methods[] = {
1042 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
1043 {NULL, NULL} /* sentinel */
1044};
1045
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001046PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001047"int(x[, base]) -> integer\n\
1048\n\
1049Convert a string or number to an integer, if possible. A floating point\n\
1050argument will be truncated towards zero (this does not include a string\n\
1051representation of a floating point number!) When converting a string, use\n\
1052the optional base. It is an error to supply a base when converting a\n\
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001053non-string. If base is zero, the proper base is guessed based on the\n\
1054string content. If the argument is outside the integer range a\n\
1055long object will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001056
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001057static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001058 (binaryfunc)int_add, /*nb_add*/
1059 (binaryfunc)int_sub, /*nb_subtract*/
1060 (binaryfunc)int_mul, /*nb_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001061 (binaryfunc)int_mod, /*nb_remainder*/
1062 (binaryfunc)int_divmod, /*nb_divmod*/
1063 (ternaryfunc)int_pow, /*nb_power*/
1064 (unaryfunc)int_neg, /*nb_negative*/
1065 (unaryfunc)int_pos, /*nb_positive*/
1066 (unaryfunc)int_abs, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001067 (inquiry)int_bool, /*nb_bool*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001068 (unaryfunc)int_invert, /*nb_invert*/
1069 (binaryfunc)int_lshift, /*nb_lshift*/
1070 (binaryfunc)int_rshift, /*nb_rshift*/
1071 (binaryfunc)int_and, /*nb_and*/
1072 (binaryfunc)int_xor, /*nb_xor*/
1073 (binaryfunc)int_or, /*nb_or*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001074 0, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001075 (unaryfunc)int_int, /*nb_int*/
1076 (unaryfunc)int_long, /*nb_long*/
1077 (unaryfunc)int_float, /*nb_float*/
1078 (unaryfunc)int_oct, /*nb_oct*/
1079 (unaryfunc)int_hex, /*nb_hex*/
1080 0, /*nb_inplace_add*/
1081 0, /*nb_inplace_subtract*/
1082 0, /*nb_inplace_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001083 0, /*nb_inplace_remainder*/
1084 0, /*nb_inplace_power*/
1085 0, /*nb_inplace_lshift*/
1086 0, /*nb_inplace_rshift*/
1087 0, /*nb_inplace_and*/
1088 0, /*nb_inplace_xor*/
1089 0, /*nb_inplace_or*/
Neal Norwitz4886cc32006-08-21 17:06:07 +00001090 (binaryfunc)int_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00001091 int_true_divide, /* nb_true_divide */
1092 0, /* nb_inplace_floor_divide */
1093 0, /* nb_inplace_true_divide */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001094 (unaryfunc)int_int, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001095};
1096
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001097PyTypeObject PyInt_Type = {
1098 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001099 0,
1100 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001101 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001102 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001103 (destructor)int_dealloc, /* tp_dealloc */
1104 (printfunc)int_print, /* tp_print */
1105 0, /* tp_getattr */
1106 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001107 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001108 (reprfunc)int_repr, /* tp_repr */
1109 &int_as_number, /* tp_as_number */
1110 0, /* tp_as_sequence */
1111 0, /* tp_as_mapping */
1112 (hashfunc)int_hash, /* tp_hash */
1113 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001114 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001115 PyObject_GenericGetAttr, /* tp_getattro */
1116 0, /* tp_setattro */
1117 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001118 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001119 int_doc, /* tp_doc */
1120 0, /* tp_traverse */
1121 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001122 int_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001123 0, /* tp_weaklistoffset */
1124 0, /* tp_iter */
1125 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001126 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001127 0, /* tp_members */
1128 0, /* tp_getset */
1129 0, /* tp_base */
1130 0, /* tp_dict */
1131 0, /* tp_descr_get */
1132 0, /* tp_descr_set */
1133 0, /* tp_dictoffset */
1134 0, /* tp_init */
1135 0, /* tp_alloc */
1136 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001137 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001138};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001139
Neal Norwitzc91ed402002-12-30 22:29:22 +00001140int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001141_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001142{
1143 PyIntObject *v;
1144 int ival;
1145#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1146 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001147 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001148 return 0;
1149 /* PyObject_New is inlined */
1150 v = free_list;
1151 free_list = (PyIntObject *)v->ob_type;
1152 PyObject_INIT(v, &PyInt_Type);
1153 v->ob_ival = ival;
1154 small_ints[ival + NSMALLNEGINTS] = v;
1155 }
1156#endif
1157 return 1;
1158}
1159
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001160void
Fred Drakea2f55112000-07-09 15:16:51 +00001161PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001162{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001163 PyIntObject *p;
1164 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001165 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001166 unsigned int ctr;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001167 int bc, bf; /* block count, number of freed blocks */
1168 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001169
Guido van Rossumda084ed1999-03-10 22:55:24 +00001170#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1171 PyIntObject **q;
1172
1173 i = NSMALLNEGINTS + NSMALLPOSINTS;
1174 q = small_ints;
1175 while (--i >= 0) {
1176 Py_XDECREF(*q);
1177 *q++ = NULL;
1178 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001179#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001180 bc = 0;
1181 bf = 0;
1182 isum = 0;
1183 list = block_list;
1184 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001185 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001186 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001187 bc++;
1188 irem = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001189 for (ctr = 0, p = &list->objects[0];
1190 ctr < N_INTOBJECTS;
1191 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001192 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001193 irem++;
1194 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001195 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001196 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001197 list->next = block_list;
1198 block_list = list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001199 for (ctr = 0, p = &list->objects[0];
1200 ctr < N_INTOBJECTS;
1201 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001202 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001203 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001204 p->ob_type = (struct _typeobject *)
1205 free_list;
1206 free_list = p;
1207 }
1208#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1209 else if (-NSMALLNEGINTS <= p->ob_ival &&
1210 p->ob_ival < NSMALLPOSINTS &&
1211 small_ints[p->ob_ival +
1212 NSMALLNEGINTS] == NULL) {
1213 Py_INCREF(p);
1214 small_ints[p->ob_ival +
1215 NSMALLNEGINTS] = p;
1216 }
1217#endif
1218 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001219 }
1220 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001221 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001222 bf++;
1223 }
1224 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001225 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001226 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001227 if (!Py_VerboseFlag)
1228 return;
1229 fprintf(stderr, "# cleanup ints");
1230 if (!isum) {
1231 fprintf(stderr, "\n");
1232 }
1233 else {
1234 fprintf(stderr,
1235 ": %d unfreed int%s in %d out of %d block%s\n",
1236 isum, isum == 1 ? "" : "s",
1237 bc - bf, bc, bc == 1 ? "" : "s");
1238 }
1239 if (Py_VerboseFlag > 1) {
1240 list = block_list;
1241 while (list != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242 for (ctr = 0, p = &list->objects[0];
1243 ctr < N_INTOBJECTS;
1244 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001245 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001246 /* XXX(twouters) cast refcount to
1247 long until %zd is universally
1248 available
1249 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001250 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001251 "# <int at %p, refcnt=%ld, val=%ld>\n",
1252 p, (long)p->ob_refcnt,
1253 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001254 }
1255 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001256 }
1257 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001258}
Guido van Rossumddefaf32007-01-14 03:31:43 +00001259#endif /* if 0 */