blob: 8477a02381792ce85b6e5515684bfc2297c89549 [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 Rossum72481a31993-10-26 15:21:51 +00007#ifndef CHAR_BIT
8#define CHAR_BIT 8
9#endif
10
Guido van Rossumb376a4a1993-11-23 17:53:17 +000011#ifndef LONG_BIT
Guido van Rossum72481a31993-10-26 15:21:51 +000012#define LONG_BIT (CHAR_BIT * sizeof(long))
Guido van Rossumb376a4a1993-11-23 17:53:17 +000013#endif
Guido van Rossum72481a31993-10-26 15:21:51 +000014
Guido van Rossum2e1d4331993-12-24 10:22:45 +000015long
Fred Drakea2f55112000-07-09 15:16:51 +000016PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +000017{
18 return LONG_MAX; /* To initialize sys.maxint */
19}
20
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021/* Standard Booleans */
Guido van Rossum3f5da241990-12-20 15:06:42 +000022
Guido van Rossumc0b618a1997-05-02 03:12:38 +000023PyIntObject _Py_ZeroStruct = {
24 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025 0
26};
Guido van Rossum3f5da241990-12-20 15:06:42 +000027
Guido van Rossumc0b618a1997-05-02 03:12:38 +000028PyIntObject _Py_TrueStruct = {
29 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030 1
31};
32
Guido van Rossumc0b618a1997-05-02 03:12:38 +000033static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000034err_ovf(char *msg)
Guido van Rossum165e67e1990-10-14 20:02:26 +000035{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000036 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossum165e67e1990-10-14 20:02:26 +000037 return NULL;
38}
39
Guido van Rossum3f5da241990-12-20 15:06:42 +000040/* Integers are quite normal objects, to make object handling uniform.
41 (Using odd pointers to represent integers would save much space
42 but require extra checks for this special case throughout the code.)
43 Since, a typical Python program spends much of its time allocating
44 and deallocating integers, these operations should be very fast.
45 Therefore we use a dedicated allocation scheme with a much lower
46 overhead (in space and time) than straight malloc(): a simple
47 dedicated free list, filled when necessary with memory from malloc().
48*/
49
50#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000051#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
52#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000053
Guido van Rossum3fce8831999-03-12 19:43:17 +000054struct _intblock {
55 struct _intblock *next;
56 PyIntObject objects[N_INTOBJECTS];
57};
58
59typedef struct _intblock PyIntBlock;
60
61static PyIntBlock *block_list = NULL;
62static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000063
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000065fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000066{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067 PyIntObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000068 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
69 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000070 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000071 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000072 ((PyIntBlock *)p)->next = block_list;
73 block_list = (PyIntBlock *)p;
74 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000075 q = p + N_INTOBJECTS;
76 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000077 q->ob_type = (struct _typeobject *)(q-1);
78 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000079 return p + N_INTOBJECTS - 1;
80}
81
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000082#ifndef NSMALLPOSINTS
83#define NSMALLPOSINTS 100
84#endif
85#ifndef NSMALLNEGINTS
86#define NSMALLNEGINTS 1
87#endif
88#if NSMALLNEGINTS + NSMALLPOSINTS > 0
89/* References to small integers are saved in this array so that they
90 can be shared.
91 The integers that are saved are those in the range
92 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
93*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000094static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000095#endif
96#ifdef COUNT_ALLOCS
97int quick_int_allocs, quick_neg_int_allocs;
98#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000099
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000101PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000103 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000104#if NSMALLNEGINTS + NSMALLPOSINTS > 0
105 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
106 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000107 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000108#ifdef COUNT_ALLOCS
109 if (ival >= 0)
110 quick_int_allocs++;
111 else
112 quick_neg_int_allocs++;
113#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000115 }
116#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 if (free_list == NULL) {
118 if ((free_list = fill_free_list()) == NULL)
119 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000121 /* PyObject_New is inlined */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000122 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000123 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000124 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000125 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000126#if NSMALLNEGINTS + NSMALLPOSINTS > 0
127 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
128 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000129 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000130 small_ints[ival + NSMALLNEGINTS] = v;
131 }
132#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000133 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000134}
135
136static void
Fred Drakea2f55112000-07-09 15:16:51 +0000137int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000138{
Guido van Rossumda084ed1999-03-10 22:55:24 +0000139 v->ob_type = (struct _typeobject *)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000140 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141}
142
143long
Fred Drakea2f55112000-07-09 15:16:51 +0000144PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000146 PyNumberMethods *nb;
147 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000148 long val;
149
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 if (op && PyInt_Check(op))
151 return PyInt_AS_LONG((PyIntObject*) op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000152
153 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
154 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000155 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156 return -1;
157 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000158
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000160 if (io == NULL)
161 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000162 if (!PyInt_Check(io)) {
163 PyErr_SetString(PyExc_TypeError,
164 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000165 return -1;
166 }
167
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 val = PyInt_AS_LONG(io);
169 Py_DECREF(io);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000170
171 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172}
173
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000174PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000175PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000176{
177 char *end;
178 long x;
179 char buffer[256]; /* For errors */
180
181 if ((base != 0 && base < 2) || base > 36) {
182 PyErr_SetString(PyExc_ValueError, "invalid base for int()");
183 return NULL;
184 }
185
186 while (*s && isspace(Py_CHARMASK(*s)))
187 s++;
188 errno = 0;
189 if (base == 0 && s[0] == '0')
190 x = (long) PyOS_strtoul(s, &end, base);
191 else
192 x = PyOS_strtol(s, &end, base);
193 if (end == s || !isalnum(end[-1]))
194 goto bad;
195 while (*end && isspace(Py_CHARMASK(*end)))
196 end++;
197 if (*end != '\0') {
198 bad:
199 sprintf(buffer, "invalid literal for int(): %.200s", s);
200 PyErr_SetString(PyExc_ValueError, buffer);
201 return NULL;
202 }
203 else if (errno != 0) {
204 sprintf(buffer, "int() literal too large: %.200s", s);
205 PyErr_SetString(PyExc_ValueError, buffer);
206 return NULL;
207 }
208 if (pend)
209 *pend = end;
210 return PyInt_FromLong(x);
211}
212
Guido van Rossum9e896b32000-04-05 20:11:21 +0000213PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000214PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000215{
216 char buffer[256];
217
218 if (length >= sizeof(buffer)) {
219 PyErr_SetString(PyExc_ValueError,
220 "int() literal too large to convert");
221 return NULL;
222 }
223 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
224 return NULL;
225 return PyInt_FromString(buffer, NULL, base);
226}
227
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228/* Methods */
229
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000230/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000231static int
Fred Drakea2f55112000-07-09 15:16:51 +0000232int_print(PyIntObject *v, FILE *fp, int flags)
233 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234{
235 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000236 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237}
238
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000239static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000240int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241{
242 char buf[20];
243 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000245}
246
247static int
Fred Drakea2f55112000-07-09 15:16:51 +0000248int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249{
250 register long i = v->ob_ival;
251 register long j = w->ob_ival;
252 return (i < j) ? -1 : (i > j) ? 1 : 0;
253}
254
Guido van Rossum9bfef441993-03-29 10:43:31 +0000255static long
Fred Drakea2f55112000-07-09 15:16:51 +0000256int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000257{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000258 /* XXX If this is changed, you also need to change the way
259 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000260 long x = v -> ob_ival;
261 if (x == -1)
262 x = -2;
263 return x;
264}
265
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000266static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000267int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268{
269 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000271 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000273 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000274 return err_ovf("integer addition");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276}
277
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000279int_sub(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 subtraction");
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 Rossumbf8c0e31994-08-29 12:48:32 +0000290/*
291Integer overflow checking used to be done using a double, but on 64
292bit machines (where both long and double are 64 bit) this fails
Thomas Wouters7e474022000-07-16 12:04:32 +0000293because the double doesn't have enough precision. John Tromp suggests
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000294the following algorithm:
295
296Suppose again we normalize a and b to be nonnegative.
297Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
298Now we test ah and bh against zero and get essentially 3 possible outcomes.
299
3001) both ah and bh > 0 : then report overflow
301
3022) both ah and bh = 0 : then compute a*b and report overflow if it comes out
303 negative
304
3053) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
306 compute al*bl and report overflow if it's negative
307 add (ah*bl)<<32 to al*bl and report overflow if
308 it's negative
309
310In case of no overflow the result is then negated if necessary.
311
312The majority of cases will be 2), in which case this method is the same as
313what I suggested before. If multiplication is expensive enough, then the
314other method is faster on case 3), but also more work to program, so I
315guess the above is the preferred solution.
316
317*/
318
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000319static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000320int_mul(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000322 long a, b, ah, bh, x, y;
323 int s = 1;
324
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000326 b = w->ob_ival;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000327 ah = a >> (LONG_BIT/2);
328 bh = b >> (LONG_BIT/2);
329
330 /* Quick test for common case: two small positive ints */
331
332 if (ah == 0 && bh == 0) {
333 x = a*b;
334 if (x < 0)
335 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000336 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000337 }
338
339 /* Arrange that a >= b >= 0 */
340
341 if (a < 0) {
342 a = -a;
343 if (a < 0) {
344 /* Largest negative */
345 if (b == 0 || b == 1) {
346 x = a*b;
347 goto ok;
348 }
349 else
350 goto bad;
351 }
352 s = -s;
353 ah = a >> (LONG_BIT/2);
354 }
355 if (b < 0) {
356 b = -b;
357 if (b < 0) {
358 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000359 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000360 x = a*b;
361 goto ok;
362 }
363 else
364 goto bad;
365 }
366 s = -s;
367 bh = b >> (LONG_BIT/2);
368 }
369
370 /* 1) both ah and bh > 0 : then report overflow */
371
372 if (ah != 0 && bh != 0)
373 goto bad;
374
375 /* 2) both ah and bh = 0 : then compute a*b and report
376 overflow if it comes out negative */
377
378 if (ah == 0 && bh == 0) {
379 x = a*b;
380 if (x < 0)
381 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000382 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000383 }
384
385 if (a < b) {
386 /* Swap */
387 x = a;
388 a = b;
389 b = x;
390 ah = bh;
391 /* bh not used beyond this point */
392 }
393
394 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
395 it's >= 2^31
396 compute al*bl and report overflow if it's negative
397 add (ah*bl)<<32 to al*bl and report overflow if
398 it's negative
399 (NB b == bl in this case, and we make a = al) */
400
401 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000402 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000403 goto bad;
404 a &= (1L << (LONG_BIT/2)) - 1;
405 x = a*b;
406 if (x < 0)
407 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000408 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000409 if (x < 0)
410 goto bad;
411 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000413
414 bad:
415 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416}
417
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000418static int
Fred Drakea2f55112000-07-09 15:16:51 +0000419i_divmod(register PyIntObject *x, register PyIntObject *y,
420 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000421{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000422 long xi = x->ob_ival;
423 long yi = y->ob_ival;
424 long xdivy, xmody;
425
426 if (yi == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000427 PyErr_SetString(PyExc_ZeroDivisionError,
428 "integer division or modulo");
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000429 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430 }
Guido van Rossum00466951991-05-05 20:08:27 +0000431 if (yi < 0) {
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000432 if (xi < 0) {
433 if (yi == -1 && -xi < 0) {
434 /* most negative / -1 */
435 err_ovf("integer division");
436 return -1;
437 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000438 xdivy = -xi / -yi;
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000439 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000440 else
441 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000442 }
443 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000444 if (xi < 0)
445 xdivy = - (-xi / yi);
446 else
447 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000448 }
449 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000450 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000451 xmody += yi;
452 xdivy -= 1;
453 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000454 *p_xdivy = xdivy;
455 *p_xmody = xmody;
456 return 0;
457}
458
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000459static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000460int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000461{
462 long d, m;
463 if (i_divmod(x, y, &d, &m) < 0)
464 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465 return PyInt_FromLong(d);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000466}
467
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000469int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000470{
471 long d, m;
472 if (i_divmod(x, y, &d, &m) < 0)
473 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000474 return PyInt_FromLong(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000475}
476
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000478int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000479{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000480 long d, m;
481 if (i_divmod(x, y, &d, &m) < 0)
482 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483 return Py_BuildValue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000484}
485
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000487int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000489#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000490 register long iv, iw, iz=0, ix, temp, prev;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000491 iv = v->ob_ival;
492 iw = w->ob_ival;
493 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000494 PyErr_SetString(PyExc_ValueError,
495 "integer to the negative power");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000496 return NULL;
497 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000498 if ((PyObject *)z != Py_None) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000499 iz = z->ob_ival;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000500 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501 PyErr_SetString(PyExc_ValueError,
502 "pow(x, y, z) with z==0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000503 return NULL;
504 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000505 }
506 /*
507 * XXX: The original exponentiation code stopped looping
508 * when temp hit zero; this code will continue onwards
509 * unnecessarily, but at least it won't cause any errors.
510 * Hopefully the speed improvement from the fast exponentiation
511 * will compensate for the slight inefficiency.
512 * XXX: Better handling of overflows is desperately needed.
513 */
514 temp = iv;
515 ix = 1;
516 while (iw > 0) {
517 prev = ix; /* Save value for overflow check */
518 if (iw & 1) {
519 ix = ix*temp;
520 if (temp == 0)
521 break; /* Avoid ix / 0 */
522 if (ix / temp != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000523 return err_ovf("integer exponentiation");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000524 }
525 iw >>= 1; /* Shift exponent down by 1 bit */
526 if (iw==0) break;
527 prev = temp;
528 temp *= temp; /* Square the value of temp */
529 if (prev!=0 && temp/prev!=prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000530 return err_ovf("integer exponentiation");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000531 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000532 /* If we did a multiplication, perform a modulo */
533 ix = ix % iz;
534 temp = temp % iz;
535 }
536 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000537 if (iz) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000538 PyObject *t1, *t2;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000539 long int div, mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540 t1=PyInt_FromLong(ix);
541 t2=PyInt_FromLong(iz);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000542 if (t1==NULL || t2==NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 i_divmod((PyIntObject *)t1,
544 (PyIntObject *)t2, &div, &mod)<0)
545 {
546 Py_XDECREF(t1);
547 Py_XDECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000548 return(NULL);
549 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000550 Py_DECREF(t1);
551 Py_DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000552 ix=mod;
553 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000555#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000557 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000558 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000559 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560 PyErr_SetString(PyExc_ValueError,
561 "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000562 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 if ((PyObject *)z != Py_None) {
565 PyErr_SetString(PyExc_TypeError,
566 "pow(int, int, int) not yet supported");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000567 return NULL;
568 }
Guido van Rossum00466951991-05-05 20:08:27 +0000569 ix = 1;
570 while (--iw >= 0) {
571 long prev = ix;
572 ix = ix * iv;
573 if (iv == 0)
574 break; /* 0 to some power -- avoid ix / 0 */
575 if (ix / iv != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000576 return err_ovf("integer exponentiation");
Guido van Rossum00466951991-05-05 20:08:27 +0000577 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000579#endif
580}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000583int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584{
585 register long a, x;
586 a = v->ob_ival;
587 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000588 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000589 return err_ovf("integer negation");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591}
592
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000594int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596 Py_INCREF(v);
597 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000598}
599
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000600static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000601int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000602{
603 if (v->ob_ival >= 0)
604 return int_pos(v);
605 else
606 return int_neg(v);
607}
608
Guido van Rossum0bff0151991-05-14 12:05:32 +0000609static int
Fred Drakea2f55112000-07-09 15:16:51 +0000610int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000611{
612 return v->ob_ival != 0;
613}
614
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000615static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000616int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000617{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000619}
620
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000622int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000623{
624 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000625 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000626 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000627 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000629 return NULL;
630 }
631 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632 Py_INCREF(v);
633 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000634 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000635 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000637 }
638 a = (unsigned long)a << b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000640}
641
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000643int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000644{
645 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000646 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000647 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000648 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000650 return NULL;
651 }
652 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653 Py_INCREF(v);
654 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000655 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000656 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000657 if (a < 0)
658 a = -1;
659 else
660 a = 0;
661 }
662 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000663 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000664 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000666}
667
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000668static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000669int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000670{
671 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000672 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000673 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000675}
676
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000678int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000679{
680 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000681 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000682 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000684}
685
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000686static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000687int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000688{
689 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000690 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000691 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000693}
694
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000696int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000697{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698 Py_INCREF(v);
699 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000700}
701
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000703int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000704{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000705 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000706}
707
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000709int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000710{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711 return PyFloat_FromDouble((double)(v -> ob_ival));
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_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000716{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000717 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000718 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000719 if (x == 0)
720 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000721 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000722 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 return PyString_FromString(buf);
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_hex(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 Rossumebee0251997-01-12 19:48:03 +0000731 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000733}
734
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735static PyNumberMethods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000736 (binaryfunc)int_add, /*nb_add*/
737 (binaryfunc)int_sub, /*nb_subtract*/
738 (binaryfunc)int_mul, /*nb_multiply*/
739 (binaryfunc)int_div, /*nb_divide*/
740 (binaryfunc)int_mod, /*nb_remainder*/
741 (binaryfunc)int_divmod, /*nb_divmod*/
742 (ternaryfunc)int_pow, /*nb_power*/
743 (unaryfunc)int_neg, /*nb_negative*/
744 (unaryfunc)int_pos, /*nb_positive*/
745 (unaryfunc)int_abs, /*nb_absolute*/
746 (inquiry)int_nonzero, /*nb_nonzero*/
747 (unaryfunc)int_invert, /*nb_invert*/
748 (binaryfunc)int_lshift, /*nb_lshift*/
749 (binaryfunc)int_rshift, /*nb_rshift*/
750 (binaryfunc)int_and, /*nb_and*/
751 (binaryfunc)int_xor, /*nb_xor*/
752 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000753 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000754 (unaryfunc)int_int, /*nb_int*/
755 (unaryfunc)int_long, /*nb_long*/
756 (unaryfunc)int_float, /*nb_float*/
757 (unaryfunc)int_oct, /*nb_oct*/
758 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000759};
760
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000761PyTypeObject PyInt_Type = {
762 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000763 0,
764 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000766 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000767 (destructor)int_dealloc, /*tp_dealloc*/
768 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769 0, /*tp_getattr*/
770 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000771 (cmpfunc)int_compare, /*tp_compare*/
772 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000773 &int_as_number, /*tp_as_number*/
774 0, /*tp_as_sequence*/
775 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000776 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000777};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000778
779void
Fred Drakea2f55112000-07-09 15:16:51 +0000780PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000781{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000782 PyIntObject *p;
783 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000784 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000785 int bc, bf; /* block count, number of freed blocks */
786 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000787
Guido van Rossumda084ed1999-03-10 22:55:24 +0000788#if NSMALLNEGINTS + NSMALLPOSINTS > 0
789 PyIntObject **q;
790
791 i = NSMALLNEGINTS + NSMALLPOSINTS;
792 q = small_ints;
793 while (--i >= 0) {
794 Py_XDECREF(*q);
795 *q++ = NULL;
796 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000797#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000798 bc = 0;
799 bf = 0;
800 isum = 0;
801 list = block_list;
802 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000803 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000804 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000805 bc++;
806 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000807 for (i = 0, p = &list->objects[0];
808 i < N_INTOBJECTS;
809 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000810 if (PyInt_Check(p) && p->ob_refcnt != 0)
811 irem++;
812 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000813 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000814 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000815 list->next = block_list;
816 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000817 for (i = 0, p = &list->objects[0];
818 i < N_INTOBJECTS;
819 i++, p++) {
820 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
821 p->ob_type = (struct _typeobject *)
822 free_list;
823 free_list = p;
824 }
825#if NSMALLNEGINTS + NSMALLPOSINTS > 0
826 else if (-NSMALLNEGINTS <= p->ob_ival &&
827 p->ob_ival < NSMALLPOSINTS &&
828 small_ints[p->ob_ival +
829 NSMALLNEGINTS] == NULL) {
830 Py_INCREF(p);
831 small_ints[p->ob_ival +
832 NSMALLNEGINTS] = p;
833 }
834#endif
835 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000836 }
837 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000838 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000839 bf++;
840 }
841 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000842 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000843 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000844 if (!Py_VerboseFlag)
845 return;
846 fprintf(stderr, "# cleanup ints");
847 if (!isum) {
848 fprintf(stderr, "\n");
849 }
850 else {
851 fprintf(stderr,
852 ": %d unfreed int%s in %d out of %d block%s\n",
853 isum, isum == 1 ? "" : "s",
854 bc - bf, bc, bc == 1 ? "" : "s");
855 }
856 if (Py_VerboseFlag > 1) {
857 list = block_list;
858 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000859 for (i = 0, p = &list->objects[0];
860 i < N_INTOBJECTS;
861 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000862 if (PyInt_Check(p) && p->ob_refcnt != 0)
863 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000864 "# <int at %p, refcnt=%d, val=%ld>\n",
865 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000866 }
867 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000868 }
869 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000870}