blob: c51eda0245b2702915657042faadcabe435b648c [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
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000260/* Needed for the new style number compare slots */
261static PyObject *
262int_cmp(PyObject *v, PyObject *w)
263{
264 register long a, b;
265 CONVERT_TO_LONG(v, a);
266 CONVERT_TO_LONG(w, b);
267 return PyInt_FromLong((a < b) ? -1 : (a > b) ? 1 : 0);
268}
269
Guido van Rossum9bfef441993-03-29 10:43:31 +0000270static long
Fred Drakea2f55112000-07-09 15:16:51 +0000271int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000272{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000273 /* XXX If this is changed, you also need to change the way
274 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000275 long x = v -> ob_ival;
276 if (x == -1)
277 x = -2;
278 return x;
279}
280
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000281static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000282int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283{
284 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000285 CONVERT_TO_LONG(v, a);
286 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000288 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000289 return err_ovf("integer addition");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000290 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291}
292
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000293static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000294int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295{
296 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000297 CONVERT_TO_LONG(v, a);
298 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000300 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000301 return err_ovf("integer subtraction");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303}
304
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000305/*
306Integer overflow checking used to be done using a double, but on 64
307bit machines (where both long and double are 64 bit) this fails
Thomas Wouters7e474022000-07-16 12:04:32 +0000308because the double doesn't have enough precision. John Tromp suggests
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000309the following algorithm:
310
311Suppose again we normalize a and b to be nonnegative.
312Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
313Now we test ah and bh against zero and get essentially 3 possible outcomes.
314
3151) both ah and bh > 0 : then report overflow
316
3172) both ah and bh = 0 : then compute a*b and report overflow if it comes out
318 negative
319
3203) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
321 compute al*bl and report overflow if it's negative
322 add (ah*bl)<<32 to al*bl and report overflow if
323 it's negative
324
325In case of no overflow the result is then negated if necessary.
326
327The majority of cases will be 2), in which case this method is the same as
328what I suggested before. If multiplication is expensive enough, then the
329other method is faster on case 3), but also more work to program, so I
330guess the above is the preferred solution.
331
332*/
333
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000334static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000335int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000336{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000337 long a, b, ah, bh, x, y;
338 int s = 1;
339
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000340 if (v->ob_type->tp_as_sequence &&
341 v->ob_type->tp_as_sequence->sq_repeat) {
342 /* sequence * int */
343 a = PyInt_AsLong(w);
344 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
345 }
346 else if (w->ob_type->tp_as_sequence &&
347 w->ob_type->tp_as_sequence->sq_repeat) {
348 /* int * sequence */
349 a = PyInt_AsLong(v);
350 return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
351 }
352
353 CONVERT_TO_LONG(v, a);
354 CONVERT_TO_LONG(w, b);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000355 ah = a >> (LONG_BIT/2);
356 bh = b >> (LONG_BIT/2);
357
358 /* Quick test for common case: two small positive ints */
359
360 if (ah == 0 && bh == 0) {
361 x = a*b;
362 if (x < 0)
363 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000365 }
366
367 /* Arrange that a >= b >= 0 */
368
369 if (a < 0) {
370 a = -a;
371 if (a < 0) {
372 /* Largest negative */
373 if (b == 0 || b == 1) {
374 x = a*b;
375 goto ok;
376 }
377 else
378 goto bad;
379 }
380 s = -s;
381 ah = a >> (LONG_BIT/2);
382 }
383 if (b < 0) {
384 b = -b;
385 if (b < 0) {
386 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000387 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000388 x = a*b;
389 goto ok;
390 }
391 else
392 goto bad;
393 }
394 s = -s;
395 bh = b >> (LONG_BIT/2);
396 }
397
398 /* 1) both ah and bh > 0 : then report overflow */
399
400 if (ah != 0 && bh != 0)
401 goto bad;
402
403 /* 2) both ah and bh = 0 : then compute a*b and report
404 overflow if it comes out negative */
405
406 if (ah == 0 && bh == 0) {
407 x = a*b;
408 if (x < 0)
409 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000411 }
412
413 if (a < b) {
414 /* Swap */
415 x = a;
416 a = b;
417 b = x;
418 ah = bh;
419 /* bh not used beyond this point */
420 }
421
422 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
423 it's >= 2^31
424 compute al*bl and report overflow if it's negative
425 add (ah*bl)<<32 to al*bl and report overflow if
426 it's negative
427 (NB b == bl in this case, and we make a = al) */
428
429 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000430 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000431 goto bad;
432 a &= (1L << (LONG_BIT/2)) - 1;
433 x = a*b;
434 if (x < 0)
435 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000436 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000437 if (x < 0)
438 goto bad;
439 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000440 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000441
442 bad:
443 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444}
445
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000446static int
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000447i_divmod(register long xi, register long yi,
Fred Drakea2f55112000-07-09 15:16:51 +0000448 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000450 long xdivy, xmody;
451
452 if (yi == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000453 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000454 "integer division or modulo by zero");
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000455 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456 }
Guido van Rossum00466951991-05-05 20:08:27 +0000457 if (yi < 0) {
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000458 if (xi < 0) {
459 if (yi == -1 && -xi < 0) {
460 /* most negative / -1 */
461 err_ovf("integer division");
462 return -1;
463 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000464 xdivy = -xi / -yi;
Guido van Rossume13ff2e1999-09-27 17:12:47 +0000465 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000466 else
467 xdivy = - (xi / -yi);
Guido van Rossum00466951991-05-05 20:08:27 +0000468 }
469 else {
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000470 if (xi < 0)
471 xdivy = - (-xi / yi);
472 else
473 xdivy = xi / yi;
Guido van Rossum00466951991-05-05 20:08:27 +0000474 }
475 xmody = xi - xdivy*yi;
Guido van Rossum9478dd41996-12-06 20:14:43 +0000476 if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) {
Guido van Rossum00466951991-05-05 20:08:27 +0000477 xmody += yi;
478 xdivy -= 1;
479 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000480 *p_xdivy = xdivy;
481 *p_xmody = xmody;
482 return 0;
483}
484
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000486int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000487{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000488 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000489 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000490 CONVERT_TO_LONG(x, xi);
491 CONVERT_TO_LONG(y, yi);
492 if (i_divmod(xi, yi, &d, &m) < 0)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000493 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000494 return PyInt_FromLong(d);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000495}
496
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000497static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000498int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000499{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000500 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000501 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000502 CONVERT_TO_LONG(x, xi);
503 CONVERT_TO_LONG(y, yi);
504 if (i_divmod(xi, yi, &d, &m) < 0)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000505 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506 return PyInt_FromLong(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000507}
508
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000509static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000510int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000511{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000512 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000513 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000514 CONVERT_TO_LONG(x, xi);
515 CONVERT_TO_LONG(y, yi);
516 if (i_divmod(xi, yi, &d, &m) < 0)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000517 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000518 return Py_BuildValue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000519}
520
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000521static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000522int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000523{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000524#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000525 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000526 CONVERT_TO_LONG(v, iv);
527 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000528 if (iw < 0) {
Tim Petersc54d1902000-10-06 00:36:09 +0000529 if (iv)
530 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000531 "cannot raise integer to a negative power");
Tim Petersc54d1902000-10-06 00:36:09 +0000532 else
533 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000534 "cannot raise 0 to a negative power");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000535 return NULL;
536 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000538 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000539 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000541 "pow() arg 3 cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000542 return NULL;
543 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000544 }
545 /*
546 * XXX: The original exponentiation code stopped looping
547 * when temp hit zero; this code will continue onwards
548 * unnecessarily, but at least it won't cause any errors.
549 * Hopefully the speed improvement from the fast exponentiation
550 * will compensate for the slight inefficiency.
551 * XXX: Better handling of overflows is desperately needed.
552 */
553 temp = iv;
554 ix = 1;
555 while (iw > 0) {
556 prev = ix; /* Save value for overflow check */
557 if (iw & 1) {
558 ix = ix*temp;
559 if (temp == 0)
560 break; /* Avoid ix / 0 */
561 if (ix / temp != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000562 return err_ovf("integer exponentiation");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000563 }
564 iw >>= 1; /* Shift exponent down by 1 bit */
565 if (iw==0) break;
566 prev = temp;
567 temp *= temp; /* Square the value of temp */
568 if (prev!=0 && temp/prev!=prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000569 return err_ovf("integer exponentiation");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000570 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000571 /* If we did a multiplication, perform a modulo */
572 ix = ix % iz;
573 temp = temp % iz;
574 }
575 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000576 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000577 long div, mod;
578 if (i_divmod(ix, iz, &div, &mod) < 0)
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000579 return(NULL);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000580 ix=mod;
581 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000583#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000584 register long iv, iw, ix;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000585 CONVERT_TO_LONG(v, iv);
586 CONVERT_TO_LONG(w, iw);
Guido van Rossum00466951991-05-05 20:08:27 +0000587 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588 PyErr_SetString(PyExc_ValueError,
589 "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000590 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592 if ((PyObject *)z != Py_None) {
593 PyErr_SetString(PyExc_TypeError,
594 "pow(int, int, int) not yet supported");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000595 return NULL;
596 }
Guido van Rossum00466951991-05-05 20:08:27 +0000597 ix = 1;
598 while (--iw >= 0) {
599 long prev = ix;
600 ix = ix * iv;
601 if (iv == 0)
602 break; /* 0 to some power -- avoid ix / 0 */
603 if (ix / iv != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000604 return err_ovf("integer exponentiation");
Guido van Rossum00466951991-05-05 20:08:27 +0000605 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000607#endif
608}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000611int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000612{
613 register long a, x;
614 a = v->ob_ival;
615 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000616 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000617 return err_ovf("integer negation");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619}
620
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000622int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000623{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 Py_INCREF(v);
625 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626}
627
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000628static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000629int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000630{
631 if (v->ob_ival >= 0)
632 return int_pos(v);
633 else
634 return int_neg(v);
635}
636
Guido van Rossum0bff0151991-05-14 12:05:32 +0000637static int
Fred Drakea2f55112000-07-09 15:16:51 +0000638int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000639{
640 return v->ob_ival != 0;
641}
642
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000644int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000645{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000647}
648
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000650int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000651{
652 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000653 CONVERT_TO_LONG(v, a);
654 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000655 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000656 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000657 return NULL;
658 }
659 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660 Py_INCREF(v);
661 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000662 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000663 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000664 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000665 }
666 a = (unsigned long)a << b;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000668}
669
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000671int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000672{
673 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000674 CONVERT_TO_LONG(v, a);
675 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000676 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000678 return NULL;
679 }
680 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000681 Py_INCREF(v);
682 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000683 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000684 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000685 if (a < 0)
686 a = -1;
687 else
688 a = 0;
689 }
690 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000691 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000692 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000694}
695
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000697int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000698{
699 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000700 CONVERT_TO_LONG(v, a);
701 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000703}
704
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000705static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000706int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000707{
708 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000709 CONVERT_TO_LONG(v, a);
710 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000712}
713
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000715int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000716{
717 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000718 CONVERT_TO_LONG(v, a);
719 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000720 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000721}
722
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000724int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000725{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726 Py_INCREF(v);
727 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000728}
729
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000730static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000731int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000732{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000734}
735
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000737int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000738{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000739 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000740}
741
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000742static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000743int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000744{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000745 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000746 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000747 if (x == 0)
748 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000749 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000750 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000752}
753
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000755int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000756{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000757 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000758 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000759 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000760 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000761}
762
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000764 (binaryfunc)int_add, /*nb_add*/
765 (binaryfunc)int_sub, /*nb_subtract*/
766 (binaryfunc)int_mul, /*nb_multiply*/
767 (binaryfunc)int_div, /*nb_divide*/
768 (binaryfunc)int_mod, /*nb_remainder*/
769 (binaryfunc)int_divmod, /*nb_divmod*/
770 (ternaryfunc)int_pow, /*nb_power*/
771 (unaryfunc)int_neg, /*nb_negative*/
772 (unaryfunc)int_pos, /*nb_positive*/
773 (unaryfunc)int_abs, /*nb_absolute*/
774 (inquiry)int_nonzero, /*nb_nonzero*/
775 (unaryfunc)int_invert, /*nb_invert*/
776 (binaryfunc)int_lshift, /*nb_lshift*/
777 (binaryfunc)int_rshift, /*nb_rshift*/
778 (binaryfunc)int_and, /*nb_and*/
779 (binaryfunc)int_xor, /*nb_xor*/
780 (binaryfunc)int_or, /*nb_or*/
781 0, /*nb_coerce*/
782 (unaryfunc)int_int, /*nb_int*/
783 (unaryfunc)int_long, /*nb_long*/
784 (unaryfunc)int_float, /*nb_float*/
785 (unaryfunc)int_oct, /*nb_oct*/
786 (unaryfunc)int_hex, /*nb_hex*/
787 0, /*nb_inplace_add*/
788 0, /*nb_inplace_subtract*/
789 0, /*nb_inplace_multiply*/
790 0, /*nb_inplace_divide*/
791 0, /*nb_inplace_remainder*/
792 0, /*nb_inplace_power*/
793 0, /*nb_inplace_lshift*/
794 0, /*nb_inplace_rshift*/
795 0, /*nb_inplace_and*/
796 0, /*nb_inplace_xor*/
797 0, /*nb_inplace_or*/
798
799 /* New style slots: */
800 (binaryfunc)int_cmp, /*nb_cmp*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000801};
802
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803PyTypeObject PyInt_Type = {
804 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000805 0,
806 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000807 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000808 0,
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000809 (destructor)int_dealloc, /*tp_dealloc*/
810 (printfunc)int_print, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000811 0, /*tp_getattr*/
812 0, /*tp_setattr*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000813 (cmpfunc)int_compare, /*tp_compare*/
814 (reprfunc)int_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000815 &int_as_number, /*tp_as_number*/
816 0, /*tp_as_sequence*/
817 0, /*tp_as_mapping*/
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000818 (hashfunc)int_hash, /*tp_hash*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000819 0, /*tp_call*/
820 0, /*tp_str*/
821 0, /*tp_getattro*/
822 0, /*tp_setattro*/
823 0, /*tp_as_buffer*/
824 Py_TPFLAGS_NEWSTYLENUMBER /*tp_flags*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000825};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000826
827void
Fred Drakea2f55112000-07-09 15:16:51 +0000828PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000829{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000830 PyIntObject *p;
831 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000832 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000833 int bc, bf; /* block count, number of freed blocks */
834 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000835
Guido van Rossumda084ed1999-03-10 22:55:24 +0000836#if NSMALLNEGINTS + NSMALLPOSINTS > 0
837 PyIntObject **q;
838
839 i = NSMALLNEGINTS + NSMALLPOSINTS;
840 q = small_ints;
841 while (--i >= 0) {
842 Py_XDECREF(*q);
843 *q++ = NULL;
844 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000845#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000846 bc = 0;
847 bf = 0;
848 isum = 0;
849 list = block_list;
850 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000851 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000852 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000853 bc++;
854 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000855 for (i = 0, p = &list->objects[0];
856 i < N_INTOBJECTS;
857 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000858 if (PyInt_Check(p) && p->ob_refcnt != 0)
859 irem++;
860 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000861 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000862 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000863 list->next = block_list;
864 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000865 for (i = 0, p = &list->objects[0];
866 i < N_INTOBJECTS;
867 i++, p++) {
868 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
869 p->ob_type = (struct _typeobject *)
870 free_list;
871 free_list = p;
872 }
873#if NSMALLNEGINTS + NSMALLPOSINTS > 0
874 else if (-NSMALLNEGINTS <= p->ob_ival &&
875 p->ob_ival < NSMALLPOSINTS &&
876 small_ints[p->ob_ival +
877 NSMALLNEGINTS] == NULL) {
878 Py_INCREF(p);
879 small_ints[p->ob_ival +
880 NSMALLNEGINTS] = p;
881 }
882#endif
883 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000884 }
885 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000886 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000887 bf++;
888 }
889 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000890 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000891 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000892 if (!Py_VerboseFlag)
893 return;
894 fprintf(stderr, "# cleanup ints");
895 if (!isum) {
896 fprintf(stderr, "\n");
897 }
898 else {
899 fprintf(stderr,
900 ": %d unfreed int%s in %d out of %d block%s\n",
901 isum, isum == 1 ? "" : "s",
902 bc - bf, bc, bc == 1 ? "" : "s");
903 }
904 if (Py_VerboseFlag > 1) {
905 list = block_list;
906 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000907 for (i = 0, p = &list->objects[0];
908 i < N_INTOBJECTS;
909 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000910 if (PyInt_Check(p) && p->ob_refcnt != 0)
911 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000912 "# <int at %p, refcnt=%d, val=%ld>\n",
913 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000914 }
915 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000916 }
917 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000918}