blob: 72d5323bf0f4d0d51910bf2b399cad1579e8190f [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) {
Fred Drake661ea262000-10-24 19:57:45 +0000174 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000175 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
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000222/* Integers are seen as the "smallest" of all numeric types and thus
223 don't have any knowledge about conversion of other types to
224 integers. */
225
226#define CONVERT_TO_LONG(obj, lng) \
227 if (PyInt_Check(obj)) { \
228 lng = PyInt_AS_LONG(obj); \
229 } \
230 else { \
231 Py_INCREF(Py_NotImplemented); \
232 return Py_NotImplemented; \
233 }
234
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000235/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000236static int
Fred Drakea2f55112000-07-09 15:16:51 +0000237int_print(PyIntObject *v, FILE *fp, int flags)
238 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000239{
240 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000241 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000242}
243
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000244static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000245int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
247 char buf[20];
248 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000249 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250}
251
252static int
Fred Drakea2f55112000-07-09 15:16:51 +0000253int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254{
255 register long i = v->ob_ival;
256 register long j = w->ob_ival;
257 return (i < j) ? -1 : (i > j) ? 1 : 0;
258}
259
Guido van Rossum9bfef441993-03-29 10:43:31 +0000260static long
Fred Drakea2f55112000-07-09 15:16:51 +0000261int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000262{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000263 /* XXX If this is changed, you also need to change the way
264 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000265 long x = v -> ob_ival;
266 if (x == -1)
267 x = -2;
268 return x;
269}
270
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000271static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000272int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273{
274 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000275 CONVERT_TO_LONG(v, a);
276 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000278 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000279 return err_ovf("integer addition");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281}
282
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000283static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000284int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285{
286 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000287 CONVERT_TO_LONG(v, a);
288 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000290 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000291 return err_ovf("integer subtraction");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000292 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293}
294
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000295/*
296Integer overflow checking used to be done using a double, but on 64
297bit machines (where both long and double are 64 bit) this fails
Thomas Wouters7e474022000-07-16 12:04:32 +0000298because the double doesn't have enough precision. John Tromp suggests
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000299the following algorithm:
300
301Suppose again we normalize a and b to be nonnegative.
302Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
303Now we test ah and bh against zero and get essentially 3 possible outcomes.
304
3051) both ah and bh > 0 : then report overflow
306
3072) both ah and bh = 0 : then compute a*b and report overflow if it comes out
308 negative
309
3103) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
311 compute al*bl and report overflow if it's negative
312 add (ah*bl)<<32 to al*bl and report overflow if
313 it's negative
314
315In case of no overflow the result is then negated if necessary.
316
317The majority of cases will be 2), in which case this method is the same as
318what I suggested before. If multiplication is expensive enough, then the
319other method is faster on case 3), but also more work to program, so I
320guess the above is the preferred solution.
321
322*/
323
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000325int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000326{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000327 long a, b, ah, bh, x, y;
328 int s = 1;
329
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000330 if (v->ob_type->tp_as_sequence &&
331 v->ob_type->tp_as_sequence->sq_repeat) {
332 /* sequence * int */
333 a = PyInt_AsLong(w);
334 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
335 }
336 else if (w->ob_type->tp_as_sequence &&
337 w->ob_type->tp_as_sequence->sq_repeat) {
338 /* int * sequence */
339 a = PyInt_AsLong(v);
340 return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
341 }
342
343 CONVERT_TO_LONG(v, a);
344 CONVERT_TO_LONG(w, b);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000345 ah = a >> (LONG_BIT/2);
346 bh = b >> (LONG_BIT/2);
347
348 /* Quick test for common case: two small positive ints */
349
350 if (ah == 0 && bh == 0) {
351 x = a*b;
352 if (x < 0)
353 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000355 }
356
357 /* Arrange that a >= b >= 0 */
358
359 if (a < 0) {
360 a = -a;
361 if (a < 0) {
362 /* Largest negative */
363 if (b == 0 || b == 1) {
364 x = a*b;
365 goto ok;
366 }
367 else
368 goto bad;
369 }
370 s = -s;
371 ah = a >> (LONG_BIT/2);
372 }
373 if (b < 0) {
374 b = -b;
375 if (b < 0) {
376 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000377 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000378 x = a*b;
379 goto ok;
380 }
381 else
382 goto bad;
383 }
384 s = -s;
385 bh = b >> (LONG_BIT/2);
386 }
387
388 /* 1) both ah and bh > 0 : then report overflow */
389
390 if (ah != 0 && bh != 0)
391 goto bad;
392
393 /* 2) both ah and bh = 0 : then compute a*b and report
394 overflow if it comes out negative */
395
396 if (ah == 0 && bh == 0) {
397 x = a*b;
398 if (x < 0)
399 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000401 }
402
403 if (a < b) {
404 /* Swap */
405 x = a;
406 a = b;
407 b = x;
408 ah = bh;
409 /* bh not used beyond this point */
410 }
411
412 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
413 it's >= 2^31
414 compute al*bl and report overflow if it's negative
415 add (ah*bl)<<32 to al*bl and report overflow if
416 it's negative
417 (NB b == bl in this case, and we make a = al) */
418
419 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000420 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000421 goto bad;
422 a &= (1L << (LONG_BIT/2)) - 1;
423 x = a*b;
424 if (x < 0)
425 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000426 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000427 if (x < 0)
428 goto bad;
429 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000430 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000431
432 bad:
433 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434}
435
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000436static int
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000437i_divmod(register long xi, register long yi,
Fred Drakea2f55112000-07-09 15:16:51 +0000438 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000440 long xdivy, xmody;
441
442 if (yi == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000444 "integer division or modulo by zero");
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000445 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446 }
Guido van Rossum00466951991-05-05 20:08:27 +0000447 if (yi < 0) {
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000448 if (xi < 0) {
449 if (yi == -1 && -xi < 0) {
450 /* most negative / -1 */
451 err_ovf("integer division");
452 return -1;
453 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000454 xdivy = -xi / -yi;
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000455 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000456 else
457 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000458 }
459 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000460 if (xi < 0)
461 xdivy = - (-xi / yi);
462 else
463 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000464 }
465 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000466 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000467 xmody += yi;
468 xdivy -= 1;
469 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000470 *p_xdivy = xdivy;
471 *p_xmody = xmody;
472 return 0;
473}
474
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000476int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000477{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000478 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000479 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000480 CONVERT_TO_LONG(x, xi);
481 CONVERT_TO_LONG(y, yi);
482 if (i_divmod(xi, yi, &d, &m) < 0)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000483 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484 return PyInt_FromLong(d);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000485}
486
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000487static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000488int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000489{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000490 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000491 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000492 CONVERT_TO_LONG(x, xi);
493 CONVERT_TO_LONG(y, yi);
494 if (i_divmod(xi, yi, &d, &m) < 0)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000495 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496 return PyInt_FromLong(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000497}
498
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000499static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000500int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000501{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000502 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000503 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000504 CONVERT_TO_LONG(x, xi);
505 CONVERT_TO_LONG(y, yi);
506 if (i_divmod(xi, yi, &d, &m) < 0)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000507 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000508 return Py_BuildValue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000509}
510
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000511static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000512int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000514#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000515 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000516 CONVERT_TO_LONG(v, iv);
517 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000518 if (iw < 0) {
Tim Petersc54d1902000-10-06 00:36:09 +0000519 if (iv)
520 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000521 "cannot raise integer to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000522 else
523 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000524 "cannot raise 0 to a negative power");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000525 return NULL;
526 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000528 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000529 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000531 "pow() arg 3 cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000532 return NULL;
533 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000534 }
535 /*
536 * XXX: The original exponentiation code stopped looping
537 * when temp hit zero; this code will continue onwards
538 * unnecessarily, but at least it won't cause any errors.
539 * Hopefully the speed improvement from the fast exponentiation
540 * will compensate for the slight inefficiency.
541 * XXX: Better handling of overflows is desperately needed.
542 */
543 temp = iv;
544 ix = 1;
545 while (iw > 0) {
546 prev = ix; /* Save value for overflow check */
547 if (iw & 1) {
548 ix = ix*temp;
549 if (temp == 0)
550 break; /* Avoid ix / 0 */
551 if (ix / temp != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000552 return err_ovf("integer exponentiation");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000553 }
554 iw >>= 1; /* Shift exponent down by 1 bit */
555 if (iw==0) break;
556 prev = temp;
557 temp *= temp; /* Square the value of temp */
558 if (prev!=0 && temp/prev!=prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000559 return err_ovf("integer exponentiation");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000560 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000561 /* If we did a multiplication, perform a modulo */
562 ix = ix % iz;
563 temp = temp % iz;
564 }
565 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000566 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000567 long div, mod;
568 if (i_divmod(ix, iz, &div, &mod) < 0)
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000569 return(NULL);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000570 ix=mod;
571 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000573#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574 register long iv, iw, ix;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000575 CONVERT_TO_LONG(v, iv);
576 CONVERT_TO_LONG(w, iw);
Guido van Rossum00466951991-05-05 20:08:27 +0000577 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 PyErr_SetString(PyExc_ValueError,
579 "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000580 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000581 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 if ((PyObject *)z != Py_None) {
583 PyErr_SetString(PyExc_TypeError,
584 "pow(int, int, int) not yet supported");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000585 return NULL;
586 }
Guido van Rossum00466951991-05-05 20:08:27 +0000587 ix = 1;
588 while (--iw >= 0) {
589 long prev = ix;
590 ix = ix * iv;
591 if (iv == 0)
592 break; /* 0 to some power -- avoid ix / 0 */
593 if (ix / iv != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000594 return err_ovf("integer exponentiation");
Guido van Rossum00466951991-05-05 20:08:27 +0000595 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000596 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000597#endif
598}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000599
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000600static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000601int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000602{
603 register long a, x;
604 a = v->ob_ival;
605 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000606 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000607 return err_ovf("integer negation");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000608 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609}
610
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000611static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000612int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000613{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614 Py_INCREF(v);
615 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000616}
617
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000619int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000620{
621 if (v->ob_ival >= 0)
622 return int_pos(v);
623 else
624 return int_neg(v);
625}
626
Guido van Rossum0bff0151991-05-14 12:05:32 +0000627static int
Fred Drakea2f55112000-07-09 15:16:51 +0000628int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000629{
630 return v->ob_ival != 0;
631}
632
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000634int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000635{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000637}
638
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000640int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000641{
642 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000643 CONVERT_TO_LONG(v, a);
644 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000645 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000647 return NULL;
648 }
649 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 Py_INCREF(v);
651 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000652 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000653 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000655 }
656 a = (unsigned long)a << b;
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_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000662{
663 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000664 CONVERT_TO_LONG(v, a);
665 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000666 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000668 return NULL;
669 }
670 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000671 Py_INCREF(v);
672 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000673 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000674 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000675 if (a < 0)
676 a = -1;
677 else
678 a = 0;
679 }
680 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000681 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000682 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 return PyInt_FromLong(a);
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_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000688{
689 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000690 CONVERT_TO_LONG(v, a);
691 CONVERT_TO_LONG(w, b);
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_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000697{
698 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000699 CONVERT_TO_LONG(v, a);
700 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000701 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000702}
703
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000704static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000705int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000706{
707 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000708 CONVERT_TO_LONG(v, a);
709 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000711}
712
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000714int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000715{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000716 Py_INCREF(v);
717 return (PyObject *)v;
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_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000722{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 return PyLong_FromLong((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_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000728{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000729 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000730}
731
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000733int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000734{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000735 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000736 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000737 if (x == 0)
738 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000739 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000740 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000741 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000742}
743
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000745int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000746{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000747 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000748 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000749 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000751}
752
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000754 (binaryfunc)int_add, /*nb_add*/
755 (binaryfunc)int_sub, /*nb_subtract*/
756 (binaryfunc)int_mul, /*nb_multiply*/
757 (binaryfunc)int_div, /*nb_divide*/
758 (binaryfunc)int_mod, /*nb_remainder*/
759 (binaryfunc)int_divmod, /*nb_divmod*/
760 (ternaryfunc)int_pow, /*nb_power*/
761 (unaryfunc)int_neg, /*nb_negative*/
762 (unaryfunc)int_pos, /*nb_positive*/
763 (unaryfunc)int_abs, /*nb_absolute*/
764 (inquiry)int_nonzero, /*nb_nonzero*/
765 (unaryfunc)int_invert, /*nb_invert*/
766 (binaryfunc)int_lshift, /*nb_lshift*/
767 (binaryfunc)int_rshift, /*nb_rshift*/
768 (binaryfunc)int_and, /*nb_and*/
769 (binaryfunc)int_xor, /*nb_xor*/
770 (binaryfunc)int_or, /*nb_or*/
771 0, /*nb_coerce*/
772 (unaryfunc)int_int, /*nb_int*/
773 (unaryfunc)int_long, /*nb_long*/
774 (unaryfunc)int_float, /*nb_float*/
775 (unaryfunc)int_oct, /*nb_oct*/
776 (unaryfunc)int_hex, /*nb_hex*/
777 0, /*nb_inplace_add*/
778 0, /*nb_inplace_subtract*/
779 0, /*nb_inplace_multiply*/
780 0, /*nb_inplace_divide*/
781 0, /*nb_inplace_remainder*/
782 0, /*nb_inplace_power*/
783 0, /*nb_inplace_lshift*/
784 0, /*nb_inplace_rshift*/
785 0, /*nb_inplace_and*/
786 0, /*nb_inplace_xor*/
787 0, /*nb_inplace_or*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000788};
789
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790PyTypeObject PyInt_Type = {
791 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792 0,
793 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000794 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000796 (destructor)int_dealloc, /*tp_dealloc*/
797 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000798 0, /*tp_getattr*/
799 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000800 (cmpfunc)int_compare, /*tp_compare*/
801 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802 &int_as_number, /*tp_as_number*/
803 0, /*tp_as_sequence*/
804 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000805 (hashfunc)int_hash, /*tp_hash*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000806 0, /*tp_call*/
807 0, /*tp_str*/
808 0, /*tp_getattro*/
809 0, /*tp_setattro*/
810 0, /*tp_as_buffer*/
Guido van Rossum3968e4c2001-01-17 15:32:23 +0000811 Py_TPFLAGS_CHECKTYPES /*tp_flags*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000812};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000813
814void
Fred Drakea2f55112000-07-09 15:16:51 +0000815PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000816{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000817 PyIntObject *p;
818 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000819 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000820 int bc, bf; /* block count, number of freed blocks */
821 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000822
Guido van Rossumda084ed1999-03-10 22:55:24 +0000823#if NSMALLNEGINTS + NSMALLPOSINTS > 0
824 PyIntObject **q;
825
826 i = NSMALLNEGINTS + NSMALLPOSINTS;
827 q = small_ints;
828 while (--i >= 0) {
829 Py_XDECREF(*q);
830 *q++ = NULL;
831 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000832#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000833 bc = 0;
834 bf = 0;
835 isum = 0;
836 list = block_list;
837 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000838 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000839 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000840 bc++;
841 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000842 for (i = 0, p = &list->objects[0];
843 i < N_INTOBJECTS;
844 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000845 if (PyInt_Check(p) && p->ob_refcnt != 0)
846 irem++;
847 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000848 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000849 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000850 list->next = block_list;
851 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000852 for (i = 0, p = &list->objects[0];
853 i < N_INTOBJECTS;
854 i++, p++) {
855 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
856 p->ob_type = (struct _typeobject *)
857 free_list;
858 free_list = p;
859 }
860#if NSMALLNEGINTS + NSMALLPOSINTS > 0
861 else if (-NSMALLNEGINTS <= p->ob_ival &&
862 p->ob_ival < NSMALLPOSINTS &&
863 small_ints[p->ob_ival +
864 NSMALLNEGINTS] == NULL) {
865 Py_INCREF(p);
866 small_ints[p->ob_ival +
867 NSMALLNEGINTS] = p;
868 }
869#endif
870 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000871 }
872 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000873 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000874 bf++;
875 }
876 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000877 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000878 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000879 if (!Py_VerboseFlag)
880 return;
881 fprintf(stderr, "# cleanup ints");
882 if (!isum) {
883 fprintf(stderr, "\n");
884 }
885 else {
886 fprintf(stderr,
887 ": %d unfreed int%s in %d out of %d block%s\n",
888 isum, isum == 1 ? "" : "s",
889 bc - bf, bc, bc == 1 ? "" : "s");
890 }
891 if (Py_VerboseFlag > 1) {
892 list = block_list;
893 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000894 for (i = 0, p = &list->objects[0];
895 i < N_INTOBJECTS;
896 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000897 if (PyInt_Check(p) && p->ob_refcnt != 0)
898 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000899 "# <int at %p, refcnt=%d, val=%ld>\n",
900 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000901 }
902 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000903 }
904 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000905}