blob: e7f618b34f77f3656587fb707a36560933fafea4 [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);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000185 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000186 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
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000205#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000206PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000207PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000208{
209 char buffer[256];
210
211 if (length >= sizeof(buffer)) {
212 PyErr_SetString(PyExc_ValueError,
213 "int() literal too large to convert");
214 return NULL;
215 }
216 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
217 return NULL;
218 return PyInt_FromString(buffer, NULL, base);
219}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000220#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000221
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222/* Methods */
223
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000224/* Integers are seen as the "smallest" of all numeric types and thus
225 don't have any knowledge about conversion of other types to
226 integers. */
227
228#define CONVERT_TO_LONG(obj, lng) \
229 if (PyInt_Check(obj)) { \
230 lng = PyInt_AS_LONG(obj); \
231 } \
232 else { \
233 Py_INCREF(Py_NotImplemented); \
234 return Py_NotImplemented; \
235 }
236
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000237/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000238static int
Fred Drakea2f55112000-07-09 15:16:51 +0000239int_print(PyIntObject *v, FILE *fp, int flags)
240 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241{
242 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000243 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244}
245
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000246static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000247int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248{
249 char buf[20];
250 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000251 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000252}
253
254static int
Fred Drakea2f55112000-07-09 15:16:51 +0000255int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256{
257 register long i = v->ob_ival;
258 register long j = w->ob_ival;
259 return (i < j) ? -1 : (i > j) ? 1 : 0;
260}
261
Guido van Rossum9bfef441993-03-29 10:43:31 +0000262static long
Fred Drakea2f55112000-07-09 15:16:51 +0000263int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000264{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000265 /* XXX If this is changed, you also need to change the way
266 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000267 long x = v -> ob_ival;
268 if (x == -1)
269 x = -2;
270 return x;
271}
272
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000274int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275{
276 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000277 CONVERT_TO_LONG(v, a);
278 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279 x = a + b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000280 if ((x^a) < 0 && (x^b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000281 return err_ovf("integer addition");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283}
284
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000285static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000286int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287{
288 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000289 CONVERT_TO_LONG(v, a);
290 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291 x = a - b;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000292 if ((x^a) < 0 && (x^~b) < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000293 return err_ovf("integer subtraction");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295}
296
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000297/*
298Integer overflow checking used to be done using a double, but on 64
299bit machines (where both long and double are 64 bit) this fails
Thomas Wouters7e474022000-07-16 12:04:32 +0000300because the double doesn't have enough precision. John Tromp suggests
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000301the following algorithm:
302
303Suppose again we normalize a and b to be nonnegative.
304Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
305Now we test ah and bh against zero and get essentially 3 possible outcomes.
306
3071) both ah and bh > 0 : then report overflow
308
3092) both ah and bh = 0 : then compute a*b and report overflow if it comes out
310 negative
311
3123) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
313 compute al*bl and report overflow if it's negative
314 add (ah*bl)<<32 to al*bl and report overflow if
315 it's negative
316
317In case of no overflow the result is then negated if necessary.
318
319The majority of cases will be 2), in which case this method is the same as
320what I suggested before. If multiplication is expensive enough, then the
321other method is faster on case 3), but also more work to program, so I
322guess the above is the preferred solution.
323
324*/
325
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000327int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000329 long a, b, ah, bh, x, y;
330 int s = 1;
331
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000332 if (v->ob_type->tp_as_sequence &&
333 v->ob_type->tp_as_sequence->sq_repeat) {
334 /* sequence * int */
335 a = PyInt_AsLong(w);
336 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
337 }
338 else if (w->ob_type->tp_as_sequence &&
339 w->ob_type->tp_as_sequence->sq_repeat) {
340 /* int * sequence */
341 a = PyInt_AsLong(v);
342 return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
343 }
344
345 CONVERT_TO_LONG(v, a);
346 CONVERT_TO_LONG(w, b);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000347 ah = a >> (LONG_BIT/2);
348 bh = b >> (LONG_BIT/2);
349
350 /* Quick test for common case: two small positive ints */
351
352 if (ah == 0 && bh == 0) {
353 x = a*b;
354 if (x < 0)
355 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000356 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000357 }
358
359 /* Arrange that a >= b >= 0 */
360
361 if (a < 0) {
362 a = -a;
363 if (a < 0) {
364 /* Largest negative */
365 if (b == 0 || b == 1) {
366 x = a*b;
367 goto ok;
368 }
369 else
370 goto bad;
371 }
372 s = -s;
373 ah = a >> (LONG_BIT/2);
374 }
375 if (b < 0) {
376 b = -b;
377 if (b < 0) {
378 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000379 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000380 x = a*b;
381 goto ok;
382 }
383 else
384 goto bad;
385 }
386 s = -s;
387 bh = b >> (LONG_BIT/2);
388 }
389
390 /* 1) both ah and bh > 0 : then report overflow */
391
392 if (ah != 0 && bh != 0)
393 goto bad;
394
395 /* 2) both ah and bh = 0 : then compute a*b and report
396 overflow if it comes out negative */
397
398 if (ah == 0 && bh == 0) {
399 x = a*b;
400 if (x < 0)
401 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000403 }
404
405 if (a < b) {
406 /* Swap */
407 x = a;
408 a = b;
409 b = x;
410 ah = bh;
411 /* bh not used beyond this point */
412 }
413
414 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
415 it's >= 2^31
416 compute al*bl and report overflow if it's negative
417 add (ah*bl)<<32 to al*bl and report overflow if
418 it's negative
419 (NB b == bl in this case, and we make a = al) */
420
421 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000422 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000423 goto bad;
424 a &= (1L << (LONG_BIT/2)) - 1;
425 x = a*b;
426 if (x < 0)
427 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000428 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000429 if (x < 0)
430 goto bad;
431 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000433
434 bad:
435 return err_ovf("integer multiplication");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436}
437
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000438static int
Tim Peters1dad6a82001-06-18 19:21:11 +0000439i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000440 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000442 long xdivy, xmody;
443
Tim Peters1dad6a82001-06-18 19:21:11 +0000444 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000446 "integer division or modulo by zero");
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000447 return -1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000449 /* (-sys.maxint-1)/-1 is the only overflow case. */
450 if (y == -1 && x < 0 && x == -x) {
451 err_ovf("integer division");
452 return -1;
Guido van Rossum00466951991-05-05 20:08:27 +0000453 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000454 xdivy = x / y;
455 xmody = x - xdivy * y;
456 /* If the signs of x and y differ, and the remainder is non-0,
457 * C89 doesn't define whether xdivy is now the floor or the
458 * ceiling of the infinitely precise quotient. We want the floor,
459 * and we have it iff the remainder's sign matches y's.
460 */
461 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
462 xmody += y;
463 --xdivy;
464 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000465 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000466 *p_xdivy = xdivy;
467 *p_xmody = xmody;
468 return 0;
469}
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000472int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000473{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000474 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000475 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000476 CONVERT_TO_LONG(x, xi);
477 CONVERT_TO_LONG(y, yi);
478 if (i_divmod(xi, yi, &d, &m) < 0)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000479 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480 return PyInt_FromLong(d);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000481}
482
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000484int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000485{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000486 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000487 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000488 CONVERT_TO_LONG(x, xi);
489 CONVERT_TO_LONG(y, yi);
490 if (i_divmod(xi, yi, &d, &m) < 0)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000491 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492 return PyInt_FromLong(m);
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000493}
494
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000495static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000496int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000497{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000498 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000499 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000500 CONVERT_TO_LONG(x, xi);
501 CONVERT_TO_LONG(y, yi);
502 if (i_divmod(xi, yi, &d, &m) < 0)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000503 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000504 return Py_BuildValue("(ll)", d, m);
Guido van Rossum00466951991-05-05 20:08:27 +0000505}
506
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000507static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000508int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000509{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000510#if 1
Guido van Rossum9478dd41996-12-06 20:14:43 +0000511 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000512 CONVERT_TO_LONG(v, iv);
513 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000514 if (iw < 0) {
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000515 /* Return a float. This works because we know that
516 this calls float_pow() which converts its
517 arguments to double. */
518 return PyFloat_Type.tp_as_number->nb_power(
519 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000520 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000521 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000522 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000523 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000525 "pow() arg 3 cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000526 return NULL;
527 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000528 }
529 /*
530 * XXX: The original exponentiation code stopped looping
531 * when temp hit zero; this code will continue onwards
532 * unnecessarily, but at least it won't cause any errors.
533 * Hopefully the speed improvement from the fast exponentiation
534 * will compensate for the slight inefficiency.
535 * XXX: Better handling of overflows is desperately needed.
536 */
537 temp = iv;
538 ix = 1;
539 while (iw > 0) {
540 prev = ix; /* Save value for overflow check */
541 if (iw & 1) {
542 ix = ix*temp;
543 if (temp == 0)
544 break; /* Avoid ix / 0 */
545 if (ix / temp != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000546 return err_ovf("integer exponentiation");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000547 }
548 iw >>= 1; /* Shift exponent down by 1 bit */
549 if (iw==0) break;
550 prev = temp;
551 temp *= temp; /* Square the value of temp */
552 if (prev!=0 && temp/prev!=prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000553 return err_ovf("integer exponentiation");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000554 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000555 /* If we did a multiplication, perform a modulo */
556 ix = ix % iz;
557 temp = temp % iz;
558 }
559 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000560 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000561 long div, mod;
562 if (i_divmod(ix, iz, &div, &mod) < 0)
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000563 return(NULL);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000564 ix=mod;
565 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000566 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000567#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568 register long iv, iw, ix;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000569 CONVERT_TO_LONG(v, iv);
570 CONVERT_TO_LONG(w, iw);
Guido van Rossum00466951991-05-05 20:08:27 +0000571 if (iw < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572 PyErr_SetString(PyExc_ValueError,
573 "integer to the negative power");
Guido van Rossum00466951991-05-05 20:08:27 +0000574 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000576 if ((PyObject *)z != Py_None) {
577 PyErr_SetString(PyExc_TypeError,
578 "pow(int, int, int) not yet supported");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000579 return NULL;
580 }
Guido van Rossum00466951991-05-05 20:08:27 +0000581 ix = 1;
582 while (--iw >= 0) {
583 long prev = ix;
584 ix = ix * iv;
585 if (iv == 0)
586 break; /* 0 to some power -- avoid ix / 0 */
587 if (ix / iv != prev)
Guido van Rossumfb4574e2000-02-15 14:51:46 +0000588 return err_ovf("integer exponentiation");
Guido van Rossum00466951991-05-05 20:08:27 +0000589 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000591#endif
592}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000593
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000594static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000595int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000596{
597 register long a, x;
598 a = v->ob_ival;
599 x = -a;
Guido van Rossum165e67e1990-10-14 20:02:26 +0000600 if (a < 0 && x < 0)
Guido van Rossum3a628451991-12-10 13:57:36 +0000601 return err_ovf("integer negation");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000603}
604
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000605static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000606int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000608 Py_INCREF(v);
609 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000610}
611
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000613int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000614{
615 if (v->ob_ival >= 0)
616 return int_pos(v);
617 else
618 return int_neg(v);
619}
620
Guido van Rossum0bff0151991-05-14 12:05:32 +0000621static int
Fred Drakea2f55112000-07-09 15:16:51 +0000622int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000623{
624 return v->ob_ival != 0;
625}
626
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000628int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000629{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000631}
632
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000634int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000635{
636 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000637 CONVERT_TO_LONG(v, a);
638 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000639 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000641 return NULL;
642 }
643 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644 Py_INCREF(v);
645 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000646 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000647 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000649 }
Fred Drake1bc8fab2001-07-19 21:49:38 +0000650 a = (long)((unsigned long)a << b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000651 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000652}
653
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000655int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000656{
657 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000658 CONVERT_TO_LONG(v, a);
659 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000660 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000662 return NULL;
663 }
664 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 Py_INCREF(v);
666 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000667 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000668 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000669 if (a < 0)
670 a = -1;
671 else
672 a = 0;
673 }
674 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000675 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000676 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000678}
679
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000680static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000681int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000682{
683 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000684 CONVERT_TO_LONG(v, a);
685 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000686 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000687}
688
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000689static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000690int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000691{
692 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000693 CONVERT_TO_LONG(v, a);
694 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000696}
697
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000699int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000700{
701 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000702 CONVERT_TO_LONG(v, a);
703 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000704 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000705}
706
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000708int_true_divide(PyObject *v, PyObject *w)
709{
710 return PyFloat_Type.tp_as_number->nb_divide(v, w);
711}
712
713static 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
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753static PyObject *
754int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
755{
756 PyObject *x = NULL;
757 int base = -909;
758 static char *kwlist[] = {"x", "base", 0};
759
760 assert(type == &PyInt_Type);
761 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
762 &x, &base))
763 return NULL;
764 if (x == NULL)
765 return PyInt_FromLong(0L);
766 if (base == -909)
767 return PyNumber_Int(x);
768 if (PyString_Check(x))
769 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000770#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771 if (PyUnicode_Check(x))
772 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
773 PyUnicode_GET_SIZE(x),
774 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000775#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000776 PyErr_SetString(PyExc_TypeError,
777 "int() can't convert non-string with explicit base");
778 return NULL;
779}
780
781static char int_doc[] =
782"int(x[, base]) -> integer\n\
783\n\
784Convert a string or number to an integer, if possible. A floating point\n\
785argument will be truncated towards zero (this does not include a string\n\
786representation of a floating point number!) When converting a string, use\n\
787the optional base. It is an error to supply a base when converting a\n\
788non-string.";
789
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000791 (binaryfunc)int_add, /*nb_add*/
792 (binaryfunc)int_sub, /*nb_subtract*/
793 (binaryfunc)int_mul, /*nb_multiply*/
794 (binaryfunc)int_div, /*nb_divide*/
795 (binaryfunc)int_mod, /*nb_remainder*/
796 (binaryfunc)int_divmod, /*nb_divmod*/
797 (ternaryfunc)int_pow, /*nb_power*/
798 (unaryfunc)int_neg, /*nb_negative*/
799 (unaryfunc)int_pos, /*nb_positive*/
800 (unaryfunc)int_abs, /*nb_absolute*/
801 (inquiry)int_nonzero, /*nb_nonzero*/
802 (unaryfunc)int_invert, /*nb_invert*/
803 (binaryfunc)int_lshift, /*nb_lshift*/
804 (binaryfunc)int_rshift, /*nb_rshift*/
805 (binaryfunc)int_and, /*nb_and*/
806 (binaryfunc)int_xor, /*nb_xor*/
807 (binaryfunc)int_or, /*nb_or*/
808 0, /*nb_coerce*/
809 (unaryfunc)int_int, /*nb_int*/
810 (unaryfunc)int_long, /*nb_long*/
811 (unaryfunc)int_float, /*nb_float*/
812 (unaryfunc)int_oct, /*nb_oct*/
813 (unaryfunc)int_hex, /*nb_hex*/
814 0, /*nb_inplace_add*/
815 0, /*nb_inplace_subtract*/
816 0, /*nb_inplace_multiply*/
817 0, /*nb_inplace_divide*/
818 0, /*nb_inplace_remainder*/
819 0, /*nb_inplace_power*/
820 0, /*nb_inplace_lshift*/
821 0, /*nb_inplace_rshift*/
822 0, /*nb_inplace_and*/
823 0, /*nb_inplace_xor*/
824 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000825 (binaryfunc)int_div, /* nb_floor_divide */
826 int_true_divide, /* nb_true_divide */
827 0, /* nb_inplace_floor_divide */
828 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000829};
830
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000831PyTypeObject PyInt_Type = {
832 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000833 0,
834 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000835 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000836 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000837 (destructor)int_dealloc, /* tp_dealloc */
838 (printfunc)int_print, /* tp_print */
839 0, /* tp_getattr */
840 0, /* tp_setattr */
841 (cmpfunc)int_compare, /* tp_compare */
842 (reprfunc)int_repr, /* tp_repr */
843 &int_as_number, /* tp_as_number */
844 0, /* tp_as_sequence */
845 0, /* tp_as_mapping */
846 (hashfunc)int_hash, /* tp_hash */
847 0, /* tp_call */
848 0, /* tp_str */
849 PyObject_GenericGetAttr, /* tp_getattro */
850 0, /* tp_setattro */
851 0, /* tp_as_buffer */
852 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
853 int_doc, /* tp_doc */
854 0, /* tp_traverse */
855 0, /* tp_clear */
856 0, /* tp_richcompare */
857 0, /* tp_weaklistoffset */
858 0, /* tp_iter */
859 0, /* tp_iternext */
860 0, /* tp_methods */
861 0, /* tp_members */
862 0, /* tp_getset */
863 0, /* tp_base */
864 0, /* tp_dict */
865 0, /* tp_descr_get */
866 0, /* tp_descr_set */
867 0, /* tp_dictoffset */
868 0, /* tp_init */
869 0, /* tp_alloc */
870 int_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000871};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000872
873void
Fred Drakea2f55112000-07-09 15:16:51 +0000874PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000875{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000876 PyIntObject *p;
877 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000878 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000879 int bc, bf; /* block count, number of freed blocks */
880 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000881
Guido van Rossumda084ed1999-03-10 22:55:24 +0000882#if NSMALLNEGINTS + NSMALLPOSINTS > 0
883 PyIntObject **q;
884
885 i = NSMALLNEGINTS + NSMALLPOSINTS;
886 q = small_ints;
887 while (--i >= 0) {
888 Py_XDECREF(*q);
889 *q++ = NULL;
890 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000891#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000892 bc = 0;
893 bf = 0;
894 isum = 0;
895 list = block_list;
896 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000897 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000898 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000899 bc++;
900 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000901 for (i = 0, p = &list->objects[0];
902 i < N_INTOBJECTS;
903 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000904 if (PyInt_Check(p) && p->ob_refcnt != 0)
905 irem++;
906 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000907 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000908 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000909 list->next = block_list;
910 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000911 for (i = 0, p = &list->objects[0];
912 i < N_INTOBJECTS;
913 i++, p++) {
914 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
915 p->ob_type = (struct _typeobject *)
916 free_list;
917 free_list = p;
918 }
919#if NSMALLNEGINTS + NSMALLPOSINTS > 0
920 else if (-NSMALLNEGINTS <= p->ob_ival &&
921 p->ob_ival < NSMALLPOSINTS &&
922 small_ints[p->ob_ival +
923 NSMALLNEGINTS] == NULL) {
924 Py_INCREF(p);
925 small_ints[p->ob_ival +
926 NSMALLNEGINTS] = p;
927 }
928#endif
929 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000930 }
931 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000932 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000933 bf++;
934 }
935 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000936 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000937 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000938 if (!Py_VerboseFlag)
939 return;
940 fprintf(stderr, "# cleanup ints");
941 if (!isum) {
942 fprintf(stderr, "\n");
943 }
944 else {
945 fprintf(stderr,
946 ": %d unfreed int%s in %d out of %d block%s\n",
947 isum, isum == 1 ? "" : "s",
948 bc - bf, bc, bc == 1 ? "" : "s");
949 }
950 if (Py_VerboseFlag > 1) {
951 list = block_list;
952 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000953 for (i = 0, p = &list->objects[0];
954 i < N_INTOBJECTS;
955 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000956 if (PyInt_Check(p) && p->ob_refcnt != 0)
957 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000958 "# <int at %p, refcnt=%d, val=%ld>\n",
959 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000960 }
961 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000962 }
963 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000964}