blob: 0aae6ac8482190fa060980a205a13535edc3bffa [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
Guido van Rossum9e896b32000-04-05 20:11:21 +0000390PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000391PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000392{
Walter Dörwald07e14762002-11-06 16:15:14 +0000393 PyObject *result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394 char *buffer = (char *)PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000395
Walter Dörwald07e14762002-11-06 16:15:14 +0000396 if (buffer == NULL)
397 return NULL;
398
399 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
400 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000401 return NULL;
402 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000403 result = PyInt_FromString(buffer, NULL, base);
404 PyMem_FREE(buffer);
405 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000406}
407
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408/* Methods */
409
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000410/* Integers are seen as the "smallest" of all numeric types and thus
411 don't have any knowledge about conversion of other types to
412 integers. */
413
414#define CONVERT_TO_LONG(obj, lng) \
415 if (PyInt_Check(obj)) { \
416 lng = PyInt_AS_LONG(obj); \
417 } \
418 else { \
419 Py_INCREF(Py_NotImplemented); \
420 return Py_NotImplemented; \
421 }
422
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000423/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000424static int
Fred Drakea2f55112000-07-09 15:16:51 +0000425int_print(PyIntObject *v, FILE *fp, int flags)
426 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427{
428 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000429 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430}
431
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000433int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434{
Tim Peters42221042001-12-01 02:52:56 +0000435 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000436 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000437 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438}
439
440static int
Fred Drakea2f55112000-07-09 15:16:51 +0000441int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442{
443 register long i = v->ob_ival;
444 register long j = w->ob_ival;
445 return (i < j) ? -1 : (i > j) ? 1 : 0;
446}
447
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000448static PyObject *
449int_richcompare(PyObject *self, PyObject *other, int op)
450{
451 if (!PyInt_Check(self) || !PyInt_Check(other)) {
452 Py_INCREF(Py_NotImplemented);
453 return Py_NotImplemented;
454 }
455 return Py_CmpToRich(op, int_compare((PyIntObject *)self,
456 (PyIntObject *)other));
457}
458
Guido van Rossum9bfef441993-03-29 10:43:31 +0000459static long
Fred Drakea2f55112000-07-09 15:16:51 +0000460int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000461{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000462 /* XXX If this is changed, you also need to change the way
463 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000464 long x = v -> ob_ival;
465 if (x == -1)
466 x = -2;
467 return x;
468}
469
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000470static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000471int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472{
473 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000474 CONVERT_TO_LONG(v, a);
475 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000476 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000477 if ((x^a) >= 0 || (x^b) >= 0)
478 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000479 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480}
481
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000482static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000483int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484{
485 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000486 CONVERT_TO_LONG(v, a);
487 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000489 if ((x^a) >= 0 || (x^~b) >= 0)
490 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000491 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
492 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493}
494
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000495/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000496Integer overflow checking for * is painful: Python tried a couple ways, but
497they didn't work on all platforms, or failed in endcases (a product of
498-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000499
Tim Petersa3c01ce2001-12-04 23:05:10 +0000500Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000501
Tim Petersa3c01ce2001-12-04 23:05:10 +0000502The native long product x*y is either exactly right or *way* off, being
503just the last n bits of the true product, where n is the number of bits
504in a long (the delivered product is the true product plus i*2**n for
505some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000506
Tim Petersa3c01ce2001-12-04 23:05:10 +0000507The native double product (double)x * (double)y is subject to three
508rounding errors: on a sizeof(long)==8 box, each cast to double can lose
509info, and even on a sizeof(long)==4 box, the multiplication can lose info.
510But, unlike the native long product, it's not in *range* trouble: even
511if sizeof(long)==32 (256-bit longs), the product easily fits in the
512dynamic range of a double. So the leading 50 (or so) bits of the double
513product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000514
Tim Petersa3c01ce2001-12-04 23:05:10 +0000515We check these two ways against each other, and declare victory if they're
516approximately the same. Else, because the native long product is the only
517one that can lose catastrophic amounts of information, it's the native long
518product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000519*/
520
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000521static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000522int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000524 long a, b;
525 long longprod; /* a*b in native long arithmetic */
526 double doubled_longprod; /* (double)longprod */
527 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000528
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000529 CONVERT_TO_LONG(v, a);
530 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000531 longprod = a * b;
532 doubleprod = (double)a * (double)b;
533 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000534
Tim Petersa3c01ce2001-12-04 23:05:10 +0000535 /* Fast path for normal case: small multiplicands, and no info
536 is lost in either method. */
537 if (doubled_longprod == doubleprod)
538 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000539
Tim Petersa3c01ce2001-12-04 23:05:10 +0000540 /* Somebody somewhere lost info. Close enough, or way off? Note
541 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
542 The difference either is or isn't significant compared to the
543 true value (of which doubleprod is a good approximation).
544 */
545 {
546 const double diff = doubled_longprod - doubleprod;
547 const double absdiff = diff >= 0.0 ? diff : -diff;
548 const double absprod = doubleprod >= 0.0 ? doubleprod :
549 -doubleprod;
550 /* absdiff/absprod <= 1/32 iff
551 32 * absdiff <= absprod -- 5 good bits is "close enough" */
552 if (32.0 * absdiff <= absprod)
553 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000554 else
555 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000556 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000557}
558
Thomas Wouters89f507f2006-12-13 04:49:30 +0000559/* Integer overflow checking for unary negation: on a 2's-complement
560 * box, -x overflows iff x is the most negative long. In this case we
561 * get -x == x. However, -x is undefined (by C) if x /is/ the most
562 * negative long (it's a signed overflow case), and some compilers care.
563 * So we cast x to unsigned long first. However, then other compilers
564 * warn about applying unary minus to an unsigned operand. Hence the
565 * weird "0-".
566 */
567#define UNARY_NEG_WOULD_OVERFLOW(x) \
568 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
569
Guido van Rossume27f7952001-08-23 02:59:04 +0000570/* Return type of i_divmod */
571enum divmod_result {
572 DIVMOD_OK, /* Correct result */
573 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
574 DIVMOD_ERROR /* Exception raised */
575};
576
577static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000578i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000579 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000580{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000581 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000582
Tim Peters1dad6a82001-06-18 19:21:11 +0000583 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000585 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000586 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000588 /* (-sys.maxint-1)/-1 is the only overflow case. */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000589 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
Guido van Rossume27f7952001-08-23 02:59:04 +0000590 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000591 xdivy = x / y;
592 xmody = x - xdivy * y;
593 /* If the signs of x and y differ, and the remainder is non-0,
594 * C89 doesn't define whether xdivy is now the floor or the
595 * ceiling of the infinitely precise quotient. We want the floor,
596 * and we have it iff the remainder's sign matches y's.
597 */
598 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
599 xmody += y;
600 --xdivy;
601 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000602 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000603 *p_xdivy = xdivy;
604 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000605 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000606}
607
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000608static PyObject *
Neal Norwitz4886cc32006-08-21 17:06:07 +0000609int_floor_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000610{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000611 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000612 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000613 CONVERT_TO_LONG(x, xi);
614 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000615 switch (i_divmod(xi, yi, &d, &m)) {
616 case DIVMOD_OK:
617 return PyInt_FromLong(d);
618 case DIVMOD_OVERFLOW:
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000619 return PyLong_Type.tp_as_number->nb_floor_divide((PyObject *)x,
620 (PyObject *)y);
Guido van Rossum393661d2001-08-31 17:40:15 +0000621 default:
622 return NULL;
623 }
624}
625
626static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000627int_true_divide(PyObject *v, PyObject *w)
628{
Tim Peterse2a60002001-09-04 06:17:36 +0000629 /* If they aren't both ints, give someone else a chance. In
630 particular, this lets int/long get handled by longs, which
631 underflows to 0 gracefully if the long is too big to convert
632 to float. */
633 if (PyInt_Check(v) && PyInt_Check(w))
634 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
635 Py_INCREF(Py_NotImplemented);
636 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000637}
638
639static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000640int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000641{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000642 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000643 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000644 CONVERT_TO_LONG(x, xi);
645 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000646 switch (i_divmod(xi, yi, &d, &m)) {
647 case DIVMOD_OK:
648 return PyInt_FromLong(m);
649 case DIVMOD_OVERFLOW:
650 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
651 (PyObject *)y);
652 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000653 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000654 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000655}
656
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000658int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000659{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000660 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000661 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000662 CONVERT_TO_LONG(x, xi);
663 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000664 switch (i_divmod(xi, yi, &d, &m)) {
665 case DIVMOD_OK:
666 return Py_BuildValue("(ll)", d, m);
667 case DIVMOD_OVERFLOW:
668 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
669 (PyObject *)y);
670 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000671 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000672 }
Guido van Rossum00466951991-05-05 20:08:27 +0000673}
674
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000676int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000677{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000678 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000679 CONVERT_TO_LONG(v, iv);
680 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000681 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000682 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000683 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
684 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000685 return NULL;
686 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000687 /* Return a float. This works because we know that
688 this calls float_pow() which converts its
689 arguments to double. */
690 return PyFloat_Type.tp_as_number->nb_power(
691 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000692 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000694 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000695 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000697 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000698 return NULL;
699 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000700 }
701 /*
702 * XXX: The original exponentiation code stopped looping
703 * when temp hit zero; this code will continue onwards
704 * unnecessarily, but at least it won't cause any errors.
705 * Hopefully the speed improvement from the fast exponentiation
706 * will compensate for the slight inefficiency.
707 * XXX: Better handling of overflows is desperately needed.
708 */
709 temp = iv;
710 ix = 1;
711 while (iw > 0) {
712 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000713 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000714 ix = ix*temp;
715 if (temp == 0)
716 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000717 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000718 return PyLong_Type.tp_as_number->nb_power(
719 (PyObject *)v,
720 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000721 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000722 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000723 }
724 iw >>= 1; /* Shift exponent down by 1 bit */
725 if (iw==0) break;
726 prev = temp;
727 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000728 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000729 return PyLong_Type.tp_as_number->nb_power(
730 (PyObject *)v, (PyObject *)w, (PyObject *)z);
731 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000732 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000733 /* If we did a multiplication, perform a modulo */
734 ix = ix % iz;
735 temp = temp % iz;
736 }
737 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000738 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000739 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000740 switch (i_divmod(ix, iz, &div, &mod)) {
741 case DIVMOD_OK:
742 ix = mod;
743 break;
744 case DIVMOD_OVERFLOW:
745 return PyLong_Type.tp_as_number->nb_power(
746 (PyObject *)v, (PyObject *)w, (PyObject *)z);
747 default:
748 return NULL;
749 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000750 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000752}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000753
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000755int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000756{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000757 register long a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758 a = v->ob_ival;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000759 /* check for overflow */
760 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
Tim Petersc8854432004-08-25 02:14:08 +0000761 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000762 if (o != NULL) {
763 PyObject *result = PyNumber_Negative(o);
764 Py_DECREF(o);
765 return result;
766 }
767 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000768 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000769 return PyInt_FromLong(-a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770}
771
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000772static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000773int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000774{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000775 if (PyInt_CheckExact(v)) {
776 Py_INCREF(v);
777 return (PyObject *)v;
778 }
779 else
780 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000781}
782
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000784int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000785{
786 if (v->ob_ival >= 0)
787 return int_pos(v);
788 else
789 return int_neg(v);
790}
791
Guido van Rossum0bff0151991-05-14 12:05:32 +0000792static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000793int_bool(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000794{
795 return v->ob_ival != 0;
796}
797
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000799int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000800{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000801 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000802}
803
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000805int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000806{
Guido van Rossum078151d2002-08-11 04:24:12 +0000807 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000808 PyObject *vv, *ww, *result;
809
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000810 CONVERT_TO_LONG(v, a);
811 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000812 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000814 return NULL;
815 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000816 if (a == 0 || b == 0)
817 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000818 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000819 vv = PyLong_FromLong(PyInt_AS_LONG(v));
820 if (vv == NULL)
821 return NULL;
822 ww = PyLong_FromLong(PyInt_AS_LONG(w));
823 if (ww == NULL) {
824 Py_DECREF(vv);
825 return NULL;
826 }
827 result = PyNumber_Lshift(vv, ww);
828 Py_DECREF(vv);
829 Py_DECREF(ww);
830 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000831 }
Tim Petersda1a2212002-08-11 17:54:42 +0000832 c = a << b;
833 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000834 vv = PyLong_FromLong(PyInt_AS_LONG(v));
835 if (vv == NULL)
836 return NULL;
837 ww = PyLong_FromLong(PyInt_AS_LONG(w));
838 if (ww == NULL) {
839 Py_DECREF(vv);
840 return NULL;
841 }
842 result = PyNumber_Lshift(vv, ww);
843 Py_DECREF(vv);
844 Py_DECREF(ww);
845 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000846 }
847 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000848}
849
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000850static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000851int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000852{
853 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000854 CONVERT_TO_LONG(v, a);
855 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000856 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000857 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000858 return NULL;
859 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000860 if (a == 0 || b == 0)
861 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000862 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000863 if (a < 0)
864 a = -1;
865 else
866 a = 0;
867 }
868 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000869 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000870 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000872}
873
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000874static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000875int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000876{
877 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000878 CONVERT_TO_LONG(v, a);
879 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000880 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000881}
882
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000883static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000884int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000885{
886 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000887 CONVERT_TO_LONG(v, a);
888 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000889 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000890}
891
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000892static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000893int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000894{
895 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000896 CONVERT_TO_LONG(v, a);
897 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000899}
900
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000901static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000902int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000903{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000904 if (PyInt_CheckExact(v))
905 Py_INCREF(v);
906 else
907 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908 return (PyObject *)v;
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_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000913{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000914 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000915}
916
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000917static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000918int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000919{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000920 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000921}
922
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000923static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000924int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000925{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000926 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000927 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000928 if (x < 0)
929 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
930 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000931 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000932 else
Barry Warsaw61975092001-11-28 20:55:34 +0000933 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000934 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000935}
936
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000937static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000938int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000939{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000940 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000941 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000942 if (x < 0)
943 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
944 else
945 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000946 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000947}
948
Jeremy Hylton938ace62002-07-17 16:30:39 +0000949static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000950int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
951
Tim Peters6d6c1a32001-08-02 04:15:00 +0000952static PyObject *
953int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
954{
955 PyObject *x = NULL;
956 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000957 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000958
Guido van Rossumbef14172001-08-29 15:47:46 +0000959 if (type != &PyInt_Type)
960 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000961 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
962 &x, &base))
963 return NULL;
964 if (x == NULL)
965 return PyInt_FromLong(0L);
966 if (base == -909)
967 return PyNumber_Int(x);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000968 if (PyString_Check(x)) {
969 /* Since PyInt_FromString doesn't have a length parameter,
970 * check here for possible NULs in the string. */
971 char *string = PyString_AS_STRING(x);
972 if (strlen(string) != PyString_Size(x)) {
973 /* create a repr() of the input string,
974 * just like PyInt_FromString does */
975 PyObject *srepr;
976 srepr = PyObject_Repr(x);
977 if (srepr == NULL)
978 return NULL;
979 PyErr_Format(PyExc_ValueError,
980 "invalid literal for int() with base %d: %s",
981 base, PyString_AS_STRING(srepr));
982 Py_DECREF(srepr);
983 return NULL;
984 }
985 return PyInt_FromString(string, NULL, base);
986 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000987 if (PyUnicode_Check(x))
988 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
989 PyUnicode_GET_SIZE(x),
990 base);
991 PyErr_SetString(PyExc_TypeError,
992 "int() can't convert non-string with explicit base");
993 return NULL;
994}
995
Guido van Rossumbef14172001-08-29 15:47:46 +0000996/* Wimpy, slow approach to tp_new calls for subtypes of int:
997 first create a regular int from whatever arguments we got,
998 then allocate a subtype instance and initialize its ob_ival
999 from the regular int. The regular int is then thrown away.
1000*/
1001static PyObject *
1002int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1003{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001004 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001005 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001006
1007 assert(PyType_IsSubtype(type, &PyInt_Type));
1008 tmp = int_new(&PyInt_Type, args, kwds);
1009 if (tmp == NULL)
1010 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001011 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001012 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001013 if (ival == -1 && PyErr_Occurred()) {
1014 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001015 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001016 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001017 } else {
1018 ival = ((PyIntObject *)tmp)->ob_ival;
1019 }
1020
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001021 newobj = type->tp_alloc(type, 0);
1022 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001023 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001024 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001025 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001026 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001027 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001028 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001029}
1030
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001031static PyObject *
1032int_getnewargs(PyIntObject *v)
1033{
1034 return Py_BuildValue("(l)", v->ob_ival);
1035}
1036
1037static PyMethodDef int_methods[] = {
1038 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
1039 {NULL, NULL} /* sentinel */
1040};
1041
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001042PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001043"int(x[, base]) -> integer\n\
1044\n\
1045Convert a string or number to an integer, if possible. A floating point\n\
1046argument will be truncated towards zero (this does not include a string\n\
1047representation of a floating point number!) When converting a string, use\n\
1048the optional base. It is an error to supply a base when converting a\n\
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001049non-string. If base is zero, the proper base is guessed based on the\n\
1050string content. If the argument is outside the integer range a\n\
1051long object will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001052
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001053static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001054 (binaryfunc)int_add, /*nb_add*/
1055 (binaryfunc)int_sub, /*nb_subtract*/
1056 (binaryfunc)int_mul, /*nb_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001057 (binaryfunc)int_mod, /*nb_remainder*/
1058 (binaryfunc)int_divmod, /*nb_divmod*/
1059 (ternaryfunc)int_pow, /*nb_power*/
1060 (unaryfunc)int_neg, /*nb_negative*/
1061 (unaryfunc)int_pos, /*nb_positive*/
1062 (unaryfunc)int_abs, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001063 (inquiry)int_bool, /*nb_bool*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001064 (unaryfunc)int_invert, /*nb_invert*/
1065 (binaryfunc)int_lshift, /*nb_lshift*/
1066 (binaryfunc)int_rshift, /*nb_rshift*/
1067 (binaryfunc)int_and, /*nb_and*/
1068 (binaryfunc)int_xor, /*nb_xor*/
1069 (binaryfunc)int_or, /*nb_or*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001070 0, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001071 (unaryfunc)int_int, /*nb_int*/
1072 (unaryfunc)int_long, /*nb_long*/
1073 (unaryfunc)int_float, /*nb_float*/
1074 (unaryfunc)int_oct, /*nb_oct*/
1075 (unaryfunc)int_hex, /*nb_hex*/
1076 0, /*nb_inplace_add*/
1077 0, /*nb_inplace_subtract*/
1078 0, /*nb_inplace_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001079 0, /*nb_inplace_remainder*/
1080 0, /*nb_inplace_power*/
1081 0, /*nb_inplace_lshift*/
1082 0, /*nb_inplace_rshift*/
1083 0, /*nb_inplace_and*/
1084 0, /*nb_inplace_xor*/
1085 0, /*nb_inplace_or*/
Neal Norwitz4886cc32006-08-21 17:06:07 +00001086 (binaryfunc)int_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00001087 int_true_divide, /* nb_true_divide */
1088 0, /* nb_inplace_floor_divide */
1089 0, /* nb_inplace_true_divide */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001090 (unaryfunc)int_int, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001091};
1092
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001093PyTypeObject PyInt_Type = {
1094 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001095 0,
1096 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001097 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001098 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001099 (destructor)int_dealloc, /* tp_dealloc */
1100 (printfunc)int_print, /* tp_print */
1101 0, /* tp_getattr */
1102 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001103 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001104 (reprfunc)int_repr, /* tp_repr */
1105 &int_as_number, /* tp_as_number */
1106 0, /* tp_as_sequence */
1107 0, /* tp_as_mapping */
1108 (hashfunc)int_hash, /* tp_hash */
1109 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001110 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001111 PyObject_GenericGetAttr, /* tp_getattro */
1112 0, /* tp_setattro */
1113 0, /* tp_as_buffer */
Thomas Wouters27d517b2007-02-25 20:39:11 +00001114 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
1115 Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001116 int_doc, /* tp_doc */
1117 0, /* tp_traverse */
1118 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001119 int_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001120 0, /* tp_weaklistoffset */
1121 0, /* tp_iter */
1122 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001123 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001124 0, /* tp_members */
1125 0, /* tp_getset */
1126 0, /* tp_base */
1127 0, /* tp_dict */
1128 0, /* tp_descr_get */
1129 0, /* tp_descr_set */
1130 0, /* tp_dictoffset */
1131 0, /* tp_init */
1132 0, /* tp_alloc */
1133 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001134 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001135};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001136
Neal Norwitzc91ed402002-12-30 22:29:22 +00001137int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001138_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001139{
1140 PyIntObject *v;
1141 int ival;
1142#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1143 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001144 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001145 return 0;
1146 /* PyObject_New is inlined */
1147 v = free_list;
1148 free_list = (PyIntObject *)v->ob_type;
1149 PyObject_INIT(v, &PyInt_Type);
1150 v->ob_ival = ival;
1151 small_ints[ival + NSMALLNEGINTS] = v;
1152 }
1153#endif
1154 return 1;
1155}
1156
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001157void
Fred Drakea2f55112000-07-09 15:16:51 +00001158PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001159{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001160 PyIntObject *p;
1161 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001162 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163 unsigned int ctr;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001164 int bc, bf; /* block count, number of freed blocks */
1165 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001166
Guido van Rossumda084ed1999-03-10 22:55:24 +00001167#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1168 PyIntObject **q;
1169
1170 i = NSMALLNEGINTS + NSMALLPOSINTS;
1171 q = small_ints;
1172 while (--i >= 0) {
1173 Py_XDECREF(*q);
1174 *q++ = NULL;
1175 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001176#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001177 bc = 0;
1178 bf = 0;
1179 isum = 0;
1180 list = block_list;
1181 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001182 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001183 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001184 bc++;
1185 irem = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001186 for (ctr = 0, p = &list->objects[0];
1187 ctr < N_INTOBJECTS;
1188 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001189 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001190 irem++;
1191 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001192 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001193 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001194 list->next = block_list;
1195 block_list = list;
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) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001200 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001201 p->ob_type = (struct _typeobject *)
1202 free_list;
1203 free_list = p;
1204 }
1205#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1206 else if (-NSMALLNEGINTS <= p->ob_ival &&
1207 p->ob_ival < NSMALLPOSINTS &&
1208 small_ints[p->ob_ival +
1209 NSMALLNEGINTS] == NULL) {
1210 Py_INCREF(p);
1211 small_ints[p->ob_ival +
1212 NSMALLNEGINTS] = p;
1213 }
1214#endif
1215 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001216 }
1217 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001218 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001219 bf++;
1220 }
1221 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001222 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001223 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001224 if (!Py_VerboseFlag)
1225 return;
1226 fprintf(stderr, "# cleanup ints");
1227 if (!isum) {
1228 fprintf(stderr, "\n");
1229 }
1230 else {
1231 fprintf(stderr,
1232 ": %d unfreed int%s in %d out of %d block%s\n",
1233 isum, isum == 1 ? "" : "s",
1234 bc - bf, bc, bc == 1 ? "" : "s");
1235 }
1236 if (Py_VerboseFlag > 1) {
1237 list = block_list;
1238 while (list != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001239 for (ctr = 0, p = &list->objects[0];
1240 ctr < N_INTOBJECTS;
1241 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001242 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001243 /* XXX(twouters) cast refcount to
1244 long until %zd is universally
1245 available
1246 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001247 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001248 "# <int at %p, refcnt=%ld, val=%ld>\n",
1249 p, (long)p->ob_refcnt,
1250 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001251 }
1252 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001253 }
1254 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001255}
Guido van Rossumddefaf32007-01-14 03:31:43 +00001256#endif /* if 0 */