blob: 6c8bdbeb2abf030595ff6c60846407f698353f88 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Integer object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Barry Warsaw226ae6c1999-10-12 19:54:53 +00005#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum2e1d4331993-12-24 10:22:45 +00007long
Fred Drakea2f55112000-07-09 15:16:51 +00008PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +00009{
10 return LONG_MAX; /* To initialize sys.maxint */
11}
12
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013/* Standard Booleans */
Guido van Rossum3f5da241990-12-20 15:06:42 +000014
Guido van Rossumc0b618a1997-05-02 03:12:38 +000015PyIntObject _Py_ZeroStruct = {
16 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000017 0
18};
Guido van Rossum3f5da241990-12-20 15:06:42 +000019
Guido van Rossumc0b618a1997-05-02 03:12:38 +000020PyIntObject _Py_TrueStruct = {
21 PyObject_HEAD_INIT(&PyInt_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022 1
23};
24
Guido van Rossume27f7952001-08-23 02:59:04 +000025/* Return 1 if exception raised, 0 if caller should retry using longs */
26static int
Fred Drakea2f55112000-07-09 15:16:51 +000027err_ovf(char *msg)
Guido van Rossum165e67e1990-10-14 20:02:26 +000028{
Guido van Rossume27f7952001-08-23 02:59:04 +000029 if (PyErr_Warn(PyExc_OverflowWarning, msg) < 0) {
Guido van Rossum0b131162001-08-23 21:32:40 +000030 if (PyErr_ExceptionMatches(PyExc_OverflowWarning))
31 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossume27f7952001-08-23 02:59:04 +000032 return 1;
33 }
34 else
35 return 0;
Guido van Rossum165e67e1990-10-14 20:02:26 +000036}
37
Guido van Rossum3f5da241990-12-20 15:06:42 +000038/* Integers are quite normal objects, to make object handling uniform.
39 (Using odd pointers to represent integers would save much space
40 but require extra checks for this special case throughout the code.)
41 Since, a typical Python program spends much of its time allocating
42 and deallocating integers, these operations should be very fast.
43 Therefore we use a dedicated allocation scheme with a much lower
44 overhead (in space and time) than straight malloc(): a simple
45 dedicated free list, filled when necessary with memory from malloc().
46*/
47
48#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000049#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
50#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000051
Guido van Rossum3fce8831999-03-12 19:43:17 +000052struct _intblock {
53 struct _intblock *next;
54 PyIntObject objects[N_INTOBJECTS];
55};
56
57typedef struct _intblock PyIntBlock;
58
59static PyIntBlock *block_list = NULL;
60static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000061
Guido van Rossumc0b618a1997-05-02 03:12:38 +000062static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000063fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000064{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000065 PyIntObject *p, *q;
Guido van Rossumb18618d2000-05-03 23:44:39 +000066 /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */
67 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000068 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000069 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000070 ((PyIntBlock *)p)->next = block_list;
71 block_list = (PyIntBlock *)p;
72 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000073 q = p + N_INTOBJECTS;
74 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000075 q->ob_type = (struct _typeobject *)(q-1);
76 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000077 return p + N_INTOBJECTS - 1;
78}
79
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000080#ifndef NSMALLPOSINTS
81#define NSMALLPOSINTS 100
82#endif
83#ifndef NSMALLNEGINTS
84#define NSMALLNEGINTS 1
85#endif
86#if NSMALLNEGINTS + NSMALLPOSINTS > 0
87/* References to small integers are saved in this array so that they
88 can be shared.
89 The integers that are saved are those in the range
90 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
91*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000093#endif
94#ifdef COUNT_ALLOCS
95int quick_int_allocs, quick_neg_int_allocs;
96#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000097
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000099PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000101 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000102#if NSMALLNEGINTS + NSMALLPOSINTS > 0
103 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
104 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000105 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000106#ifdef COUNT_ALLOCS
107 if (ival >= 0)
108 quick_int_allocs++;
109 else
110 quick_neg_int_allocs++;
111#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000113 }
114#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000115 if (free_list == NULL) {
116 if ((free_list = fill_free_list()) == NULL)
117 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118 }
Guido van Rossumb18618d2000-05-03 23:44:39 +0000119 /* PyObject_New is inlined */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000121 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000122 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000123 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000124#if NSMALLNEGINTS + NSMALLPOSINTS > 0
125 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
126 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000128 small_ints[ival + NSMALLNEGINTS] = v;
129 }
130#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000131 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132}
133
134static void
Fred Drakea2f55112000-07-09 15:16:51 +0000135int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000136{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000137 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000138 v->ob_type = (struct _typeobject *)free_list;
139 free_list = v;
140 }
141 else
142 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143}
144
145long
Fred Drakea2f55112000-07-09 15:16:51 +0000146PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148 PyNumberMethods *nb;
149 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000150 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000151
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 if (op && PyInt_Check(op))
153 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000154
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000155 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
156 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000157 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000158 return -1;
159 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000160
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000162 if (io == NULL)
163 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 if (!PyInt_Check(io)) {
165 PyErr_SetString(PyExc_TypeError,
166 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000167 return -1;
168 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000169
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000170 val = PyInt_AS_LONG(io);
171 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000172
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000173 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174}
175
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000176PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000177PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000178{
179 char *end;
180 long x;
181 char buffer[256]; /* For errors */
182
183 if ((base != 0 && base < 2) || base > 36) {
Fred Drake661ea262000-10-24 19:57:45 +0000184 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000185 return NULL;
186 }
187
188 while (*s && isspace(Py_CHARMASK(*s)))
189 s++;
190 errno = 0;
191 if (base == 0 && s[0] == '0')
192 x = (long) PyOS_strtoul(s, &end, base);
193 else
194 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000195 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000196 goto bad;
197 while (*end && isspace(Py_CHARMASK(*end)))
198 end++;
199 if (*end != '\0') {
200 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000201 PyOS_snprintf(buffer, sizeof(buffer),
202 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000203 PyErr_SetString(PyExc_ValueError, buffer);
204 return NULL;
205 }
206 else if (errno != 0) {
Barry Warsaw61975092001-11-28 20:55:34 +0000207 PyOS_snprintf(buffer, sizeof(buffer),
208 "int() literal too large: %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000209 PyErr_SetString(PyExc_ValueError, buffer);
210 return NULL;
211 }
212 if (pend)
213 *pend = end;
214 return PyInt_FromLong(x);
215}
216
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000217#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000218PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000219PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000220{
221 char buffer[256];
Tim Petersa3c01ce2001-12-04 23:05:10 +0000222
Guido van Rossum9e896b32000-04-05 20:11:21 +0000223 if (length >= sizeof(buffer)) {
224 PyErr_SetString(PyExc_ValueError,
225 "int() literal too large to convert");
226 return NULL;
227 }
228 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL))
229 return NULL;
230 return PyInt_FromString(buffer, NULL, base);
231}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000232#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000233
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000234/* Methods */
235
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000236/* Integers are seen as the "smallest" of all numeric types and thus
237 don't have any knowledge about conversion of other types to
238 integers. */
239
240#define CONVERT_TO_LONG(obj, lng) \
241 if (PyInt_Check(obj)) { \
242 lng = PyInt_AS_LONG(obj); \
243 } \
244 else { \
245 Py_INCREF(Py_NotImplemented); \
246 return Py_NotImplemented; \
247 }
248
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000249/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000250static int
Fred Drakea2f55112000-07-09 15:16:51 +0000251int_print(PyIntObject *v, FILE *fp, int flags)
252 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000253{
254 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000255 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000256}
257
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000258static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000259int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260{
Tim Peters42221042001-12-01 02:52:56 +0000261 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000262 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000263 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264}
265
266static int
Fred Drakea2f55112000-07-09 15:16:51 +0000267int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000268{
269 register long i = v->ob_ival;
270 register long j = w->ob_ival;
271 return (i < j) ? -1 : (i > j) ? 1 : 0;
272}
273
Guido van Rossum9bfef441993-03-29 10:43:31 +0000274static long
Fred Drakea2f55112000-07-09 15:16:51 +0000275int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000276{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000277 /* XXX If this is changed, you also need to change the way
278 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000279 long x = v -> ob_ival;
280 if (x == -1)
281 x = -2;
282 return x;
283}
284
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000285static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000286int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287{
288 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000289 CONVERT_TO_LONG(v, a);
290 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000291 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000292 if ((x^a) >= 0 || (x^b) >= 0)
293 return PyInt_FromLong(x);
294 if (err_ovf("integer addition"))
295 return NULL;
296 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297}
298
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000299static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000300int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301{
302 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000303 CONVERT_TO_LONG(v, a);
304 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000306 if ((x^a) >= 0 || (x^~b) >= 0)
307 return PyInt_FromLong(x);
308 if (err_ovf("integer subtraction"))
309 return NULL;
310 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
311 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000312}
313
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000314/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000315Integer overflow checking for * is painful: Python tried a couple ways, but
316they didn't work on all platforms, or failed in endcases (a product of
317-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000318
Tim Petersa3c01ce2001-12-04 23:05:10 +0000319Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000320
Tim Petersa3c01ce2001-12-04 23:05:10 +0000321The native long product x*y is either exactly right or *way* off, being
322just the last n bits of the true product, where n is the number of bits
323in a long (the delivered product is the true product plus i*2**n for
324some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000325
Tim Petersa3c01ce2001-12-04 23:05:10 +0000326The native double product (double)x * (double)y is subject to three
327rounding errors: on a sizeof(long)==8 box, each cast to double can lose
328info, and even on a sizeof(long)==4 box, the multiplication can lose info.
329But, unlike the native long product, it's not in *range* trouble: even
330if sizeof(long)==32 (256-bit longs), the product easily fits in the
331dynamic range of a double. So the leading 50 (or so) bits of the double
332product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000333
Tim Petersa3c01ce2001-12-04 23:05:10 +0000334We check these two ways against each other, and declare victory if they're
335approximately the same. Else, because the native long product is the only
336one that can lose catastrophic amounts of information, it's the native long
337product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000338*/
339
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000340static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000341int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000342{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000343 long a, b;
344 long longprod; /* a*b in native long arithmetic */
345 double doubled_longprod; /* (double)longprod */
346 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000347
Guido van Rossum7e35d572001-09-15 03:14:32 +0000348 if (!PyInt_Check(v) &&
349 v->ob_type->tp_as_sequence &&
350 v->ob_type->tp_as_sequence->sq_repeat) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000351 /* sequence * int */
352 a = PyInt_AsLong(w);
353 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
354 }
Guido van Rossum7e35d572001-09-15 03:14:32 +0000355 if (!PyInt_Check(w) &&
356 w->ob_type->tp_as_sequence &&
357 w->ob_type->tp_as_sequence->sq_repeat) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000358 /* int * sequence */
359 a = PyInt_AsLong(v);
360 return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a);
361 }
362
363 CONVERT_TO_LONG(v, a);
364 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000365 longprod = a * b;
366 doubleprod = (double)a * (double)b;
367 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000368
Tim Petersa3c01ce2001-12-04 23:05:10 +0000369 /* Fast path for normal case: small multiplicands, and no info
370 is lost in either method. */
371 if (doubled_longprod == doubleprod)
372 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000373
Tim Petersa3c01ce2001-12-04 23:05:10 +0000374 /* Somebody somewhere lost info. Close enough, or way off? Note
375 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
376 The difference either is or isn't significant compared to the
377 true value (of which doubleprod is a good approximation).
378 */
379 {
380 const double diff = doubled_longprod - doubleprod;
381 const double absdiff = diff >= 0.0 ? diff : -diff;
382 const double absprod = doubleprod >= 0.0 ? doubleprod :
383 -doubleprod;
384 /* absdiff/absprod <= 1/32 iff
385 32 * absdiff <= absprod -- 5 good bits is "close enough" */
386 if (32.0 * absdiff <= absprod)
387 return PyInt_FromLong(longprod);
388 else if (err_ovf("integer multiplication"))
389 return NULL;
390 else
391 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000392 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393}
394
Guido van Rossume27f7952001-08-23 02:59:04 +0000395/* Return type of i_divmod */
396enum divmod_result {
397 DIVMOD_OK, /* Correct result */
398 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
399 DIVMOD_ERROR /* Exception raised */
400};
401
402static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000403i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000404 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000406 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000407
Tim Peters1dad6a82001-06-18 19:21:11 +0000408 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000409 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000410 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000411 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000413 /* (-sys.maxint-1)/-1 is the only overflow case. */
414 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000415 if (err_ovf("integer division"))
416 return DIVMOD_ERROR;
417 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000418 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000419 xdivy = x / y;
420 xmody = x - xdivy * y;
421 /* If the signs of x and y differ, and the remainder is non-0,
422 * C89 doesn't define whether xdivy is now the floor or the
423 * ceiling of the infinitely precise quotient. We want the floor,
424 * and we have it iff the remainder's sign matches y's.
425 */
426 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
427 xmody += y;
428 --xdivy;
429 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000430 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000431 *p_xdivy = xdivy;
432 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000433 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000434}
435
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000436static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000437int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000438{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000439 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000440 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000441 CONVERT_TO_LONG(x, xi);
442 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000443 switch (i_divmod(xi, yi, &d, &m)) {
444 case DIVMOD_OK:
445 return PyInt_FromLong(d);
446 case DIVMOD_OVERFLOW:
447 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
448 (PyObject *)y);
449 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000450 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000451 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000452}
453
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000455int_classic_div(PyIntObject *x, PyIntObject *y)
456{
457 long xi, yi;
458 long d, m;
459 CONVERT_TO_LONG(x, xi);
460 CONVERT_TO_LONG(y, yi);
461 if (Py_DivisionWarningFlag &&
462 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
463 return NULL;
464 switch (i_divmod(xi, yi, &d, &m)) {
465 case DIVMOD_OK:
466 return PyInt_FromLong(d);
467 case DIVMOD_OVERFLOW:
468 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
469 (PyObject *)y);
470 default:
471 return NULL;
472 }
473}
474
475static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000476int_true_divide(PyObject *v, PyObject *w)
477{
Tim Peterse2a60002001-09-04 06:17:36 +0000478 /* If they aren't both ints, give someone else a chance. In
479 particular, this lets int/long get handled by longs, which
480 underflows to 0 gracefully if the long is too big to convert
481 to float. */
482 if (PyInt_Check(v) && PyInt_Check(w))
483 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
484 Py_INCREF(Py_NotImplemented);
485 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000486}
487
488static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000489int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000490{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000491 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000492 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000493 CONVERT_TO_LONG(x, xi);
494 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000495 switch (i_divmod(xi, yi, &d, &m)) {
496 case DIVMOD_OK:
497 return PyInt_FromLong(m);
498 case DIVMOD_OVERFLOW:
499 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
500 (PyObject *)y);
501 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000502 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000503 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000504}
505
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000506static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000507int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000508{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000509 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000510 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000511 CONVERT_TO_LONG(x, xi);
512 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000513 switch (i_divmod(xi, yi, &d, &m)) {
514 case DIVMOD_OK:
515 return Py_BuildValue("(ll)", d, m);
516 case DIVMOD_OVERFLOW:
517 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
518 (PyObject *)y);
519 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000520 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000521 }
Guido van Rossum00466951991-05-05 20:08:27 +0000522}
523
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000525int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000526{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000527 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000528 CONVERT_TO_LONG(v, iv);
529 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000530 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000531 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000532 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
533 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000534 return NULL;
535 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000536 /* Return a float. This works because we know that
537 this calls float_pow() which converts its
538 arguments to double. */
539 return PyFloat_Type.tp_as_number->nb_power(
540 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000541 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000542 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000543 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000544 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000546 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000547 return NULL;
548 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000549 }
550 /*
551 * XXX: The original exponentiation code stopped looping
552 * when temp hit zero; this code will continue onwards
553 * unnecessarily, but at least it won't cause any errors.
554 * Hopefully the speed improvement from the fast exponentiation
555 * will compensate for the slight inefficiency.
556 * XXX: Better handling of overflows is desperately needed.
557 */
558 temp = iv;
559 ix = 1;
560 while (iw > 0) {
561 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000562 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000563 ix = ix*temp;
564 if (temp == 0)
565 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000566 if (ix / temp != prev) {
567 if (err_ovf("integer exponentiation"))
568 return NULL;
569 return PyLong_Type.tp_as_number->nb_power(
570 (PyObject *)v,
571 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000572 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000573 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000574 }
575 iw >>= 1; /* Shift exponent down by 1 bit */
576 if (iw==0) break;
577 prev = temp;
578 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000579 if (prev!=0 && temp/prev!=prev) {
580 if (err_ovf("integer exponentiation"))
581 return NULL;
582 return PyLong_Type.tp_as_number->nb_power(
583 (PyObject *)v, (PyObject *)w, (PyObject *)z);
584 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000585 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000586 /* If we did a multiplication, perform a modulo */
587 ix = ix % iz;
588 temp = temp % iz;
589 }
590 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000591 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000592 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000593 switch (i_divmod(ix, iz, &div, &mod)) {
594 case DIVMOD_OK:
595 ix = mod;
596 break;
597 case DIVMOD_OVERFLOW:
598 return PyLong_Type.tp_as_number->nb_power(
599 (PyObject *)v, (PyObject *)w, (PyObject *)z);
600 default:
601 return NULL;
602 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000603 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000605}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000606
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000607static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000608int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000609{
610 register long a, x;
611 a = v->ob_ival;
612 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000613 if (a < 0 && x < 0) {
614 if (err_ovf("integer negation"))
615 return NULL;
616 return PyNumber_Negative(PyLong_FromLong(a));
617 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000618 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619}
620
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000622int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000623{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000624 if (PyInt_CheckExact(v)) {
625 Py_INCREF(v);
626 return (PyObject *)v;
627 }
628 else
629 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630}
631
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000632static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000633int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000634{
635 if (v->ob_ival >= 0)
636 return int_pos(v);
637 else
638 return int_neg(v);
639}
640
Guido van Rossum0bff0151991-05-14 12:05:32 +0000641static int
Fred Drakea2f55112000-07-09 15:16:51 +0000642int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000643{
644 return v->ob_ival != 0;
645}
646
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000648int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000649{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000651}
652
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000654int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000655{
656 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000657 CONVERT_TO_LONG(v, a);
658 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000659 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000660 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000661 return NULL;
662 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000663 if (a == 0 || b == 0)
664 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000665 if (b >= LONG_BIT) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000667 }
Fred Drake1bc8fab2001-07-19 21:49:38 +0000668 a = (long)((unsigned long)a << b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000670}
671
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000673int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000674{
675 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000676 CONVERT_TO_LONG(v, a);
677 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000678 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000680 return NULL;
681 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000682 if (a == 0 || b == 0)
683 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000684 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000685 if (a < 0)
686 a = -1;
687 else
688 a = 0;
689 }
690 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000691 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000692 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000694}
695
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000697int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000698{
699 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000700 CONVERT_TO_LONG(v, a);
701 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000703}
704
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000705static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000706int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000707{
708 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000709 CONVERT_TO_LONG(v, a);
710 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000712}
713
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000715int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000716{
717 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000718 CONVERT_TO_LONG(v, a);
719 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000720 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000721}
722
Guido van Rossum1952e382001-09-19 01:25:16 +0000723static int
724int_coerce(PyObject **pv, PyObject **pw)
725{
726 if (PyInt_Check(*pw)) {
727 Py_INCREF(*pv);
728 Py_INCREF(*pw);
729 return 0;
730 }
731 return 1; /* Can't do it */
732}
733
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000734static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000735int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000736{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737 Py_INCREF(v);
738 return (PyObject *)v;
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_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000743{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000744 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000745}
746
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000748int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000749{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750 return PyFloat_FromDouble((double)(v -> ob_ival));
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_oct(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;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000758 if (x == 0)
759 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000760 else
Barry Warsaw61975092001-11-28 20:55:34 +0000761 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000762 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000763}
764
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000766int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000767{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000768 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000769 long x = v -> ob_ival;
Barry Warsaw61975092001-11-28 20:55:34 +0000770 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000771 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000772}
773
Guido van Rossumbef14172001-08-29 15:47:46 +0000774staticforward PyObject *
775int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
776
Tim Peters6d6c1a32001-08-02 04:15:00 +0000777static PyObject *
778int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
779{
780 PyObject *x = NULL;
781 int base = -909;
782 static char *kwlist[] = {"x", "base", 0};
783
Guido van Rossumbef14172001-08-29 15:47:46 +0000784 if (type != &PyInt_Type)
785 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
787 &x, &base))
788 return NULL;
789 if (x == NULL)
790 return PyInt_FromLong(0L);
791 if (base == -909)
792 return PyNumber_Int(x);
793 if (PyString_Check(x))
794 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000795#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000796 if (PyUnicode_Check(x))
797 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
798 PyUnicode_GET_SIZE(x),
799 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000800#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000801 PyErr_SetString(PyExc_TypeError,
802 "int() can't convert non-string with explicit base");
803 return NULL;
804}
805
Guido van Rossumbef14172001-08-29 15:47:46 +0000806/* Wimpy, slow approach to tp_new calls for subtypes of int:
807 first create a regular int from whatever arguments we got,
808 then allocate a subtype instance and initialize its ob_ival
809 from the regular int. The regular int is then thrown away.
810*/
811static PyObject *
812int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
813{
814 PyObject *tmp, *new;
815
816 assert(PyType_IsSubtype(type, &PyInt_Type));
817 tmp = int_new(&PyInt_Type, args, kwds);
818 if (tmp == NULL)
819 return NULL;
820 assert(PyInt_Check(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000821 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000822 if (new == NULL)
823 return NULL;
824 ((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
825 Py_DECREF(tmp);
826 return new;
827}
828
Tim Peters6d6c1a32001-08-02 04:15:00 +0000829static char int_doc[] =
830"int(x[, base]) -> integer\n\
831\n\
832Convert a string or number to an integer, if possible. A floating point\n\
833argument will be truncated towards zero (this does not include a string\n\
834representation of a floating point number!) When converting a string, use\n\
835the optional base. It is an error to supply a base when converting a\n\
836non-string.";
837
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000838static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000839 (binaryfunc)int_add, /*nb_add*/
840 (binaryfunc)int_sub, /*nb_subtract*/
841 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000842 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000843 (binaryfunc)int_mod, /*nb_remainder*/
844 (binaryfunc)int_divmod, /*nb_divmod*/
845 (ternaryfunc)int_pow, /*nb_power*/
846 (unaryfunc)int_neg, /*nb_negative*/
847 (unaryfunc)int_pos, /*nb_positive*/
848 (unaryfunc)int_abs, /*nb_absolute*/
849 (inquiry)int_nonzero, /*nb_nonzero*/
850 (unaryfunc)int_invert, /*nb_invert*/
851 (binaryfunc)int_lshift, /*nb_lshift*/
852 (binaryfunc)int_rshift, /*nb_rshift*/
853 (binaryfunc)int_and, /*nb_and*/
854 (binaryfunc)int_xor, /*nb_xor*/
855 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +0000856 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000857 (unaryfunc)int_int, /*nb_int*/
858 (unaryfunc)int_long, /*nb_long*/
859 (unaryfunc)int_float, /*nb_float*/
860 (unaryfunc)int_oct, /*nb_oct*/
861 (unaryfunc)int_hex, /*nb_hex*/
862 0, /*nb_inplace_add*/
863 0, /*nb_inplace_subtract*/
864 0, /*nb_inplace_multiply*/
865 0, /*nb_inplace_divide*/
866 0, /*nb_inplace_remainder*/
867 0, /*nb_inplace_power*/
868 0, /*nb_inplace_lshift*/
869 0, /*nb_inplace_rshift*/
870 0, /*nb_inplace_and*/
871 0, /*nb_inplace_xor*/
872 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000873 (binaryfunc)int_div, /* nb_floor_divide */
874 int_true_divide, /* nb_true_divide */
875 0, /* nb_inplace_floor_divide */
876 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000877};
878
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000879PyTypeObject PyInt_Type = {
880 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000881 0,
882 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000883 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000884 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000885 (destructor)int_dealloc, /* tp_dealloc */
886 (printfunc)int_print, /* tp_print */
887 0, /* tp_getattr */
888 0, /* tp_setattr */
889 (cmpfunc)int_compare, /* tp_compare */
890 (reprfunc)int_repr, /* tp_repr */
891 &int_as_number, /* tp_as_number */
892 0, /* tp_as_sequence */
893 0, /* tp_as_mapping */
894 (hashfunc)int_hash, /* tp_hash */
895 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +0000896 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000897 PyObject_GenericGetAttr, /* tp_getattro */
898 0, /* tp_setattro */
899 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000900 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
901 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000902 int_doc, /* tp_doc */
903 0, /* tp_traverse */
904 0, /* tp_clear */
905 0, /* tp_richcompare */
906 0, /* tp_weaklistoffset */
907 0, /* tp_iter */
908 0, /* tp_iternext */
909 0, /* tp_methods */
910 0, /* tp_members */
911 0, /* tp_getset */
912 0, /* tp_base */
913 0, /* tp_dict */
914 0, /* tp_descr_get */
915 0, /* tp_descr_set */
916 0, /* tp_dictoffset */
917 0, /* tp_init */
918 0, /* tp_alloc */
919 int_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000920};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000921
922void
Fred Drakea2f55112000-07-09 15:16:51 +0000923PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000924{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000925 PyIntObject *p;
926 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000927 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000928 int bc, bf; /* block count, number of freed blocks */
929 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000930
Guido van Rossumda084ed1999-03-10 22:55:24 +0000931#if NSMALLNEGINTS + NSMALLPOSINTS > 0
932 PyIntObject **q;
933
934 i = NSMALLNEGINTS + NSMALLPOSINTS;
935 q = small_ints;
936 while (--i >= 0) {
937 Py_XDECREF(*q);
938 *q++ = NULL;
939 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000940#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000941 bc = 0;
942 bf = 0;
943 isum = 0;
944 list = block_list;
945 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000946 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000947 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000948 bc++;
949 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000950 for (i = 0, p = &list->objects[0];
951 i < N_INTOBJECTS;
952 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000953 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +0000954 irem++;
955 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000956 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000957 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000958 list->next = block_list;
959 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000960 for (i = 0, p = &list->objects[0];
961 i < N_INTOBJECTS;
962 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000963 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000964 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000965 p->ob_type = (struct _typeobject *)
966 free_list;
967 free_list = p;
968 }
969#if NSMALLNEGINTS + NSMALLPOSINTS > 0
970 else if (-NSMALLNEGINTS <= p->ob_ival &&
971 p->ob_ival < NSMALLPOSINTS &&
972 small_ints[p->ob_ival +
973 NSMALLNEGINTS] == NULL) {
974 Py_INCREF(p);
975 small_ints[p->ob_ival +
976 NSMALLNEGINTS] = p;
977 }
978#endif
979 }
Guido van Rossumda084ed1999-03-10 22:55:24 +0000980 }
981 else {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000982 PyMem_FREE(list); /* XXX PyObject_FREE ??? */
Guido van Rossumda084ed1999-03-10 22:55:24 +0000983 bf++;
984 }
985 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +0000986 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000987 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000988 if (!Py_VerboseFlag)
989 return;
990 fprintf(stderr, "# cleanup ints");
991 if (!isum) {
992 fprintf(stderr, "\n");
993 }
994 else {
995 fprintf(stderr,
996 ": %d unfreed int%s in %d out of %d block%s\n",
997 isum, isum == 1 ? "" : "s",
998 bc - bf, bc, bc == 1 ? "" : "s");
999 }
1000 if (Py_VerboseFlag > 1) {
1001 list = block_list;
1002 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001003 for (i = 0, p = &list->objects[0];
1004 i < N_INTOBJECTS;
1005 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001006 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001007 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001008 "# <int at %p, refcnt=%d, val=%ld>\n",
1009 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001010 }
1011 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001012 }
1013 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001014}