blob: b88a05de19753ba76e5367ef7bb53f4634bc4938 [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 Rossum85a5fbb1990-10-14 12:07:46 +000013/* Standard Booleans */
Guido van Rossum3f5da241990-12-20 15:06:42 +000014
Guido van Rossumc0b618a1997-05-02 03:12:38 +000015PyIntObject _Py_ZeroStruct = {
16 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017 0
18};
Guido van Rossum3f5da241990-12-20 15:06:42 +000019
Guido van Rossumc0b618a1997-05-02 03:12:38 +000020PyIntObject _Py_TrueStruct = {
21 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022 1
23};
24
Guido van Rossumc0b618a1997-05-02 03:12:38 +000025static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000026err_ovf(char *msg)
Guido van Rossum165e67e1990-10-14 20:02:26 +000027{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000028 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossum165e67e1990-10-14 20:02:26 +000029 return NULL;
30}
31
Guido van Rossum3f5da241990-12-20 15:06:42 +000032/* Integers are quite normal objects, to make object handling uniform.
33 (Using odd pointers to represent integers would save much space
34 but require extra checks for this special case throughout the code.)
35 Since, a typical Python program spends much of its time allocating
36 and deallocating integers, these operations should be very fast.
37 Therefore we use a dedicated allocation scheme with a much lower
38 overhead (in space and time) than straight malloc(): a simple
39 dedicated free list, filled when necessary with memory from malloc().
40*/
41
42#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000043#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
44#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000045
Guido van Rossum3fce8831999-03-12 19:43:17 +000046struct _intblock {
47 struct _intblock *next;
48 PyIntObject objects[N_INTOBJECTS];
49};
50
51typedef struct _intblock PyIntBlock;
52
53static PyIntBlock *block_list = NULL;
54static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000055
Guido van Rossumc0b618a1997-05-02 03:12:38 +000056static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000057fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000058{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000059 PyIntObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000060 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
61 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000062 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000063 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000064 ((PyIntBlock *)p)->next = block_list;
65 block_list = (PyIntBlock *)p;
66 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000067 q = p + N_INTOBJECTS;
68 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000069 q->ob_type = (struct _typeobject *)(q-1);
70 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000071 return p + N_INTOBJECTS - 1;
72}
73
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000074#ifndef NSMALLPOSINTS
75#define NSMALLPOSINTS 100
76#endif
77#ifndef NSMALLNEGINTS
78#define NSMALLNEGINTS 1
79#endif
80#if NSMALLNEGINTS + NSMALLPOSINTS > 0
81/* References to small integers are saved in this array so that they
82 can be shared.
83 The integers that are saved are those in the range
84 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
85*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000087#endif
88#ifdef COUNT_ALLOCS
89int quick_int_allocs, quick_neg_int_allocs;
90#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000091
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000093PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000096#if NSMALLNEGINTS + NSMALLPOSINTS > 0
97 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
98 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000100#ifdef COUNT_ALLOCS
101 if (ival >= 0)
102 quick_int_allocs++;
103 else
104 quick_neg_int_allocs++;
105#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000106 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000107 }
108#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000109 if (free_list == NULL) {
110 if ((free_list = fill_free_list()) == NULL)
111 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000113 /* PyObject_New is inlined */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000114 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000115 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000116 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000118#if NSMALLNEGINTS + NSMALLPOSINTS > 0
119 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
120 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000122 small_ints[ival + NSMALLNEGINTS] = v;
123 }
124#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000126}
127
128static void
Fred Drakea2f55112000-07-09 15:16:51 +0000129int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000130{
Guido van Rossumda084ed1999-03-10 22:55:24 +0000131 v->ob_type = (struct _typeobject *)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
135long
Fred Drakea2f55112000-07-09 15:16:51 +0000136PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000138 PyNumberMethods *nb;
139 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000140 long val;
141
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000142 if (op && PyInt_Check(op))
143 return PyInt_AS_LONG((PyIntObject*) op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000144
145 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
146 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000147 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148 return -1;
149 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000150
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000151 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000152 if (io == NULL)
153 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154 if (!PyInt_Check(io)) {
155 PyErr_SetString(PyExc_TypeError,
156 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000157 return -1;
158 }
159
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160 val = PyInt_AS_LONG(io);
161 Py_DECREF(io);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000162
163 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164}
165
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000166PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000167PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000168{
169 char *end;
170 long x;
171 char buffer[256]; /* For errors */
172
173 if ((base != 0 && base < 2) || base > 36) {
174 PyErr_SetString(PyExc_ValueError, "invalid base for int()");
175 return NULL;
176 }
177
178 while (*s && isspace(Py_CHARMASK(*s)))
179 s++;
180 errno = 0;
181 if (base == 0 && s[0] == '0')
182 x = (long) PyOS_strtoul(s, &end, base);
183 else
184 x = PyOS_strtol(s, &end, base);
185 if (end == s || !isalnum(end[-1]))
186 goto bad;
187 while (*end && isspace(Py_CHARMASK(*end)))
188 end++;
189 if (*end != '\0') {
190 bad:
191 sprintf(buffer, "invalid literal for int(): %.200s", s);
192 PyErr_SetString(PyExc_ValueError, buffer);
193 return NULL;
194 }
195 else if (errno != 0) {
196 sprintf(buffer, "int() literal too large: %.200s", s);
197 PyErr_SetString(PyExc_ValueError, buffer);
198 return NULL;
199 }
200 if (pend)
201 *pend = end;
202 return PyInt_FromLong(x);
203}
204
Guido van Rossum9e896b32000-04-05 20:11:21 +0000205PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000206PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000207{
208 char buffer[256];
209
210 if (length >= sizeof(buffer)) {
211 PyErr_SetString(PyExc_ValueError,
212 "int() literal too large to convert");
213 return NULL;
214 }
215 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
216 return NULL;
217 return PyInt_FromString(buffer, NULL, base);
218}
219
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000220/* Methods */
221
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000222/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000223static int
Fred Drakea2f55112000-07-09 15:16:51 +0000224int_print(PyIntObject *v, FILE *fp, int flags)
225 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226{
227 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000228 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229}
230
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000231static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000232int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000233{
234 char buf[20];
235 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000236 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237}
238
239static int
Fred Drakea2f55112000-07-09 15:16:51 +0000240int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241{
242 register long i = v->ob_ival;
243 register long j = w->ob_ival;
244 return (i < j) ? -1 : (i > j) ? 1 : 0;
245}
246
Guido van Rossum9bfef441993-03-29 10:43:31 +0000247static long
Fred Drakea2f55112000-07-09 15:16:51 +0000248int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000249{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000250 /* XXX If this is changed, you also need to change the way
251 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000252 long x = v -> ob_ival;
253 if (x == -1)
254 x = -2;
255 return x;
256}
257
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000258static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000259int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260{
261 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000263 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000265 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000266 return err_ovf("integer addition");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000267 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268}
269
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000270static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000271int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000272{
273 register long a, b, x;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000275 b = w->ob_ival;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000277 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000278 return err_ovf("integer subtraction");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000279 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280}
281
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000282/*
283Integer overflow checking used to be done using a double, but on 64
284bit machines (where both long and double are 64 bit) this fails
Thomas Wouters7e474022000-07-16 12:04:32 +0000285because the double doesn't have enough precision. John Tromp suggests
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000286the following algorithm:
287
288Suppose again we normalize a and b to be nonnegative.
289Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
290Now we test ah and bh against zero and get essentially 3 possible outcomes.
291
2921) both ah and bh > 0 : then report overflow
293
2942) both ah and bh = 0 : then compute a*b and report overflow if it comes out
295 negative
296
2973) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
298 compute al*bl and report overflow if it's negative
299 add (ah*bl)<<32 to al*bl and report overflow if
300 it's negative
301
302In case of no overflow the result is then negated if necessary.
303
304The majority of cases will be 2), in which case this method is the same as
305what I suggested before. If multiplication is expensive enough, then the
306other method is faster on case 3), but also more work to program, so I
307guess the above is the preferred solution.
308
309*/
310
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000312int_mul(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000314 long a, b, ah, bh, x, y;
315 int s = 1;
316
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000318 b = w->ob_ival;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000319 ah = a >> (LONG_BIT/2);
320 bh = b >> (LONG_BIT/2);
321
322 /* Quick test for common case: two small positive ints */
323
324 if (ah == 0 && bh == 0) {
325 x = a*b;
326 if (x < 0)
327 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000329 }
330
331 /* Arrange that a >= b >= 0 */
332
333 if (a < 0) {
334 a = -a;
335 if (a < 0) {
336 /* Largest negative */
337 if (b == 0 || b == 1) {
338 x = a*b;
339 goto ok;
340 }
341 else
342 goto bad;
343 }
344 s = -s;
345 ah = a >> (LONG_BIT/2);
346 }
347 if (b < 0) {
348 b = -b;
349 if (b < 0) {
350 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000351 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000352 x = a*b;
353 goto ok;
354 }
355 else
356 goto bad;
357 }
358 s = -s;
359 bh = b >> (LONG_BIT/2);
360 }
361
362 /* 1) both ah and bh > 0 : then report overflow */
363
364 if (ah != 0 && bh != 0)
365 goto bad;
366
367 /* 2) both ah and bh = 0 : then compute a*b and report
368 overflow if it comes out negative */
369
370 if (ah == 0 && bh == 0) {
371 x = a*b;
372 if (x < 0)
373 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000374 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000375 }
376
377 if (a < b) {
378 /* Swap */
379 x = a;
380 a = b;
381 b = x;
382 ah = bh;
383 /* bh not used beyond this point */
384 }
385
386 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
387 it's >= 2^31
388 compute al*bl and report overflow if it's negative
389 add (ah*bl)<<32 to al*bl and report overflow if
390 it's negative
391 (NB b == bl in this case, and we make a = al) */
392
393 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000394 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000395 goto bad;
396 a &= (1L << (LONG_BIT/2)) - 1;
397 x = a*b;
398 if (x < 0)
399 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000400 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000401 if (x < 0)
402 goto bad;
403 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000405
406 bad:
407 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000408}
409
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000410static int
Fred Drakea2f55112000-07-09 15:16:51 +0000411i_divmod(register PyIntObject *x, register PyIntObject *y,
412 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000413{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000414 long xi = x->ob_ival;
415 long yi = y->ob_ival;
416 long xdivy, xmody;
417
418 if (yi == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419 PyErr_SetString(PyExc_ZeroDivisionError,
420 "integer division or modulo");
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000421 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000422 }
Guido van Rossum00466951991-05-05 20:08:27 +0000423 if (yi < 0) {
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000424 if (xi < 0) {
425 if (yi == -1 && -xi < 0) {
426 /* most negative / -1 */
427 err_ovf("integer division");
428 return -1;
429 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000430 xdivy = -xi / -yi;
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000431 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000432 else
433 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000434 }
435 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000436 if (xi < 0)
437 xdivy = - (-xi / yi);
438 else
439 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000440 }
441 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000442 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000443 xmody += yi;
444 xdivy -= 1;
445 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000446 *p_xdivy = xdivy;
447 *p_xmody = xmody;
448 return 0;
449}
450
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000452int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000453{
454 long d, m;
455 if (i_divmod(x, y, &d, &m) < 0)
456 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457 return PyInt_FromLong(d);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000458}
459
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000460static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000461int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000462{
463 long d, m;
464 if (i_divmod(x, y, &d, &m) < 0)
465 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466 return PyInt_FromLong(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000467}
468
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000470int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000471{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000472 long d, m;
473 if (i_divmod(x, y, &d, &m) < 0)
474 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475 return Py_BuildValue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000476}
477
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000479int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000481#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000482 register long iv, iw, iz=0, ix, temp, prev;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000483 iv = v->ob_ival;
484 iw = w->ob_ival;
485 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486 PyErr_SetString(PyExc_ValueError,
487 "integer to the negative power");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000488 return NULL;
489 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490 if ((PyObject *)z != Py_None) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000491 iz = z->ob_ival;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000492 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000493 PyErr_SetString(PyExc_ValueError,
494 "pow(x, y, z) with z==0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000495 return NULL;
496 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000497 }
498 /*
499 * XXX: The original exponentiation code stopped looping
500 * when temp hit zero; this code will continue onwards
501 * unnecessarily, but at least it won't cause any errors.
502 * Hopefully the speed improvement from the fast exponentiation
503 * will compensate for the slight inefficiency.
504 * XXX: Better handling of overflows is desperately needed.
505 */
506 temp = iv;
507 ix = 1;
508 while (iw > 0) {
509 prev = ix; /* Save value for overflow check */
510 if (iw & 1) {
511 ix = ix*temp;
512 if (temp == 0)
513 break; /* Avoid ix / 0 */
514 if (ix / temp != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000515 return err_ovf("integer exponentiation");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000516 }
517 iw >>= 1; /* Shift exponent down by 1 bit */
518 if (iw==0) break;
519 prev = temp;
520 temp *= temp; /* Square the value of temp */
521 if (prev!=0 && temp/prev!=prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000522 return err_ovf("integer exponentiation");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000523 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000524 /* If we did a multiplication, perform a modulo */
525 ix = ix % iz;
526 temp = temp % iz;
527 }
528 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000529 if (iz) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 PyObject *t1, *t2;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000531 long int div, mod;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000532 t1=PyInt_FromLong(ix);
533 t2=PyInt_FromLong(iz);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000534 if (t1==NULL || t2==NULL ||
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000535 i_divmod((PyIntObject *)t1,
536 (PyIntObject *)t2, &div, &mod)<0)
537 {
538 Py_XDECREF(t1);
539 Py_XDECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000540 return(NULL);
541 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000542 Py_DECREF(t1);
543 Py_DECREF(t2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000544 ix=mod;
545 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000547#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548 register long iv, iw, ix;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000549 iv = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000550 iw = w->ob_ival;
Guido van Rossum00466951991-05-05 20:08:27 +0000551 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552 PyErr_SetString(PyExc_ValueError,
553 "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000554 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000555 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556 if ((PyObject *)z != Py_None) {
557 PyErr_SetString(PyExc_TypeError,
558 "pow(int, int, int) not yet supported");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000559 return NULL;
560 }
Guido van Rossum00466951991-05-05 20:08:27 +0000561 ix = 1;
562 while (--iw >= 0) {
563 long prev = ix;
564 ix = ix * iv;
565 if (iv == 0)
566 break; /* 0 to some power -- avoid ix / 0 */
567 if (ix / iv != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000568 return err_ovf("integer exponentiation");
Guido van Rossum00466951991-05-05 20:08:27 +0000569 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000570 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000571#endif
572}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000574static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000575int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000576{
577 register long a, x;
578 a = v->ob_ival;
579 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000580 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000581 return err_ovf("integer negation");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000583}
584
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000585static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000586int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000587{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588 Py_INCREF(v);
589 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000590}
591
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000593int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000594{
595 if (v->ob_ival >= 0)
596 return int_pos(v);
597 else
598 return int_neg(v);
599}
600
Guido van Rossum0bff0151991-05-14 12:05:32 +0000601static int
Fred Drakea2f55112000-07-09 15:16:51 +0000602int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000603{
604 return v->ob_ival != 0;
605}
606
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000608int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000609{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000611}
612
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000613static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000614int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000615{
616 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000617 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000618 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000619 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000621 return NULL;
622 }
623 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 Py_INCREF(v);
625 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000626 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000627 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000629 }
630 a = (unsigned long)a << b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000632}
633
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000635int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000636{
637 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000638 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000639 b = w->ob_ival;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000640 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000642 return NULL;
643 }
644 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 Py_INCREF(v);
646 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000647 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000648 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000649 if (a < 0)
650 a = -1;
651 else
652 a = 0;
653 }
654 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000655 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000656 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000658}
659
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000661int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000662{
663 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000664 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000665 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000667}
668
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000670int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000671{
672 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000673 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000674 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000676}
677
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000679int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000680{
681 register long a, b;
Guido van Rossum7928cd71991-10-24 14:59:31 +0000682 a = v->ob_ival;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000683 b = w->ob_ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000685}
686
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000687static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000688int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000689{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 Py_INCREF(v);
691 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000692}
693
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000695int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000696{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000698}
699
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000701int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000702{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000704}
705
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000707int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000708{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000709 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000710 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000711 if (x == 0)
712 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000713 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000714 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000716}
717
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000719int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000720{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000721 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000722 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000723 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000724 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000725}
726
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727static PyNumberMethods int_as_number = {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000728 (binaryfunc)int_add, /*nb_add*/
729 (binaryfunc)int_sub, /*nb_subtract*/
730 (binaryfunc)int_mul, /*nb_multiply*/
731 (binaryfunc)int_div, /*nb_divide*/
732 (binaryfunc)int_mod, /*nb_remainder*/
733 (binaryfunc)int_divmod, /*nb_divmod*/
734 (ternaryfunc)int_pow, /*nb_power*/
735 (unaryfunc)int_neg, /*nb_negative*/
736 (unaryfunc)int_pos, /*nb_positive*/
737 (unaryfunc)int_abs, /*nb_absolute*/
738 (inquiry)int_nonzero, /*nb_nonzero*/
739 (unaryfunc)int_invert, /*nb_invert*/
740 (binaryfunc)int_lshift, /*nb_lshift*/
741 (binaryfunc)int_rshift, /*nb_rshift*/
742 (binaryfunc)int_and, /*nb_and*/
743 (binaryfunc)int_xor, /*nb_xor*/
744 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000745 0, /*nb_coerce*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000746 (unaryfunc)int_int, /*nb_int*/
747 (unaryfunc)int_long, /*nb_long*/
748 (unaryfunc)int_float, /*nb_float*/
749 (unaryfunc)int_oct, /*nb_oct*/
750 (unaryfunc)int_hex, /*nb_hex*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000751};
752
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753PyTypeObject PyInt_Type = {
754 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755 0,
756 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000758 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000759 (destructor)int_dealloc, /*tp_dealloc*/
760 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000761 0, /*tp_getattr*/
762 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000763 (cmpfunc)int_compare, /*tp_compare*/
764 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765 &int_as_number, /*tp_as_number*/
766 0, /*tp_as_sequence*/
767 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000768 (hashfunc)int_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000770
771void
Fred Drakea2f55112000-07-09 15:16:51 +0000772PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000773{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000774 PyIntObject *p;
775 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000776 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000777 int bc, bf; /* block count, number of freed blocks */
778 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000779
Guido van Rossumda084ed1999-03-10 22:55:24 +0000780#if NSMALLNEGINTS + NSMALLPOSINTS > 0
781 PyIntObject **q;
782
783 i = NSMALLNEGINTS + NSMALLPOSINTS;
784 q = small_ints;
785 while (--i >= 0) {
786 Py_XDECREF(*q);
787 *q++ = NULL;
788 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000789#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000790 bc = 0;
791 bf = 0;
792 isum = 0;
793 list = block_list;
794 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000795 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000796 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000797 bc++;
798 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000799 for (i = 0, p = &list->objects[0];
800 i < N_INTOBJECTS;
801 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000802 if (PyInt_Check(p) && p->ob_refcnt != 0)
803 irem++;
804 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000805 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000806 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000807 list->next = block_list;
808 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000809 for (i = 0, p = &list->objects[0];
810 i < N_INTOBJECTS;
811 i++, p++) {
812 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
813 p->ob_type = (struct _typeobject *)
814 free_list;
815 free_list = p;
816 }
817#if NSMALLNEGINTS + NSMALLPOSINTS > 0
818 else if (-NSMALLNEGINTS <= p->ob_ival &&
819 p->ob_ival < NSMALLPOSINTS &&
820 small_ints[p->ob_ival +
821 NSMALLNEGINTS] == NULL) {
822 Py_INCREF(p);
823 small_ints[p->ob_ival +
824 NSMALLNEGINTS] = p;
825 }
826#endif
827 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000828 }
829 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000830 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000831 bf++;
832 }
833 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000834 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000835 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000836 if (!Py_VerboseFlag)
837 return;
838 fprintf(stderr, "# cleanup ints");
839 if (!isum) {
840 fprintf(stderr, "\n");
841 }
842 else {
843 fprintf(stderr,
844 ": %d unfreed int%s in %d out of %d block%s\n",
845 isum, isum == 1 ? "" : "s",
846 bc - bf, bc, bc == 1 ? "" : "s");
847 }
848 if (Py_VerboseFlag > 1) {
849 list = block_list;
850 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000851 for (i = 0, p = &list->objects[0];
852 i < N_INTOBJECTS;
853 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000854 if (PyInt_Check(p) && p->ob_refcnt != 0)
855 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000856 "# <int at %p, refcnt=%d, val=%ld>\n",
857 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000858 }
859 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000860 }
861 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000862}