blob: 77247606670f179fe3ac8ddf3f02a6da55075b38 [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 Rossume27f7952001-08-23 02:59:04 +000025/* Return 1 if exception raised, 0 if caller should retry using longs */
26static int
Fred Drakea2f55112000-07-09 15:16:51 +000027err_ovf(char *msg)
Guido van Rossum165e67e1990-10-14 20:02:26 +000028{
Guido van Rossume27f7952001-08-23 02:59:04 +000029 if (PyErr_Warn(PyExc_OverflowWarning, msg) < 0) {
30 PyErr_SetString(PyExc_OverflowError, msg);
31 return 1;
32 }
33 else
34 return 0;
Guido van Rossum165e67e1990-10-14 20:02:26 +000035}
36
Guido van Rossum3f5da241990-12-20 15:06:42 +000037/* Integers are quite normal objects, to make object handling uniform.
38 (Using odd pointers to represent integers would save much space
39 but require extra checks for this special case throughout the code.)
40 Since, a typical Python program spends much of its time allocating
41 and deallocating integers, these operations should be very fast.
42 Therefore we use a dedicated allocation scheme with a much lower
43 overhead (in space and time) than straight malloc(): a simple
44 dedicated free list, filled when necessary with memory from malloc().
45*/
46
47#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000048#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
49#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000050
Guido van Rossum3fce8831999-03-12 19:43:17 +000051struct _intblock {
52 struct _intblock *next;
53 PyIntObject objects[N_INTOBJECTS];
54};
55
56typedef struct _intblock PyIntBlock;
57
58static PyIntBlock *block_list = NULL;
59static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000060
Guido van Rossumc0b618a1997-05-02 03:12:38 +000061static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000062fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000063{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064 PyIntObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000065 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
66 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000067 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000068 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000069 ((PyIntBlock *)p)->next = block_list;
70 block_list = (PyIntBlock *)p;
71 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000072 q = p + N_INTOBJECTS;
73 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000074 q->ob_type = (struct _typeobject *)(q-1);
75 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000076 return p + N_INTOBJECTS - 1;
77}
78
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000079#ifndef NSMALLPOSINTS
80#define NSMALLPOSINTS 100
81#endif
82#ifndef NSMALLNEGINTS
83#define NSMALLNEGINTS 1
84#endif
85#if NSMALLNEGINTS + NSMALLPOSINTS > 0
86/* References to small integers are saved in this array so that they
87 can be shared.
88 The integers that are saved are those in the range
89 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
90*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000092#endif
93#ifdef COUNT_ALLOCS
94int quick_int_allocs, quick_neg_int_allocs;
95#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000096
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000098PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000101#if NSMALLNEGINTS + NSMALLPOSINTS > 0
102 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
103 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000104 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000105#ifdef COUNT_ALLOCS
106 if (ival >= 0)
107 quick_int_allocs++;
108 else
109 quick_neg_int_allocs++;
110#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000112 }
113#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000114 if (free_list == NULL) {
115 if ((free_list = fill_free_list()) == NULL)
116 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000118 /* PyObject_New is inlined */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000119 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000120 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000121 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000122 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000123#if NSMALLNEGINTS + NSMALLPOSINTS > 0
124 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
125 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000126 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000127 small_ints[ival + NSMALLNEGINTS] = v;
128 }
129#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000131}
132
133static void
Fred Drakea2f55112000-07-09 15:16:51 +0000134int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000135{
Guido van Rossumda084ed1999-03-10 22:55:24 +0000136 v->ob_type = (struct _typeobject *)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000137 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138}
139
140long
Fred Drakea2f55112000-07-09 15:16:51 +0000141PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000143 PyNumberMethods *nb;
144 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000145 long val;
146
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147 if (op && PyInt_Check(op))
148 return PyInt_AS_LONG((PyIntObject*) op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000149
150 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
151 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000152 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 return -1;
154 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000155
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000157 if (io == NULL)
158 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 if (!PyInt_Check(io)) {
160 PyErr_SetString(PyExc_TypeError,
161 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000162 return -1;
163 }
164
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 val = PyInt_AS_LONG(io);
166 Py_DECREF(io);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000167
168 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169}
170
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000171PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000172PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000173{
174 char *end;
175 long x;
176 char buffer[256]; /* For errors */
177
178 if ((base != 0 && base < 2) || base > 36) {
Fred Drake661ea262000-10-24 19:57:45 +0000179 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000180 return NULL;
181 }
182
183 while (*s && isspace(Py_CHARMASK(*s)))
184 s++;
185 errno = 0;
186 if (base == 0 && s[0] == '0')
187 x = (long) PyOS_strtoul(s, &end, base);
188 else
189 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000190 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000191 goto bad;
192 while (*end && isspace(Py_CHARMASK(*end)))
193 end++;
194 if (*end != '\0') {
195 bad:
196 sprintf(buffer, "invalid literal for int(): %.200s", s);
197 PyErr_SetString(PyExc_ValueError, buffer);
198 return NULL;
199 }
200 else if (errno != 0) {
201 sprintf(buffer, "int() literal too large: %.200s", s);
202 PyErr_SetString(PyExc_ValueError, buffer);
203 return NULL;
204 }
205 if (pend)
206 *pend = end;
207 return PyInt_FromLong(x);
208}
209
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000210#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000211PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000212PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000213{
214 char buffer[256];
215
216 if (length >= sizeof(buffer)) {
217 PyErr_SetString(PyExc_ValueError,
218 "int() literal too large to convert");
219 return NULL;
220 }
221 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
222 return NULL;
223 return PyInt_FromString(buffer, NULL, base);
224}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000225#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000226
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227/* Methods */
228
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000229/* Integers are seen as the "smallest" of all numeric types and thus
230 don't have any knowledge about conversion of other types to
231 integers. */
232
233#define CONVERT_TO_LONG(obj, lng) \
234 if (PyInt_Check(obj)) { \
235 lng = PyInt_AS_LONG(obj); \
236 } \
237 else { \
238 Py_INCREF(Py_NotImplemented); \
239 return Py_NotImplemented; \
240 }
241
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000242/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000243static int
Fred Drakea2f55112000-07-09 15:16:51 +0000244int_print(PyIntObject *v, FILE *fp, int flags)
245 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000246{
247 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000248 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249}
250
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000252int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253{
254 char buf[20];
255 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000257}
258
259static int
Fred Drakea2f55112000-07-09 15:16:51 +0000260int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261{
262 register long i = v->ob_ival;
263 register long j = w->ob_ival;
264 return (i < j) ? -1 : (i > j) ? 1 : 0;
265}
266
Guido van Rossum9bfef441993-03-29 10:43:31 +0000267static long
Fred Drakea2f55112000-07-09 15:16:51 +0000268int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000269{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000270 /* XXX If this is changed, you also need to change the way
271 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000272 long x = v -> ob_ival;
273 if (x == -1)
274 x = -2;
275 return x;
276}
277
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000279int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280{
281 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000282 CONVERT_TO_LONG(v, a);
283 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000285 if ((x^a) >= 0 || (x^b) >= 0)
286 return PyInt_FromLong(x);
287 if (err_ovf("integer addition"))
288 return NULL;
289 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000290}
291
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000292static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000293int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294{
295 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000296 CONVERT_TO_LONG(v, a);
297 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000298 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000299 if ((x^a) >= 0 || (x^~b) >= 0)
300 return PyInt_FromLong(x);
301 if (err_ovf("integer subtraction"))
302 return NULL;
303 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
304 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305}
306
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000307/*
308Integer overflow checking used to be done using a double, but on 64
309bit machines (where both long and double are 64 bit) this fails
Thomas Wouters7e474022000-07-16 12:04:32 +0000310because the double doesn't have enough precision. John Tromp suggests
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000311the following algorithm:
312
313Suppose again we normalize a and b to be nonnegative.
314Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
315Now we test ah and bh against zero and get essentially 3 possible outcomes.
316
3171) both ah and bh > 0 : then report overflow
318
3192) both ah and bh = 0 : then compute a*b and report overflow if it comes out
320 negative
321
3223) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
323 compute al*bl and report overflow if it's negative
324 add (ah*bl)<<32 to al*bl and report overflow if
325 it's negative
326
327In case of no overflow the result is then negated if necessary.
328
329The majority of cases will be 2), in which case this method is the same as
330what I suggested before. If multiplication is expensive enough, then the
331other method is faster on case 3), but also more work to program, so I
332guess the above is the preferred solution.
333
334*/
335
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000336static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000337int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000338{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000339 long a, b, ah, bh, x, y;
340 int s = 1;
341
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000342 if (v->ob_type->tp_as_sequence &&
343 v->ob_type->tp_as_sequence->sq_repeat) {
344 /* sequence * int */
345 a = PyInt_AsLong(w);
346 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
347 }
348 else if (w->ob_type->tp_as_sequence &&
349 w->ob_type->tp_as_sequence->sq_repeat) {
350 /* int * sequence */
351 a = PyInt_AsLong(v);
352 return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
353 }
354
355 CONVERT_TO_LONG(v, a);
356 CONVERT_TO_LONG(w, b);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000357 ah = a >> (LONG_BIT/2);
358 bh = b >> (LONG_BIT/2);
359
360 /* Quick test for common case: two small positive ints */
361
362 if (ah == 0 && bh == 0) {
363 x = a*b;
364 if (x < 0)
365 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000366 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000367 }
368
369 /* Arrange that a >= b >= 0 */
370
371 if (a < 0) {
372 a = -a;
373 if (a < 0) {
374 /* Largest negative */
375 if (b == 0 || b == 1) {
376 x = a*b;
377 goto ok;
378 }
379 else
380 goto bad;
381 }
382 s = -s;
383 ah = a >> (LONG_BIT/2);
384 }
385 if (b < 0) {
386 b = -b;
387 if (b < 0) {
388 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000389 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000390 x = a*b;
391 goto ok;
392 }
393 else
394 goto bad;
395 }
396 s = -s;
397 bh = b >> (LONG_BIT/2);
398 }
399
400 /* 1) both ah and bh > 0 : then report overflow */
401
402 if (ah != 0 && bh != 0)
403 goto bad;
404
405 /* 2) both ah and bh = 0 : then compute a*b and report
406 overflow if it comes out negative */
407
408 if (ah == 0 && bh == 0) {
409 x = a*b;
410 if (x < 0)
411 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000412 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000413 }
414
415 if (a < b) {
416 /* Swap */
417 x = a;
418 a = b;
419 b = x;
420 ah = bh;
421 /* bh not used beyond this point */
422 }
423
424 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
425 it's >= 2^31
426 compute al*bl and report overflow if it's negative
427 add (ah*bl)<<32 to al*bl and report overflow if
428 it's negative
429 (NB b == bl in this case, and we make a = al) */
430
431 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000432 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000433 goto bad;
434 a &= (1L << (LONG_BIT/2)) - 1;
435 x = a*b;
436 if (x < 0)
437 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000438 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000439 if (x < 0)
440 goto bad;
441 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000443
444 bad:
Guido van Rossume27f7952001-08-23 02:59:04 +0000445 if (err_ovf("integer multiplication"))
446 return NULL;
447 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448}
449
Guido van Rossume27f7952001-08-23 02:59:04 +0000450/* Return type of i_divmod */
451enum divmod_result {
452 DIVMOD_OK, /* Correct result */
453 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
454 DIVMOD_ERROR /* Exception raised */
455};
456
457static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000458i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000459 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000461 long xdivy, xmody;
462
Tim Peters1dad6a82001-06-18 19:21:11 +0000463 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000465 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000466 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000468 /* (-sys.maxint-1)/-1 is the only overflow case. */
469 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000470 if (err_ovf("integer division"))
471 return DIVMOD_ERROR;
472 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000473 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000474 xdivy = x / y;
475 xmody = x - xdivy * y;
476 /* If the signs of x and y differ, and the remainder is non-0,
477 * C89 doesn't define whether xdivy is now the floor or the
478 * ceiling of the infinitely precise quotient. We want the floor,
479 * and we have it iff the remainder's sign matches y's.
480 */
481 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
482 xmody += y;
483 --xdivy;
484 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000485 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000486 *p_xdivy = xdivy;
487 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000488 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000489}
490
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000492int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000493{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000494 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000495 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000496 CONVERT_TO_LONG(x, xi);
497 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000498 switch (i_divmod(xi, yi, &d, &m)) {
499 case DIVMOD_OK:
500 return PyInt_FromLong(d);
501 case DIVMOD_OVERFLOW:
502 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
503 (PyObject *)y);
504 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000505 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000506 }
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_mod(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);
Guido van Rossume27f7952001-08-23 02:59:04 +0000516 switch (i_divmod(xi, yi, &d, &m)) {
517 case DIVMOD_OK:
518 return PyInt_FromLong(m);
519 case DIVMOD_OVERFLOW:
520 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
521 (PyObject *)y);
522 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000523 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000524 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000525}
526
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000528int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000529{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000530 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000531 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000532 CONVERT_TO_LONG(x, xi);
533 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000534 switch (i_divmod(xi, yi, &d, &m)) {
535 case DIVMOD_OK:
536 return Py_BuildValue("(ll)", d, m);
537 case DIVMOD_OVERFLOW:
538 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
539 (PyObject *)y);
540 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000541 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000542 }
Guido van Rossum00466951991-05-05 20:08:27 +0000543}
544
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000546int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000547{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000548 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000549 CONVERT_TO_LONG(v, iv);
550 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000551 if (iw < 0) {
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000552 /* Return a float. This works because we know that
553 this calls float_pow() which converts its
554 arguments to double. */
555 return PyFloat_Type.tp_as_number->nb_power(
556 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000557 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000558 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000559 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000560 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000562 "pow() arg 3 cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000563 return NULL;
564 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000565 }
566 /*
567 * XXX: The original exponentiation code stopped looping
568 * when temp hit zero; this code will continue onwards
569 * unnecessarily, but at least it won't cause any errors.
570 * Hopefully the speed improvement from the fast exponentiation
571 * will compensate for the slight inefficiency.
572 * XXX: Better handling of overflows is desperately needed.
573 */
574 temp = iv;
575 ix = 1;
576 while (iw > 0) {
577 prev = ix; /* Save value for overflow check */
578 if (iw & 1) {
579 ix = ix*temp;
580 if (temp == 0)
581 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000582 if (ix / temp != prev) {
583 if (err_ovf("integer exponentiation"))
584 return NULL;
585 return PyLong_Type.tp_as_number->nb_power(
586 (PyObject *)v,
587 (PyObject *)w,
588 (PyObject *)w);
589 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000590 }
591 iw >>= 1; /* Shift exponent down by 1 bit */
592 if (iw==0) break;
593 prev = temp;
594 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000595 if (prev!=0 && temp/prev!=prev) {
596 if (err_ovf("integer exponentiation"))
597 return NULL;
598 return PyLong_Type.tp_as_number->nb_power(
599 (PyObject *)v, (PyObject *)w, (PyObject *)z);
600 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000601 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000602 /* If we did a multiplication, perform a modulo */
603 ix = ix % iz;
604 temp = temp % iz;
605 }
606 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000607 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000608 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000609 switch (i_divmod(ix, iz, &div, &mod)) {
610 case DIVMOD_OK:
611 ix = mod;
612 break;
613 case DIVMOD_OVERFLOW:
614 return PyLong_Type.tp_as_number->nb_power(
615 (PyObject *)v, (PyObject *)w, (PyObject *)z);
616 default:
617 return NULL;
618 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000619 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000621}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000622
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000623static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000624int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625{
626 register long a, x;
627 a = v->ob_ival;
628 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000629 if (a < 0 && x < 0) {
630 if (err_ovf("integer negation"))
631 return NULL;
632 return PyNumber_Negative(PyLong_FromLong(a));
633 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000635}
636
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000638int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 Py_INCREF(v);
641 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642}
643
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000645int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000646{
647 if (v->ob_ival >= 0)
648 return int_pos(v);
649 else
650 return int_neg(v);
651}
652
Guido van Rossum0bff0151991-05-14 12:05:32 +0000653static int
Fred Drakea2f55112000-07-09 15:16:51 +0000654int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000655{
656 return v->ob_ival != 0;
657}
658
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000659static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000660int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000661{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000662 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000663}
664
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000666int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000667{
668 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000669 CONVERT_TO_LONG(v, a);
670 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000671 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000673 return NULL;
674 }
675 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000676 Py_INCREF(v);
677 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000678 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000679 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000680 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000681 }
Fred Drake1bc8fab2001-07-19 21:49:38 +0000682 a = (long)((unsigned long)a << b);
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_rshift(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 Rossumf3b351f1992-01-14 18:33:22 +0000692 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000694 return NULL;
695 }
696 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697 Py_INCREF(v);
698 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000699 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000700 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000701 if (a < 0)
702 a = -1;
703 else
704 a = 0;
705 }
706 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000707 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000708 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000710}
711
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000713int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000714{
715 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000716 CONVERT_TO_LONG(v, a);
717 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000719}
720
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000721static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000722int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000723{
724 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000725 CONVERT_TO_LONG(v, a);
726 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000728}
729
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000730static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000731int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000732{
733 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000734 CONVERT_TO_LONG(v, a);
735 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000737}
738
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000739static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000740int_true_divide(PyObject *v, PyObject *w)
741{
742 return PyFloat_Type.tp_as_number->nb_divide(v, w);
743}
744
745static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000746int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000747{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748 Py_INCREF(v);
749 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000750}
751
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000752static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000753int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000754{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000756}
757
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000759int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000760{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000761 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000762}
763
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000764static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000765int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000766{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000767 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000768 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000769 if (x == 0)
770 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000771 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000772 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000774}
775
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000776static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000777int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000778{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000779 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000780 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000781 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000782 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000783}
784
Tim Peters6d6c1a32001-08-02 04:15:00 +0000785static PyObject *
786int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
787{
788 PyObject *x = NULL;
789 int base = -909;
790 static char *kwlist[] = {"x", "base", 0};
791
792 assert(type == &PyInt_Type);
793 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
794 &x, &base))
795 return NULL;
796 if (x == NULL)
797 return PyInt_FromLong(0L);
798 if (base == -909)
799 return PyNumber_Int(x);
800 if (PyString_Check(x))
801 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000802#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000803 if (PyUnicode_Check(x))
804 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
805 PyUnicode_GET_SIZE(x),
806 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000807#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000808 PyErr_SetString(PyExc_TypeError,
809 "int() can't convert non-string with explicit base");
810 return NULL;
811}
812
813static char int_doc[] =
814"int(x[, base]) -> integer\n\
815\n\
816Convert a string or number to an integer, if possible. A floating point\n\
817argument will be truncated towards zero (this does not include a string\n\
818representation of a floating point number!) When converting a string, use\n\
819the optional base. It is an error to supply a base when converting a\n\
820non-string.";
821
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000822static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000823 (binaryfunc)int_add, /*nb_add*/
824 (binaryfunc)int_sub, /*nb_subtract*/
825 (binaryfunc)int_mul, /*nb_multiply*/
826 (binaryfunc)int_div, /*nb_divide*/
827 (binaryfunc)int_mod, /*nb_remainder*/
828 (binaryfunc)int_divmod, /*nb_divmod*/
829 (ternaryfunc)int_pow, /*nb_power*/
830 (unaryfunc)int_neg, /*nb_negative*/
831 (unaryfunc)int_pos, /*nb_positive*/
832 (unaryfunc)int_abs, /*nb_absolute*/
833 (inquiry)int_nonzero, /*nb_nonzero*/
834 (unaryfunc)int_invert, /*nb_invert*/
835 (binaryfunc)int_lshift, /*nb_lshift*/
836 (binaryfunc)int_rshift, /*nb_rshift*/
837 (binaryfunc)int_and, /*nb_and*/
838 (binaryfunc)int_xor, /*nb_xor*/
839 (binaryfunc)int_or, /*nb_or*/
840 0, /*nb_coerce*/
841 (unaryfunc)int_int, /*nb_int*/
842 (unaryfunc)int_long, /*nb_long*/
843 (unaryfunc)int_float, /*nb_float*/
844 (unaryfunc)int_oct, /*nb_oct*/
845 (unaryfunc)int_hex, /*nb_hex*/
846 0, /*nb_inplace_add*/
847 0, /*nb_inplace_subtract*/
848 0, /*nb_inplace_multiply*/
849 0, /*nb_inplace_divide*/
850 0, /*nb_inplace_remainder*/
851 0, /*nb_inplace_power*/
852 0, /*nb_inplace_lshift*/
853 0, /*nb_inplace_rshift*/
854 0, /*nb_inplace_and*/
855 0, /*nb_inplace_xor*/
856 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000857 (binaryfunc)int_div, /* nb_floor_divide */
858 int_true_divide, /* nb_true_divide */
859 0, /* nb_inplace_floor_divide */
860 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861};
862
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000863PyTypeObject PyInt_Type = {
864 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865 0,
866 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000867 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000868 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000869 (destructor)int_dealloc, /* tp_dealloc */
870 (printfunc)int_print, /* tp_print */
871 0, /* tp_getattr */
872 0, /* tp_setattr */
873 (cmpfunc)int_compare, /* tp_compare */
874 (reprfunc)int_repr, /* tp_repr */
875 &int_as_number, /* tp_as_number */
876 0, /* tp_as_sequence */
877 0, /* tp_as_mapping */
878 (hashfunc)int_hash, /* tp_hash */
879 0, /* tp_call */
880 0, /* tp_str */
881 PyObject_GenericGetAttr, /* tp_getattro */
882 0, /* tp_setattro */
883 0, /* tp_as_buffer */
884 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
885 int_doc, /* tp_doc */
886 0, /* tp_traverse */
887 0, /* tp_clear */
888 0, /* tp_richcompare */
889 0, /* tp_weaklistoffset */
890 0, /* tp_iter */
891 0, /* tp_iternext */
892 0, /* tp_methods */
893 0, /* tp_members */
894 0, /* tp_getset */
895 0, /* tp_base */
896 0, /* tp_dict */
897 0, /* tp_descr_get */
898 0, /* tp_descr_set */
899 0, /* tp_dictoffset */
900 0, /* tp_init */
901 0, /* tp_alloc */
902 int_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000903};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000904
905void
Fred Drakea2f55112000-07-09 15:16:51 +0000906PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000907{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000908 PyIntObject *p;
909 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000910 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000911 int bc, bf; /* block count, number of freed blocks */
912 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000913
Guido van Rossumda084ed1999-03-10 22:55:24 +0000914#if NSMALLNEGINTS + NSMALLPOSINTS > 0
915 PyIntObject **q;
916
917 i = NSMALLNEGINTS + NSMALLPOSINTS;
918 q = small_ints;
919 while (--i >= 0) {
920 Py_XDECREF(*q);
921 *q++ = NULL;
922 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000923#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000924 bc = 0;
925 bf = 0;
926 isum = 0;
927 list = block_list;
928 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000929 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000930 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000931 bc++;
932 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000933 for (i = 0, p = &list->objects[0];
934 i < N_INTOBJECTS;
935 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000936 if (PyInt_Check(p) && p->ob_refcnt != 0)
937 irem++;
938 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000939 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000940 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000941 list->next = block_list;
942 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000943 for (i = 0, p = &list->objects[0];
944 i < N_INTOBJECTS;
945 i++, p++) {
946 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
947 p->ob_type = (struct _typeobject *)
948 free_list;
949 free_list = p;
950 }
951#if NSMALLNEGINTS + NSMALLPOSINTS > 0
952 else if (-NSMALLNEGINTS <= p->ob_ival &&
953 p->ob_ival < NSMALLPOSINTS &&
954 small_ints[p->ob_ival +
955 NSMALLNEGINTS] == NULL) {
956 Py_INCREF(p);
957 small_ints[p->ob_ival +
958 NSMALLNEGINTS] = p;
959 }
960#endif
961 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000962 }
963 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000964 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000965 bf++;
966 }
967 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000968 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000969 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000970 if (!Py_VerboseFlag)
971 return;
972 fprintf(stderr, "# cleanup ints");
973 if (!isum) {
974 fprintf(stderr, "\n");
975 }
976 else {
977 fprintf(stderr,
978 ": %d unfreed int%s in %d out of %d block%s\n",
979 isum, isum == 1 ? "" : "s",
980 bc - bf, bc, bc == 1 ? "" : "s");
981 }
982 if (Py_VerboseFlag > 1) {
983 list = block_list;
984 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000985 for (i = 0, p = &list->objects[0];
986 i < N_INTOBJECTS;
987 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000988 if (PyInt_Check(p) && p->ob_refcnt != 0)
989 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000990 "# <int at %p, refcnt=%d, val=%ld>\n",
991 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000992 }
993 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000994 }
995 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000996}