blob: 0b746dee8d4dc0f859732e0bc0aae93a0bf550b5 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Integer object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Barry Warsaw226ae6c1999-10-12 19:54:53 +00005#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum2e1d4331993-12-24 10:22:45 +00007long
Fred Drakea2f55112000-07-09 15:16:51 +00008PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +00009{
10 return LONG_MAX; /* To initialize sys.maxint */
11}
12
Guido van Rossum3f5da241990-12-20 15:06:42 +000013/* Integers are quite normal objects, to make object handling uniform.
14 (Using odd pointers to represent integers would save much space
15 but require extra checks for this special case throughout the code.)
Tim Peters29c0afc2002-04-28 16:57:34 +000016 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000017 and deallocating integers, these operations should be very fast.
18 Therefore we use a dedicated allocation scheme with a much lower
19 overhead (in space and time) than straight malloc(): a simple
20 dedicated free list, filled when necessary with memory from malloc().
Tim Peters29c0afc2002-04-28 16:57:34 +000021
22 block_list is a singly-linked list of all PyIntBlocks ever allocated,
23 linked via their next members. PyIntBlocks are never returned to the
24 system before shutdown (PyInt_Fini).
25
26 free_list is a singly-linked list of available PyIntObjects, linked
27 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000028*/
29
30#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000031#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
32#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000033
Guido van Rossum3fce8831999-03-12 19:43:17 +000034struct _intblock {
35 struct _intblock *next;
36 PyIntObject objects[N_INTOBJECTS];
37};
38
39typedef struct _intblock PyIntBlock;
40
41static PyIntBlock *block_list = NULL;
42static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000043
Guido van Rossumc0b618a1997-05-02 03:12:38 +000044static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000045fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000046{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000048 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000049 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000050 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000051 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000052 ((PyIntBlock *)p)->next = block_list;
53 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000054 /* Link the int objects together, from rear to front, then return
55 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000056 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000057 q = p + N_INTOBJECTS;
58 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000059 q->ob_type = (struct _typeobject *)(q-1);
60 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000061 return p + N_INTOBJECTS - 1;
62}
63
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000064#ifndef NSMALLPOSINTS
Georg Brandl418a1ef2006-02-22 11:30:06 +000065#define NSMALLPOSINTS 257
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000066#endif
67#ifndef NSMALLNEGINTS
Neal Norwitzc91ed402002-12-30 22:29:22 +000068#define NSMALLNEGINTS 5
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000069#endif
70#if NSMALLNEGINTS + NSMALLPOSINTS > 0
71/* References to small integers are saved in this array so that they
72 can be shared.
73 The integers that are saved are those in the range
74 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
75*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000077#endif
78#ifdef COUNT_ALLOCS
79int quick_int_allocs, quick_neg_int_allocs;
80#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000081
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000083PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000086#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Neal Norwitzc91ed402002-12-30 22:29:22 +000087 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
88 v = small_ints[ival + NSMALLNEGINTS];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#ifdef COUNT_ALLOCS
91 if (ival >= 0)
92 quick_int_allocs++;
93 else
94 quick_neg_int_allocs++;
95#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000097 }
98#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000099 if (free_list == NULL) {
100 if ((free_list = fill_free_list()) == NULL)
101 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000103 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000104 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000105 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000106 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000108 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000109}
110
Martin v. Löwis18e16552006-02-15 17:27:45 +0000111PyObject *
112PyInt_FromSize_t(size_t ival)
113{
114 if (ival <= LONG_MAX)
115 return PyInt_FromLong((long)ival);
116 return _PyLong_FromSize_t(ival);
117}
118
119PyObject *
120PyInt_FromSsize_t(Py_ssize_t ival)
121{
122 if (ival >= LONG_MIN && ival <= LONG_MAX)
123 return PyInt_FromLong((long)ival);
124 return _PyLong_FromSsize_t(ival);
125}
126
Guido van Rossum3f5da241990-12-20 15:06:42 +0000127static void
Fred Drakea2f55112000-07-09 15:16:51 +0000128int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000130 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000131 v->ob_type = (struct _typeobject *)free_list;
132 free_list = v;
133 }
134 else
135 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136}
137
Guido van Rossum93646982002-04-26 00:53:34 +0000138static void
139int_free(PyIntObject *v)
140{
141 v->ob_type = (struct _typeobject *)free_list;
142 free_list = v;
143}
144
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145long
Fred Drakea2f55112000-07-09 15:16:51 +0000146PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 PyNumberMethods *nb;
149 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000150 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000151
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 if (op && PyInt_Check(op))
153 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000154
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000155 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
156 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000157 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158 return -1;
159 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000160
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000162 if (io == NULL)
163 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000165 if (PyLong_Check(io)) {
166 /* got a long? => retry int conversion */
167 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000168 Py_DECREF(io);
169 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000170 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000171 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000172 }
173 else
174 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000175 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000176 PyErr_SetString(PyExc_TypeError,
177 "nb_int should return int object");
178 return -1;
179 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000180 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000181
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 val = PyInt_AS_LONG(io);
183 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000184
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000185 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186}
187
Martin v. Löwis18e16552006-02-15 17:27:45 +0000188Py_ssize_t
189PyInt_AsSsize_t(register PyObject *op)
190{
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000191#if SIZEOF_SIZE_T != SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000192 PyNumberMethods *nb;
193 PyIntObject *io;
194 Py_ssize_t val;
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000195#endif
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000196
197 if (op == NULL) {
198 PyErr_SetString(PyExc_TypeError, "an integer is required");
199 return -1;
200 }
201
202 if (PyInt_Check(op))
203 return PyInt_AS_LONG((PyIntObject*) op);
204 if (PyLong_Check(op))
Martin v. Löwis18e16552006-02-15 17:27:45 +0000205 return _PyLong_AsSsize_t(op);
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000206#if SIZEOF_SIZE_T == SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000207 return PyInt_AsLong(op);
208#else
209
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000210 if ((nb = op->ob_type->tp_as_number) == NULL ||
Martin v. Löwis18e16552006-02-15 17:27:45 +0000211 (nb->nb_int == NULL && nb->nb_long == 0)) {
212 PyErr_SetString(PyExc_TypeError, "an integer is required");
213 return -1;
214 }
215
216 if (nb->nb_long != 0) {
217 io = (PyIntObject*) (*nb->nb_long) (op);
218 } else {
219 io = (PyIntObject*) (*nb->nb_int) (op);
220 }
221 if (io == NULL)
222 return -1;
223 if (!PyInt_Check(io)) {
224 if (PyLong_Check(io)) {
225 /* got a long? => retry int conversion */
226 val = _PyLong_AsSsize_t((PyObject *)io);
227 Py_DECREF(io);
228 if ((val == -1) && PyErr_Occurred())
229 return -1;
230 return val;
231 }
232 else
233 {
234 Py_DECREF(io);
235 PyErr_SetString(PyExc_TypeError,
236 "nb_int should return int object");
237 return -1;
238 }
239 }
240
241 val = PyInt_AS_LONG(io);
242 Py_DECREF(io);
243
244 return val;
245#endif
246}
247
Thomas Hellera4ea6032003-04-17 18:55:45 +0000248unsigned long
249PyInt_AsUnsignedLongMask(register PyObject *op)
250{
251 PyNumberMethods *nb;
252 PyIntObject *io;
253 unsigned long val;
254
255 if (op && PyInt_Check(op))
256 return PyInt_AS_LONG((PyIntObject*) op);
257 if (op && PyLong_Check(op))
258 return PyLong_AsUnsignedLongMask(op);
259
260 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
261 nb->nb_int == NULL) {
262 PyErr_SetString(PyExc_TypeError, "an integer is required");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000263 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000264 }
265
266 io = (PyIntObject*) (*nb->nb_int) (op);
267 if (io == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000268 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000269 if (!PyInt_Check(io)) {
270 if (PyLong_Check(io)) {
271 val = PyLong_AsUnsignedLongMask((PyObject *)io);
272 Py_DECREF(io);
273 if (PyErr_Occurred())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000275 return val;
276 }
277 else
278 {
279 Py_DECREF(io);
280 PyErr_SetString(PyExc_TypeError,
281 "nb_int should return int object");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000282 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000283 }
284 }
285
286 val = PyInt_AS_LONG(io);
287 Py_DECREF(io);
288
289 return val;
290}
291
292#ifdef HAVE_LONG_LONG
293unsigned PY_LONG_LONG
294PyInt_AsUnsignedLongLongMask(register PyObject *op)
295{
296 PyNumberMethods *nb;
297 PyIntObject *io;
298 unsigned PY_LONG_LONG val;
299
300 if (op && PyInt_Check(op))
301 return PyInt_AS_LONG((PyIntObject*) op);
302 if (op && PyLong_Check(op))
303 return PyLong_AsUnsignedLongLongMask(op);
304
305 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
306 nb->nb_int == NULL) {
307 PyErr_SetString(PyExc_TypeError, "an integer is required");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000308 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000309 }
310
311 io = (PyIntObject*) (*nb->nb_int) (op);
312 if (io == NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000314 if (!PyInt_Check(io)) {
315 if (PyLong_Check(io)) {
316 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
317 Py_DECREF(io);
318 if (PyErr_Occurred())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000320 return val;
321 }
322 else
323 {
324 Py_DECREF(io);
325 PyErr_SetString(PyExc_TypeError,
326 "nb_int should return int object");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000328 }
329 }
330
331 val = PyInt_AS_LONG(io);
332 Py_DECREF(io);
333
334 return val;
335}
336#endif
337
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000338PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000339PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000340{
341 char *end;
342 long x;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343 Py_ssize_t slen;
344 PyObject *sobj, *srepr;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000345
346 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000347 PyErr_SetString(PyExc_ValueError,
348 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000349 return NULL;
350 }
351
352 while (*s && isspace(Py_CHARMASK(*s)))
353 s++;
354 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000355 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000356 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000357 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000358 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000359 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000360 else
361 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000362 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000363 goto bad;
364 while (*end && isspace(Py_CHARMASK(*end)))
365 end++;
366 if (*end != '\0') {
367 bad:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000368 slen = strlen(s) < 200 ? strlen(s) : 200;
369 sobj = PyString_FromStringAndSize(s, slen);
370 if (sobj == NULL)
371 return NULL;
372 srepr = PyObject_Repr(sobj);
373 Py_DECREF(sobj);
374 if (srepr == NULL)
375 return NULL;
376 PyErr_Format(PyExc_ValueError,
377 "invalid literal for int() with base %d: %s",
378 base, PyString_AS_STRING(srepr));
379 Py_DECREF(srepr);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000380 return NULL;
381 }
Tim Petersc8854432004-08-25 02:14:08 +0000382 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000383 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000384 if (pend)
385 *pend = end;
386 return PyInt_FromLong(x);
387}
388
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000389#ifdef Py_USING_UNICODE
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}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000407#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000408
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409/* Methods */
410
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000411/* Integers are seen as the "smallest" of all numeric types and thus
412 don't have any knowledge about conversion of other types to
413 integers. */
414
415#define CONVERT_TO_LONG(obj, lng) \
416 if (PyInt_Check(obj)) { \
417 lng = PyInt_AS_LONG(obj); \
418 } \
419 else { \
420 Py_INCREF(Py_NotImplemented); \
421 return Py_NotImplemented; \
422 }
423
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000424/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000425static int
Fred Drakea2f55112000-07-09 15:16:51 +0000426int_print(PyIntObject *v, FILE *fp, int flags)
427 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428{
429 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000430 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000431}
432
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000433static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000434int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435{
Tim Peters42221042001-12-01 02:52:56 +0000436 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000437 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439}
440
441static int
Fred Drakea2f55112000-07-09 15:16:51 +0000442int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443{
444 register long i = v->ob_ival;
445 register long j = w->ob_ival;
446 return (i < j) ? -1 : (i > j) ? 1 : 0;
447}
448
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000449static PyObject *
450int_richcompare(PyObject *self, PyObject *other, int op)
451{
452 if (!PyInt_Check(self) || !PyInt_Check(other)) {
453 Py_INCREF(Py_NotImplemented);
454 return Py_NotImplemented;
455 }
456 return Py_CmpToRich(op, int_compare((PyIntObject *)self,
457 (PyIntObject *)other));
458}
459
Guido van Rossum9bfef441993-03-29 10:43:31 +0000460static long
Fred Drakea2f55112000-07-09 15:16:51 +0000461int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000462{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000463 /* XXX If this is changed, you also need to change the way
464 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000465 long x = v -> ob_ival;
466 if (x == -1)
467 x = -2;
468 return x;
469}
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000472int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473{
474 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000475 CONVERT_TO_LONG(v, a);
476 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000478 if ((x^a) >= 0 || (x^b) >= 0)
479 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000480 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481}
482
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000484int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485{
486 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000487 CONVERT_TO_LONG(v, a);
488 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000490 if ((x^a) >= 0 || (x^~b) >= 0)
491 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000492 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
493 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494}
495
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000496/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000497Integer overflow checking for * is painful: Python tried a couple ways, but
498they didn't work on all platforms, or failed in endcases (a product of
499-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000500
Tim Petersa3c01ce2001-12-04 23:05:10 +0000501Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000502
Tim Petersa3c01ce2001-12-04 23:05:10 +0000503The native long product x*y is either exactly right or *way* off, being
504just the last n bits of the true product, where n is the number of bits
505in a long (the delivered product is the true product plus i*2**n for
506some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000507
Tim Petersa3c01ce2001-12-04 23:05:10 +0000508The native double product (double)x * (double)y is subject to three
509rounding errors: on a sizeof(long)==8 box, each cast to double can lose
510info, and even on a sizeof(long)==4 box, the multiplication can lose info.
511But, unlike the native long product, it's not in *range* trouble: even
512if sizeof(long)==32 (256-bit longs), the product easily fits in the
513dynamic range of a double. So the leading 50 (or so) bits of the double
514product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000515
Tim Petersa3c01ce2001-12-04 23:05:10 +0000516We check these two ways against each other, and declare victory if they're
517approximately the same. Else, because the native long product is the only
518one that can lose catastrophic amounts of information, it's the native long
519product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000520*/
521
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000523int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000524{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000525 long a, b;
526 long longprod; /* a*b in native long arithmetic */
527 double doubled_longprod; /* (double)longprod */
528 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000529
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000530 CONVERT_TO_LONG(v, a);
531 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000532 longprod = a * b;
533 doubleprod = (double)a * (double)b;
534 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000535
Tim Petersa3c01ce2001-12-04 23:05:10 +0000536 /* Fast path for normal case: small multiplicands, and no info
537 is lost in either method. */
538 if (doubled_longprod == doubleprod)
539 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000540
Tim Petersa3c01ce2001-12-04 23:05:10 +0000541 /* Somebody somewhere lost info. Close enough, or way off? Note
542 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
543 The difference either is or isn't significant compared to the
544 true value (of which doubleprod is a good approximation).
545 */
546 {
547 const double diff = doubled_longprod - doubleprod;
548 const double absdiff = diff >= 0.0 ? diff : -diff;
549 const double absprod = doubleprod >= 0.0 ? doubleprod :
550 -doubleprod;
551 /* absdiff/absprod <= 1/32 iff
552 32 * absdiff <= absprod -- 5 good bits is "close enough" */
553 if (32.0 * absdiff <= absprod)
554 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000555 else
556 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000557 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000558}
559
Thomas Wouters89f507f2006-12-13 04:49:30 +0000560/* Integer overflow checking for unary negation: on a 2's-complement
561 * box, -x overflows iff x is the most negative long. In this case we
562 * get -x == x. However, -x is undefined (by C) if x /is/ the most
563 * negative long (it's a signed overflow case), and some compilers care.
564 * So we cast x to unsigned long first. However, then other compilers
565 * warn about applying unary minus to an unsigned operand. Hence the
566 * weird "0-".
567 */
568#define UNARY_NEG_WOULD_OVERFLOW(x) \
569 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
570
Guido van Rossume27f7952001-08-23 02:59:04 +0000571/* Return type of i_divmod */
572enum divmod_result {
573 DIVMOD_OK, /* Correct result */
574 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
575 DIVMOD_ERROR /* Exception raised */
576};
577
578static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000579i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000580 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000582 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000583
Tim Peters1dad6a82001-06-18 19:21:11 +0000584 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000586 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000587 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000589 /* (-sys.maxint-1)/-1 is the only overflow case. */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000590 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
Guido van Rossume27f7952001-08-23 02:59:04 +0000591 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000592 xdivy = x / y;
593 xmody = x - xdivy * y;
594 /* If the signs of x and y differ, and the remainder is non-0,
595 * C89 doesn't define whether xdivy is now the floor or the
596 * ceiling of the infinitely precise quotient. We want the floor,
597 * and we have it iff the remainder's sign matches y's.
598 */
599 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
600 xmody += y;
601 --xdivy;
602 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000603 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000604 *p_xdivy = xdivy;
605 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000606 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000607}
608
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609static PyObject *
Neal Norwitz4886cc32006-08-21 17:06:07 +0000610int_floor_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000611{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000612 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000613 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000614 CONVERT_TO_LONG(x, xi);
615 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000616 switch (i_divmod(xi, yi, &d, &m)) {
617 case DIVMOD_OK:
618 return PyInt_FromLong(d);
619 case DIVMOD_OVERFLOW:
Neal Norwitzbcc0db82006-03-24 08:14:36 +0000620 return PyLong_Type.tp_as_number->nb_floor_divide((PyObject *)x,
621 (PyObject *)y);
Guido van Rossum393661d2001-08-31 17:40:15 +0000622 default:
623 return NULL;
624 }
625}
626
627static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000628int_true_divide(PyObject *v, PyObject *w)
629{
Tim Peterse2a60002001-09-04 06:17:36 +0000630 /* If they aren't both ints, give someone else a chance. In
631 particular, this lets int/long get handled by longs, which
632 underflows to 0 gracefully if the long is too big to convert
633 to float. */
634 if (PyInt_Check(v) && PyInt_Check(w))
635 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
636 Py_INCREF(Py_NotImplemented);
637 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000638}
639
640static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000641int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000642{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000643 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000644 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000645 CONVERT_TO_LONG(x, xi);
646 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000647 switch (i_divmod(xi, yi, &d, &m)) {
648 case DIVMOD_OK:
649 return PyInt_FromLong(m);
650 case DIVMOD_OVERFLOW:
651 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
652 (PyObject *)y);
653 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000654 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000655 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000656}
657
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000658static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000659int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000660{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000661 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000662 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000663 CONVERT_TO_LONG(x, xi);
664 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000665 switch (i_divmod(xi, yi, &d, &m)) {
666 case DIVMOD_OK:
667 return Py_BuildValue("(ll)", d, m);
668 case DIVMOD_OVERFLOW:
669 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
670 (PyObject *)y);
671 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000672 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000673 }
Guido van Rossum00466951991-05-05 20:08:27 +0000674}
675
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000676static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000677int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000679 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000680 CONVERT_TO_LONG(v, iv);
681 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000682 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000683 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000684 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
685 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000686 return NULL;
687 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000688 /* Return a float. This works because we know that
689 this calls float_pow() which converts its
690 arguments to double. */
691 return PyFloat_Type.tp_as_number->nb_power(
692 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000693 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000695 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000696 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000698 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000699 return NULL;
700 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000701 }
702 /*
703 * XXX: The original exponentiation code stopped looping
704 * when temp hit zero; this code will continue onwards
705 * unnecessarily, but at least it won't cause any errors.
706 * Hopefully the speed improvement from the fast exponentiation
707 * will compensate for the slight inefficiency.
708 * XXX: Better handling of overflows is desperately needed.
709 */
710 temp = iv;
711 ix = 1;
712 while (iw > 0) {
713 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000714 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000715 ix = ix*temp;
716 if (temp == 0)
717 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000718 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000719 return PyLong_Type.tp_as_number->nb_power(
720 (PyObject *)v,
721 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000722 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000723 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000724 }
725 iw >>= 1; /* Shift exponent down by 1 bit */
726 if (iw==0) break;
727 prev = temp;
728 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000729 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000730 return PyLong_Type.tp_as_number->nb_power(
731 (PyObject *)v, (PyObject *)w, (PyObject *)z);
732 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000733 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000734 /* If we did a multiplication, perform a modulo */
735 ix = ix % iz;
736 temp = temp % iz;
737 }
738 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000739 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000740 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000741 switch (i_divmod(ix, iz, &div, &mod)) {
742 case DIVMOD_OK:
743 ix = mod;
744 break;
745 case DIVMOD_OVERFLOW:
746 return PyLong_Type.tp_as_number->nb_power(
747 (PyObject *)v, (PyObject *)w, (PyObject *)z);
748 default:
749 return NULL;
750 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000751 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000752 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000753}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000754
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000756int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000757{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000758 register long a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000759 a = v->ob_ival;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000760 /* check for overflow */
761 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
Tim Petersc8854432004-08-25 02:14:08 +0000762 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000763 if (o != NULL) {
764 PyObject *result = PyNumber_Negative(o);
765 Py_DECREF(o);
766 return result;
767 }
768 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000769 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000770 return PyInt_FromLong(-a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771}
772
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000774int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000775{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000776 if (PyInt_CheckExact(v)) {
777 Py_INCREF(v);
778 return (PyObject *)v;
779 }
780 else
781 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000782}
783
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000784static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000785int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000786{
787 if (v->ob_ival >= 0)
788 return int_pos(v);
789 else
790 return int_neg(v);
791}
792
Guido van Rossum0bff0151991-05-14 12:05:32 +0000793static int
Jack Diederich4dafcc42006-11-28 19:15:13 +0000794int_bool(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000795{
796 return v->ob_ival != 0;
797}
798
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000799static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000800int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000801{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000802 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000803}
804
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000806int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000807{
Guido van Rossum078151d2002-08-11 04:24:12 +0000808 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000809 PyObject *vv, *ww, *result;
810
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000811 CONVERT_TO_LONG(v, a);
812 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000813 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000815 return NULL;
816 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000817 if (a == 0 || b == 0)
818 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000819 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000820 vv = PyLong_FromLong(PyInt_AS_LONG(v));
821 if (vv == NULL)
822 return NULL;
823 ww = PyLong_FromLong(PyInt_AS_LONG(w));
824 if (ww == NULL) {
825 Py_DECREF(vv);
826 return NULL;
827 }
828 result = PyNumber_Lshift(vv, ww);
829 Py_DECREF(vv);
830 Py_DECREF(ww);
831 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000832 }
Tim Petersda1a2212002-08-11 17:54:42 +0000833 c = a << b;
834 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000835 vv = PyLong_FromLong(PyInt_AS_LONG(v));
836 if (vv == NULL)
837 return NULL;
838 ww = PyLong_FromLong(PyInt_AS_LONG(w));
839 if (ww == NULL) {
840 Py_DECREF(vv);
841 return NULL;
842 }
843 result = PyNumber_Lshift(vv, ww);
844 Py_DECREF(vv);
845 Py_DECREF(ww);
846 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000847 }
848 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000849}
850
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000851static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000852int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000853{
854 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000855 CONVERT_TO_LONG(v, a);
856 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000857 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000858 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000859 return NULL;
860 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000861 if (a == 0 || b == 0)
862 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000863 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000864 if (a < 0)
865 a = -1;
866 else
867 a = 0;
868 }
869 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000870 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000871 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000872 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000873}
874
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000876int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000877{
878 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000879 CONVERT_TO_LONG(v, a);
880 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000881 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000882}
883
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000884static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000885int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000886{
887 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000888 CONVERT_TO_LONG(v, a);
889 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000890 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000891}
892
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000893static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000894int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000895{
896 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000897 CONVERT_TO_LONG(v, a);
898 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000900}
901
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000902static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000903int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000904{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000905 if (PyInt_CheckExact(v))
906 Py_INCREF(v);
907 else
908 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000909 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000910}
911
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000912static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000913int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000914{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000915 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000916}
917
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000918static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000919int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000920{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000921 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000922}
923
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000924static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000925int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000926{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000927 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000928 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000929 if (x < 0)
930 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
931 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000932 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000933 else
Barry Warsaw61975092001-11-28 20:55:34 +0000934 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000936}
937
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000938static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000939int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000940{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000941 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000942 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000943 if (x < 0)
944 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
945 else
946 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000948}
949
Jeremy Hylton938ace62002-07-17 16:30:39 +0000950static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000951int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
952
Tim Peters6d6c1a32001-08-02 04:15:00 +0000953static PyObject *
954int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
955{
956 PyObject *x = NULL;
957 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000958 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000959
Guido van Rossumbef14172001-08-29 15:47:46 +0000960 if (type != &PyInt_Type)
961 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000962 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
963 &x, &base))
964 return NULL;
965 if (x == NULL)
966 return PyInt_FromLong(0L);
967 if (base == -909)
968 return PyNumber_Int(x);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000969 if (PyString_Check(x)) {
970 /* Since PyInt_FromString doesn't have a length parameter,
971 * check here for possible NULs in the string. */
972 char *string = PyString_AS_STRING(x);
973 if (strlen(string) != PyString_Size(x)) {
974 /* create a repr() of the input string,
975 * just like PyInt_FromString does */
976 PyObject *srepr;
977 srepr = PyObject_Repr(x);
978 if (srepr == NULL)
979 return NULL;
980 PyErr_Format(PyExc_ValueError,
981 "invalid literal for int() with base %d: %s",
982 base, PyString_AS_STRING(srepr));
983 Py_DECREF(srepr);
984 return NULL;
985 }
986 return PyInt_FromString(string, NULL, base);
987 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000988#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000989 if (PyUnicode_Check(x))
990 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
991 PyUnicode_GET_SIZE(x),
992 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000993#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000994 PyErr_SetString(PyExc_TypeError,
995 "int() can't convert non-string with explicit base");
996 return NULL;
997}
998
Guido van Rossumbef14172001-08-29 15:47:46 +0000999/* Wimpy, slow approach to tp_new calls for subtypes of int:
1000 first create a regular int from whatever arguments we got,
1001 then allocate a subtype instance and initialize its ob_ival
1002 from the regular int. The regular int is then thrown away.
1003*/
1004static PyObject *
1005int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1006{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001007 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001008 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001009
1010 assert(PyType_IsSubtype(type, &PyInt_Type));
1011 tmp = int_new(&PyInt_Type, args, kwds);
1012 if (tmp == NULL)
1013 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001014 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001015 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001016 if (ival == -1 && PyErr_Occurred()) {
1017 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001018 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001019 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001020 } else {
1021 ival = ((PyIntObject *)tmp)->ob_ival;
1022 }
1023
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001024 newobj = type->tp_alloc(type, 0);
1025 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001026 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001027 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001028 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001029 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001030 Py_DECREF(tmp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001031 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001032}
1033
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001034static PyObject *
1035int_getnewargs(PyIntObject *v)
1036{
1037 return Py_BuildValue("(l)", v->ob_ival);
1038}
1039
1040static PyMethodDef int_methods[] = {
1041 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
1042 {NULL, NULL} /* sentinel */
1043};
1044
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001045PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001046"int(x[, base]) -> integer\n\
1047\n\
1048Convert a string or number to an integer, if possible. A floating point\n\
1049argument will be truncated towards zero (this does not include a string\n\
1050representation of a floating point number!) When converting a string, use\n\
1051the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +00001052non-string. If the argument is outside the integer range a long object\n\
1053will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001054
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001055static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001056 (binaryfunc)int_add, /*nb_add*/
1057 (binaryfunc)int_sub, /*nb_subtract*/
1058 (binaryfunc)int_mul, /*nb_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001059 (binaryfunc)int_mod, /*nb_remainder*/
1060 (binaryfunc)int_divmod, /*nb_divmod*/
1061 (ternaryfunc)int_pow, /*nb_power*/
1062 (unaryfunc)int_neg, /*nb_negative*/
1063 (unaryfunc)int_pos, /*nb_positive*/
1064 (unaryfunc)int_abs, /*nb_absolute*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001065 (inquiry)int_bool, /*nb_bool*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001066 (unaryfunc)int_invert, /*nb_invert*/
1067 (binaryfunc)int_lshift, /*nb_lshift*/
1068 (binaryfunc)int_rshift, /*nb_rshift*/
1069 (binaryfunc)int_and, /*nb_and*/
1070 (binaryfunc)int_xor, /*nb_xor*/
1071 (binaryfunc)int_or, /*nb_or*/
Jack Diederich4dafcc42006-11-28 19:15:13 +00001072 0, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001073 (unaryfunc)int_int, /*nb_int*/
1074 (unaryfunc)int_long, /*nb_long*/
1075 (unaryfunc)int_float, /*nb_float*/
1076 (unaryfunc)int_oct, /*nb_oct*/
1077 (unaryfunc)int_hex, /*nb_hex*/
1078 0, /*nb_inplace_add*/
1079 0, /*nb_inplace_subtract*/
1080 0, /*nb_inplace_multiply*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001081 0, /*nb_inplace_remainder*/
1082 0, /*nb_inplace_power*/
1083 0, /*nb_inplace_lshift*/
1084 0, /*nb_inplace_rshift*/
1085 0, /*nb_inplace_and*/
1086 0, /*nb_inplace_xor*/
1087 0, /*nb_inplace_or*/
Neal Norwitz4886cc32006-08-21 17:06:07 +00001088 (binaryfunc)int_floor_div, /* nb_floor_divide */
Guido van Rossum4668b002001-08-08 05:00:18 +00001089 int_true_divide, /* nb_true_divide */
1090 0, /* nb_inplace_floor_divide */
1091 0, /* nb_inplace_true_divide */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001092 (unaryfunc)int_int, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001093};
1094
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001095PyTypeObject PyInt_Type = {
1096 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001097 0,
1098 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001099 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001100 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001101 (destructor)int_dealloc, /* tp_dealloc */
1102 (printfunc)int_print, /* tp_print */
1103 0, /* tp_getattr */
1104 0, /* tp_setattr */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001105 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001106 (reprfunc)int_repr, /* tp_repr */
1107 &int_as_number, /* tp_as_number */
1108 0, /* tp_as_sequence */
1109 0, /* tp_as_mapping */
1110 (hashfunc)int_hash, /* tp_hash */
1111 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001112 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001113 PyObject_GenericGetAttr, /* tp_getattro */
1114 0, /* tp_setattro */
1115 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00001116 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001117 int_doc, /* tp_doc */
1118 0, /* tp_traverse */
1119 0, /* tp_clear */
Guido van Rossum47b9ff62006-08-24 00:41:19 +00001120 int_richcompare, /* tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001121 0, /* tp_weaklistoffset */
1122 0, /* tp_iter */
1123 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001124 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001125 0, /* tp_members */
1126 0, /* tp_getset */
1127 0, /* tp_base */
1128 0, /* tp_dict */
1129 0, /* tp_descr_get */
1130 0, /* tp_descr_set */
1131 0, /* tp_dictoffset */
1132 0, /* tp_init */
1133 0, /* tp_alloc */
1134 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001135 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001136};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001137
Neal Norwitzc91ed402002-12-30 22:29:22 +00001138int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001139_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001140{
1141 PyIntObject *v;
1142 int ival;
1143#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1144 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001145 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001146 return 0;
1147 /* PyObject_New is inlined */
1148 v = free_list;
1149 free_list = (PyIntObject *)v->ob_type;
1150 PyObject_INIT(v, &PyInt_Type);
1151 v->ob_ival = ival;
1152 small_ints[ival + NSMALLNEGINTS] = v;
1153 }
1154#endif
1155 return 1;
1156}
1157
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001158void
Fred Drakea2f55112000-07-09 15:16:51 +00001159PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001160{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001161 PyIntObject *p;
1162 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001163 int i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001164 unsigned int ctr;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001165 int bc, bf; /* block count, number of freed blocks */
1166 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001167
Guido van Rossumda084ed1999-03-10 22:55:24 +00001168#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1169 PyIntObject **q;
1170
1171 i = NSMALLNEGINTS + NSMALLPOSINTS;
1172 q = small_ints;
1173 while (--i >= 0) {
1174 Py_XDECREF(*q);
1175 *q++ = NULL;
1176 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001177#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001178 bc = 0;
1179 bf = 0;
1180 isum = 0;
1181 list = block_list;
1182 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001183 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001184 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001185 bc++;
1186 irem = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001187 for (ctr = 0, p = &list->objects[0];
1188 ctr < N_INTOBJECTS;
1189 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001190 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001191 irem++;
1192 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001193 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001194 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001195 list->next = block_list;
1196 block_list = list;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197 for (ctr = 0, p = &list->objects[0];
1198 ctr < N_INTOBJECTS;
1199 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001200 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001201 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001202 p->ob_type = (struct _typeobject *)
1203 free_list;
1204 free_list = p;
1205 }
1206#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1207 else if (-NSMALLNEGINTS <= p->ob_ival &&
1208 p->ob_ival < NSMALLPOSINTS &&
1209 small_ints[p->ob_ival +
1210 NSMALLNEGINTS] == NULL) {
1211 Py_INCREF(p);
1212 small_ints[p->ob_ival +
1213 NSMALLNEGINTS] = p;
1214 }
1215#endif
1216 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001217 }
1218 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001219 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001220 bf++;
1221 }
1222 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001223 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001224 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001225 if (!Py_VerboseFlag)
1226 return;
1227 fprintf(stderr, "# cleanup ints");
1228 if (!isum) {
1229 fprintf(stderr, "\n");
1230 }
1231 else {
1232 fprintf(stderr,
1233 ": %d unfreed int%s in %d out of %d block%s\n",
1234 isum, isum == 1 ? "" : "s",
1235 bc - bf, bc, bc == 1 ? "" : "s");
1236 }
1237 if (Py_VerboseFlag > 1) {
1238 list = block_list;
1239 while (list != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001240 for (ctr = 0, p = &list->objects[0];
1241 ctr < N_INTOBJECTS;
1242 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001243 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001244 /* XXX(twouters) cast refcount to
1245 long until %zd is universally
1246 available
1247 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001248 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001249 "# <int at %p, refcnt=%ld, val=%ld>\n",
1250 p, (long)p->ob_refcnt,
1251 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001252 }
1253 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001254 }
1255 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001256}