blob: 58a6beb46ee5820c82b4cc1dca6ca8f59e0eb4f3 [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
Guido van Rossum93646982002-04-26 00:53:34 +0000133static void
134int_free(PyIntObject *v)
135{
136 v->ob_type = (struct _typeobject *)free_list;
137 free_list = v;
138}
139
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140long
Fred Drakea2f55112000-07-09 15:16:51 +0000141PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000143 PyNumberMethods *nb;
144 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000145 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000146
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000147 if (op && PyInt_Check(op))
148 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000149
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000150 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
151 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000152 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153 return -1;
154 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000155
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000157 if (io == NULL)
158 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 if (!PyInt_Check(io)) {
160 PyErr_SetString(PyExc_TypeError,
161 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000162 return -1;
163 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000164
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 val = PyInt_AS_LONG(io);
166 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000167
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000168 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169}
170
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000171PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000172PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000173{
174 char *end;
175 long x;
176 char buffer[256]; /* For errors */
177
178 if ((base != 0 && base < 2) || base > 36) {
Fred Drake661ea262000-10-24 19:57:45 +0000179 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000180 return NULL;
181 }
182
183 while (*s && isspace(Py_CHARMASK(*s)))
184 s++;
185 errno = 0;
186 if (base == 0 && s[0] == '0')
187 x = (long) PyOS_strtoul(s, &end, base);
188 else
189 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000190 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000191 goto bad;
192 while (*end && isspace(Py_CHARMASK(*end)))
193 end++;
194 if (*end != '\0') {
195 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000196 PyOS_snprintf(buffer, sizeof(buffer),
197 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000198 PyErr_SetString(PyExc_ValueError, buffer);
199 return NULL;
200 }
201 else if (errno != 0) {
Barry Warsaw61975092001-11-28 20:55:34 +0000202 PyOS_snprintf(buffer, sizeof(buffer),
203 "int() literal too large: %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000204 PyErr_SetString(PyExc_ValueError, buffer);
205 return NULL;
206 }
207 if (pend)
208 *pend = end;
209 return PyInt_FromLong(x);
210}
211
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000212#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000213PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000214PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000215{
216 char buffer[256];
Tim Petersa3c01ce2001-12-04 23:05:10 +0000217
Guido van Rossum9e896b32000-04-05 20:11:21 +0000218 if (length >= sizeof(buffer)) {
219 PyErr_SetString(PyExc_ValueError,
220 "int() literal too large to convert");
221 return NULL;
222 }
223 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
224 return NULL;
225 return PyInt_FromString(buffer, NULL, base);
226}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000227#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000228
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229/* Methods */
230
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000231/* Integers are seen as the "smallest" of all numeric types and thus
232 don't have any knowledge about conversion of other types to
233 integers. */
234
235#define CONVERT_TO_LONG(obj, lng) \
236 if (PyInt_Check(obj)) { \
237 lng = PyInt_AS_LONG(obj); \
238 } \
239 else { \
240 Py_INCREF(Py_NotImplemented); \
241 return Py_NotImplemented; \
242 }
243
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000244/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000245static int
Fred Drakea2f55112000-07-09 15:16:51 +0000246int_print(PyIntObject *v, FILE *fp, int flags)
247 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000248{
249 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000250 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251}
252
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000253static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000254int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000255{
Tim Peters42221042001-12-01 02:52:56 +0000256 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000257 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000258 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259}
260
261static int
Fred Drakea2f55112000-07-09 15:16:51 +0000262int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263{
264 register long i = v->ob_ival;
265 register long j = w->ob_ival;
266 return (i < j) ? -1 : (i > j) ? 1 : 0;
267}
268
Guido van Rossum9bfef441993-03-29 10:43:31 +0000269static long
Fred Drakea2f55112000-07-09 15:16:51 +0000270int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000271{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000272 /* XXX If this is changed, you also need to change the way
273 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000274 long x = v -> ob_ival;
275 if (x == -1)
276 x = -2;
277 return x;
278}
279
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000281int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282{
283 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000284 CONVERT_TO_LONG(v, a);
285 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000287 if ((x^a) >= 0 || (x^b) >= 0)
288 return PyInt_FromLong(x);
289 if (err_ovf("integer addition"))
290 return NULL;
291 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292}
293
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000294static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000295int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296{
297 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000298 CONVERT_TO_LONG(v, a);
299 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000301 if ((x^a) >= 0 || (x^~b) >= 0)
302 return PyInt_FromLong(x);
303 if (err_ovf("integer subtraction"))
304 return NULL;
305 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
306 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307}
308
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000309/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000310Integer overflow checking for * is painful: Python tried a couple ways, but
311they didn't work on all platforms, or failed in endcases (a product of
312-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000313
Tim Petersa3c01ce2001-12-04 23:05:10 +0000314Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000315
Tim Petersa3c01ce2001-12-04 23:05:10 +0000316The native long product x*y is either exactly right or *way* off, being
317just the last n bits of the true product, where n is the number of bits
318in a long (the delivered product is the true product plus i*2**n for
319some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000320
Tim Petersa3c01ce2001-12-04 23:05:10 +0000321The native double product (double)x * (double)y is subject to three
322rounding errors: on a sizeof(long)==8 box, each cast to double can lose
323info, and even on a sizeof(long)==4 box, the multiplication can lose info.
324But, unlike the native long product, it's not in *range* trouble: even
325if sizeof(long)==32 (256-bit longs), the product easily fits in the
326dynamic range of a double. So the leading 50 (or so) bits of the double
327product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000328
Tim Petersa3c01ce2001-12-04 23:05:10 +0000329We check these two ways against each other, and declare victory if they're
330approximately the same. Else, because the native long product is the only
331one that can lose catastrophic amounts of information, it's the native long
332product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000333*/
334
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000335static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000336int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000338 long a, b;
339 long longprod; /* a*b in native long arithmetic */
340 double doubled_longprod; /* (double)longprod */
341 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000342
Guido van Rossum7e35d572001-09-15 03:14:32 +0000343 if (!PyInt_Check(v) &&
344 v->ob_type->tp_as_sequence &&
345 v->ob_type->tp_as_sequence->sq_repeat) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000346 /* sequence * int */
347 a = PyInt_AsLong(w);
348 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
349 }
Guido van Rossum7e35d572001-09-15 03:14:32 +0000350 if (!PyInt_Check(w) &&
351 w->ob_type->tp_as_sequence &&
352 w->ob_type->tp_as_sequence->sq_repeat) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000353 /* int * sequence */
354 a = PyInt_AsLong(v);
355 return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
356 }
357
358 CONVERT_TO_LONG(v, a);
359 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000360 longprod = a * b;
361 doubleprod = (double)a * (double)b;
362 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000363
Tim Petersa3c01ce2001-12-04 23:05:10 +0000364 /* Fast path for normal case: small multiplicands, and no info
365 is lost in either method. */
366 if (doubled_longprod == doubleprod)
367 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000368
Tim Petersa3c01ce2001-12-04 23:05:10 +0000369 /* Somebody somewhere lost info. Close enough, or way off? Note
370 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
371 The difference either is or isn't significant compared to the
372 true value (of which doubleprod is a good approximation).
373 */
374 {
375 const double diff = doubled_longprod - doubleprod;
376 const double absdiff = diff >= 0.0 ? diff : -diff;
377 const double absprod = doubleprod >= 0.0 ? doubleprod :
378 -doubleprod;
379 /* absdiff/absprod <= 1/32 iff
380 32 * absdiff <= absprod -- 5 good bits is "close enough" */
381 if (32.0 * absdiff <= absprod)
382 return PyInt_FromLong(longprod);
383 else if (err_ovf("integer multiplication"))
384 return NULL;
385 else
386 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000387 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388}
389
Guido van Rossume27f7952001-08-23 02:59:04 +0000390/* Return type of i_divmod */
391enum divmod_result {
392 DIVMOD_OK, /* Correct result */
393 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
394 DIVMOD_ERROR /* Exception raised */
395};
396
397static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000398i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000399 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000401 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000402
Tim Peters1dad6a82001-06-18 19:21:11 +0000403 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000405 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000406 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000408 /* (-sys.maxint-1)/-1 is the only overflow case. */
409 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000410 if (err_ovf("integer division"))
411 return DIVMOD_ERROR;
412 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000413 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000414 xdivy = x / y;
415 xmody = x - xdivy * y;
416 /* If the signs of x and y differ, and the remainder is non-0,
417 * C89 doesn't define whether xdivy is now the floor or the
418 * ceiling of the infinitely precise quotient. We want the floor,
419 * and we have it iff the remainder's sign matches y's.
420 */
421 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
422 xmody += y;
423 --xdivy;
424 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000425 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000426 *p_xdivy = xdivy;
427 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000428 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000429}
430
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000432int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000433{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000434 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000435 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000436 CONVERT_TO_LONG(x, xi);
437 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000438 switch (i_divmod(xi, yi, &d, &m)) {
439 case DIVMOD_OK:
440 return PyInt_FromLong(d);
441 case DIVMOD_OVERFLOW:
442 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
443 (PyObject *)y);
444 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000445 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000446 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000447}
448
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000450int_classic_div(PyIntObject *x, PyIntObject *y)
451{
452 long xi, yi;
453 long d, m;
454 CONVERT_TO_LONG(x, xi);
455 CONVERT_TO_LONG(y, yi);
456 if (Py_DivisionWarningFlag &&
457 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
458 return NULL;
459 switch (i_divmod(xi, yi, &d, &m)) {
460 case DIVMOD_OK:
461 return PyInt_FromLong(d);
462 case DIVMOD_OVERFLOW:
463 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
464 (PyObject *)y);
465 default:
466 return NULL;
467 }
468}
469
470static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000471int_true_divide(PyObject *v, PyObject *w)
472{
Tim Peterse2a60002001-09-04 06:17:36 +0000473 /* If they aren't both ints, give someone else a chance. In
474 particular, this lets int/long get handled by longs, which
475 underflows to 0 gracefully if the long is too big to convert
476 to float. */
477 if (PyInt_Check(v) && PyInt_Check(w))
478 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
479 Py_INCREF(Py_NotImplemented);
480 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000481}
482
483static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000484int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000485{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000486 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000487 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000488 CONVERT_TO_LONG(x, xi);
489 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000490 switch (i_divmod(xi, yi, &d, &m)) {
491 case DIVMOD_OK:
492 return PyInt_FromLong(m);
493 case DIVMOD_OVERFLOW:
494 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
495 (PyObject *)y);
496 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000497 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000498 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000499}
500
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000502int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000503{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000504 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000505 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000506 CONVERT_TO_LONG(x, xi);
507 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000508 switch (i_divmod(xi, yi, &d, &m)) {
509 case DIVMOD_OK:
510 return Py_BuildValue("(ll)", d, m);
511 case DIVMOD_OVERFLOW:
512 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
513 (PyObject *)y);
514 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000515 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000516 }
Guido van Rossum00466951991-05-05 20:08:27 +0000517}
518
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000520int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000522 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000523 CONVERT_TO_LONG(v, iv);
524 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000525 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000526 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000527 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
528 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000529 return NULL;
530 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000531 /* Return a float. This works because we know that
532 this calls float_pow() which converts its
533 arguments to double. */
534 return PyFloat_Type.tp_as_number->nb_power(
535 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000536 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000538 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000539 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000541 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000542 return NULL;
543 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000544 }
545 /*
546 * XXX: The original exponentiation code stopped looping
547 * when temp hit zero; this code will continue onwards
548 * unnecessarily, but at least it won't cause any errors.
549 * Hopefully the speed improvement from the fast exponentiation
550 * will compensate for the slight inefficiency.
551 * XXX: Better handling of overflows is desperately needed.
552 */
553 temp = iv;
554 ix = 1;
555 while (iw > 0) {
556 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000557 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000558 ix = ix*temp;
559 if (temp == 0)
560 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000561 if (ix / temp != prev) {
562 if (err_ovf("integer exponentiation"))
563 return NULL;
564 return PyLong_Type.tp_as_number->nb_power(
565 (PyObject *)v,
566 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000567 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000568 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000569 }
570 iw >>= 1; /* Shift exponent down by 1 bit */
571 if (iw==0) break;
572 prev = temp;
573 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000574 if (prev!=0 && temp/prev!=prev) {
575 if (err_ovf("integer exponentiation"))
576 return NULL;
577 return PyLong_Type.tp_as_number->nb_power(
578 (PyObject *)v, (PyObject *)w, (PyObject *)z);
579 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000580 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000581 /* If we did a multiplication, perform a modulo */
582 ix = ix % iz;
583 temp = temp % iz;
584 }
585 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000586 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000587 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000588 switch (i_divmod(ix, iz, &div, &mod)) {
589 case DIVMOD_OK:
590 ix = mod;
591 break;
592 case DIVMOD_OVERFLOW:
593 return PyLong_Type.tp_as_number->nb_power(
594 (PyObject *)v, (PyObject *)w, (PyObject *)z);
595 default:
596 return NULL;
597 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000598 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000600}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000603int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604{
605 register long a, x;
606 a = v->ob_ival;
607 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000608 if (a < 0 && x < 0) {
609 if (err_ovf("integer negation"))
610 return NULL;
611 return PyNumber_Negative(PyLong_FromLong(a));
612 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000613 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614}
615
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000617int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000619 if (PyInt_CheckExact(v)) {
620 Py_INCREF(v);
621 return (PyObject *)v;
622 }
623 else
624 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625}
626
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000628int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000629{
630 if (v->ob_ival >= 0)
631 return int_pos(v);
632 else
633 return int_neg(v);
634}
635
Guido van Rossum0bff0151991-05-14 12:05:32 +0000636static int
Fred Drakea2f55112000-07-09 15:16:51 +0000637int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000638{
639 return v->ob_ival != 0;
640}
641
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000643int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000644{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000646}
647
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000649int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000650{
651 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000652 CONVERT_TO_LONG(v, a);
653 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000654 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000656 return NULL;
657 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000658 if (a == 0 || b == 0)
659 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000660 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000662 }
Fred Drake1bc8fab2001-07-19 21:49:38 +0000663 a = (long)((unsigned long)a << b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000664 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000665}
666
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000668int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000669{
670 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000671 CONVERT_TO_LONG(v, a);
672 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000673 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000674 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000675 return NULL;
676 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000677 if (a == 0 || b == 0)
678 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000679 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000680 if (a < 0)
681 a = -1;
682 else
683 a = 0;
684 }
685 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000686 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000687 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688 return PyInt_FromLong(a);
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_and(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 Rossumc0b618a1997-05-02 03:12:38 +0000697 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000698}
699
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000701int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000702{
703 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000704 CONVERT_TO_LONG(v, a);
705 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000707}
708
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000710int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000711{
712 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000713 CONVERT_TO_LONG(v, a);
714 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000716}
717
Guido van Rossum1952e382001-09-19 01:25:16 +0000718static int
719int_coerce(PyObject **pv, PyObject **pw)
720{
721 if (PyInt_Check(*pw)) {
722 Py_INCREF(*pv);
723 Py_INCREF(*pw);
724 return 0;
725 }
726 return 1; /* Can't do it */
727}
728
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000729static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000730int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000731{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732 Py_INCREF(v);
733 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000734}
735
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000736static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000737int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000738{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000739 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000740}
741
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000742static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000743int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000744{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000745 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000746}
747
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000749int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000750{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000751 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000752 long x = v -> ob_ival;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000753 if (x == 0)
754 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000755 else
Barry Warsaw61975092001-11-28 20:55:34 +0000756 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000758}
759
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000760static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000761int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000762{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000763 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000764 long x = v -> ob_ival;
Barry Warsaw61975092001-11-28 20:55:34 +0000765 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000766 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000767}
768
Guido van Rossumbef14172001-08-29 15:47:46 +0000769staticforward PyObject *
770int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
771
Tim Peters6d6c1a32001-08-02 04:15:00 +0000772static PyObject *
773int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
774{
775 PyObject *x = NULL;
776 int base = -909;
777 static char *kwlist[] = {"x", "base", 0};
778
Guido van Rossumbef14172001-08-29 15:47:46 +0000779 if (type != &PyInt_Type)
780 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000781 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
782 &x, &base))
783 return NULL;
784 if (x == NULL)
785 return PyInt_FromLong(0L);
786 if (base == -909)
787 return PyNumber_Int(x);
788 if (PyString_Check(x))
789 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000790#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000791 if (PyUnicode_Check(x))
792 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
793 PyUnicode_GET_SIZE(x),
794 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000795#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000796 PyErr_SetString(PyExc_TypeError,
797 "int() can't convert non-string with explicit base");
798 return NULL;
799}
800
Guido van Rossumbef14172001-08-29 15:47:46 +0000801/* Wimpy, slow approach to tp_new calls for subtypes of int:
802 first create a regular int from whatever arguments we got,
803 then allocate a subtype instance and initialize its ob_ival
804 from the regular int. The regular int is then thrown away.
805*/
806static PyObject *
807int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
808{
809 PyObject *tmp, *new;
810
811 assert(PyType_IsSubtype(type, &PyInt_Type));
812 tmp = int_new(&PyInt_Type, args, kwds);
813 if (tmp == NULL)
814 return NULL;
815 assert(PyInt_Check(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000816 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000817 if (new == NULL)
818 return NULL;
819 ((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
820 Py_DECREF(tmp);
821 return new;
822}
823
Tim Peters6d6c1a32001-08-02 04:15:00 +0000824static char int_doc[] =
825"int(x[, base]) -> integer\n\
826\n\
827Convert a string or number to an integer, if possible. A floating point\n\
828argument will be truncated towards zero (this does not include a string\n\
829representation of a floating point number!) When converting a string, use\n\
830the optional base. It is an error to supply a base when converting a\n\
831non-string.";
832
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000833static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000834 (binaryfunc)int_add, /*nb_add*/
835 (binaryfunc)int_sub, /*nb_subtract*/
836 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000837 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000838 (binaryfunc)int_mod, /*nb_remainder*/
839 (binaryfunc)int_divmod, /*nb_divmod*/
840 (ternaryfunc)int_pow, /*nb_power*/
841 (unaryfunc)int_neg, /*nb_negative*/
842 (unaryfunc)int_pos, /*nb_positive*/
843 (unaryfunc)int_abs, /*nb_absolute*/
844 (inquiry)int_nonzero, /*nb_nonzero*/
845 (unaryfunc)int_invert, /*nb_invert*/
846 (binaryfunc)int_lshift, /*nb_lshift*/
847 (binaryfunc)int_rshift, /*nb_rshift*/
848 (binaryfunc)int_and, /*nb_and*/
849 (binaryfunc)int_xor, /*nb_xor*/
850 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +0000851 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000852 (unaryfunc)int_int, /*nb_int*/
853 (unaryfunc)int_long, /*nb_long*/
854 (unaryfunc)int_float, /*nb_float*/
855 (unaryfunc)int_oct, /*nb_oct*/
856 (unaryfunc)int_hex, /*nb_hex*/
857 0, /*nb_inplace_add*/
858 0, /*nb_inplace_subtract*/
859 0, /*nb_inplace_multiply*/
860 0, /*nb_inplace_divide*/
861 0, /*nb_inplace_remainder*/
862 0, /*nb_inplace_power*/
863 0, /*nb_inplace_lshift*/
864 0, /*nb_inplace_rshift*/
865 0, /*nb_inplace_and*/
866 0, /*nb_inplace_xor*/
867 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000868 (binaryfunc)int_div, /* nb_floor_divide */
869 int_true_divide, /* nb_true_divide */
870 0, /* nb_inplace_floor_divide */
871 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000872};
873
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000874PyTypeObject PyInt_Type = {
875 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000876 0,
877 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000878 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000879 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880 (destructor)int_dealloc, /* tp_dealloc */
881 (printfunc)int_print, /* tp_print */
882 0, /* tp_getattr */
883 0, /* tp_setattr */
884 (cmpfunc)int_compare, /* tp_compare */
885 (reprfunc)int_repr, /* tp_repr */
886 &int_as_number, /* tp_as_number */
887 0, /* tp_as_sequence */
888 0, /* tp_as_mapping */
889 (hashfunc)int_hash, /* tp_hash */
890 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +0000891 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000892 PyObject_GenericGetAttr, /* tp_getattro */
893 0, /* tp_setattro */
894 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000895 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
896 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000897 int_doc, /* tp_doc */
898 0, /* tp_traverse */
899 0, /* tp_clear */
900 0, /* tp_richcompare */
901 0, /* tp_weaklistoffset */
902 0, /* tp_iter */
903 0, /* tp_iternext */
904 0, /* tp_methods */
905 0, /* tp_members */
906 0, /* tp_getset */
907 0, /* tp_base */
908 0, /* tp_dict */
909 0, /* tp_descr_get */
910 0, /* tp_descr_set */
911 0, /* tp_dictoffset */
912 0, /* tp_init */
913 0, /* tp_alloc */
914 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +0000915 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000916};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000917
918void
Fred Drakea2f55112000-07-09 15:16:51 +0000919PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000920{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000921 PyIntObject *p;
922 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000923 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000924 int bc, bf; /* block count, number of freed blocks */
925 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000926
Guido van Rossumda084ed1999-03-10 22:55:24 +0000927#if NSMALLNEGINTS + NSMALLPOSINTS > 0
928 PyIntObject **q;
929
930 i = NSMALLNEGINTS + NSMALLPOSINTS;
931 q = small_ints;
932 while (--i >= 0) {
933 Py_XDECREF(*q);
934 *q++ = NULL;
935 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000936#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000937 bc = 0;
938 bf = 0;
939 isum = 0;
940 list = block_list;
941 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000942 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000943 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000944 bc++;
945 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000946 for (i = 0, p = &list->objects[0];
947 i < N_INTOBJECTS;
948 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000949 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +0000950 irem++;
951 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000952 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000953 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000954 list->next = block_list;
955 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000956 for (i = 0, p = &list->objects[0];
957 i < N_INTOBJECTS;
958 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000959 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000960 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000961 p->ob_type = (struct _typeobject *)
962 free_list;
963 free_list = p;
964 }
965#if NSMALLNEGINTS + NSMALLPOSINTS > 0
966 else if (-NSMALLNEGINTS <= p->ob_ival &&
967 p->ob_ival < NSMALLPOSINTS &&
968 small_ints[p->ob_ival +
969 NSMALLNEGINTS] == NULL) {
970 Py_INCREF(p);
971 small_ints[p->ob_ival +
972 NSMALLNEGINTS] = p;
973 }
974#endif
975 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000976 }
977 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000978 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000979 bf++;
980 }
981 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000982 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000983 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000984 if (!Py_VerboseFlag)
985 return;
986 fprintf(stderr, "# cleanup ints");
987 if (!isum) {
988 fprintf(stderr, "\n");
989 }
990 else {
991 fprintf(stderr,
992 ": %d unfreed int%s in %d out of %d block%s\n",
993 isum, isum == 1 ? "" : "s",
994 bc - bf, bc, bc == 1 ? "" : "s");
995 }
996 if (Py_VerboseFlag > 1) {
997 list = block_list;
998 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000999 for (i = 0, p = &list->objects[0];
1000 i < N_INTOBJECTS;
1001 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001002 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001003 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001004 "# <int at %p, refcnt=%d, val=%ld>\n",
1005 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001006 }
1007 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001008 }
1009 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001010}