blob: 58862092406086b1466f209f0003aeaac8302a26 [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 Rossume27f7952001-08-23 02:59:04 +000013/* Return 1 if exception raised, 0 if caller should retry using longs */
14static int
Fred Drakea2f55112000-07-09 15:16:51 +000015err_ovf(char *msg)
Guido van Rossum165e67e1990-10-14 20:02:26 +000016{
Guido van Rossume27f7952001-08-23 02:59:04 +000017 if (PyErr_Warn(PyExc_OverflowWarning, msg) < 0) {
Guido van Rossum0b131162001-08-23 21:32:40 +000018 if (PyErr_ExceptionMatches(PyExc_OverflowWarning))
19 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossume27f7952001-08-23 02:59:04 +000020 return 1;
21 }
22 else
23 return 0;
Guido van Rossum165e67e1990-10-14 20:02:26 +000024}
25
Guido van Rossum3f5da241990-12-20 15:06:42 +000026/* Integers are quite normal objects, to make object handling uniform.
27 (Using odd pointers to represent integers would save much space
28 but require extra checks for this special case throughout the code.)
29 Since, a typical Python program spends much of its time allocating
30 and deallocating integers, these operations should be very fast.
31 Therefore we use a dedicated allocation scheme with a much lower
32 overhead (in space and time) than straight malloc(): a simple
33 dedicated free list, filled when necessary with memory from malloc().
34*/
35
36#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000037#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
38#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000039
Guido van Rossum3fce8831999-03-12 19:43:17 +000040struct _intblock {
41 struct _intblock *next;
42 PyIntObject objects[N_INTOBJECTS];
43};
44
45typedef struct _intblock PyIntBlock;
46
47static PyIntBlock *block_list = NULL;
48static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000049
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000051fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000052{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000053 PyIntObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000054 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
55 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000056 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000057 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000058 ((PyIntBlock *)p)->next = block_list;
59 block_list = (PyIntBlock *)p;
60 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000061 q = p + N_INTOBJECTS;
62 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000063 q->ob_type = (struct _typeobject *)(q-1);
64 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000065 return p + N_INTOBJECTS - 1;
66}
67
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000068#ifndef NSMALLPOSINTS
69#define NSMALLPOSINTS 100
70#endif
71#ifndef NSMALLNEGINTS
72#define NSMALLNEGINTS 1
73#endif
74#if NSMALLNEGINTS + NSMALLPOSINTS > 0
75/* References to small integers are saved in this array so that they
76 can be shared.
77 The integers that are saved are those in the range
78 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
79*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000081#endif
82#ifdef COUNT_ALLOCS
83int quick_int_allocs, quick_neg_int_allocs;
84#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000085
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000087PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#if NSMALLNEGINTS + NSMALLPOSINTS > 0
91 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
92 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000093 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000094#ifdef COUNT_ALLOCS
95 if (ival >= 0)
96 quick_int_allocs++;
97 else
98 quick_neg_int_allocs++;
99#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000101 }
102#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103 if (free_list == NULL) {
104 if ((free_list = fill_free_list()) == NULL)
105 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000107 /* PyObject_New is inlined */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000109 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000110 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000112#if NSMALLNEGINTS + NSMALLPOSINTS > 0
113 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
114 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000116 small_ints[ival + NSMALLNEGINTS] = v;
117 }
118#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000119 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120}
121
122static void
Fred Drakea2f55112000-07-09 15:16:51 +0000123int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000125 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000126 v->ob_type = (struct _typeobject *)free_list;
127 free_list = v;
128 }
129 else
130 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131}
132
133long
Fred Drakea2f55112000-07-09 15:16:51 +0000134PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000136 PyNumberMethods *nb;
137 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000138 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000139
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000140 if (op && PyInt_Check(op))
141 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000142
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000143 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
144 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000145 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146 return -1;
147 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000148
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000150 if (io == NULL)
151 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 if (!PyInt_Check(io)) {
153 PyErr_SetString(PyExc_TypeError,
154 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000155 return -1;
156 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000157
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158 val = PyInt_AS_LONG(io);
159 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000160
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000161 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162}
163
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000164PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000165PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000166{
167 char *end;
168 long x;
169 char buffer[256]; /* For errors */
170
171 if ((base != 0 && base < 2) || base > 36) {
Fred Drake661ea262000-10-24 19:57:45 +0000172 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000173 return NULL;
174 }
175
176 while (*s && isspace(Py_CHARMASK(*s)))
177 s++;
178 errno = 0;
179 if (base == 0 && s[0] == '0')
180 x = (long) PyOS_strtoul(s, &end, base);
181 else
182 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000183 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000184 goto bad;
185 while (*end && isspace(Py_CHARMASK(*end)))
186 end++;
187 if (*end != '\0') {
188 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000189 PyOS_snprintf(buffer, sizeof(buffer),
190 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000191 PyErr_SetString(PyExc_ValueError, buffer);
192 return NULL;
193 }
194 else if (errno != 0) {
Barry Warsaw61975092001-11-28 20:55:34 +0000195 PyOS_snprintf(buffer, sizeof(buffer),
196 "int() literal too large: %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000197 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];
Tim Petersa3c01ce2001-12-04 23:05:10 +0000210
Guido van Rossum9e896b32000-04-05 20:11:21 +0000211 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{
Tim Peters42221042001-12-01 02:52:56 +0000249 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000250 PyOS_snprintf(buf, sizeof(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 Rossume27f7952001-08-23 02:59:04 +0000280 if ((x^a) >= 0 || (x^b) >= 0)
281 return PyInt_FromLong(x);
282 if (err_ovf("integer addition"))
283 return NULL;
284 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285}
286
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000287static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000288int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289{
290 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000291 CONVERT_TO_LONG(v, a);
292 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000294 if ((x^a) >= 0 || (x^~b) >= 0)
295 return PyInt_FromLong(x);
296 if (err_ovf("integer subtraction"))
297 return NULL;
298 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
299 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300}
301
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000302/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000303Integer overflow checking for * is painful: Python tried a couple ways, but
304they didn't work on all platforms, or failed in endcases (a product of
305-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000306
Tim Petersa3c01ce2001-12-04 23:05:10 +0000307Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000308
Tim Petersa3c01ce2001-12-04 23:05:10 +0000309The native long product x*y is either exactly right or *way* off, being
310just the last n bits of the true product, where n is the number of bits
311in a long (the delivered product is the true product plus i*2**n for
312some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000313
Tim Petersa3c01ce2001-12-04 23:05:10 +0000314The native double product (double)x * (double)y is subject to three
315rounding errors: on a sizeof(long)==8 box, each cast to double can lose
316info, and even on a sizeof(long)==4 box, the multiplication can lose info.
317But, unlike the native long product, it's not in *range* trouble: even
318if sizeof(long)==32 (256-bit longs), the product easily fits in the
319dynamic range of a double. So the leading 50 (or so) bits of the double
320product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000321
Tim Petersa3c01ce2001-12-04 23:05:10 +0000322We check these two ways against each other, and declare victory if they're
323approximately the same. Else, because the native long product is the only
324one that can lose catastrophic amounts of information, it's the native long
325product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000326*/
327
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000328static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000329int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000331 long a, b;
332 long longprod; /* a*b in native long arithmetic */
333 double doubled_longprod; /* (double)longprod */
334 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000335
Guido van Rossum7e35d572001-09-15 03:14:32 +0000336 if (!PyInt_Check(v) &&
337 v->ob_type->tp_as_sequence &&
338 v->ob_type->tp_as_sequence->sq_repeat) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000339 /* sequence * int */
340 a = PyInt_AsLong(w);
341 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
342 }
Guido van Rossum7e35d572001-09-15 03:14:32 +0000343 if (!PyInt_Check(w) &&
344 w->ob_type->tp_as_sequence &&
345 w->ob_type->tp_as_sequence->sq_repeat) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000346 /* int * sequence */
347 a = PyInt_AsLong(v);
348 return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
349 }
350
351 CONVERT_TO_LONG(v, a);
352 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000353 longprod = a * b;
354 doubleprod = (double)a * (double)b;
355 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000356
Tim Petersa3c01ce2001-12-04 23:05:10 +0000357 /* Fast path for normal case: small multiplicands, and no info
358 is lost in either method. */
359 if (doubled_longprod == doubleprod)
360 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000361
Tim Petersa3c01ce2001-12-04 23:05:10 +0000362 /* Somebody somewhere lost info. Close enough, or way off? Note
363 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
364 The difference either is or isn't significant compared to the
365 true value (of which doubleprod is a good approximation).
366 */
367 {
368 const double diff = doubled_longprod - doubleprod;
369 const double absdiff = diff >= 0.0 ? diff : -diff;
370 const double absprod = doubleprod >= 0.0 ? doubleprod :
371 -doubleprod;
372 /* absdiff/absprod <= 1/32 iff
373 32 * absdiff <= absprod -- 5 good bits is "close enough" */
374 if (32.0 * absdiff <= absprod)
375 return PyInt_FromLong(longprod);
376 else if (err_ovf("integer multiplication"))
377 return NULL;
378 else
379 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000380 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000381}
382
Guido van Rossume27f7952001-08-23 02:59:04 +0000383/* Return type of i_divmod */
384enum divmod_result {
385 DIVMOD_OK, /* Correct result */
386 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
387 DIVMOD_ERROR /* Exception raised */
388};
389
390static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000391i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000392 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000394 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000395
Tim Peters1dad6a82001-06-18 19:21:11 +0000396 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000398 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000399 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000401 /* (-sys.maxint-1)/-1 is the only overflow case. */
402 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000403 if (err_ovf("integer division"))
404 return DIVMOD_ERROR;
405 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000406 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000407 xdivy = x / y;
408 xmody = x - xdivy * y;
409 /* If the signs of x and y differ, and the remainder is non-0,
410 * C89 doesn't define whether xdivy is now the floor or the
411 * ceiling of the infinitely precise quotient. We want the floor,
412 * and we have it iff the remainder's sign matches y's.
413 */
414 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
415 xmody += y;
416 --xdivy;
417 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000418 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000419 *p_xdivy = xdivy;
420 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000421 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000422}
423
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000425int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000426{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000427 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000428 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000429 CONVERT_TO_LONG(x, xi);
430 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000431 switch (i_divmod(xi, yi, &d, &m)) {
432 case DIVMOD_OK:
433 return PyInt_FromLong(d);
434 case DIVMOD_OVERFLOW:
435 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
436 (PyObject *)y);
437 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000438 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000439 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000440}
441
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000442static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000443int_classic_div(PyIntObject *x, PyIntObject *y)
444{
445 long xi, yi;
446 long d, m;
447 CONVERT_TO_LONG(x, xi);
448 CONVERT_TO_LONG(y, yi);
449 if (Py_DivisionWarningFlag &&
450 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
451 return NULL;
452 switch (i_divmod(xi, yi, &d, &m)) {
453 case DIVMOD_OK:
454 return PyInt_FromLong(d);
455 case DIVMOD_OVERFLOW:
456 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
457 (PyObject *)y);
458 default:
459 return NULL;
460 }
461}
462
463static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000464int_true_divide(PyObject *v, PyObject *w)
465{
Tim Peterse2a60002001-09-04 06:17:36 +0000466 /* If they aren't both ints, give someone else a chance. In
467 particular, this lets int/long get handled by longs, which
468 underflows to 0 gracefully if the long is too big to convert
469 to float. */
470 if (PyInt_Check(v) && PyInt_Check(w))
471 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
472 Py_INCREF(Py_NotImplemented);
473 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000474}
475
476static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000477int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000478{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000479 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000480 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000481 CONVERT_TO_LONG(x, xi);
482 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000483 switch (i_divmod(xi, yi, &d, &m)) {
484 case DIVMOD_OK:
485 return PyInt_FromLong(m);
486 case DIVMOD_OVERFLOW:
487 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
488 (PyObject *)y);
489 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000490 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000491 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000492}
493
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000494static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000495int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000496{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000497 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000498 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000499 CONVERT_TO_LONG(x, xi);
500 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000501 switch (i_divmod(xi, yi, &d, &m)) {
502 case DIVMOD_OK:
503 return Py_BuildValue("(ll)", d, m);
504 case DIVMOD_OVERFLOW:
505 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
506 (PyObject *)y);
507 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000508 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000509 }
Guido van Rossum00466951991-05-05 20:08:27 +0000510}
511
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000512static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000513int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000514{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000515 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000516 CONVERT_TO_LONG(v, iv);
517 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000518 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000519 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000520 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
521 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000522 return NULL;
523 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000524 /* Return a float. This works because we know that
525 this calls float_pow() which converts its
526 arguments to double. */
527 return PyFloat_Type.tp_as_number->nb_power(
528 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000529 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000530 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000531 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000532 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000534 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000535 return NULL;
536 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000537 }
538 /*
539 * XXX: The original exponentiation code stopped looping
540 * when temp hit zero; this code will continue onwards
541 * unnecessarily, but at least it won't cause any errors.
542 * Hopefully the speed improvement from the fast exponentiation
543 * will compensate for the slight inefficiency.
544 * XXX: Better handling of overflows is desperately needed.
545 */
546 temp = iv;
547 ix = 1;
548 while (iw > 0) {
549 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000550 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000551 ix = ix*temp;
552 if (temp == 0)
553 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000554 if (ix / temp != prev) {
555 if (err_ovf("integer exponentiation"))
556 return NULL;
557 return PyLong_Type.tp_as_number->nb_power(
558 (PyObject *)v,
559 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000560 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000561 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000562 }
563 iw >>= 1; /* Shift exponent down by 1 bit */
564 if (iw==0) break;
565 prev = temp;
566 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000567 if (prev!=0 && temp/prev!=prev) {
568 if (err_ovf("integer exponentiation"))
569 return NULL;
570 return PyLong_Type.tp_as_number->nb_power(
571 (PyObject *)v, (PyObject *)w, (PyObject *)z);
572 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000573 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000574 /* If we did a multiplication, perform a modulo */
575 ix = ix % iz;
576 temp = temp % iz;
577 }
578 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000579 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000580 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000581 switch (i_divmod(ix, iz, &div, &mod)) {
582 case DIVMOD_OK:
583 ix = mod;
584 break;
585 case DIVMOD_OVERFLOW:
586 return PyLong_Type.tp_as_number->nb_power(
587 (PyObject *)v, (PyObject *)w, (PyObject *)z);
588 default:
589 return NULL;
590 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000591 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000593}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000595static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000596int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000597{
598 register long a, x;
599 a = v->ob_ival;
600 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000601 if (a < 0 && x < 0) {
602 if (err_ovf("integer negation"))
603 return NULL;
604 return PyNumber_Negative(PyLong_FromLong(a));
605 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607}
608
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000610int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000611{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000612 if (PyInt_CheckExact(v)) {
613 Py_INCREF(v);
614 return (PyObject *)v;
615 }
616 else
617 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618}
619
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000621int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000622{
623 if (v->ob_ival >= 0)
624 return int_pos(v);
625 else
626 return int_neg(v);
627}
628
Guido van Rossum0bff0151991-05-14 12:05:32 +0000629static int
Fred Drakea2f55112000-07-09 15:16:51 +0000630int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000631{
632 return v->ob_ival != 0;
633}
634
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000635static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000636int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000637{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000638 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000639}
640
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000642int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000643{
644 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000645 CONVERT_TO_LONG(v, a);
646 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000647 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000649 return NULL;
650 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000651 if (a == 0 || b == 0)
652 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000653 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000655 }
Fred Drake1bc8fab2001-07-19 21:49:38 +0000656 a = (long)((unsigned long)a << b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000658}
659
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000661int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000662{
663 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000664 CONVERT_TO_LONG(v, a);
665 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000666 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000668 return NULL;
669 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000670 if (a == 0 || b == 0)
671 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000672 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000673 if (a < 0)
674 a = -1;
675 else
676 a = 0;
677 }
678 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000679 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000680 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000681 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000682}
683
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000685int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000686{
687 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000688 CONVERT_TO_LONG(v, a);
689 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000691}
692
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000694int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000695{
696 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000697 CONVERT_TO_LONG(v, a);
698 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000699 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000700}
701
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000703int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000704{
705 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000706 CONVERT_TO_LONG(v, a);
707 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000709}
710
Guido van Rossum1952e382001-09-19 01:25:16 +0000711static int
712int_coerce(PyObject **pv, PyObject **pw)
713{
714 if (PyInt_Check(*pw)) {
715 Py_INCREF(*pv);
716 Py_INCREF(*pw);
717 return 0;
718 }
719 return 1; /* Can't do it */
720}
721
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000722static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000723int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000724{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000725 Py_INCREF(v);
726 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000727}
728
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000729static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000730int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000731{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000733}
734
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000736int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000737{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000739}
740
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000741static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000742int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000743{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000744 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000745 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000746 if (x == 0)
747 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000748 else
Barry Warsaw61975092001-11-28 20:55:34 +0000749 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750 return PyString_FromString(buf);
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_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000755{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000756 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000757 long x = v -> ob_ival;
Barry Warsaw61975092001-11-28 20:55:34 +0000758 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000759 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000760}
761
Guido van Rossumbef14172001-08-29 15:47:46 +0000762staticforward PyObject *
763int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
764
Tim Peters6d6c1a32001-08-02 04:15:00 +0000765static PyObject *
766int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
767{
768 PyObject *x = NULL;
769 int base = -909;
770 static char *kwlist[] = {"x", "base", 0};
771
Guido van Rossumbef14172001-08-29 15:47:46 +0000772 if (type != &PyInt_Type)
773 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000774 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
775 &x, &base))
776 return NULL;
777 if (x == NULL)
778 return PyInt_FromLong(0L);
779 if (base == -909)
780 return PyNumber_Int(x);
781 if (PyString_Check(x))
782 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000783#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000784 if (PyUnicode_Check(x))
785 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
786 PyUnicode_GET_SIZE(x),
787 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000788#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000789 PyErr_SetString(PyExc_TypeError,
790 "int() can't convert non-string with explicit base");
791 return NULL;
792}
793
Guido van Rossumbef14172001-08-29 15:47:46 +0000794/* Wimpy, slow approach to tp_new calls for subtypes of int:
795 first create a regular int from whatever arguments we got,
796 then allocate a subtype instance and initialize its ob_ival
797 from the regular int. The regular int is then thrown away.
798*/
799static PyObject *
800int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
801{
802 PyObject *tmp, *new;
803
804 assert(PyType_IsSubtype(type, &PyInt_Type));
805 tmp = int_new(&PyInt_Type, args, kwds);
806 if (tmp == NULL)
807 return NULL;
808 assert(PyInt_Check(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000809 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000810 if (new == NULL)
811 return NULL;
812 ((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
813 Py_DECREF(tmp);
814 return new;
815}
816
Tim Peters6d6c1a32001-08-02 04:15:00 +0000817static char int_doc[] =
818"int(x[, base]) -> integer\n\
819\n\
820Convert a string or number to an integer, if possible. A floating point\n\
821argument will be truncated towards zero (this does not include a string\n\
822representation of a floating point number!) When converting a string, use\n\
823the optional base. It is an error to supply a base when converting a\n\
824non-string.";
825
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000826static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000827 (binaryfunc)int_add, /*nb_add*/
828 (binaryfunc)int_sub, /*nb_subtract*/
829 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000830 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000831 (binaryfunc)int_mod, /*nb_remainder*/
832 (binaryfunc)int_divmod, /*nb_divmod*/
833 (ternaryfunc)int_pow, /*nb_power*/
834 (unaryfunc)int_neg, /*nb_negative*/
835 (unaryfunc)int_pos, /*nb_positive*/
836 (unaryfunc)int_abs, /*nb_absolute*/
837 (inquiry)int_nonzero, /*nb_nonzero*/
838 (unaryfunc)int_invert, /*nb_invert*/
839 (binaryfunc)int_lshift, /*nb_lshift*/
840 (binaryfunc)int_rshift, /*nb_rshift*/
841 (binaryfunc)int_and, /*nb_and*/
842 (binaryfunc)int_xor, /*nb_xor*/
843 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +0000844 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000845 (unaryfunc)int_int, /*nb_int*/
846 (unaryfunc)int_long, /*nb_long*/
847 (unaryfunc)int_float, /*nb_float*/
848 (unaryfunc)int_oct, /*nb_oct*/
849 (unaryfunc)int_hex, /*nb_hex*/
850 0, /*nb_inplace_add*/
851 0, /*nb_inplace_subtract*/
852 0, /*nb_inplace_multiply*/
853 0, /*nb_inplace_divide*/
854 0, /*nb_inplace_remainder*/
855 0, /*nb_inplace_power*/
856 0, /*nb_inplace_lshift*/
857 0, /*nb_inplace_rshift*/
858 0, /*nb_inplace_and*/
859 0, /*nb_inplace_xor*/
860 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000861 (binaryfunc)int_div, /* nb_floor_divide */
862 int_true_divide, /* nb_true_divide */
863 0, /* nb_inplace_floor_divide */
864 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865};
866
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000867PyTypeObject PyInt_Type = {
868 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869 0,
870 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000872 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000873 (destructor)int_dealloc, /* tp_dealloc */
874 (printfunc)int_print, /* tp_print */
875 0, /* tp_getattr */
876 0, /* tp_setattr */
877 (cmpfunc)int_compare, /* tp_compare */
878 (reprfunc)int_repr, /* tp_repr */
879 &int_as_number, /* tp_as_number */
880 0, /* tp_as_sequence */
881 0, /* tp_as_mapping */
882 (hashfunc)int_hash, /* tp_hash */
883 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +0000884 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000885 PyObject_GenericGetAttr, /* tp_getattro */
886 0, /* tp_setattro */
887 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000888 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
889 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000890 int_doc, /* tp_doc */
891 0, /* tp_traverse */
892 0, /* tp_clear */
893 0, /* tp_richcompare */
894 0, /* tp_weaklistoffset */
895 0, /* tp_iter */
896 0, /* tp_iternext */
897 0, /* tp_methods */
898 0, /* tp_members */
899 0, /* tp_getset */
900 0, /* tp_base */
901 0, /* tp_dict */
902 0, /* tp_descr_get */
903 0, /* tp_descr_set */
904 0, /* tp_dictoffset */
905 0, /* tp_init */
906 0, /* tp_alloc */
907 int_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000908};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000909
910void
Fred Drakea2f55112000-07-09 15:16:51 +0000911PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000912{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000913 PyIntObject *p;
914 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000915 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000916 int bc, bf; /* block count, number of freed blocks */
917 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000918
Guido van Rossumda084ed1999-03-10 22:55:24 +0000919#if NSMALLNEGINTS + NSMALLPOSINTS > 0
920 PyIntObject **q;
921
922 i = NSMALLNEGINTS + NSMALLPOSINTS;
923 q = small_ints;
924 while (--i >= 0) {
925 Py_XDECREF(*q);
926 *q++ = NULL;
927 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000928#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000929 bc = 0;
930 bf = 0;
931 isum = 0;
932 list = block_list;
933 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000934 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000935 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000936 bc++;
937 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000938 for (i = 0, p = &list->objects[0];
939 i < N_INTOBJECTS;
940 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000941 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +0000942 irem++;
943 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000944 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000945 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000946 list->next = block_list;
947 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000948 for (i = 0, p = &list->objects[0];
949 i < N_INTOBJECTS;
950 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000951 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000952 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000953 p->ob_type = (struct _typeobject *)
954 free_list;
955 free_list = p;
956 }
957#if NSMALLNEGINTS + NSMALLPOSINTS > 0
958 else if (-NSMALLNEGINTS <= p->ob_ival &&
959 p->ob_ival < NSMALLPOSINTS &&
960 small_ints[p->ob_ival +
961 NSMALLNEGINTS] == NULL) {
962 Py_INCREF(p);
963 small_ints[p->ob_ival +
964 NSMALLNEGINTS] = p;
965 }
966#endif
967 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000968 }
969 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000970 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000971 bf++;
972 }
973 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000974 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000975 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000976 if (!Py_VerboseFlag)
977 return;
978 fprintf(stderr, "# cleanup ints");
979 if (!isum) {
980 fprintf(stderr, "\n");
981 }
982 else {
983 fprintf(stderr,
984 ": %d unfreed int%s in %d out of %d block%s\n",
985 isum, isum == 1 ? "" : "s",
986 bc - bf, bc, bc == 1 ? "" : "s");
987 }
988 if (Py_VerboseFlag > 1) {
989 list = block_list;
990 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000991 for (i = 0, p = &list->objects[0];
992 i < N_INTOBJECTS;
993 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000994 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +0000995 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +0000996 "# <int at %p, refcnt=%d, val=%ld>\n",
997 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +0000998 }
999 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001000 }
1001 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001002}