blob: 108e658755b1fb0a3be70f68bcb0890b80990be0 [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 Rossumbef14172001-08-29 15:47:46 +0000137 if (v->ob_type == &PyInt_Type) {
138 v->ob_type = (struct _typeobject *)free_list;
139 free_list = v;
140 }
141 else
142 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143}
144
145long
Fred Drakea2f55112000-07-09 15:16:51 +0000146PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 PyNumberMethods *nb;
149 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000150 long val;
151
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 if (op && PyInt_Check(op))
153 return PyInt_AS_LONG((PyIntObject*) op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000154
155 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
156 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000157 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158 return -1;
159 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000160
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000162 if (io == NULL)
163 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 if (!PyInt_Check(io)) {
165 PyErr_SetString(PyExc_TypeError,
166 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000167 return -1;
168 }
169
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000170 val = PyInt_AS_LONG(io);
171 Py_DECREF(io);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000172
173 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174}
175
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000176PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000177PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000178{
179 char *end;
180 long x;
181 char buffer[256]; /* For errors */
182
183 if ((base != 0 && base < 2) || base > 36) {
Fred Drake661ea262000-10-24 19:57:45 +0000184 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000185 return NULL;
186 }
187
188 while (*s && isspace(Py_CHARMASK(*s)))
189 s++;
190 errno = 0;
191 if (base == 0 && s[0] == '0')
192 x = (long) PyOS_strtoul(s, &end, base);
193 else
194 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000195 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000196 goto bad;
197 while (*end && isspace(Py_CHARMASK(*end)))
198 end++;
199 if (*end != '\0') {
200 bad:
201 sprintf(buffer, "invalid literal for int(): %.200s", s);
202 PyErr_SetString(PyExc_ValueError, buffer);
203 return NULL;
204 }
205 else if (errno != 0) {
206 sprintf(buffer, "int() literal too large: %.200s", s);
207 PyErr_SetString(PyExc_ValueError, buffer);
208 return NULL;
209 }
210 if (pend)
211 *pend = end;
212 return PyInt_FromLong(x);
213}
214
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000215#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000216PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000217PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000218{
219 char buffer[256];
220
221 if (length >= sizeof(buffer)) {
222 PyErr_SetString(PyExc_ValueError,
223 "int() literal too large to convert");
224 return NULL;
225 }
226 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
227 return NULL;
228 return PyInt_FromString(buffer, NULL, base);
229}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000230#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000231
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232/* Methods */
233
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000234/* Integers are seen as the "smallest" of all numeric types and thus
235 don't have any knowledge about conversion of other types to
236 integers. */
237
238#define CONVERT_TO_LONG(obj, lng) \
239 if (PyInt_Check(obj)) { \
240 lng = PyInt_AS_LONG(obj); \
241 } \
242 else { \
243 Py_INCREF(Py_NotImplemented); \
244 return Py_NotImplemented; \
245 }
246
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000247/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000248static int
Fred Drakea2f55112000-07-09 15:16:51 +0000249int_print(PyIntObject *v, FILE *fp, int flags)
250 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251{
252 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000253 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254}
255
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000256static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000257int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258{
259 char buf[20];
260 sprintf(buf, "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000261 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
264static int
Fred Drakea2f55112000-07-09 15:16:51 +0000265int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266{
267 register long i = v->ob_ival;
268 register long j = w->ob_ival;
269 return (i < j) ? -1 : (i > j) ? 1 : 0;
270}
271
Guido van Rossum9bfef441993-03-29 10:43:31 +0000272static long
Fred Drakea2f55112000-07-09 15:16:51 +0000273int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000274{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000275 /* XXX If this is changed, you also need to change the way
276 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000277 long x = v -> ob_ival;
278 if (x == -1)
279 x = -2;
280 return x;
281}
282
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000283static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000284int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285{
286 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000287 CONVERT_TO_LONG(v, a);
288 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000290 if ((x^a) >= 0 || (x^b) >= 0)
291 return PyInt_FromLong(x);
292 if (err_ovf("integer addition"))
293 return NULL;
294 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295}
296
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000298int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299{
300 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000301 CONVERT_TO_LONG(v, a);
302 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000304 if ((x^a) >= 0 || (x^~b) >= 0)
305 return PyInt_FromLong(x);
306 if (err_ovf("integer subtraction"))
307 return NULL;
308 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
309 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310}
311
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000312/*
313Integer overflow checking used to be done using a double, but on 64
314bit machines (where both long and double are 64 bit) this fails
Thomas Wouters7e474022000-07-16 12:04:32 +0000315because the double doesn't have enough precision. John Tromp suggests
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000316the following algorithm:
317
318Suppose again we normalize a and b to be nonnegative.
319Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.).
320Now we test ah and bh against zero and get essentially 3 possible outcomes.
321
3221) both ah and bh > 0 : then report overflow
323
3242) both ah and bh = 0 : then compute a*b and report overflow if it comes out
325 negative
326
3273) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31
328 compute al*bl and report overflow if it's negative
329 add (ah*bl)<<32 to al*bl and report overflow if
330 it's negative
331
332In case of no overflow the result is then negated if necessary.
333
334The majority of cases will be 2), in which case this method is the same as
335what I suggested before. If multiplication is expensive enough, then the
336other method is faster on case 3), but also more work to program, so I
337guess the above is the preferred solution.
338
339*/
340
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000342int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343{
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000344 long a, b, ah, bh, x, y;
345 int s = 1;
346
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000347 if (v->ob_type->tp_as_sequence &&
348 v->ob_type->tp_as_sequence->sq_repeat) {
349 /* sequence * int */
350 a = PyInt_AsLong(w);
351 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
352 }
353 else if (w->ob_type->tp_as_sequence &&
354 w->ob_type->tp_as_sequence->sq_repeat) {
355 /* int * sequence */
356 a = PyInt_AsLong(v);
357 return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
358 }
359
360 CONVERT_TO_LONG(v, a);
361 CONVERT_TO_LONG(w, b);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000362 ah = a >> (LONG_BIT/2);
363 bh = b >> (LONG_BIT/2);
364
365 /* Quick test for common case: two small positive ints */
366
367 if (ah == 0 && bh == 0) {
368 x = a*b;
369 if (x < 0)
370 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000371 return PyInt_FromLong(x);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000372 }
373
374 /* Arrange that a >= b >= 0 */
375
376 if (a < 0) {
377 a = -a;
378 if (a < 0) {
379 /* Largest negative */
380 if (b == 0 || b == 1) {
381 x = a*b;
382 goto ok;
383 }
384 else
385 goto bad;
386 }
387 s = -s;
388 ah = a >> (LONG_BIT/2);
389 }
390 if (b < 0) {
391 b = -b;
392 if (b < 0) {
393 /* Largest negative */
Guido van Rossum9478dd41996-12-06 20:14:43 +0000394 if (a == 0 || (a == 1 && s == 1)) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000395 x = a*b;
396 goto ok;
397 }
398 else
399 goto bad;
400 }
401 s = -s;
402 bh = b >> (LONG_BIT/2);
403 }
404
405 /* 1) both ah and bh > 0 : then report overflow */
406
407 if (ah != 0 && bh != 0)
408 goto bad;
409
410 /* 2) both ah and bh = 0 : then compute a*b and report
411 overflow if it comes out negative */
412
413 if (ah == 0 && bh == 0) {
414 x = a*b;
415 if (x < 0)
416 goto bad;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000417 return PyInt_FromLong(x*s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000418 }
419
420 if (a < b) {
421 /* Swap */
422 x = a;
423 a = b;
424 b = x;
425 ah = bh;
426 /* bh not used beyond this point */
427 }
428
429 /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if
430 it's >= 2^31
431 compute al*bl and report overflow if it's negative
432 add (ah*bl)<<32 to al*bl and report overflow if
433 it's negative
434 (NB b == bl in this case, and we make a = al) */
435
436 y = ah*b;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000437 if (y >= (1L << (LONG_BIT/2 - 1)))
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000438 goto bad;
439 a &= (1L << (LONG_BIT/2)) - 1;
440 x = a*b;
441 if (x < 0)
442 goto bad;
Guido van Rossum541cdd81997-01-06 22:53:20 +0000443 x += y << (LONG_BIT/2);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000444 if (x < 0)
445 goto bad;
446 ok:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447 return PyInt_FromLong(x * s);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000448
449 bad:
Guido van Rossume27f7952001-08-23 02:59:04 +0000450 if (err_ovf("integer multiplication"))
451 return NULL;
452 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453}
454
Guido van Rossume27f7952001-08-23 02:59:04 +0000455/* Return type of i_divmod */
456enum divmod_result {
457 DIVMOD_OK, /* Correct result */
458 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
459 DIVMOD_ERROR /* Exception raised */
460};
461
462static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000463i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000464 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000466 long xdivy, xmody;
467
Tim Peters1dad6a82001-06-18 19:21:11 +0000468 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000470 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000471 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000473 /* (-sys.maxint-1)/-1 is the only overflow case. */
474 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000475 if (err_ovf("integer division"))
476 return DIVMOD_ERROR;
477 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000478 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000479 xdivy = x / y;
480 xmody = x - xdivy * y;
481 /* If the signs of x and y differ, and the remainder is non-0,
482 * C89 doesn't define whether xdivy is now the floor or the
483 * ceiling of the infinitely precise quotient. We want the floor,
484 * and we have it iff the remainder's sign matches y's.
485 */
486 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
487 xmody += y;
488 --xdivy;
489 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000490 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000491 *p_xdivy = xdivy;
492 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000493 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000494}
495
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000497int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000498{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000499 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000500 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000501 CONVERT_TO_LONG(x, xi);
502 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000503 switch (i_divmod(xi, yi, &d, &m)) {
504 case DIVMOD_OK:
505 return PyInt_FromLong(d);
506 case DIVMOD_OVERFLOW:
507 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
508 (PyObject *)y);
509 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000510 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000511 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000512}
513
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000514static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000515int_classic_div(PyIntObject *x, PyIntObject *y)
516{
517 long xi, yi;
518 long d, m;
519 CONVERT_TO_LONG(x, xi);
520 CONVERT_TO_LONG(y, yi);
521 if (Py_DivisionWarningFlag &&
522 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
523 return NULL;
524 switch (i_divmod(xi, yi, &d, &m)) {
525 case DIVMOD_OK:
526 return PyInt_FromLong(d);
527 case DIVMOD_OVERFLOW:
528 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
529 (PyObject *)y);
530 default:
531 return NULL;
532 }
533}
534
535static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000536int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000537{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000538 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000539 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000540 CONVERT_TO_LONG(x, xi);
541 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000542 switch (i_divmod(xi, yi, &d, &m)) {
543 case DIVMOD_OK:
544 return PyInt_FromLong(m);
545 case DIVMOD_OVERFLOW:
546 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
547 (PyObject *)y);
548 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000549 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000550 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000551}
552
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000553static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000554int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000555{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000556 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000557 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000558 CONVERT_TO_LONG(x, xi);
559 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000560 switch (i_divmod(xi, yi, &d, &m)) {
561 case DIVMOD_OK:
562 return Py_BuildValue("(ll)", d, m);
563 case DIVMOD_OVERFLOW:
564 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
565 (PyObject *)y);
566 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000567 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000568 }
Guido van Rossum00466951991-05-05 20:08:27 +0000569}
570
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000572int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000574 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000575 CONVERT_TO_LONG(v, iv);
576 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000577 if (iw < 0) {
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000578 /* Return a float. This works because we know that
579 this calls float_pow() which converts its
580 arguments to double. */
581 return PyFloat_Type.tp_as_number->nb_power(
582 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000583 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000585 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000586 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000587 PyErr_SetString(PyExc_ValueError,
Fred Drake661ea262000-10-24 19:57:45 +0000588 "pow() arg 3 cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000589 return NULL;
590 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000591 }
592 /*
593 * XXX: The original exponentiation code stopped looping
594 * when temp hit zero; this code will continue onwards
595 * unnecessarily, but at least it won't cause any errors.
596 * Hopefully the speed improvement from the fast exponentiation
597 * will compensate for the slight inefficiency.
598 * XXX: Better handling of overflows is desperately needed.
599 */
600 temp = iv;
601 ix = 1;
602 while (iw > 0) {
603 prev = ix; /* Save value for overflow check */
604 if (iw & 1) {
605 ix = ix*temp;
606 if (temp == 0)
607 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000608 if (ix / temp != prev) {
609 if (err_ovf("integer exponentiation"))
610 return NULL;
611 return PyLong_Type.tp_as_number->nb_power(
612 (PyObject *)v,
613 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000614 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000615 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000616 }
617 iw >>= 1; /* Shift exponent down by 1 bit */
618 if (iw==0) break;
619 prev = temp;
620 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000621 if (prev!=0 && temp/prev!=prev) {
622 if (err_ovf("integer exponentiation"))
623 return NULL;
624 return PyLong_Type.tp_as_number->nb_power(
625 (PyObject *)v, (PyObject *)w, (PyObject *)z);
626 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000627 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000628 /* If we did a multiplication, perform a modulo */
629 ix = ix % iz;
630 temp = temp % iz;
631 }
632 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000633 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000634 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000635 switch (i_divmod(ix, iz, &div, &mod)) {
636 case DIVMOD_OK:
637 ix = mod;
638 break;
639 case DIVMOD_OVERFLOW:
640 return PyLong_Type.tp_as_number->nb_power(
641 (PyObject *)v, (PyObject *)w, (PyObject *)z);
642 default:
643 return NULL;
644 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000645 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 return PyInt_FromLong(ix);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000647}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000650int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000651{
652 register long a, x;
653 a = v->ob_ival;
654 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000655 if (a < 0 && x < 0) {
656 if (err_ovf("integer negation"))
657 return NULL;
658 return PyNumber_Negative(PyLong_FromLong(a));
659 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000661}
662
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000663static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000664int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000665{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 Py_INCREF(v);
667 return (PyObject *)v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000668}
669
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000671int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000672{
673 if (v->ob_ival >= 0)
674 return int_pos(v);
675 else
676 return int_neg(v);
677}
678
Guido van Rossum0bff0151991-05-14 12:05:32 +0000679static int
Fred Drakea2f55112000-07-09 15:16:51 +0000680int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000681{
682 return v->ob_ival != 0;
683}
684
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000685static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000686int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000687{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000689}
690
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000691static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000692int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000693{
694 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000695 CONVERT_TO_LONG(v, a);
696 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000697 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000699 return NULL;
700 }
701 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702 Py_INCREF(v);
703 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000704 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000705 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000707 }
Fred Drake1bc8fab2001-07-19 21:49:38 +0000708 a = (long)((unsigned long)a << b);
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_rshift(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 Rossumf3b351f1992-01-14 18:33:22 +0000718 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000719 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000720 return NULL;
721 }
722 if (a == 0 || b == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 Py_INCREF(v);
724 return (PyObject *) v;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000725 }
Guido van Rossum72481a31993-10-26 15:21:51 +0000726 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000727 if (a < 0)
728 a = -1;
729 else
730 a = 0;
731 }
732 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000733 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000734 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000736}
737
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000739int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000740{
741 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000742 CONVERT_TO_LONG(v, a);
743 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000745}
746
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000748int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000749{
750 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000751 CONVERT_TO_LONG(v, a);
752 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000754}
755
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000757int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000758{
759 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000760 CONVERT_TO_LONG(v, a);
761 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000762 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000763}
764
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765static PyObject *
Guido van Rossum4668b002001-08-08 05:00:18 +0000766int_true_divide(PyObject *v, PyObject *w)
767{
Guido van Rossum393661d2001-08-31 17:40:15 +0000768 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
Guido van Rossum4668b002001-08-08 05:00:18 +0000769}
770
771static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000772int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000773{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774 Py_INCREF(v);
775 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000776}
777
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000778static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000779int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000780{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000781 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000782}
783
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000784static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000785int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000786{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000787 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000788}
789
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000791int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000792{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000793 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000794 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000795 if (x == 0)
796 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000797 else
Guido van Rossumebee0251997-01-12 19:48:03 +0000798 sprintf(buf, "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000799 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000800}
801
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000802static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000803int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000804{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000805 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000806 long x = v -> ob_ival;
Guido van Rossumebee0251997-01-12 19:48:03 +0000807 sprintf(buf, "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000808 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000809}
810
Guido van Rossumbef14172001-08-29 15:47:46 +0000811staticforward PyObject *
812int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
813
Tim Peters6d6c1a32001-08-02 04:15:00 +0000814static PyObject *
815int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
816{
817 PyObject *x = NULL;
818 int base = -909;
819 static char *kwlist[] = {"x", "base", 0};
820
Guido van Rossumbef14172001-08-29 15:47:46 +0000821 if (type != &PyInt_Type)
822 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000823 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
824 &x, &base))
825 return NULL;
826 if (x == NULL)
827 return PyInt_FromLong(0L);
828 if (base == -909)
829 return PyNumber_Int(x);
830 if (PyString_Check(x))
831 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000832#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000833 if (PyUnicode_Check(x))
834 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
835 PyUnicode_GET_SIZE(x),
836 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000837#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000838 PyErr_SetString(PyExc_TypeError,
839 "int() can't convert non-string with explicit base");
840 return NULL;
841}
842
Guido van Rossumbef14172001-08-29 15:47:46 +0000843/* Wimpy, slow approach to tp_new calls for subtypes of int:
844 first create a regular int from whatever arguments we got,
845 then allocate a subtype instance and initialize its ob_ival
846 from the regular int. The regular int is then thrown away.
847*/
848static PyObject *
849int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
850{
851 PyObject *tmp, *new;
852
853 assert(PyType_IsSubtype(type, &PyInt_Type));
854 tmp = int_new(&PyInt_Type, args, kwds);
855 if (tmp == NULL)
856 return NULL;
857 assert(PyInt_Check(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000858 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000859 if (new == NULL)
860 return NULL;
861 ((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
862 Py_DECREF(tmp);
863 return new;
864}
865
Tim Peters6d6c1a32001-08-02 04:15:00 +0000866static char int_doc[] =
867"int(x[, base]) -> integer\n\
868\n\
869Convert a string or number to an integer, if possible. A floating point\n\
870argument will be truncated towards zero (this does not include a string\n\
871representation of a floating point number!) When converting a string, use\n\
872the optional base. It is an error to supply a base when converting a\n\
873non-string.";
874
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000876 (binaryfunc)int_add, /*nb_add*/
877 (binaryfunc)int_sub, /*nb_subtract*/
878 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000879 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000880 (binaryfunc)int_mod, /*nb_remainder*/
881 (binaryfunc)int_divmod, /*nb_divmod*/
882 (ternaryfunc)int_pow, /*nb_power*/
883 (unaryfunc)int_neg, /*nb_negative*/
884 (unaryfunc)int_pos, /*nb_positive*/
885 (unaryfunc)int_abs, /*nb_absolute*/
886 (inquiry)int_nonzero, /*nb_nonzero*/
887 (unaryfunc)int_invert, /*nb_invert*/
888 (binaryfunc)int_lshift, /*nb_lshift*/
889 (binaryfunc)int_rshift, /*nb_rshift*/
890 (binaryfunc)int_and, /*nb_and*/
891 (binaryfunc)int_xor, /*nb_xor*/
892 (binaryfunc)int_or, /*nb_or*/
893 0, /*nb_coerce*/
894 (unaryfunc)int_int, /*nb_int*/
895 (unaryfunc)int_long, /*nb_long*/
896 (unaryfunc)int_float, /*nb_float*/
897 (unaryfunc)int_oct, /*nb_oct*/
898 (unaryfunc)int_hex, /*nb_hex*/
899 0, /*nb_inplace_add*/
900 0, /*nb_inplace_subtract*/
901 0, /*nb_inplace_multiply*/
902 0, /*nb_inplace_divide*/
903 0, /*nb_inplace_remainder*/
904 0, /*nb_inplace_power*/
905 0, /*nb_inplace_lshift*/
906 0, /*nb_inplace_rshift*/
907 0, /*nb_inplace_and*/
908 0, /*nb_inplace_xor*/
909 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000910 (binaryfunc)int_div, /* nb_floor_divide */
911 int_true_divide, /* nb_true_divide */
912 0, /* nb_inplace_floor_divide */
913 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000914};
915
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000916PyTypeObject PyInt_Type = {
917 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000918 0,
919 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000920 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000921 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000922 (destructor)int_dealloc, /* tp_dealloc */
923 (printfunc)int_print, /* tp_print */
924 0, /* tp_getattr */
925 0, /* tp_setattr */
926 (cmpfunc)int_compare, /* tp_compare */
927 (reprfunc)int_repr, /* tp_repr */
928 &int_as_number, /* tp_as_number */
929 0, /* tp_as_sequence */
930 0, /* tp_as_mapping */
931 (hashfunc)int_hash, /* tp_hash */
932 0, /* tp_call */
933 0, /* tp_str */
934 PyObject_GenericGetAttr, /* tp_getattro */
935 0, /* tp_setattro */
936 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000937 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
938 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000939 int_doc, /* tp_doc */
940 0, /* tp_traverse */
941 0, /* tp_clear */
942 0, /* tp_richcompare */
943 0, /* tp_weaklistoffset */
944 0, /* tp_iter */
945 0, /* tp_iternext */
946 0, /* tp_methods */
947 0, /* tp_members */
948 0, /* tp_getset */
949 0, /* tp_base */
950 0, /* tp_dict */
951 0, /* tp_descr_get */
952 0, /* tp_descr_set */
953 0, /* tp_dictoffset */
954 0, /* tp_init */
955 0, /* tp_alloc */
956 int_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000957};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000958
959void
Fred Drakea2f55112000-07-09 15:16:51 +0000960PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000961{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000962 PyIntObject *p;
963 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000964 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000965 int bc, bf; /* block count, number of freed blocks */
966 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000967
Guido van Rossumda084ed1999-03-10 22:55:24 +0000968#if NSMALLNEGINTS + NSMALLPOSINTS > 0
969 PyIntObject **q;
970
971 i = NSMALLNEGINTS + NSMALLPOSINTS;
972 q = small_ints;
973 while (--i >= 0) {
974 Py_XDECREF(*q);
975 *q++ = NULL;
976 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000977#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000978 bc = 0;
979 bf = 0;
980 isum = 0;
981 list = block_list;
982 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000983 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000984 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000985 bc++;
986 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000987 for (i = 0, p = &list->objects[0];
988 i < N_INTOBJECTS;
989 i++, p++) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000990 if (p->ob_type == &PyInt_Type && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +0000991 irem++;
992 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000993 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000994 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000995 list->next = block_list;
996 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000997 for (i = 0, p = &list->objects[0];
998 i < N_INTOBJECTS;
999 i++, p++) {
Guido van Rossumbef14172001-08-29 15:47:46 +00001000 if (p->ob_type != &PyInt_Type ||
1001 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001002 p->ob_type = (struct _typeobject *)
1003 free_list;
1004 free_list = p;
1005 }
1006#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1007 else if (-NSMALLNEGINTS <= p->ob_ival &&
1008 p->ob_ival < NSMALLPOSINTS &&
1009 small_ints[p->ob_ival +
1010 NSMALLNEGINTS] == NULL) {
1011 Py_INCREF(p);
1012 small_ints[p->ob_ival +
1013 NSMALLNEGINTS] = p;
1014 }
1015#endif
1016 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001017 }
1018 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +00001019 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +00001020 bf++;
1021 }
1022 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001023 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001024 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001025 if (!Py_VerboseFlag)
1026 return;
1027 fprintf(stderr, "# cleanup ints");
1028 if (!isum) {
1029 fprintf(stderr, "\n");
1030 }
1031 else {
1032 fprintf(stderr,
1033 ": %d unfreed int%s in %d out of %d block%s\n",
1034 isum, isum == 1 ? "" : "s",
1035 bc - bf, bc, bc == 1 ? "" : "s");
1036 }
1037 if (Py_VerboseFlag > 1) {
1038 list = block_list;
1039 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001040 for (i = 0, p = &list->objects[0];
1041 i < N_INTOBJECTS;
1042 i++, p++) {
Guido van Rossumbef14172001-08-29 15:47:46 +00001043 if (p->ob_type == &PyInt_Type && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001044 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001045 "# <int at %p, refcnt=%d, val=%ld>\n",
1046 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001047 }
1048 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001049 }
1050 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001051}