blob: 411e4dd17467325537448663e8d4a2f9a69832e7 [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 Rossumbf8c0e31994-08-29 12:48:32 +00007#ifdef HAVE_LIMITS_H
Guido van Rossum72481a31993-10-26 15:21:51 +00008#include <limits.h>
9#endif
10
11#ifndef LONG_MAX
12#define LONG_MAX 0X7FFFFFFFL
13#endif
14
15#ifndef LONG_MIN
16#define LONG_MIN (-LONG_MAX-1)
17#endif
18
19#ifndef CHAR_BIT
20#define CHAR_BIT 8
21#endif
22
Guido van Rossumb376a4a1993-11-23 17:53:17 +000023#ifndef LONG_BIT
Guido van Rossum72481a31993-10-26 15:21:51 +000024#define LONG_BIT (CHAR_BIT * sizeof(long))
Guido van Rossumb376a4a1993-11-23 17:53:17 +000025#endif
Guido van Rossum72481a31993-10-26 15:21:51 +000026
Guido van Rossum2e1d4331993-12-24 10:22:45 +000027long
Fred Drakea2f55112000-07-09 15:16:51 +000028PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +000029{
30 return LONG_MAX; /* To initialize sys.maxint */
31}
32
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033/* Standard Booleans */
Guido van Rossum3f5da241990-12-20 15:06:42 +000034
Guido van Rossumc0b618a1997-05-02 03:12:38 +000035PyIntObject _Py_ZeroStruct = {
36 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037 0
38};
Guido van Rossum3f5da241990-12-20 15:06:42 +000039
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040PyIntObject _Py_TrueStruct = {
41 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042 1
43};
44
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000046err_ovf(char *msg)
Guido van Rossum165e67e1990-10-14 20:02:26 +000047{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossum165e67e1990-10-14 20:02:26 +000049 return NULL;
50}
51
Guido van Rossum3f5da241990-12-20 15:06:42 +000052/* Integers are quite normal objects, to make object handling uniform.
53 (Using odd pointers to represent integers would save much space
54 but require extra checks for this special case throughout the code.)
55 Since, a typical Python program spends much of its time allocating
56 and deallocating integers, these operations should be very fast.
57 Therefore we use a dedicated allocation scheme with a much lower
58 overhead (in space and time) than straight malloc(): a simple
59 dedicated free list, filled when necessary with memory from malloc().
60*/
61
62#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000063#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
64#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000065
Guido van Rossum3fce8831999-03-12 19:43:17 +000066struct _intblock {
67 struct _intblock *next;
68 PyIntObject objects[N_INTOBJECTS];
69};
70
71typedef struct _intblock PyIntBlock;
72
73static PyIntBlock *block_list = NULL;
74static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000075
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000077fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000078{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000079 PyIntObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000080 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
81 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000082 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000083 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000084 ((PyIntBlock *)p)->next = block_list;
85 block_list = (PyIntBlock *)p;
86 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000087 q = p + N_INTOBJECTS;
88 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000089 q->ob_type = (struct _typeobject *)(q-1);
90 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000091 return p + N_INTOBJECTS - 1;
92}
93
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000094#ifndef NSMALLPOSINTS
95#define NSMALLPOSINTS 100
96#endif
97#ifndef NSMALLNEGINTS
98#define NSMALLNEGINTS 1
99#endif
100#if NSMALLNEGINTS + NSMALLPOSINTS > 0
101/* References to small integers are saved in this array so that they
102 can be shared.
103 The integers that are saved are those in the range
104 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
105*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000106static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000107#endif
108#ifdef COUNT_ALLOCS
109int quick_int_allocs, quick_neg_int_allocs;
110#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000113PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000116#if NSMALLNEGINTS + NSMALLPOSINTS > 0
117 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
118 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000119 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000120#ifdef COUNT_ALLOCS
121 if (ival >= 0)
122 quick_int_allocs++;
123 else
124 quick_neg_int_allocs++;
125#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000126 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000127 }
128#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129 if (free_list == NULL) {
130 if ((free_list = fill_free_list()) == NULL)
131 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000133 /* PyObject_New is inlined */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000134 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000135 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000136 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000137 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000138#if NSMALLNEGINTS + NSMALLPOSINTS > 0
139 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
140 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000141 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000142 small_ints[ival + NSMALLNEGINTS] = v;
143 }
144#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000146}
147
148static void
Fred Drakea2f55112000-07-09 15:16:51 +0000149int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000150{
Guido van Rossumda084ed1999-03-10 22:55:24 +0000151 v->ob_type = (struct _typeobject *)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000152 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153}
154
155long
Fred Drakea2f55112000-07-09 15:16:51 +0000156PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158 PyNumberMethods *nb;
159 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000160 long val;
161
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 if (op && PyInt_Check(op))
163 return PyInt_AS_LONG((PyIntObject*) op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000164
165 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
166 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000167 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168 return -1;
169 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000170
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000171 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000172 if (io == NULL)
173 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 if (!PyInt_Check(io)) {
175 PyErr_SetString(PyExc_TypeError,
176 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000177 return -1;
178 }
179
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000180 val = PyInt_AS_LONG(io);
181 Py_DECREF(io);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000182
183 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184}
185
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000186PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000187PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000188{
189 char *end;
190 long x;
191 char buffer[256]; /* For errors */
192
193 if ((base != 0 && base < 2) || base > 36) {
194 PyErr_SetString(PyExc_ValueError, "invalid base for int()");
195 return NULL;
196 }
197
198 while (*s && isspace(Py_CHARMASK(*s)))
199 s++;
200 errno = 0;
201 if (base == 0 && s[0] == '0')
202 x = (long) PyOS_strtoul(s, &end, base);
203 else
204 x = PyOS_strtol(s, &end, base);
205 if (end == s || !isalnum(end[-1]))
206 goto bad;
207 while (*end && isspace(Py_CHARMASK(*end)))
208 end++;
209 if (*end != '\0') {
210 bad:
211 sprintf(buffer, "invalid literal for int(): %.200s", s);
212 PyErr_SetString(PyExc_ValueError, buffer);
213 return NULL;
214 }
215 else if (errno != 0) {
216 sprintf(buffer, "int() literal too large: %.200s", s);
217 PyErr_SetString(PyExc_ValueError, buffer);
218 return NULL;
219 }
220 if (pend)
221 *pend = end;
222 return PyInt_FromLong(x);
223}
224
Guido van Rossum9e896b32000-04-05 20:11:21 +0000225PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000226PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000227{
228 char buffer[256];
229
230 if (length >= sizeof(buffer)) {
231 PyErr_SetString(PyExc_ValueError,
232 "int() literal too large to convert");
233 return NULL;
234 }
235 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
236 return NULL;
237 return PyInt_FromString(buffer, NULL, base);
238}
239
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240/* Methods */
241
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000242/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000243static int
Fred Drakea2f55112000-07-09 15:16:51 +0000244int_print(PyIntObject *v, FILE *fp, int flags)
245 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
247 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000248 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249}
250
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000252int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253{
254 char buf[20];
255 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257}
258
259static int
Fred Drakea2f55112000-07-09 15:16:51 +0000260int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261{
262 register long i = v->ob_ival;
263 register long j = w->ob_ival;
264 return (i < j) ? -1 : (i > j) ? 1 : 0;
265}
266
Guido van Rossum9bfef441993-03-29 10:43:31 +0000267static long
Fred Drakea2f55112000-07-09 15:16:51 +0000268int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000269{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000270 /* XXX If this is changed, you also need to change the way
271 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000272 long x = v -> ob_ival;
273 if (x == -1)
274 x = -2;
275 return x;
276}
277
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000279int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280{
281 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000283 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000285 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000286 return err_ovf("integer addition");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288}
289
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000291int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292{
293 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000295 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000297 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000298 return err_ovf("integer subtraction");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000299 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300}
301
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000302/*
303Integer overflow checking used to be done using a double, but on 64
304bit machines (where both long and double are 64 bit) this fails
Thomas Wouters7e474022000-07-16 12:04:32 +0000305because the double doesn't have enough precision. John Tromp suggests
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000306the following algorithm:
307
308Suppose again we normalize a and b to be nonnegative.
309Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
310Now we test ah and bh against zero and get essentially 3 possible outcomes.
311
3121) both ah and bh > 0 : then report overflow
313
3142) both ah and bh = 0 : then compute a*b and report overflow if it comes out
315 negative
316
3173) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
318 compute al*bl and report overflow if it's negative
319 add (ah*bl)<<32 to al*bl and report overflow if
320 it's negative
321
322In case of no overflow the result is then negated if necessary.
323
324The majority of cases will be 2), in which case this method is the same as
325what I suggested before. If multiplication is expensive enough, then the
326other method is faster on case 3), but also more work to program, so I
327guess the above is the preferred solution.
328
329*/
330
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000332int_mul(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000333{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000334 long a, b, ah, bh, x, y;
335 int s = 1;
336
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000338 b = w->ob_ival;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000339 ah = a >> (LONG_BIT/2);
340 bh = b >> (LONG_BIT/2);
341
342 /* Quick test for common case: two small positive ints */
343
344 if (ah == 0 && bh == 0) {
345 x = a*b;
346 if (x < 0)
347 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000348 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000349 }
350
351 /* Arrange that a >= b >= 0 */
352
353 if (a < 0) {
354 a = -a;
355 if (a < 0) {
356 /* Largest negative */
357 if (b == 0 || b == 1) {
358 x = a*b;
359 goto ok;
360 }
361 else
362 goto bad;
363 }
364 s = -s;
365 ah = a >> (LONG_BIT/2);
366 }
367 if (b < 0) {
368 b = -b;
369 if (b < 0) {
370 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000371 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000372 x = a*b;
373 goto ok;
374 }
375 else
376 goto bad;
377 }
378 s = -s;
379 bh = b >> (LONG_BIT/2);
380 }
381
382 /* 1) both ah and bh > 0 : then report overflow */
383
384 if (ah != 0 && bh != 0)
385 goto bad;
386
387 /* 2) both ah and bh = 0 : then compute a*b and report
388 overflow if it comes out negative */
389
390 if (ah == 0 && bh == 0) {
391 x = a*b;
392 if (x < 0)
393 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000394 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000395 }
396
397 if (a < b) {
398 /* Swap */
399 x = a;
400 a = b;
401 b = x;
402 ah = bh;
403 /* bh not used beyond this point */
404 }
405
406 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
407 it's >= 2^31
408 compute al*bl and report overflow if it's negative
409 add (ah*bl)<<32 to al*bl and report overflow if
410 it's negative
411 (NB b == bl in this case, and we make a = al) */
412
413 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000414 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000415 goto bad;
416 a &= (1L << (LONG_BIT/2)) - 1;
417 x = a*b;
418 if (x < 0)
419 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000420 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000421 if (x < 0)
422 goto bad;
423 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000425
426 bad:
427 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000428}
429
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000430static int
Fred Drakea2f55112000-07-09 15:16:51 +0000431i_divmod(register PyIntObject *x, register PyIntObject *y,
432 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000433{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000434 long xi = x->ob_ival;
435 long yi = y->ob_ival;
436 long xdivy, xmody;
437
438 if (yi == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000439 PyErr_SetString(PyExc_ZeroDivisionError,
440 "integer division or modulo");
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000441 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442 }
Guido van Rossum00466951991-05-05 20:08:27 +0000443 if (yi < 0) {
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000444 if (xi < 0) {
445 if (yi == -1 && -xi < 0) {
446 /* most negative / -1 */
447 err_ovf("integer division");
448 return -1;
449 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000450 xdivy = -xi / -yi;
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000451 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000452 else
453 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000454 }
455 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000456 if (xi < 0)
457 xdivy = - (-xi / yi);
458 else
459 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000460 }
461 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000462 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000463 xmody += yi;
464 xdivy -= 1;
465 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000466 *p_xdivy = xdivy;
467 *p_xmody = xmody;
468 return 0;
469}
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000472int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000473{
474 long d, m;
475 if (i_divmod(x, y, &d, &m) < 0)
476 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 return PyInt_FromLong(d);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000478}
479
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000481int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000482{
483 long d, m;
484 if (i_divmod(x, y, &d, &m) < 0)
485 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486 return PyInt_FromLong(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000487}
488
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000489static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000490int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000491{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000492 long d, m;
493 if (i_divmod(x, y, &d, &m) < 0)
494 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495 return Py_BuildValue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000496}
497
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000498static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000499int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000500{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000501#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000502 register long iv, iw, iz=0, ix, temp, prev;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000503 iv = v->ob_ival;
504 iw = w->ob_ival;
505 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506 PyErr_SetString(PyExc_ValueError,
507 "integer to the negative power");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000508 return NULL;
509 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 if ((PyObject *)z != Py_None) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000511 iz = z->ob_ival;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000512 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 PyErr_SetString(PyExc_ValueError,
514 "pow(x, y, z) with z==0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000515 return NULL;
516 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000517 }
518 /*
519 * XXX: The original exponentiation code stopped looping
520 * when temp hit zero; this code will continue onwards
521 * unnecessarily, but at least it won't cause any errors.
522 * Hopefully the speed improvement from the fast exponentiation
523 * will compensate for the slight inefficiency.
524 * XXX: Better handling of overflows is desperately needed.
525 */
526 temp = iv;
527 ix = 1;
528 while (iw > 0) {
529 prev = ix; /* Save value for overflow check */
530 if (iw & 1) {
531 ix = ix*temp;
532 if (temp == 0)
533 break; /* Avoid ix / 0 */
534 if (ix / temp != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000535 return err_ovf("integer exponentiation");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000536 }
537 iw >>= 1; /* Shift exponent down by 1 bit */
538 if (iw==0) break;
539 prev = temp;
540 temp *= temp; /* Square the value of temp */
541 if (prev!=0 && temp/prev!=prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000542 return err_ovf("integer exponentiation");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000543 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000544 /* If we did a multiplication, perform a modulo */
545 ix = ix % iz;
546 temp = temp % iz;
547 }
548 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000549 if (iz) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550 PyObject *t1, *t2;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000551 long int div, mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 t1=PyInt_FromLong(ix);
553 t2=PyInt_FromLong(iz);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000554 if (t1==NULL || t2==NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000555 i_divmod((PyIntObject *)t1,
556 (PyIntObject *)t2, &div, &mod)<0)
557 {
558 Py_XDECREF(t1);
559 Py_XDECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000560 return(NULL);
561 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562 Py_DECREF(t1);
563 Py_DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000564 ix=mod;
565 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000567#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000569 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000570 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000571 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 PyErr_SetString(PyExc_ValueError,
573 "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000574 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000576 if ((PyObject *)z != Py_None) {
577 PyErr_SetString(PyExc_TypeError,
578 "pow(int, int, int) not yet supported");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000579 return NULL;
580 }
Guido van Rossum00466951991-05-05 20:08:27 +0000581 ix = 1;
582 while (--iw >= 0) {
583 long prev = ix;
584 ix = ix * iv;
585 if (iv == 0)
586 break; /* 0 to some power -- avoid ix / 0 */
587 if (ix / iv != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000588 return err_ovf("integer exponentiation");
Guido van Rossum00466951991-05-05 20:08:27 +0000589 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000591#endif
592}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000595int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000596{
597 register long a, x;
598 a = v->ob_ival;
599 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000600 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000601 return err_ovf("integer negation");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000603}
604
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000606int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000608 Py_INCREF(v);
609 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610}
611
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000613int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000614{
615 if (v->ob_ival >= 0)
616 return int_pos(v);
617 else
618 return int_neg(v);
619}
620
Guido van Rossum0bff0151991-05-14 12:05:32 +0000621static int
Fred Drakea2f55112000-07-09 15:16:51 +0000622int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000623{
624 return v->ob_ival != 0;
625}
626
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000628int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000629{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000631}
632
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000634int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000635{
636 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000637 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000638 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000639 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000641 return NULL;
642 }
643 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644 Py_INCREF(v);
645 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000646 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000647 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000649 }
650 a = (unsigned long)a << b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000651 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000652}
653
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000655int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000656{
657 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000658 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000659 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000660 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000662 return NULL;
663 }
664 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 Py_INCREF(v);
666 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000667 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000668 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000669 if (a < 0)
670 a = -1;
671 else
672 a = 0;
673 }
674 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000675 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000676 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000678}
679
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000680static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000681int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000682{
683 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000684 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000685 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000686 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000687}
688
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000689static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000690int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000691{
692 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000693 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000694 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000696}
697
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000699int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000700{
701 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000702 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000703 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000704 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000705}
706
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000708int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000709{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710 Py_INCREF(v);
711 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000712}
713
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000715int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000716{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000717 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000718}
719
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000720static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000721int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000722{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000724}
725
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000727int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000728{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000729 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000730 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000731 if (x == 0)
732 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000733 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000734 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000736}
737
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000739int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000740{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000741 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000742 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000743 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000745}
746
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747static PyNumberMethods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000748 (binaryfunc)int_add, /*nb_add*/
749 (binaryfunc)int_sub, /*nb_subtract*/
750 (binaryfunc)int_mul, /*nb_multiply*/
751 (binaryfunc)int_div, /*nb_divide*/
752 (binaryfunc)int_mod, /*nb_remainder*/
753 (binaryfunc)int_divmod, /*nb_divmod*/
754 (ternaryfunc)int_pow, /*nb_power*/
755 (unaryfunc)int_neg, /*nb_negative*/
756 (unaryfunc)int_pos, /*nb_positive*/
757 (unaryfunc)int_abs, /*nb_absolute*/
758 (inquiry)int_nonzero, /*nb_nonzero*/
759 (unaryfunc)int_invert, /*nb_invert*/
760 (binaryfunc)int_lshift, /*nb_lshift*/
761 (binaryfunc)int_rshift, /*nb_rshift*/
762 (binaryfunc)int_and, /*nb_and*/
763 (binaryfunc)int_xor, /*nb_xor*/
764 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000765 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000766 (unaryfunc)int_int, /*nb_int*/
767 (unaryfunc)int_long, /*nb_long*/
768 (unaryfunc)int_float, /*nb_float*/
769 (unaryfunc)int_oct, /*nb_oct*/
770 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771};
772
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773PyTypeObject PyInt_Type = {
774 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000775 0,
776 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000777 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000779 (destructor)int_dealloc, /*tp_dealloc*/
780 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000781 0, /*tp_getattr*/
782 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000783 (cmpfunc)int_compare, /*tp_compare*/
784 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000785 &int_as_number, /*tp_as_number*/
786 0, /*tp_as_sequence*/
787 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000788 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000789};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000790
791void
Fred Drakea2f55112000-07-09 15:16:51 +0000792PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000793{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000794 PyIntObject *p;
795 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000796 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000797 int bc, bf; /* block count, number of freed blocks */
798 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000799
Guido van Rossumda084ed1999-03-10 22:55:24 +0000800#if NSMALLNEGINTS + NSMALLPOSINTS > 0
801 PyIntObject **q;
802
803 i = NSMALLNEGINTS + NSMALLPOSINTS;
804 q = small_ints;
805 while (--i >= 0) {
806 Py_XDECREF(*q);
807 *q++ = NULL;
808 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000809#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000810 bc = 0;
811 bf = 0;
812 isum = 0;
813 list = block_list;
814 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000815 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000816 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000817 bc++;
818 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000819 for (i = 0, p = &list->objects[0];
820 i < N_INTOBJECTS;
821 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000822 if (PyInt_Check(p) && p->ob_refcnt != 0)
823 irem++;
824 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000825 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000826 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000827 list->next = block_list;
828 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000829 for (i = 0, p = &list->objects[0];
830 i < N_INTOBJECTS;
831 i++, p++) {
832 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
833 p->ob_type = (struct _typeobject *)
834 free_list;
835 free_list = p;
836 }
837#if NSMALLNEGINTS + NSMALLPOSINTS > 0
838 else if (-NSMALLNEGINTS <= p->ob_ival &&
839 p->ob_ival < NSMALLPOSINTS &&
840 small_ints[p->ob_ival +
841 NSMALLNEGINTS] == NULL) {
842 Py_INCREF(p);
843 small_ints[p->ob_ival +
844 NSMALLNEGINTS] = p;
845 }
846#endif
847 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000848 }
849 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000850 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000851 bf++;
852 }
853 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000854 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000855 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000856 if (!Py_VerboseFlag)
857 return;
858 fprintf(stderr, "# cleanup ints");
859 if (!isum) {
860 fprintf(stderr, "\n");
861 }
862 else {
863 fprintf(stderr,
864 ": %d unfreed int%s in %d out of %d block%s\n",
865 isum, isum == 1 ? "" : "s",
866 bc - bf, bc, bc == 1 ? "" : "s");
867 }
868 if (Py_VerboseFlag > 1) {
869 list = block_list;
870 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000871 for (i = 0, p = &list->objects[0];
872 i < N_INTOBJECTS;
873 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000874 if (PyInt_Check(p) && p->ob_refcnt != 0)
875 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000876 "# <int at %p, refcnt=%d, val=%ld>\n",
877 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000878 }
879 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000880 }
881 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000882}