blob: 77d7e387bd85828cc96e675e0821ce147c1c9357 [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) {
Guido van Rossum0b131162001-08-23 21:32:40 +000030 if (PyErr_ExceptionMatches(PyExc_OverflowWarning))
31 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossume27f7952001-08-23 02:59:04 +000032 return 1;
33 }
34 else
35 return 0;
Guido van Rossum165e67e1990-10-14 20:02:26 +000036}
37
Guido van Rossum3f5da241990-12-20 15:06:42 +000038/* Integers are quite normal objects, to make object handling uniform.
39 (Using odd pointers to represent integers would save much space
40 but require extra checks for this special case throughout the code.)
41 Since, a typical Python program spends much of its time allocating
42 and deallocating integers, these operations should be very fast.
43 Therefore we use a dedicated allocation scheme with a much lower
44 overhead (in space and time) than straight malloc(): a simple
45 dedicated free list, filled when necessary with memory from malloc().
46*/
47
48#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000049#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
50#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000051
Guido van Rossum3fce8831999-03-12 19:43:17 +000052struct _intblock {
53 struct _intblock *next;
54 PyIntObject objects[N_INTOBJECTS];
55};
56
57typedef struct _intblock PyIntBlock;
58
59static PyIntBlock *block_list = NULL;
60static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000061
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000063fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000064{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065 PyIntObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000066 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
67 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000068 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000069 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000070 ((PyIntBlock *)p)->next = block_list;
71 block_list = (PyIntBlock *)p;
72 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000073 q = p + N_INTOBJECTS;
74 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000075 q->ob_type = (struct _typeobject *)(q-1);
76 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000077 return p + N_INTOBJECTS - 1;
78}
79
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000080#ifndef NSMALLPOSINTS
81#define NSMALLPOSINTS 100
82#endif
83#ifndef NSMALLNEGINTS
84#define NSMALLNEGINTS 1
85#endif
86#if NSMALLNEGINTS + NSMALLPOSINTS > 0
87/* References to small integers are saved in this array so that they
88 can be shared.
89 The integers that are saved are those in the range
90 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
91*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000093#endif
94#ifdef COUNT_ALLOCS
95int quick_int_allocs, quick_neg_int_allocs;
96#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000097
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000099PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000101 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000102#if NSMALLNEGINTS + NSMALLPOSINTS > 0
103 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
104 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000105 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000106#ifdef COUNT_ALLOCS
107 if (ival >= 0)
108 quick_int_allocs++;
109 else
110 quick_neg_int_allocs++;
111#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000113 }
114#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000115 if (free_list == NULL) {
116 if ((free_list = fill_free_list()) == NULL)
117 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000119 /* PyObject_New is inlined */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000121 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000122 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000123 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000124#if NSMALLNEGINTS + NSMALLPOSINTS > 0
125 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
126 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000128 small_ints[ival + NSMALLNEGINTS] = v;
129 }
130#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000131 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132}
133
134static void
Fred Drakea2f55112000-07-09 15:16:51 +0000135int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000136{
Guido van Rossumda084ed1999-03-10 22:55:24 +0000137 v->ob_type = (struct _typeobject *)free_list;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000138 free_list = v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139}
140
141long
Fred Drakea2f55112000-07-09 15:16:51 +0000142PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000144 PyNumberMethods *nb;
145 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000146 long val;
147
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 if (op && PyInt_Check(op))
149 return PyInt_AS_LONG((PyIntObject*) op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000150
151 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
152 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000153 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154 return -1;
155 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000156
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000158 if (io == NULL)
159 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160 if (!PyInt_Check(io)) {
161 PyErr_SetString(PyExc_TypeError,
162 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000163 return -1;
164 }
165
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000166 val = PyInt_AS_LONG(io);
167 Py_DECREF(io);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000168
169 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000172PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000173PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000174{
175 char *end;
176 long x;
177 char buffer[256]; /* For errors */
178
179 if ((base != 0 && base < 2) || base > 36) {
Fred Drake661ea262000-10-24 19:57:45 +0000180 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000181 return NULL;
182 }
183
184 while (*s && isspace(Py_CHARMASK(*s)))
185 s++;
186 errno = 0;
187 if (base == 0 && s[0] == '0')
188 x = (long) PyOS_strtoul(s, &end, base);
189 else
190 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000191 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000192 goto bad;
193 while (*end && isspace(Py_CHARMASK(*end)))
194 end++;
195 if (*end != '\0') {
196 bad:
197 sprintf(buffer, "invalid literal for int(): %.200s", s);
198 PyErr_SetString(PyExc_ValueError, buffer);
199 return NULL;
200 }
201 else if (errno != 0) {
202 sprintf(buffer, "int() literal too large: %.200s", s);
203 PyErr_SetString(PyExc_ValueError, buffer);
204 return NULL;
205 }
206 if (pend)
207 *pend = end;
208 return PyInt_FromLong(x);
209}
210
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000211#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000212PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000213PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000214{
215 char buffer[256];
216
217 if (length >= sizeof(buffer)) {
218 PyErr_SetString(PyExc_ValueError,
219 "int() literal too large to convert");
220 return NULL;
221 }
222 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
223 return NULL;
224 return PyInt_FromString(buffer, NULL, base);
225}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000226#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000227
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228/* Methods */
229
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000230/* Integers are seen as the "smallest" of all numeric types and thus
231 don't have any knowledge about conversion of other types to
232 integers. */
233
234#define CONVERT_TO_LONG(obj, lng) \
235 if (PyInt_Check(obj)) { \
236 lng = PyInt_AS_LONG(obj); \
237 } \
238 else { \
239 Py_INCREF(Py_NotImplemented); \
240 return Py_NotImplemented; \
241 }
242
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000243/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000244static int
Fred Drakea2f55112000-07-09 15:16:51 +0000245int_print(PyIntObject *v, FILE *fp, int flags)
246 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000247{
248 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000249 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250}
251
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000253int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254{
255 char buf[20];
256 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000257 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258}
259
260static int
Fred Drakea2f55112000-07-09 15:16:51 +0000261int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262{
263 register long i = v->ob_ival;
264 register long j = w->ob_ival;
265 return (i < j) ? -1 : (i > j) ? 1 : 0;
266}
267
Guido van Rossum9bfef441993-03-29 10:43:31 +0000268static long
Fred Drakea2f55112000-07-09 15:16:51 +0000269int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000270{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000271 /* XXX If this is changed, you also need to change the way
272 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000273 long x = v -> ob_ival;
274 if (x == -1)
275 x = -2;
276 return x;
277}
278
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000279static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000280int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281{
282 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000283 CONVERT_TO_LONG(v, a);
284 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000286 if ((x^a) >= 0 || (x^b) >= 0)
287 return PyInt_FromLong(x);
288 if (err_ovf("integer addition"))
289 return NULL;
290 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
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 Rossume27f7952001-08-23 02:59:04 +0000300 if ((x^a) >= 0 || (x^~b) >= 0)
301 return PyInt_FromLong(x);
302 if (err_ovf("integer subtraction"))
303 return NULL;
304 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
305 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306}
307
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000308/*
309Integer overflow checking used to be done using a double, but on 64
310bit machines (where both long and double are 64 bit) this fails
Thomas Wouters7e474022000-07-16 12:04:32 +0000311because the double doesn't have enough precision. John Tromp suggests
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000312the following algorithm:
313
314Suppose again we normalize a and b to be nonnegative.
315Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
316Now we test ah and bh against zero and get essentially 3 possible outcomes.
317
3181) both ah and bh > 0 : then report overflow
319
3202) both ah and bh = 0 : then compute a*b and report overflow if it comes out
321 negative
322
3233) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
324 compute al*bl and report overflow if it's negative
325 add (ah*bl)<<32 to al*bl and report overflow if
326 it's negative
327
328In case of no overflow the result is then negated if necessary.
329
330The majority of cases will be 2), in which case this method is the same as
331what I suggested before. If multiplication is expensive enough, then the
332other method is faster on case 3), but also more work to program, so I
333guess the above is the preferred solution.
334
335*/
336
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000337static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000338int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000339{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000340 long a, b, ah, bh, x, y;
341 int s = 1;
342
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000343 if (v->ob_type->tp_as_sequence &&
344 v->ob_type->tp_as_sequence->sq_repeat) {
345 /* sequence * int */
346 a = PyInt_AsLong(w);
347 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
348 }
349 else if (w->ob_type->tp_as_sequence &&
350 w->ob_type->tp_as_sequence->sq_repeat) {
351 /* int * sequence */
352 a = PyInt_AsLong(v);
353 return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
354 }
355
356 CONVERT_TO_LONG(v, a);
357 CONVERT_TO_LONG(w, b);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000358 ah = a >> (LONG_BIT/2);
359 bh = b >> (LONG_BIT/2);
360
361 /* Quick test for common case: two small positive ints */
362
363 if (ah == 0 && bh == 0) {
364 x = a*b;
365 if (x < 0)
366 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000367 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000368 }
369
370 /* Arrange that a >= b >= 0 */
371
372 if (a < 0) {
373 a = -a;
374 if (a < 0) {
375 /* Largest negative */
376 if (b == 0 || b == 1) {
377 x = a*b;
378 goto ok;
379 }
380 else
381 goto bad;
382 }
383 s = -s;
384 ah = a >> (LONG_BIT/2);
385 }
386 if (b < 0) {
387 b = -b;
388 if (b < 0) {
389 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000390 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000391 x = a*b;
392 goto ok;
393 }
394 else
395 goto bad;
396 }
397 s = -s;
398 bh = b >> (LONG_BIT/2);
399 }
400
401 /* 1) both ah and bh > 0 : then report overflow */
402
403 if (ah != 0 && bh != 0)
404 goto bad;
405
406 /* 2) both ah and bh = 0 : then compute a*b and report
407 overflow if it comes out negative */
408
409 if (ah == 0 && bh == 0) {
410 x = a*b;
411 if (x < 0)
412 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000414 }
415
416 if (a < b) {
417 /* Swap */
418 x = a;
419 a = b;
420 b = x;
421 ah = bh;
422 /* bh not used beyond this point */
423 }
424
425 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
426 it's >= 2^31
427 compute al*bl and report overflow if it's negative
428 add (ah*bl)<<32 to al*bl and report overflow if
429 it's negative
430 (NB b == bl in this case, and we make a = al) */
431
432 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000433 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000434 goto bad;
435 a &= (1L << (LONG_BIT/2)) - 1;
436 x = a*b;
437 if (x < 0)
438 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000439 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000440 if (x < 0)
441 goto bad;
442 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000443 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000444
445 bad:
Guido van Rossume27f7952001-08-23 02:59:04 +0000446 if (err_ovf("integer multiplication"))
447 return NULL;
448 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449}
450
Guido van Rossume27f7952001-08-23 02:59:04 +0000451/* Return type of i_divmod */
452enum divmod_result {
453 DIVMOD_OK, /* Correct result */
454 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
455 DIVMOD_ERROR /* Exception raised */
456};
457
458static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000459i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000460 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000462 long xdivy, xmody;
463
Tim Peters1dad6a82001-06-18 19:21:11 +0000464 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000465 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000466 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000467 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000469 /* (-sys.maxint-1)/-1 is the only overflow case. */
470 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000471 if (err_ovf("integer division"))
472 return DIVMOD_ERROR;
473 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000474 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000475 xdivy = x / y;
476 xmody = x - xdivy * y;
477 /* If the signs of x and y differ, and the remainder is non-0,
478 * C89 doesn't define whether xdivy is now the floor or the
479 * ceiling of the infinitely precise quotient. We want the floor,
480 * and we have it iff the remainder's sign matches y's.
481 */
482 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
483 xmody += y;
484 --xdivy;
485 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000486 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000487 *p_xdivy = xdivy;
488 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000489 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000490}
491
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000493int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000494{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000495 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000496 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000497 CONVERT_TO_LONG(x, xi);
498 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000499 switch (i_divmod(xi, yi, &d, &m)) {
500 case DIVMOD_OK:
501 return PyInt_FromLong(d);
502 case DIVMOD_OVERFLOW:
503 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
504 (PyObject *)y);
505 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000506 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000507 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000508}
509
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000511int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000512{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000513 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000514 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000515 CONVERT_TO_LONG(x, xi);
516 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000517 switch (i_divmod(xi, yi, &d, &m)) {
518 case DIVMOD_OK:
519 return PyInt_FromLong(m);
520 case DIVMOD_OVERFLOW:
521 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
522 (PyObject *)y);
523 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000524 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000525 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000526}
527
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000528static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000529int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000530{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000531 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000532 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000533 CONVERT_TO_LONG(x, xi);
534 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000535 switch (i_divmod(xi, yi, &d, &m)) {
536 case DIVMOD_OK:
537 return Py_BuildValue("(ll)", d, m);
538 case DIVMOD_OVERFLOW:
539 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
540 (PyObject *)y);
541 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000542 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000543 }
Guido van Rossum00466951991-05-05 20:08:27 +0000544}
545
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000547int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000549 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000550 CONVERT_TO_LONG(v, iv);
551 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000552 if (iw < 0) {
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000553 /* Return a float. This works because we know that
554 this calls float_pow() which converts its
555 arguments to double. */
556 return PyFloat_Type.tp_as_number->nb_power(
557 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000558 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000559 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000560 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000561 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000563 "pow() arg 3 cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000564 return NULL;
565 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000566 }
567 /*
568 * XXX: The original exponentiation code stopped looping
569 * when temp hit zero; this code will continue onwards
570 * unnecessarily, but at least it won't cause any errors.
571 * Hopefully the speed improvement from the fast exponentiation
572 * will compensate for the slight inefficiency.
573 * XXX: Better handling of overflows is desperately needed.
574 */
575 temp = iv;
576 ix = 1;
577 while (iw > 0) {
578 prev = ix; /* Save value for overflow check */
579 if (iw & 1) {
580 ix = ix*temp;
581 if (temp == 0)
582 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000583 if (ix / temp != prev) {
584 if (err_ovf("integer exponentiation"))
585 return NULL;
586 return PyLong_Type.tp_as_number->nb_power(
587 (PyObject *)v,
588 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000589 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000590 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000591 }
592 iw >>= 1; /* Shift exponent down by 1 bit */
593 if (iw==0) break;
594 prev = temp;
595 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000596 if (prev!=0 && temp/prev!=prev) {
597 if (err_ovf("integer exponentiation"))
598 return NULL;
599 return PyLong_Type.tp_as_number->nb_power(
600 (PyObject *)v, (PyObject *)w, (PyObject *)z);
601 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000602 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000603 /* If we did a multiplication, perform a modulo */
604 ix = ix % iz;
605 temp = temp % iz;
606 }
607 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000608 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000609 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000610 switch (i_divmod(ix, iz, &div, &mod)) {
611 case DIVMOD_OK:
612 ix = mod;
613 break;
614 case DIVMOD_OVERFLOW:
615 return PyLong_Type.tp_as_number->nb_power(
616 (PyObject *)v, (PyObject *)w, (PyObject *)z);
617 default:
618 return NULL;
619 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000620 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000622}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000623
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000625int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626{
627 register long a, x;
628 a = v->ob_ival;
629 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000630 if (a < 0 && x < 0) {
631 if (err_ovf("integer negation"))
632 return NULL;
633 return PyNumber_Negative(PyLong_FromLong(a));
634 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000635 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000636}
637
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000638static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000639int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000640{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641 Py_INCREF(v);
642 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643}
644
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000646int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000647{
648 if (v->ob_ival >= 0)
649 return int_pos(v);
650 else
651 return int_neg(v);
652}
653
Guido van Rossum0bff0151991-05-14 12:05:32 +0000654static int
Fred Drakea2f55112000-07-09 15:16:51 +0000655int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000656{
657 return v->ob_ival != 0;
658}
659
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000661int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000662{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000663 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000664}
665
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000667int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000668{
669 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000670 CONVERT_TO_LONG(v, a);
671 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000672 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000673 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000674 return NULL;
675 }
676 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677 Py_INCREF(v);
678 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000679 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000680 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000681 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000682 }
Fred Drake1bc8fab2001-07-19 21:49:38 +0000683 a = (long)((unsigned long)a << b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000685}
686
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000687static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000688int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000689{
690 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000691 CONVERT_TO_LONG(v, a);
692 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000693 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000694 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000695 return NULL;
696 }
697 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698 Py_INCREF(v);
699 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000700 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000701 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000702 if (a < 0)
703 a = -1;
704 else
705 a = 0;
706 }
707 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000708 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000709 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000711}
712
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000714int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000715{
716 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000717 CONVERT_TO_LONG(v, a);
718 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000719 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000720}
721
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000722static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000723int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000724{
725 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000726 CONVERT_TO_LONG(v, a);
727 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000728 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000729}
730
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000732int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000733{
734 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000735 CONVERT_TO_LONG(v, a);
736 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000738}
739
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000740static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000741int_true_divide(PyObject *v, PyObject *w)
742{
743 return PyFloat_Type.tp_as_number->nb_divide(v, w);
744}
745
746static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000747int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000748{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000749 Py_INCREF(v);
750 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000751}
752
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000754int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000755{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000757}
758
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000759static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000760int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000761{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000762 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000763}
764
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000766int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000767{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000768 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000769 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000770 if (x == 0)
771 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000772 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000773 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000775}
776
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000777static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000778int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000779{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000780 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000781 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000782 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000784}
785
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786static PyObject *
787int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
788{
789 PyObject *x = NULL;
790 int base = -909;
791 static char *kwlist[] = {"x", "base", 0};
792
793 assert(type == &PyInt_Type);
794 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
795 &x, &base))
796 return NULL;
797 if (x == NULL)
798 return PyInt_FromLong(0L);
799 if (base == -909)
800 return PyNumber_Int(x);
801 if (PyString_Check(x))
802 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000803#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000804 if (PyUnicode_Check(x))
805 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
806 PyUnicode_GET_SIZE(x),
807 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000808#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000809 PyErr_SetString(PyExc_TypeError,
810 "int() can't convert non-string with explicit base");
811 return NULL;
812}
813
814static char int_doc[] =
815"int(x[, base]) -> integer\n\
816\n\
817Convert a string or number to an integer, if possible. A floating point\n\
818argument will be truncated towards zero (this does not include a string\n\
819representation of a floating point number!) When converting a string, use\n\
820the optional base. It is an error to supply a base when converting a\n\
821non-string.";
822
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000824 (binaryfunc)int_add, /*nb_add*/
825 (binaryfunc)int_sub, /*nb_subtract*/
826 (binaryfunc)int_mul, /*nb_multiply*/
827 (binaryfunc)int_div, /*nb_divide*/
828 (binaryfunc)int_mod, /*nb_remainder*/
829 (binaryfunc)int_divmod, /*nb_divmod*/
830 (ternaryfunc)int_pow, /*nb_power*/
831 (unaryfunc)int_neg, /*nb_negative*/
832 (unaryfunc)int_pos, /*nb_positive*/
833 (unaryfunc)int_abs, /*nb_absolute*/
834 (inquiry)int_nonzero, /*nb_nonzero*/
835 (unaryfunc)int_invert, /*nb_invert*/
836 (binaryfunc)int_lshift, /*nb_lshift*/
837 (binaryfunc)int_rshift, /*nb_rshift*/
838 (binaryfunc)int_and, /*nb_and*/
839 (binaryfunc)int_xor, /*nb_xor*/
840 (binaryfunc)int_or, /*nb_or*/
841 0, /*nb_coerce*/
842 (unaryfunc)int_int, /*nb_int*/
843 (unaryfunc)int_long, /*nb_long*/
844 (unaryfunc)int_float, /*nb_float*/
845 (unaryfunc)int_oct, /*nb_oct*/
846 (unaryfunc)int_hex, /*nb_hex*/
847 0, /*nb_inplace_add*/
848 0, /*nb_inplace_subtract*/
849 0, /*nb_inplace_multiply*/
850 0, /*nb_inplace_divide*/
851 0, /*nb_inplace_remainder*/
852 0, /*nb_inplace_power*/
853 0, /*nb_inplace_lshift*/
854 0, /*nb_inplace_rshift*/
855 0, /*nb_inplace_and*/
856 0, /*nb_inplace_xor*/
857 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000858 (binaryfunc)int_div, /* nb_floor_divide */
859 int_true_divide, /* nb_true_divide */
860 0, /* nb_inplace_floor_divide */
861 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000862};
863
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000864PyTypeObject PyInt_Type = {
865 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000866 0,
867 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000870 (destructor)int_dealloc, /* tp_dealloc */
871 (printfunc)int_print, /* tp_print */
872 0, /* tp_getattr */
873 0, /* tp_setattr */
874 (cmpfunc)int_compare, /* tp_compare */
875 (reprfunc)int_repr, /* tp_repr */
876 &int_as_number, /* tp_as_number */
877 0, /* tp_as_sequence */
878 0, /* tp_as_mapping */
879 (hashfunc)int_hash, /* tp_hash */
880 0, /* tp_call */
881 0, /* tp_str */
882 PyObject_GenericGetAttr, /* tp_getattro */
883 0, /* tp_setattro */
884 0, /* tp_as_buffer */
885 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
886 int_doc, /* tp_doc */
887 0, /* tp_traverse */
888 0, /* tp_clear */
889 0, /* tp_richcompare */
890 0, /* tp_weaklistoffset */
891 0, /* tp_iter */
892 0, /* tp_iternext */
893 0, /* tp_methods */
894 0, /* tp_members */
895 0, /* tp_getset */
896 0, /* tp_base */
897 0, /* tp_dict */
898 0, /* tp_descr_get */
899 0, /* tp_descr_set */
900 0, /* tp_dictoffset */
901 0, /* tp_init */
902 0, /* tp_alloc */
903 int_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000905
906void
Fred Drakea2f55112000-07-09 15:16:51 +0000907PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000908{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000909 PyIntObject *p;
910 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000911 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000912 int bc, bf; /* block count, number of freed blocks */
913 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000914
Guido van Rossumda084ed1999-03-10 22:55:24 +0000915#if NSMALLNEGINTS + NSMALLPOSINTS > 0
916 PyIntObject **q;
917
918 i = NSMALLNEGINTS + NSMALLPOSINTS;
919 q = small_ints;
920 while (--i >= 0) {
921 Py_XDECREF(*q);
922 *q++ = NULL;
923 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000924#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000925 bc = 0;
926 bf = 0;
927 isum = 0;
928 list = block_list;
929 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000930 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000931 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000932 bc++;
933 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000934 for (i = 0, p = &list->objects[0];
935 i < N_INTOBJECTS;
936 i++, p++) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000937 if (PyInt_Check(p) && p->ob_refcnt != 0)
938 irem++;
939 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000940 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000941 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000942 list->next = block_list;
943 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000944 for (i = 0, p = &list->objects[0];
945 i < N_INTOBJECTS;
946 i++, p++) {
947 if (!PyInt_Check(p) || p->ob_refcnt == 0) {
948 p->ob_type = (struct _typeobject *)
949 free_list;
950 free_list = p;
951 }
952#if NSMALLNEGINTS + NSMALLPOSINTS > 0
953 else if (-NSMALLNEGINTS <= p->ob_ival &&
954 p->ob_ival < NSMALLPOSINTS &&
955 small_ints[p->ob_ival +
956 NSMALLNEGINTS] == NULL) {
957 Py_INCREF(p);
958 small_ints[p->ob_ival +
959 NSMALLNEGINTS] = p;
960 }
961#endif
962 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000963 }
964 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000965 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000966 bf++;
967 }
968 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000969 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000970 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000971 if (!Py_VerboseFlag)
972 return;
973 fprintf(stderr, "# cleanup ints");
974 if (!isum) {
975 fprintf(stderr, "\n");
976 }
977 else {
978 fprintf(stderr,
979 ": %d unfreed int%s in %d out of %d block%s\n",
980 isum, isum == 1 ? "" : "s",
981 bc - bf, bc, bc == 1 ? "" : "s");
982 }
983 if (Py_VerboseFlag > 1) {
984 list = block_list;
985 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000986 for (i = 0, p = &list->objects[0];
987 i < N_INTOBJECTS;
988 i++, p++) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000989 if (PyInt_Check(p) && p->ob_refcnt != 0)
990 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000991 "# <int at %p, refcnt=%d, val=%ld>\n",
992 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000993 }
994 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000995 }
996 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000997}