blob: a82b3c889e53c1466cbeb0f693606f6592bf202e [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
Neal Norwitz8a87f5d2006-08-12 17:03:09 +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
Neal Norwitz8a87f5d2006-08-12 17:03:09 +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);
Kristján Valur Jónssonf0303942007-05-03 20:27:03 +0000218 if (io == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
219 PyErr_Clear();
220 io = (PyIntObject*) (*nb->nb_int) (op);
221 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000222 } else {
223 io = (PyIntObject*) (*nb->nb_int) (op);
224 }
225 if (io == NULL)
226 return -1;
227 if (!PyInt_Check(io)) {
228 if (PyLong_Check(io)) {
229 /* got a long? => retry int conversion */
230 val = _PyLong_AsSsize_t((PyObject *)io);
231 Py_DECREF(io);
232 if ((val == -1) && PyErr_Occurred())
233 return -1;
234 return val;
235 }
236 else
237 {
238 Py_DECREF(io);
239 PyErr_SetString(PyExc_TypeError,
240 "nb_int should return int object");
241 return -1;
242 }
243 }
244
245 val = PyInt_AS_LONG(io);
246 Py_DECREF(io);
247
248 return val;
249#endif
250}
251
Thomas Hellera4ea6032003-04-17 18:55:45 +0000252unsigned long
253PyInt_AsUnsignedLongMask(register PyObject *op)
254{
255 PyNumberMethods *nb;
256 PyIntObject *io;
257 unsigned long val;
258
259 if (op && PyInt_Check(op))
260 return PyInt_AS_LONG((PyIntObject*) op);
261 if (op && PyLong_Check(op))
262 return PyLong_AsUnsignedLongMask(op);
263
264 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
265 nb->nb_int == NULL) {
266 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000267 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000268 }
269
270 io = (PyIntObject*) (*nb->nb_int) (op);
271 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000272 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000273 if (!PyInt_Check(io)) {
274 if (PyLong_Check(io)) {
275 val = PyLong_AsUnsignedLongMask((PyObject *)io);
276 Py_DECREF(io);
277 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000278 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000279 return val;
280 }
281 else
282 {
283 Py_DECREF(io);
284 PyErr_SetString(PyExc_TypeError,
285 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000286 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000287 }
288 }
289
290 val = PyInt_AS_LONG(io);
291 Py_DECREF(io);
292
293 return val;
294}
295
296#ifdef HAVE_LONG_LONG
297unsigned PY_LONG_LONG
298PyInt_AsUnsignedLongLongMask(register PyObject *op)
299{
300 PyNumberMethods *nb;
301 PyIntObject *io;
302 unsigned PY_LONG_LONG val;
303
304 if (op && PyInt_Check(op))
305 return PyInt_AS_LONG((PyIntObject*) op);
306 if (op && PyLong_Check(op))
307 return PyLong_AsUnsignedLongLongMask(op);
308
309 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
310 nb->nb_int == NULL) {
311 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000312 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000313 }
314
315 io = (PyIntObject*) (*nb->nb_int) (op);
316 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000317 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000318 if (!PyInt_Check(io)) {
319 if (PyLong_Check(io)) {
320 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
321 Py_DECREF(io);
322 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000323 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000324 return val;
325 }
326 else
327 {
328 Py_DECREF(io);
329 PyErr_SetString(PyExc_TypeError,
330 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000331 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000332 }
333 }
334
335 val = PyInt_AS_LONG(io);
336 Py_DECREF(io);
337
338 return val;
339}
340#endif
341
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000342PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000343PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000344{
345 char *end;
346 long x;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000347 Py_ssize_t slen;
348 PyObject *sobj, *srepr;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000349
350 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000351 PyErr_SetString(PyExc_ValueError,
352 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000353 return NULL;
354 }
355
356 while (*s && isspace(Py_CHARMASK(*s)))
357 s++;
358 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000359 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000360 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000361 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000362 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000363 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000364 else
365 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000366 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000367 goto bad;
368 while (*end && isspace(Py_CHARMASK(*end)))
369 end++;
370 if (*end != '\0') {
371 bad:
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000372 slen = strlen(s) < 200 ? strlen(s) : 200;
373 sobj = PyString_FromStringAndSize(s, slen);
374 if (sobj == NULL)
375 return NULL;
376 srepr = PyObject_Repr(sobj);
377 Py_DECREF(sobj);
378 if (srepr == NULL)
379 return NULL;
380 PyErr_Format(PyExc_ValueError,
381 "invalid literal for int() with base %d: %s",
382 base, PyString_AS_STRING(srepr));
383 Py_DECREF(srepr);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000384 return NULL;
385 }
Tim Petersc8854432004-08-25 02:14:08 +0000386 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000387 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000388 if (pend)
389 *pend = end;
390 return PyInt_FromLong(x);
391}
392
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000393#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000394PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000395PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000396{
Walter Dörwald07e14762002-11-06 16:15:14 +0000397 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +0000398 char *buffer = (char *)PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000399
Walter Dörwald07e14762002-11-06 16:15:14 +0000400 if (buffer == NULL)
401 return NULL;
402
403 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
404 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000405 return NULL;
406 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000407 result = PyInt_FromString(buffer, NULL, base);
408 PyMem_FREE(buffer);
409 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000410}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000411#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000412
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413/* Methods */
414
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000415/* Integers are seen as the "smallest" of all numeric types and thus
416 don't have any knowledge about conversion of other types to
417 integers. */
418
419#define CONVERT_TO_LONG(obj, lng) \
420 if (PyInt_Check(obj)) { \
421 lng = PyInt_AS_LONG(obj); \
422 } \
423 else { \
424 Py_INCREF(Py_NotImplemented); \
425 return Py_NotImplemented; \
426 }
427
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000428/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000429static int
Fred Drakea2f55112000-07-09 15:16:51 +0000430int_print(PyIntObject *v, FILE *fp, int flags)
431 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000432{
433 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000434 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435}
436
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000437static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000438int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439{
Tim Peters42221042001-12-01 02:52:56 +0000440 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000441 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443}
444
445static int
Fred Drakea2f55112000-07-09 15:16:51 +0000446int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447{
448 register long i = v->ob_ival;
449 register long j = w->ob_ival;
450 return (i < j) ? -1 : (i > j) ? 1 : 0;
451}
452
Guido van Rossum9bfef441993-03-29 10:43:31 +0000453static long
Fred Drakea2f55112000-07-09 15:16:51 +0000454int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000455{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000456 /* XXX If this is changed, you also need to change the way
457 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000458 long x = v -> ob_ival;
459 if (x == -1)
460 x = -2;
461 return x;
462}
463
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000465int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466{
467 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000468 CONVERT_TO_LONG(v, a);
469 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000470 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000471 if ((x^a) >= 0 || (x^b) >= 0)
472 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000473 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474}
475
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000476static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000477int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478{
479 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000480 CONVERT_TO_LONG(v, a);
481 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000483 if ((x^a) >= 0 || (x^~b) >= 0)
484 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000485 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
486 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487}
488
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000489/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000490Integer overflow checking for * is painful: Python tried a couple ways, but
491they didn't work on all platforms, or failed in endcases (a product of
492-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000493
Tim Petersa3c01ce2001-12-04 23:05:10 +0000494Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000495
Tim Petersa3c01ce2001-12-04 23:05:10 +0000496The native long product x*y is either exactly right or *way* off, being
497just the last n bits of the true product, where n is the number of bits
498in a long (the delivered product is the true product plus i*2**n for
499some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000500
Tim Petersa3c01ce2001-12-04 23:05:10 +0000501The native double product (double)x * (double)y is subject to three
502rounding errors: on a sizeof(long)==8 box, each cast to double can lose
503info, and even on a sizeof(long)==4 box, the multiplication can lose info.
504But, unlike the native long product, it's not in *range* trouble: even
505if sizeof(long)==32 (256-bit longs), the product easily fits in the
506dynamic range of a double. So the leading 50 (or so) bits of the double
507product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000508
Tim Petersa3c01ce2001-12-04 23:05:10 +0000509We check these two ways against each other, and declare victory if they're
510approximately the same. Else, because the native long product is the only
511one that can lose catastrophic amounts of information, it's the native long
512product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000513*/
514
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000516int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000518 long a, b;
519 long longprod; /* a*b in native long arithmetic */
520 double doubled_longprod; /* (double)longprod */
521 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000522
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000523 CONVERT_TO_LONG(v, a);
524 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000525 longprod = a * b;
526 doubleprod = (double)a * (double)b;
527 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000528
Tim Petersa3c01ce2001-12-04 23:05:10 +0000529 /* Fast path for normal case: small multiplicands, and no info
530 is lost in either method. */
531 if (doubled_longprod == doubleprod)
532 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000533
Tim Petersa3c01ce2001-12-04 23:05:10 +0000534 /* Somebody somewhere lost info. Close enough, or way off? Note
535 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
536 The difference either is or isn't significant compared to the
537 true value (of which doubleprod is a good approximation).
538 */
539 {
540 const double diff = doubled_longprod - doubleprod;
541 const double absdiff = diff >= 0.0 ? diff : -diff;
542 const double absprod = doubleprod >= 0.0 ? doubleprod :
543 -doubleprod;
544 /* absdiff/absprod <= 1/32 iff
545 32 * absdiff <= absprod -- 5 good bits is "close enough" */
546 if (32.0 * absdiff <= absprod)
547 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000548 else
549 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000550 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000551}
552
Armin Rigo7ccbca92006-10-04 12:17:45 +0000553/* Integer overflow checking for unary negation: on a 2's-complement
554 * box, -x overflows iff x is the most negative long. In this case we
555 * get -x == x. However, -x is undefined (by C) if x /is/ the most
556 * negative long (it's a signed overflow case), and some compilers care.
557 * So we cast x to unsigned long first. However, then other compilers
558 * warn about applying unary minus to an unsigned operand. Hence the
559 * weird "0-".
560 */
561#define UNARY_NEG_WOULD_OVERFLOW(x) \
562 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
563
Guido van Rossume27f7952001-08-23 02:59:04 +0000564/* Return type of i_divmod */
565enum divmod_result {
566 DIVMOD_OK, /* Correct result */
567 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
568 DIVMOD_ERROR /* Exception raised */
569};
570
571static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000572i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000573 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000575 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000576
Tim Peters1dad6a82001-06-18 19:21:11 +0000577 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000579 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000580 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000582 /* (-sys.maxint-1)/-1 is the only overflow case. */
Armin Rigo7ccbca92006-10-04 12:17:45 +0000583 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
Guido van Rossume27f7952001-08-23 02:59:04 +0000584 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000585 xdivy = x / y;
586 xmody = x - xdivy * y;
587 /* If the signs of x and y differ, and the remainder is non-0,
588 * C89 doesn't define whether xdivy is now the floor or the
589 * ceiling of the infinitely precise quotient. We want the floor,
590 * and we have it iff the remainder's sign matches y's.
591 */
592 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
593 xmody += y;
594 --xdivy;
595 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000596 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000597 *p_xdivy = xdivy;
598 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000599 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000600}
601
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000603int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000604{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000605 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000606 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000607 CONVERT_TO_LONG(x, xi);
608 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000609 switch (i_divmod(xi, yi, &d, &m)) {
610 case DIVMOD_OK:
611 return PyInt_FromLong(d);
612 case DIVMOD_OVERFLOW:
613 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
614 (PyObject *)y);
615 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000616 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000617 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000618}
619
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000621int_classic_div(PyIntObject *x, PyIntObject *y)
622{
623 long xi, yi;
624 long d, m;
625 CONVERT_TO_LONG(x, xi);
626 CONVERT_TO_LONG(y, yi);
627 if (Py_DivisionWarningFlag &&
628 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
629 return NULL;
630 switch (i_divmod(xi, yi, &d, &m)) {
631 case DIVMOD_OK:
632 return PyInt_FromLong(d);
633 case DIVMOD_OVERFLOW:
634 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
635 (PyObject *)y);
636 default:
637 return NULL;
638 }
639}
640
641static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000642int_true_divide(PyObject *v, PyObject *w)
643{
Tim Peterse2a60002001-09-04 06:17:36 +0000644 /* If they aren't both ints, give someone else a chance. In
645 particular, this lets int/long get handled by longs, which
646 underflows to 0 gracefully if the long is too big to convert
647 to float. */
648 if (PyInt_Check(v) && PyInt_Check(w))
649 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
650 Py_INCREF(Py_NotImplemented);
651 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000652}
653
654static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000655int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000656{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000657 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000658 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000659 CONVERT_TO_LONG(x, xi);
660 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000661 switch (i_divmod(xi, yi, &d, &m)) {
662 case DIVMOD_OK:
663 return PyInt_FromLong(m);
664 case DIVMOD_OVERFLOW:
665 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
666 (PyObject *)y);
667 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000668 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000669 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000670}
671
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000673int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000674{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000675 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000676 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000677 CONVERT_TO_LONG(x, xi);
678 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000679 switch (i_divmod(xi, yi, &d, &m)) {
680 case DIVMOD_OK:
681 return Py_BuildValue("(ll)", d, m);
682 case DIVMOD_OVERFLOW:
683 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
684 (PyObject *)y);
685 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000686 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000687 }
Guido van Rossum00466951991-05-05 20:08:27 +0000688}
689
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000691int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000692{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000693 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000694 CONVERT_TO_LONG(v, iv);
695 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000696 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000697 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000698 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
699 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000700 return NULL;
701 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000702 /* Return a float. This works because we know that
703 this calls float_pow() which converts its
704 arguments to double. */
705 return PyFloat_Type.tp_as_number->nb_power(
706 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000707 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000709 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000710 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000712 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000713 return NULL;
714 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000715 }
716 /*
717 * XXX: The original exponentiation code stopped looping
718 * when temp hit zero; this code will continue onwards
719 * unnecessarily, but at least it won't cause any errors.
720 * Hopefully the speed improvement from the fast exponentiation
721 * will compensate for the slight inefficiency.
722 * XXX: Better handling of overflows is desperately needed.
723 */
724 temp = iv;
725 ix = 1;
726 while (iw > 0) {
727 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000728 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000729 ix = ix*temp;
730 if (temp == 0)
731 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000732 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000733 return PyLong_Type.tp_as_number->nb_power(
734 (PyObject *)v,
735 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000736 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000737 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000738 }
739 iw >>= 1; /* Shift exponent down by 1 bit */
740 if (iw==0) break;
741 prev = temp;
742 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000743 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000744 return PyLong_Type.tp_as_number->nb_power(
745 (PyObject *)v, (PyObject *)w, (PyObject *)z);
746 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000747 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000748 /* If we did a multiplication, perform a modulo */
749 ix = ix % iz;
750 temp = temp % iz;
751 }
752 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000753 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000754 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000755 switch (i_divmod(ix, iz, &div, &mod)) {
756 case DIVMOD_OK:
757 ix = mod;
758 break;
759 case DIVMOD_OVERFLOW:
760 return PyLong_Type.tp_as_number->nb_power(
761 (PyObject *)v, (PyObject *)w, (PyObject *)z);
762 default:
763 return NULL;
764 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000765 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000767}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000768
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000769static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000770int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771{
Martin v. Löwis820d6ac2006-10-04 05:47:34 +0000772 register long a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000773 a = v->ob_ival;
Armin Rigo7ccbca92006-10-04 12:17:45 +0000774 /* check for overflow */
775 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
Tim Petersc8854432004-08-25 02:14:08 +0000776 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000777 if (o != NULL) {
778 PyObject *result = PyNumber_Negative(o);
779 Py_DECREF(o);
780 return result;
781 }
782 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000783 }
Martin v. Löwis820d6ac2006-10-04 05:47:34 +0000784 return PyInt_FromLong(-a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000785}
786
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000788int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000789{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000790 if (PyInt_CheckExact(v)) {
791 Py_INCREF(v);
792 return (PyObject *)v;
793 }
794 else
795 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000796}
797
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000799int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000800{
801 if (v->ob_ival >= 0)
802 return int_pos(v);
803 else
804 return int_neg(v);
805}
806
Guido van Rossum0bff0151991-05-14 12:05:32 +0000807static int
Fred Drakea2f55112000-07-09 15:16:51 +0000808int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000809{
810 return v->ob_ival != 0;
811}
812
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000814int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000815{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000816 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000817}
818
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000819static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000820int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000821{
Guido van Rossum078151d2002-08-11 04:24:12 +0000822 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000823 PyObject *vv, *ww, *result;
824
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000825 CONVERT_TO_LONG(v, a);
826 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000827 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000828 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000829 return NULL;
830 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000831 if (a == 0 || b == 0)
832 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000833 if (b >= LONG_BIT) {
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 Rossumf3b351f1992-01-14 18:33:22 +0000846 }
Tim Petersda1a2212002-08-11 17:54:42 +0000847 c = a << b;
848 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000849 vv = PyLong_FromLong(PyInt_AS_LONG(v));
850 if (vv == NULL)
851 return NULL;
852 ww = PyLong_FromLong(PyInt_AS_LONG(w));
853 if (ww == NULL) {
854 Py_DECREF(vv);
855 return NULL;
856 }
857 result = PyNumber_Lshift(vv, ww);
858 Py_DECREF(vv);
859 Py_DECREF(ww);
860 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000861 }
862 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000863}
864
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000865static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000866int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000867{
868 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000869 CONVERT_TO_LONG(v, a);
870 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000871 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000872 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000873 return NULL;
874 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000875 if (a == 0 || b == 0)
876 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000877 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000878 if (a < 0)
879 a = -1;
880 else
881 a = 0;
882 }
883 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000884 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000885 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000886 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000887}
888
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000889static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000890int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000891{
892 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000893 CONVERT_TO_LONG(v, a);
894 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000895 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000896}
897
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000898static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000899int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000900{
901 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000902 CONVERT_TO_LONG(v, a);
903 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000904 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000905}
906
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000907static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000908int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000909{
910 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000911 CONVERT_TO_LONG(v, a);
912 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000913 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000914}
915
Guido van Rossum1952e382001-09-19 01:25:16 +0000916static int
917int_coerce(PyObject **pv, PyObject **pw)
918{
919 if (PyInt_Check(*pw)) {
920 Py_INCREF(*pv);
921 Py_INCREF(*pw);
922 return 0;
923 }
924 return 1; /* Can't do it */
925}
926
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000927static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000928int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000929{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000930 if (PyInt_CheckExact(v))
931 Py_INCREF(v);
932 else
933 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000934 return (PyObject *)v;
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_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000939{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000940 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000941}
942
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000943static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000944int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000945{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000946 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000947}
948
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000949static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000950int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000951{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000952 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000953 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000954 if (x < 0)
955 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
956 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000957 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000958 else
Barry Warsaw61975092001-11-28 20:55:34 +0000959 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000960 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000961}
962
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000963static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000964int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000965{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000966 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000967 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000968 if (x < 0)
969 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
970 else
971 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000972 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000973}
974
Jeremy Hylton938ace62002-07-17 16:30:39 +0000975static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000976int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
977
Tim Peters6d6c1a32001-08-02 04:15:00 +0000978static PyObject *
979int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
980{
981 PyObject *x = NULL;
982 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000983 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000984
Guido van Rossumbef14172001-08-29 15:47:46 +0000985 if (type != &PyInt_Type)
986 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000987 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
988 &x, &base))
989 return NULL;
990 if (x == NULL)
991 return PyInt_FromLong(0L);
992 if (base == -909)
993 return PyNumber_Int(x);
Georg Brandl2c1375c2006-10-12 11:27:59 +0000994 if (PyString_Check(x)) {
995 /* Since PyInt_FromString doesn't have a length parameter,
996 * check here for possible NULs in the string. */
997 char *string = PyString_AS_STRING(x);
998 if (strlen(string) != PyString_Size(x)) {
999 /* create a repr() of the input string,
1000 * just like PyInt_FromString does */
1001 PyObject *srepr;
1002 srepr = PyObject_Repr(x);
1003 if (srepr == NULL)
1004 return NULL;
1005 PyErr_Format(PyExc_ValueError,
1006 "invalid literal for int() with base %d: %s",
1007 base, PyString_AS_STRING(srepr));
1008 Py_DECREF(srepr);
1009 return NULL;
1010 }
1011 return PyInt_FromString(string, NULL, base);
1012 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001013#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014 if (PyUnicode_Check(x))
1015 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
1016 PyUnicode_GET_SIZE(x),
1017 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001018#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00001019 PyErr_SetString(PyExc_TypeError,
1020 "int() can't convert non-string with explicit base");
1021 return NULL;
1022}
1023
Guido van Rossumbef14172001-08-29 15:47:46 +00001024/* Wimpy, slow approach to tp_new calls for subtypes of int:
1025 first create a regular int from whatever arguments we got,
1026 then allocate a subtype instance and initialize its ob_ival
1027 from the regular int. The regular int is then thrown away.
1028*/
1029static PyObject *
1030int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1031{
Anthony Baxter377be112006-04-11 06:54:30 +00001032 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001033 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001034
1035 assert(PyType_IsSubtype(type, &PyInt_Type));
1036 tmp = int_new(&PyInt_Type, args, kwds);
1037 if (tmp == NULL)
1038 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001039 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001040 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001041 if (ival == -1 && PyErr_Occurred()) {
1042 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001043 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001044 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001045 } else {
1046 ival = ((PyIntObject *)tmp)->ob_ival;
1047 }
1048
Anthony Baxter377be112006-04-11 06:54:30 +00001049 newobj = type->tp_alloc(type, 0);
1050 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001051 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001052 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001053 }
Anthony Baxter377be112006-04-11 06:54:30 +00001054 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001055 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00001056 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001057}
1058
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001059static PyObject *
1060int_getnewargs(PyIntObject *v)
1061{
1062 return Py_BuildValue("(l)", v->ob_ival);
1063}
1064
1065static PyMethodDef int_methods[] = {
1066 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
1067 {NULL, NULL} /* sentinel */
1068};
1069
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001070PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001071"int(x[, base]) -> integer\n\
1072\n\
1073Convert a string or number to an integer, if possible. A floating point\n\
1074argument will be truncated towards zero (this does not include a string\n\
1075representation of a floating point number!) When converting a string, use\n\
1076the optional base. It is an error to supply a base when converting a\n\
Gustavo Niemeyer37e65022007-01-10 16:15:48 +00001077non-string. If base is zero, the proper base is guessed based on the\n\
Gustavo Niemeyera443bc82007-01-10 16:13:40 +00001078string content. If the argument is outside the integer range a\n\
1079long object will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001080
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001081static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001082 (binaryfunc)int_add, /*nb_add*/
1083 (binaryfunc)int_sub, /*nb_subtract*/
1084 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00001085 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001086 (binaryfunc)int_mod, /*nb_remainder*/
1087 (binaryfunc)int_divmod, /*nb_divmod*/
1088 (ternaryfunc)int_pow, /*nb_power*/
1089 (unaryfunc)int_neg, /*nb_negative*/
1090 (unaryfunc)int_pos, /*nb_positive*/
1091 (unaryfunc)int_abs, /*nb_absolute*/
1092 (inquiry)int_nonzero, /*nb_nonzero*/
1093 (unaryfunc)int_invert, /*nb_invert*/
1094 (binaryfunc)int_lshift, /*nb_lshift*/
1095 (binaryfunc)int_rshift, /*nb_rshift*/
1096 (binaryfunc)int_and, /*nb_and*/
1097 (binaryfunc)int_xor, /*nb_xor*/
1098 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +00001099 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001100 (unaryfunc)int_int, /*nb_int*/
1101 (unaryfunc)int_long, /*nb_long*/
1102 (unaryfunc)int_float, /*nb_float*/
1103 (unaryfunc)int_oct, /*nb_oct*/
1104 (unaryfunc)int_hex, /*nb_hex*/
1105 0, /*nb_inplace_add*/
1106 0, /*nb_inplace_subtract*/
1107 0, /*nb_inplace_multiply*/
1108 0, /*nb_inplace_divide*/
1109 0, /*nb_inplace_remainder*/
1110 0, /*nb_inplace_power*/
1111 0, /*nb_inplace_lshift*/
1112 0, /*nb_inplace_rshift*/
1113 0, /*nb_inplace_and*/
1114 0, /*nb_inplace_xor*/
1115 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001116 (binaryfunc)int_div, /* nb_floor_divide */
1117 int_true_divide, /* nb_true_divide */
1118 0, /* nb_inplace_floor_divide */
1119 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001120 (unaryfunc)int_int, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001121};
1122
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001123PyTypeObject PyInt_Type = {
1124 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001125 0,
1126 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001127 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001128 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001129 (destructor)int_dealloc, /* tp_dealloc */
1130 (printfunc)int_print, /* tp_print */
1131 0, /* tp_getattr */
1132 0, /* tp_setattr */
1133 (cmpfunc)int_compare, /* tp_compare */
1134 (reprfunc)int_repr, /* tp_repr */
1135 &int_as_number, /* tp_as_number */
1136 0, /* tp_as_sequence */
1137 0, /* tp_as_mapping */
1138 (hashfunc)int_hash, /* tp_hash */
1139 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001140 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001141 PyObject_GenericGetAttr, /* tp_getattro */
1142 0, /* tp_setattro */
1143 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001144 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00001145 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001146 int_doc, /* tp_doc */
1147 0, /* tp_traverse */
1148 0, /* tp_clear */
1149 0, /* tp_richcompare */
1150 0, /* tp_weaklistoffset */
1151 0, /* tp_iter */
1152 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001153 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001154 0, /* tp_members */
1155 0, /* tp_getset */
1156 0, /* tp_base */
1157 0, /* tp_dict */
1158 0, /* tp_descr_get */
1159 0, /* tp_descr_set */
1160 0, /* tp_dictoffset */
1161 0, /* tp_init */
1162 0, /* tp_alloc */
1163 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001164 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001165};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001166
Neal Norwitzc91ed402002-12-30 22:29:22 +00001167int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001168_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001169{
1170 PyIntObject *v;
1171 int ival;
1172#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1173 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001174 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001175 return 0;
1176 /* PyObject_New is inlined */
1177 v = free_list;
1178 free_list = (PyIntObject *)v->ob_type;
1179 PyObject_INIT(v, &PyInt_Type);
1180 v->ob_ival = ival;
1181 small_ints[ival + NSMALLNEGINTS] = v;
1182 }
1183#endif
1184 return 1;
1185}
1186
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001187void
Fred Drakea2f55112000-07-09 15:16:51 +00001188PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001189{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001190 PyIntObject *p;
1191 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001192 int i;
Skip Montanaro429433b2006-04-18 00:35:43 +00001193 unsigned int ctr;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001194 int bc, bf; /* block count, number of freed blocks */
1195 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001196
Guido van Rossumda084ed1999-03-10 22:55:24 +00001197#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1198 PyIntObject **q;
1199
1200 i = NSMALLNEGINTS + NSMALLPOSINTS;
1201 q = small_ints;
1202 while (--i >= 0) {
1203 Py_XDECREF(*q);
1204 *q++ = NULL;
1205 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001206#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001207 bc = 0;
1208 bf = 0;
1209 isum = 0;
1210 list = block_list;
1211 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001212 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001213 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001214 bc++;
1215 irem = 0;
Skip Montanaro429433b2006-04-18 00:35:43 +00001216 for (ctr = 0, p = &list->objects[0];
1217 ctr < N_INTOBJECTS;
1218 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001219 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001220 irem++;
1221 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001222 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001223 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001224 list->next = block_list;
1225 block_list = list;
Skip Montanaro429433b2006-04-18 00:35:43 +00001226 for (ctr = 0, p = &list->objects[0];
1227 ctr < N_INTOBJECTS;
1228 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001229 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001230 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001231 p->ob_type = (struct _typeobject *)
1232 free_list;
1233 free_list = p;
1234 }
1235#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1236 else if (-NSMALLNEGINTS <= p->ob_ival &&
1237 p->ob_ival < NSMALLPOSINTS &&
1238 small_ints[p->ob_ival +
1239 NSMALLNEGINTS] == NULL) {
1240 Py_INCREF(p);
1241 small_ints[p->ob_ival +
1242 NSMALLNEGINTS] = p;
1243 }
1244#endif
1245 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001246 }
1247 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001248 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001249 bf++;
1250 }
1251 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001252 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001253 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001254 if (!Py_VerboseFlag)
1255 return;
1256 fprintf(stderr, "# cleanup ints");
1257 if (!isum) {
1258 fprintf(stderr, "\n");
1259 }
1260 else {
1261 fprintf(stderr,
1262 ": %d unfreed int%s in %d out of %d block%s\n",
1263 isum, isum == 1 ? "" : "s",
1264 bc - bf, bc, bc == 1 ? "" : "s");
1265 }
1266 if (Py_VerboseFlag > 1) {
1267 list = block_list;
1268 while (list != NULL) {
Skip Montanaro429433b2006-04-18 00:35:43 +00001269 for (ctr = 0, p = &list->objects[0];
1270 ctr < N_INTOBJECTS;
1271 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001272 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001273 /* XXX(twouters) cast refcount to
1274 long until %zd is universally
1275 available
1276 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001277 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001278 "# <int at %p, refcnt=%ld, val=%ld>\n",
1279 p, (long)p->ob_refcnt,
1280 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001281 }
1282 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001283 }
1284 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001285}