blob: 9333a55873aaf5181628a560feb06f95f0cc7c93 [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
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +0000216 if (nb->nb_long != 0)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000217 io = (PyIntObject*) (*nb->nb_long) (op);
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +0000218 else
Martin v. Löwis18e16552006-02-15 17:27:45 +0000219 io = (PyIntObject*) (*nb->nb_int) (op);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000220 if (io == NULL)
221 return -1;
222 if (!PyInt_Check(io)) {
223 if (PyLong_Check(io)) {
224 /* got a long? => retry int conversion */
225 val = _PyLong_AsSsize_t((PyObject *)io);
226 Py_DECREF(io);
227 if ((val == -1) && PyErr_Occurred())
228 return -1;
229 return val;
230 }
231 else
232 {
233 Py_DECREF(io);
234 PyErr_SetString(PyExc_TypeError,
235 "nb_int should return int object");
236 return -1;
237 }
238 }
239
240 val = PyInt_AS_LONG(io);
241 Py_DECREF(io);
242
243 return val;
244#endif
245}
246
Thomas Hellera4ea6032003-04-17 18:55:45 +0000247unsigned long
248PyInt_AsUnsignedLongMask(register PyObject *op)
249{
250 PyNumberMethods *nb;
251 PyIntObject *io;
252 unsigned long val;
253
254 if (op && PyInt_Check(op))
255 return PyInt_AS_LONG((PyIntObject*) op);
256 if (op && PyLong_Check(op))
257 return PyLong_AsUnsignedLongMask(op);
258
259 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
260 nb->nb_int == NULL) {
261 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000262 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000263 }
264
265 io = (PyIntObject*) (*nb->nb_int) (op);
266 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000267 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000268 if (!PyInt_Check(io)) {
269 if (PyLong_Check(io)) {
270 val = PyLong_AsUnsignedLongMask((PyObject *)io);
271 Py_DECREF(io);
272 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000273 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000274 return val;
275 }
276 else
277 {
278 Py_DECREF(io);
279 PyErr_SetString(PyExc_TypeError,
280 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000281 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000282 }
283 }
284
285 val = PyInt_AS_LONG(io);
286 Py_DECREF(io);
287
288 return val;
289}
290
291#ifdef HAVE_LONG_LONG
292unsigned PY_LONG_LONG
293PyInt_AsUnsignedLongLongMask(register PyObject *op)
294{
295 PyNumberMethods *nb;
296 PyIntObject *io;
297 unsigned PY_LONG_LONG val;
298
299 if (op && PyInt_Check(op))
300 return PyInt_AS_LONG((PyIntObject*) op);
301 if (op && PyLong_Check(op))
302 return PyLong_AsUnsignedLongLongMask(op);
303
304 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
305 nb->nb_int == NULL) {
306 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000307 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000308 }
309
310 io = (PyIntObject*) (*nb->nb_int) (op);
311 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000312 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000313 if (!PyInt_Check(io)) {
314 if (PyLong_Check(io)) {
315 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
316 Py_DECREF(io);
317 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000318 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000319 return val;
320 }
321 else
322 {
323 Py_DECREF(io);
324 PyErr_SetString(PyExc_TypeError,
325 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000326 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000327 }
328 }
329
330 val = PyInt_AS_LONG(io);
331 Py_DECREF(io);
332
333 return val;
334}
335#endif
336
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000337PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000338PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000339{
340 char *end;
341 long x;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000342 Py_ssize_t slen;
343 PyObject *sobj, *srepr;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000344
345 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000346 PyErr_SetString(PyExc_ValueError,
347 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000348 return NULL;
349 }
350
351 while (*s && isspace(Py_CHARMASK(*s)))
352 s++;
353 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000354 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000355 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000356 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000357 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000358 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000359 else
360 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000361 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000362 goto bad;
363 while (*end && isspace(Py_CHARMASK(*end)))
364 end++;
365 if (*end != '\0') {
366 bad:
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000367 slen = strlen(s) < 200 ? strlen(s) : 200;
368 sobj = PyString_FromStringAndSize(s, slen);
369 if (sobj == NULL)
370 return NULL;
371 srepr = PyObject_Repr(sobj);
372 Py_DECREF(sobj);
373 if (srepr == NULL)
374 return NULL;
375 PyErr_Format(PyExc_ValueError,
376 "invalid literal for int() with base %d: %s",
377 base, PyString_AS_STRING(srepr));
378 Py_DECREF(srepr);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000379 return NULL;
380 }
Tim Petersc8854432004-08-25 02:14:08 +0000381 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000382 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000383 if (pend)
384 *pend = end;
385 return PyInt_FromLong(x);
386}
387
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000388#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000389PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000390PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000391{
Walter Dörwald07e14762002-11-06 16:15:14 +0000392 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +0000393 char *buffer = (char *)PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000394
Walter Dörwald07e14762002-11-06 16:15:14 +0000395 if (buffer == NULL)
396 return NULL;
397
398 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
399 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000400 return NULL;
401 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000402 result = PyInt_FromString(buffer, NULL, base);
403 PyMem_FREE(buffer);
404 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000405}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000406#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000407
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408/* Methods */
409
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000410/* Integers are seen as the "smallest" of all numeric types and thus
411 don't have any knowledge about conversion of other types to
412 integers. */
413
414#define CONVERT_TO_LONG(obj, lng) \
415 if (PyInt_Check(obj)) { \
416 lng = PyInt_AS_LONG(obj); \
417 } \
418 else { \
419 Py_INCREF(Py_NotImplemented); \
420 return Py_NotImplemented; \
421 }
422
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000423/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000424static int
Fred Drakea2f55112000-07-09 15:16:51 +0000425int_print(PyIntObject *v, FILE *fp, int flags)
426 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427{
428 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000429 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430}
431
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000433int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434{
Tim Peters42221042001-12-01 02:52:56 +0000435 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000436 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000437 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438}
439
440static int
Fred Drakea2f55112000-07-09 15:16:51 +0000441int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442{
443 register long i = v->ob_ival;
444 register long j = w->ob_ival;
445 return (i < j) ? -1 : (i > j) ? 1 : 0;
446}
447
Guido van Rossum9bfef441993-03-29 10:43:31 +0000448static long
Fred Drakea2f55112000-07-09 15:16:51 +0000449int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000450{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000451 /* XXX If this is changed, you also need to change the way
452 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000453 long x = v -> ob_ival;
454 if (x == -1)
455 x = -2;
456 return x;
457}
458
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000460int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461{
462 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000463 CONVERT_TO_LONG(v, a);
464 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000466 if ((x^a) >= 0 || (x^b) >= 0)
467 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000468 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469}
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000472int_sub(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_subtract((PyObject *)v,
481 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482}
483
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000484/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000485Integer overflow checking for * is painful: Python tried a couple ways, but
486they didn't work on all platforms, or failed in endcases (a product of
487-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000488
Tim Petersa3c01ce2001-12-04 23:05:10 +0000489Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000490
Tim Petersa3c01ce2001-12-04 23:05:10 +0000491The native long product x*y is either exactly right or *way* off, being
492just the last n bits of the true product, where n is the number of bits
493in a long (the delivered product is the true product plus i*2**n for
494some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000495
Tim Petersa3c01ce2001-12-04 23:05:10 +0000496The native double product (double)x * (double)y is subject to three
497rounding errors: on a sizeof(long)==8 box, each cast to double can lose
498info, and even on a sizeof(long)==4 box, the multiplication can lose info.
499But, unlike the native long product, it's not in *range* trouble: even
500if sizeof(long)==32 (256-bit longs), the product easily fits in the
501dynamic range of a double. So the leading 50 (or so) bits of the double
502product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000503
Tim Petersa3c01ce2001-12-04 23:05:10 +0000504We check these two ways against each other, and declare victory if they're
505approximately the same. Else, because the native long product is the only
506one that can lose catastrophic amounts of information, it's the native long
507product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000508*/
509
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000511int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000513 long a, b;
514 long longprod; /* a*b in native long arithmetic */
515 double doubled_longprod; /* (double)longprod */
516 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000517
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000518 CONVERT_TO_LONG(v, a);
519 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000520 longprod = a * b;
521 doubleprod = (double)a * (double)b;
522 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000523
Tim Petersa3c01ce2001-12-04 23:05:10 +0000524 /* Fast path for normal case: small multiplicands, and no info
525 is lost in either method. */
526 if (doubled_longprod == doubleprod)
527 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000528
Tim Petersa3c01ce2001-12-04 23:05:10 +0000529 /* Somebody somewhere lost info. Close enough, or way off? Note
530 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
531 The difference either is or isn't significant compared to the
532 true value (of which doubleprod is a good approximation).
533 */
534 {
535 const double diff = doubled_longprod - doubleprod;
536 const double absdiff = diff >= 0.0 ? diff : -diff;
537 const double absprod = doubleprod >= 0.0 ? doubleprod :
538 -doubleprod;
539 /* absdiff/absprod <= 1/32 iff
540 32 * absdiff <= absprod -- 5 good bits is "close enough" */
541 if (32.0 * absdiff <= absprod)
542 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000543 else
544 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000545 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546}
547
Armin Rigo7ccbca92006-10-04 12:17:45 +0000548/* Integer overflow checking for unary negation: on a 2's-complement
549 * box, -x overflows iff x is the most negative long. In this case we
550 * get -x == x. However, -x is undefined (by C) if x /is/ the most
551 * negative long (it's a signed overflow case), and some compilers care.
552 * So we cast x to unsigned long first. However, then other compilers
553 * warn about applying unary minus to an unsigned operand. Hence the
554 * weird "0-".
555 */
556#define UNARY_NEG_WOULD_OVERFLOW(x) \
557 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
558
Guido van Rossume27f7952001-08-23 02:59:04 +0000559/* Return type of i_divmod */
560enum divmod_result {
561 DIVMOD_OK, /* Correct result */
562 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
563 DIVMOD_ERROR /* Exception raised */
564};
565
566static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000567i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000568 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000570 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000571
Tim Peters1dad6a82001-06-18 19:21:11 +0000572 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000573 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000574 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000575 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000577 /* (-sys.maxint-1)/-1 is the only overflow case. */
Armin Rigo7ccbca92006-10-04 12:17:45 +0000578 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
Guido van Rossume27f7952001-08-23 02:59:04 +0000579 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000580 xdivy = x / y;
581 xmody = x - xdivy * y;
582 /* If the signs of x and y differ, and the remainder is non-0,
583 * C89 doesn't define whether xdivy is now the floor or the
584 * ceiling of the infinitely precise quotient. We want the floor,
585 * and we have it iff the remainder's sign matches y's.
586 */
587 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
588 xmody += y;
589 --xdivy;
590 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000591 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000592 *p_xdivy = xdivy;
593 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000594 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000595}
596
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000597static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000598int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000599{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000600 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000601 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000602 CONVERT_TO_LONG(x, xi);
603 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000604 switch (i_divmod(xi, yi, &d, &m)) {
605 case DIVMOD_OK:
606 return PyInt_FromLong(d);
607 case DIVMOD_OVERFLOW:
608 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
609 (PyObject *)y);
610 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000611 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000612 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000613}
614
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000616int_classic_div(PyIntObject *x, PyIntObject *y)
617{
618 long xi, yi;
619 long d, m;
620 CONVERT_TO_LONG(x, xi);
621 CONVERT_TO_LONG(y, yi);
622 if (Py_DivisionWarningFlag &&
623 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
624 return NULL;
625 switch (i_divmod(xi, yi, &d, &m)) {
626 case DIVMOD_OK:
627 return PyInt_FromLong(d);
628 case DIVMOD_OVERFLOW:
629 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
630 (PyObject *)y);
631 default:
632 return NULL;
633 }
634}
635
636static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000637int_true_divide(PyObject *v, PyObject *w)
638{
Tim Peterse2a60002001-09-04 06:17:36 +0000639 /* If they aren't both ints, give someone else a chance. In
640 particular, this lets int/long get handled by longs, which
641 underflows to 0 gracefully if the long is too big to convert
642 to float. */
643 if (PyInt_Check(v) && PyInt_Check(w))
644 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
645 Py_INCREF(Py_NotImplemented);
646 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000647}
648
649static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000650int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000651{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000652 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000653 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000654 CONVERT_TO_LONG(x, xi);
655 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000656 switch (i_divmod(xi, yi, &d, &m)) {
657 case DIVMOD_OK:
658 return PyInt_FromLong(m);
659 case DIVMOD_OVERFLOW:
660 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
661 (PyObject *)y);
662 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000663 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000664 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000665}
666
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000668int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000669{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000670 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000671 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000672 CONVERT_TO_LONG(x, xi);
673 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000674 switch (i_divmod(xi, yi, &d, &m)) {
675 case DIVMOD_OK:
676 return Py_BuildValue("(ll)", d, m);
677 case DIVMOD_OVERFLOW:
678 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
679 (PyObject *)y);
680 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000681 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000682 }
Guido van Rossum00466951991-05-05 20:08:27 +0000683}
684
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000686int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000687{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000688 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000689 CONVERT_TO_LONG(v, iv);
690 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000691 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000692 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000693 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
694 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000695 return NULL;
696 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000697 /* Return a float. This works because we know that
698 this calls float_pow() which converts its
699 arguments to double. */
700 return PyFloat_Type.tp_as_number->nb_power(
701 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000702 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000704 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000705 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000707 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000708 return NULL;
709 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000710 }
711 /*
712 * XXX: The original exponentiation code stopped looping
713 * when temp hit zero; this code will continue onwards
714 * unnecessarily, but at least it won't cause any errors.
715 * Hopefully the speed improvement from the fast exponentiation
716 * will compensate for the slight inefficiency.
717 * XXX: Better handling of overflows is desperately needed.
718 */
719 temp = iv;
720 ix = 1;
721 while (iw > 0) {
722 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000723 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000724 ix = ix*temp;
725 if (temp == 0)
726 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000727 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000728 return PyLong_Type.tp_as_number->nb_power(
729 (PyObject *)v,
730 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000731 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000732 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000733 }
734 iw >>= 1; /* Shift exponent down by 1 bit */
735 if (iw==0) break;
736 prev = temp;
737 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000738 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000739 return PyLong_Type.tp_as_number->nb_power(
740 (PyObject *)v, (PyObject *)w, (PyObject *)z);
741 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000742 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000743 /* If we did a multiplication, perform a modulo */
744 ix = ix % iz;
745 temp = temp % iz;
746 }
747 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000748 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000749 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000750 switch (i_divmod(ix, iz, &div, &mod)) {
751 case DIVMOD_OK:
752 ix = mod;
753 break;
754 case DIVMOD_OVERFLOW:
755 return PyLong_Type.tp_as_number->nb_power(
756 (PyObject *)v, (PyObject *)w, (PyObject *)z);
757 default:
758 return NULL;
759 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000760 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000761 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000762}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000763
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000765int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000766{
Martin v. Löwis820d6ac2006-10-04 05:47:34 +0000767 register long a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000768 a = v->ob_ival;
Armin Rigo7ccbca92006-10-04 12:17:45 +0000769 /* check for overflow */
770 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
Tim Petersc8854432004-08-25 02:14:08 +0000771 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000772 if (o != NULL) {
773 PyObject *result = PyNumber_Negative(o);
774 Py_DECREF(o);
775 return result;
776 }
777 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000778 }
Martin v. Löwis820d6ac2006-10-04 05:47:34 +0000779 return PyInt_FromLong(-a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780}
781
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000782static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000783int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000784{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000785 if (PyInt_CheckExact(v)) {
786 Py_INCREF(v);
787 return (PyObject *)v;
788 }
789 else
790 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000791}
792
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000793static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000794int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000795{
796 if (v->ob_ival >= 0)
797 return int_pos(v);
798 else
799 return int_neg(v);
800}
801
Guido van Rossum0bff0151991-05-14 12:05:32 +0000802static int
Fred Drakea2f55112000-07-09 15:16:51 +0000803int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000804{
805 return v->ob_ival != 0;
806}
807
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000808static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000809int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000810{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000811 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000812}
813
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000815int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000816{
Guido van Rossum078151d2002-08-11 04:24:12 +0000817 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000818 PyObject *vv, *ww, *result;
819
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000820 CONVERT_TO_LONG(v, a);
821 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000822 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000824 return NULL;
825 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000826 if (a == 0 || b == 0)
827 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000828 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000829 vv = PyLong_FromLong(PyInt_AS_LONG(v));
830 if (vv == NULL)
831 return NULL;
832 ww = PyLong_FromLong(PyInt_AS_LONG(w));
833 if (ww == NULL) {
834 Py_DECREF(vv);
835 return NULL;
836 }
837 result = PyNumber_Lshift(vv, ww);
838 Py_DECREF(vv);
839 Py_DECREF(ww);
840 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000841 }
Tim Petersda1a2212002-08-11 17:54:42 +0000842 c = a << b;
843 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000844 vv = PyLong_FromLong(PyInt_AS_LONG(v));
845 if (vv == NULL)
846 return NULL;
847 ww = PyLong_FromLong(PyInt_AS_LONG(w));
848 if (ww == NULL) {
849 Py_DECREF(vv);
850 return NULL;
851 }
852 result = PyNumber_Lshift(vv, ww);
853 Py_DECREF(vv);
854 Py_DECREF(ww);
855 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000856 }
857 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000858}
859
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000860static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000861int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000862{
863 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000864 CONVERT_TO_LONG(v, a);
865 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000866 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000867 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000868 return NULL;
869 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000870 if (a == 0 || b == 0)
871 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000872 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000873 if (a < 0)
874 a = -1;
875 else
876 a = 0;
877 }
878 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000879 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000880 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000881 return PyInt_FromLong(a);
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_and(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_xor(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_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000904{
905 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000906 CONVERT_TO_LONG(v, a);
907 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000909}
910
Guido van Rossum1952e382001-09-19 01:25:16 +0000911static int
912int_coerce(PyObject **pv, PyObject **pw)
913{
914 if (PyInt_Check(*pw)) {
915 Py_INCREF(*pv);
916 Py_INCREF(*pw);
917 return 0;
918 }
919 return 1; /* Can't do it */
920}
921
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000922static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000923int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000924{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000925 if (PyInt_CheckExact(v))
926 Py_INCREF(v);
927 else
928 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000929 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000930}
931
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000932static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000933int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000934{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935 return PyLong_FromLong((v -> ob_ival));
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_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000940{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000941 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000942}
943
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000944static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000945int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000946{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000947 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000948 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000949 if (x < 0)
950 PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x);
951 else if (x == 0)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000952 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000953 else
Barry Warsaw61975092001-11-28 20:55:34 +0000954 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000955 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000956}
957
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000958static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000959int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000960{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000961 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000962 long x = v -> ob_ival;
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000963 if (x < 0)
964 PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x);
965 else
966 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000967 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000968}
969
Jeremy Hylton938ace62002-07-17 16:30:39 +0000970static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000971int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
972
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973static PyObject *
974int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
975{
976 PyObject *x = NULL;
977 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000978 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000979
Guido van Rossumbef14172001-08-29 15:47:46 +0000980 if (type != &PyInt_Type)
981 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000982 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
983 &x, &base))
984 return NULL;
985 if (x == NULL)
986 return PyInt_FromLong(0L);
987 if (base == -909)
988 return PyNumber_Int(x);
Georg Brandl2c1375c2006-10-12 11:27:59 +0000989 if (PyString_Check(x)) {
990 /* Since PyInt_FromString doesn't have a length parameter,
991 * check here for possible NULs in the string. */
992 char *string = PyString_AS_STRING(x);
993 if (strlen(string) != PyString_Size(x)) {
994 /* create a repr() of the input string,
995 * just like PyInt_FromString does */
996 PyObject *srepr;
997 srepr = PyObject_Repr(x);
998 if (srepr == NULL)
999 return NULL;
1000 PyErr_Format(PyExc_ValueError,
1001 "invalid literal for int() with base %d: %s",
1002 base, PyString_AS_STRING(srepr));
1003 Py_DECREF(srepr);
1004 return NULL;
1005 }
1006 return PyInt_FromString(string, NULL, base);
1007 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001008#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00001009 if (PyUnicode_Check(x))
1010 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
1011 PyUnicode_GET_SIZE(x),
1012 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001013#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014 PyErr_SetString(PyExc_TypeError,
1015 "int() can't convert non-string with explicit base");
1016 return NULL;
1017}
1018
Guido van Rossumbef14172001-08-29 15:47:46 +00001019/* Wimpy, slow approach to tp_new calls for subtypes of int:
1020 first create a regular int from whatever arguments we got,
1021 then allocate a subtype instance and initialize its ob_ival
1022 from the regular int. The regular int is then thrown away.
1023*/
1024static PyObject *
1025int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1026{
Anthony Baxter377be112006-04-11 06:54:30 +00001027 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001028 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001029
1030 assert(PyType_IsSubtype(type, &PyInt_Type));
1031 tmp = int_new(&PyInt_Type, args, kwds);
1032 if (tmp == NULL)
1033 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001034 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001035 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001036 if (ival == -1 && PyErr_Occurred()) {
1037 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001038 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001039 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001040 } else {
1041 ival = ((PyIntObject *)tmp)->ob_ival;
1042 }
1043
Anthony Baxter377be112006-04-11 06:54:30 +00001044 newobj = type->tp_alloc(type, 0);
1045 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001046 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001047 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001048 }
Anthony Baxter377be112006-04-11 06:54:30 +00001049 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001050 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00001051 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001052}
1053
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001054static PyObject *
1055int_getnewargs(PyIntObject *v)
1056{
1057 return Py_BuildValue("(l)", v->ob_ival);
1058}
1059
1060static PyMethodDef int_methods[] = {
1061 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
1062 {NULL, NULL} /* sentinel */
1063};
1064
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001065PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001066"int(x[, base]) -> integer\n\
1067\n\
1068Convert a string or number to an integer, if possible. A floating point\n\
1069argument will be truncated towards zero (this does not include a string\n\
1070representation of a floating point number!) When converting a string, use\n\
1071the optional base. It is an error to supply a base when converting a\n\
Gustavo Niemeyer37e65022007-01-10 16:15:48 +00001072non-string. If base is zero, the proper base is guessed based on the\n\
Gustavo Niemeyera443bc82007-01-10 16:13:40 +00001073string content. If the argument is outside the integer range a\n\
1074long object will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001075
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001076static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001077 (binaryfunc)int_add, /*nb_add*/
1078 (binaryfunc)int_sub, /*nb_subtract*/
1079 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00001080 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001081 (binaryfunc)int_mod, /*nb_remainder*/
1082 (binaryfunc)int_divmod, /*nb_divmod*/
1083 (ternaryfunc)int_pow, /*nb_power*/
1084 (unaryfunc)int_neg, /*nb_negative*/
1085 (unaryfunc)int_pos, /*nb_positive*/
1086 (unaryfunc)int_abs, /*nb_absolute*/
1087 (inquiry)int_nonzero, /*nb_nonzero*/
1088 (unaryfunc)int_invert, /*nb_invert*/
1089 (binaryfunc)int_lshift, /*nb_lshift*/
1090 (binaryfunc)int_rshift, /*nb_rshift*/
1091 (binaryfunc)int_and, /*nb_and*/
1092 (binaryfunc)int_xor, /*nb_xor*/
1093 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +00001094 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001095 (unaryfunc)int_int, /*nb_int*/
1096 (unaryfunc)int_long, /*nb_long*/
1097 (unaryfunc)int_float, /*nb_float*/
1098 (unaryfunc)int_oct, /*nb_oct*/
1099 (unaryfunc)int_hex, /*nb_hex*/
1100 0, /*nb_inplace_add*/
1101 0, /*nb_inplace_subtract*/
1102 0, /*nb_inplace_multiply*/
1103 0, /*nb_inplace_divide*/
1104 0, /*nb_inplace_remainder*/
1105 0, /*nb_inplace_power*/
1106 0, /*nb_inplace_lshift*/
1107 0, /*nb_inplace_rshift*/
1108 0, /*nb_inplace_and*/
1109 0, /*nb_inplace_xor*/
1110 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001111 (binaryfunc)int_div, /* nb_floor_divide */
1112 int_true_divide, /* nb_true_divide */
1113 0, /* nb_inplace_floor_divide */
1114 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001115 (unaryfunc)int_int, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001116};
1117
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001118PyTypeObject PyInt_Type = {
1119 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001120 0,
1121 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001122 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001123 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001124 (destructor)int_dealloc, /* tp_dealloc */
1125 (printfunc)int_print, /* tp_print */
1126 0, /* tp_getattr */
1127 0, /* tp_setattr */
1128 (cmpfunc)int_compare, /* tp_compare */
1129 (reprfunc)int_repr, /* tp_repr */
1130 &int_as_number, /* tp_as_number */
1131 0, /* tp_as_sequence */
1132 0, /* tp_as_mapping */
1133 (hashfunc)int_hash, /* tp_hash */
1134 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001135 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001136 PyObject_GenericGetAttr, /* tp_getattro */
1137 0, /* tp_setattro */
1138 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001139 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00001140 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001141 int_doc, /* tp_doc */
1142 0, /* tp_traverse */
1143 0, /* tp_clear */
1144 0, /* tp_richcompare */
1145 0, /* tp_weaklistoffset */
1146 0, /* tp_iter */
1147 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001148 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001149 0, /* tp_members */
1150 0, /* tp_getset */
1151 0, /* tp_base */
1152 0, /* tp_dict */
1153 0, /* tp_descr_get */
1154 0, /* tp_descr_set */
1155 0, /* tp_dictoffset */
1156 0, /* tp_init */
1157 0, /* tp_alloc */
1158 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001159 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001160};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001161
Neal Norwitzc91ed402002-12-30 22:29:22 +00001162int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001163_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001164{
1165 PyIntObject *v;
1166 int ival;
1167#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1168 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001169 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001170 return 0;
1171 /* PyObject_New is inlined */
1172 v = free_list;
1173 free_list = (PyIntObject *)v->ob_type;
1174 PyObject_INIT(v, &PyInt_Type);
1175 v->ob_ival = ival;
1176 small_ints[ival + NSMALLNEGINTS] = v;
1177 }
1178#endif
1179 return 1;
1180}
1181
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001182void
Fred Drakea2f55112000-07-09 15:16:51 +00001183PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001184{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001185 PyIntObject *p;
1186 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001187 int i;
Skip Montanaro429433b2006-04-18 00:35:43 +00001188 unsigned int ctr;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001189 int bc, bf; /* block count, number of freed blocks */
1190 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001191
Guido van Rossumda084ed1999-03-10 22:55:24 +00001192#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1193 PyIntObject **q;
1194
1195 i = NSMALLNEGINTS + NSMALLPOSINTS;
1196 q = small_ints;
1197 while (--i >= 0) {
1198 Py_XDECREF(*q);
1199 *q++ = NULL;
1200 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001201#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001202 bc = 0;
1203 bf = 0;
1204 isum = 0;
1205 list = block_list;
1206 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001207 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001208 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001209 bc++;
1210 irem = 0;
Skip Montanaro429433b2006-04-18 00:35:43 +00001211 for (ctr = 0, p = &list->objects[0];
1212 ctr < N_INTOBJECTS;
1213 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001214 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001215 irem++;
1216 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001217 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001218 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001219 list->next = block_list;
1220 block_list = list;
Skip Montanaro429433b2006-04-18 00:35:43 +00001221 for (ctr = 0, p = &list->objects[0];
1222 ctr < N_INTOBJECTS;
1223 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001224 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001225 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001226 p->ob_type = (struct _typeobject *)
1227 free_list;
1228 free_list = p;
1229 }
1230#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1231 else if (-NSMALLNEGINTS <= p->ob_ival &&
1232 p->ob_ival < NSMALLPOSINTS &&
1233 small_ints[p->ob_ival +
1234 NSMALLNEGINTS] == NULL) {
1235 Py_INCREF(p);
1236 small_ints[p->ob_ival +
1237 NSMALLNEGINTS] = p;
1238 }
1239#endif
1240 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001241 }
1242 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001243 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001244 bf++;
1245 }
1246 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001247 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001248 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001249 if (!Py_VerboseFlag)
1250 return;
1251 fprintf(stderr, "# cleanup ints");
1252 if (!isum) {
1253 fprintf(stderr, "\n");
1254 }
1255 else {
1256 fprintf(stderr,
1257 ": %d unfreed int%s in %d out of %d block%s\n",
1258 isum, isum == 1 ? "" : "s",
1259 bc - bf, bc, bc == 1 ? "" : "s");
1260 }
1261 if (Py_VerboseFlag > 1) {
1262 list = block_list;
1263 while (list != NULL) {
Skip Montanaro429433b2006-04-18 00:35:43 +00001264 for (ctr = 0, p = &list->objects[0];
1265 ctr < N_INTOBJECTS;
1266 ctr++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001267 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001268 /* XXX(twouters) cast refcount to
1269 long until %zd is universally
1270 available
1271 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001272 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001273 "# <int at %p, refcnt=%ld, val=%ld>\n",
1274 p, (long)p->ob_refcnt,
1275 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001276 }
1277 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001278 }
1279 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001280}