blob: 19d18d30793a7f14cf88c81cf64f6a8f3646bd35 [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.)
Tim Peters29c0afc2002-04-28 16:57:34 +000029 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000030 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().
Tim Peters29c0afc2002-04-28 16:57:34 +000034
35 block_list is a singly-linked list of all PyIntBlocks ever allocated,
36 linked via their next members. PyIntBlocks are never returned to the
37 system before shutdown (PyInt_Fini).
38
39 free_list is a singly-linked list of available PyIntObjects, linked
40 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000041*/
42
43#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000044#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
45#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000046
Guido van Rossum3fce8831999-03-12 19:43:17 +000047struct _intblock {
48 struct _intblock *next;
49 PyIntObject objects[N_INTOBJECTS];
50};
51
52typedef struct _intblock PyIntBlock;
53
54static PyIntBlock *block_list = NULL;
55static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000056
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000058fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000059{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000061 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000062 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000063 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000064 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000065 ((PyIntBlock *)p)->next = block_list;
66 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000067 /* Link the int objects together, from rear to front, then return
68 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000069 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000070 q = p + N_INTOBJECTS;
71 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000072 q->ob_type = (struct _typeobject *)(q-1);
73 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000074 return p + N_INTOBJECTS - 1;
75}
76
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000077#ifndef NSMALLPOSINTS
78#define NSMALLPOSINTS 100
79#endif
80#ifndef NSMALLNEGINTS
81#define NSMALLNEGINTS 1
82#endif
83#if NSMALLNEGINTS + NSMALLPOSINTS > 0
84/* References to small integers are saved in this array so that they
85 can be shared.
86 The integers that are saved are those in the range
87 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
88*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#endif
91#ifdef COUNT_ALLOCS
92int quick_int_allocs, quick_neg_int_allocs;
93#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000094
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000096PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000099#if NSMALLNEGINTS + NSMALLPOSINTS > 0
100 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
101 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000103#ifdef COUNT_ALLOCS
104 if (ival >= 0)
105 quick_int_allocs++;
106 else
107 quick_neg_int_allocs++;
108#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000110 }
111#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112 if (free_list == NULL) {
113 if ((free_list = fill_free_list()) == NULL)
114 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000116 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000118 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000119 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000121#if NSMALLNEGINTS + NSMALLPOSINTS > 0
122 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
123 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000125 small_ints[ival + NSMALLNEGINTS] = v;
126 }
127#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129}
130
131static void
Fred Drakea2f55112000-07-09 15:16:51 +0000132int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000133{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000134 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000135 v->ob_type = (struct _typeobject *)free_list;
136 free_list = v;
137 }
138 else
139 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140}
141
Guido van Rossum93646982002-04-26 00:53:34 +0000142static void
143int_free(PyIntObject *v)
144{
145 v->ob_type = (struct _typeobject *)free_list;
146 free_list = v;
147}
148
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149long
Fred Drakea2f55112000-07-09 15:16:51 +0000150PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 PyNumberMethods *nb;
153 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000154 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000155
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 if (op && PyInt_Check(op))
157 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000158
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000159 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
160 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000161 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162 return -1;
163 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000164
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000166 if (io == NULL)
167 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000169 if (PyLong_Check(io)) {
170 /* got a long? => retry int conversion */
171 val = PyLong_AsLong((PyObject *)io);
172 if (PyErr_Occurred()) {
173 Py_DECREF(io);
174 return -1;
175 }
176 }
177 else
178 {
179 PyErr_SetString(PyExc_TypeError,
180 "nb_int should return int object");
181 return -1;
182 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000183 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000184
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000185 val = PyInt_AS_LONG(io);
186 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000187
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000188 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189}
190
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000191PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000192PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000193{
194 char *end;
195 long x;
196 char buffer[256]; /* For errors */
197
198 if ((base != 0 && base < 2) || base > 36) {
Fred Drake661ea262000-10-24 19:57:45 +0000199 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000200 return NULL;
201 }
202
203 while (*s && isspace(Py_CHARMASK(*s)))
204 s++;
205 errno = 0;
206 if (base == 0 && s[0] == '0')
207 x = (long) PyOS_strtoul(s, &end, base);
208 else
209 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000210 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000211 goto bad;
212 while (*end && isspace(Py_CHARMASK(*end)))
213 end++;
214 if (*end != '\0') {
215 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000216 PyOS_snprintf(buffer, sizeof(buffer),
217 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000218 PyErr_SetString(PyExc_ValueError, buffer);
219 return NULL;
220 }
221 else if (errno != 0) {
Walter Dörwald07e14762002-11-06 16:15:14 +0000222 if (err_ovf("string/unicode conversion"))
223 return NULL;
224 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000225 }
226 if (pend)
227 *pend = end;
228 return PyInt_FromLong(x);
229}
230
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000231#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000232PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000233PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000234{
Walter Dörwald07e14762002-11-06 16:15:14 +0000235 PyObject *result;
236 char *buffer = PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000237
Walter Dörwald07e14762002-11-06 16:15:14 +0000238 if (buffer == NULL)
239 return NULL;
240
241 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
242 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000243 return NULL;
244 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000245 result = PyInt_FromString(buffer, NULL, base);
246 PyMem_FREE(buffer);
247 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000248}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000249#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000250
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251/* Methods */
252
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000253/* Integers are seen as the "smallest" of all numeric types and thus
254 don't have any knowledge about conversion of other types to
255 integers. */
256
257#define CONVERT_TO_LONG(obj, lng) \
258 if (PyInt_Check(obj)) { \
259 lng = PyInt_AS_LONG(obj); \
260 } \
261 else { \
262 Py_INCREF(Py_NotImplemented); \
263 return Py_NotImplemented; \
264 }
265
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000266/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000267static int
Fred Drakea2f55112000-07-09 15:16:51 +0000268int_print(PyIntObject *v, FILE *fp, int flags)
269 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270{
271 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000272 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273}
274
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000276int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277{
Tim Peters42221042001-12-01 02:52:56 +0000278 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000279 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281}
282
283static int
Fred Drakea2f55112000-07-09 15:16:51 +0000284int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285{
286 register long i = v->ob_ival;
287 register long j = w->ob_ival;
288 return (i < j) ? -1 : (i > j) ? 1 : 0;
289}
290
Guido van Rossum9bfef441993-03-29 10:43:31 +0000291static long
Fred Drakea2f55112000-07-09 15:16:51 +0000292int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000293{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000294 /* XXX If this is changed, you also need to change the way
295 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000296 long x = v -> ob_ival;
297 if (x == -1)
298 x = -2;
299 return x;
300}
301
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000303int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304{
305 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000306 CONVERT_TO_LONG(v, a);
307 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000309 if ((x^a) >= 0 || (x^b) >= 0)
310 return PyInt_FromLong(x);
311 if (err_ovf("integer addition"))
312 return NULL;
313 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314}
315
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000316static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000317int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318{
319 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000320 CONVERT_TO_LONG(v, a);
321 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000323 if ((x^a) >= 0 || (x^~b) >= 0)
324 return PyInt_FromLong(x);
325 if (err_ovf("integer subtraction"))
326 return NULL;
327 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
328 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329}
330
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000331/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000332Integer overflow checking for * is painful: Python tried a couple ways, but
333they didn't work on all platforms, or failed in endcases (a product of
334-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000335
Tim Petersa3c01ce2001-12-04 23:05:10 +0000336Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000337
Tim Petersa3c01ce2001-12-04 23:05:10 +0000338The native long product x*y is either exactly right or *way* off, being
339just the last n bits of the true product, where n is the number of bits
340in a long (the delivered product is the true product plus i*2**n for
341some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000342
Tim Petersa3c01ce2001-12-04 23:05:10 +0000343The native double product (double)x * (double)y is subject to three
344rounding errors: on a sizeof(long)==8 box, each cast to double can lose
345info, and even on a sizeof(long)==4 box, the multiplication can lose info.
346But, unlike the native long product, it's not in *range* trouble: even
347if sizeof(long)==32 (256-bit longs), the product easily fits in the
348dynamic range of a double. So the leading 50 (or so) bits of the double
349product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000350
Tim Petersa3c01ce2001-12-04 23:05:10 +0000351We check these two ways against each other, and declare victory if they're
352approximately the same. Else, because the native long product is the only
353one that can lose catastrophic amounts of information, it's the native long
354product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000355*/
356
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000357static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000358int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000360 long a, b;
361 long longprod; /* a*b in native long arithmetic */
362 double doubled_longprod; /* (double)longprod */
363 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000364
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000365 CONVERT_TO_LONG(v, a);
366 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000367 longprod = a * b;
368 doubleprod = (double)a * (double)b;
369 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000370
Tim Petersa3c01ce2001-12-04 23:05:10 +0000371 /* Fast path for normal case: small multiplicands, and no info
372 is lost in either method. */
373 if (doubled_longprod == doubleprod)
374 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000375
Tim Petersa3c01ce2001-12-04 23:05:10 +0000376 /* Somebody somewhere lost info. Close enough, or way off? Note
377 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
378 The difference either is or isn't significant compared to the
379 true value (of which doubleprod is a good approximation).
380 */
381 {
382 const double diff = doubled_longprod - doubleprod;
383 const double absdiff = diff >= 0.0 ? diff : -diff;
384 const double absprod = doubleprod >= 0.0 ? doubleprod :
385 -doubleprod;
386 /* absdiff/absprod <= 1/32 iff
387 32 * absdiff <= absprod -- 5 good bits is "close enough" */
388 if (32.0 * absdiff <= absprod)
389 return PyInt_FromLong(longprod);
390 else if (err_ovf("integer multiplication"))
391 return NULL;
392 else
393 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000394 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000395}
396
Guido van Rossume27f7952001-08-23 02:59:04 +0000397/* Return type of i_divmod */
398enum divmod_result {
399 DIVMOD_OK, /* Correct result */
400 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
401 DIVMOD_ERROR /* Exception raised */
402};
403
404static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000405i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000406 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000408 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000409
Tim Peters1dad6a82001-06-18 19:21:11 +0000410 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000411 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000412 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000413 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000414 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000415 /* (-sys.maxint-1)/-1 is the only overflow case. */
416 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000417 if (err_ovf("integer division"))
418 return DIVMOD_ERROR;
419 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000420 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000421 xdivy = x / y;
422 xmody = x - xdivy * y;
423 /* If the signs of x and y differ, and the remainder is non-0,
424 * C89 doesn't define whether xdivy is now the floor or the
425 * ceiling of the infinitely precise quotient. We want the floor,
426 * and we have it iff the remainder's sign matches y's.
427 */
428 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
429 xmody += y;
430 --xdivy;
431 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000432 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000433 *p_xdivy = xdivy;
434 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000435 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000436}
437
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000439int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000440{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000441 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000442 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000443 CONVERT_TO_LONG(x, xi);
444 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000445 switch (i_divmod(xi, yi, &d, &m)) {
446 case DIVMOD_OK:
447 return PyInt_FromLong(d);
448 case DIVMOD_OVERFLOW:
449 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
450 (PyObject *)y);
451 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000452 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000453 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000454}
455
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000457int_classic_div(PyIntObject *x, PyIntObject *y)
458{
459 long xi, yi;
460 long d, m;
461 CONVERT_TO_LONG(x, xi);
462 CONVERT_TO_LONG(y, yi);
463 if (Py_DivisionWarningFlag &&
464 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
465 return NULL;
466 switch (i_divmod(xi, yi, &d, &m)) {
467 case DIVMOD_OK:
468 return PyInt_FromLong(d);
469 case DIVMOD_OVERFLOW:
470 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
471 (PyObject *)y);
472 default:
473 return NULL;
474 }
475}
476
477static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000478int_true_divide(PyObject *v, PyObject *w)
479{
Tim Peterse2a60002001-09-04 06:17:36 +0000480 /* If they aren't both ints, give someone else a chance. In
481 particular, this lets int/long get handled by longs, which
482 underflows to 0 gracefully if the long is too big to convert
483 to float. */
484 if (PyInt_Check(v) && PyInt_Check(w))
485 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
486 Py_INCREF(Py_NotImplemented);
487 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000488}
489
490static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000491int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000492{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000493 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000494 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000495 CONVERT_TO_LONG(x, xi);
496 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000497 switch (i_divmod(xi, yi, &d, &m)) {
498 case DIVMOD_OK:
499 return PyInt_FromLong(m);
500 case DIVMOD_OVERFLOW:
501 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
502 (PyObject *)y);
503 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000504 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000505 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000506}
507
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000508static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000509int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000510{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000511 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000512 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000513 CONVERT_TO_LONG(x, xi);
514 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000515 switch (i_divmod(xi, yi, &d, &m)) {
516 case DIVMOD_OK:
517 return Py_BuildValue("(ll)", d, m);
518 case DIVMOD_OVERFLOW:
519 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
520 (PyObject *)y);
521 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000522 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000523 }
Guido van Rossum00466951991-05-05 20:08:27 +0000524}
525
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000527int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000529 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000530 CONVERT_TO_LONG(v, iv);
531 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000532 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000533 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000534 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
535 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000536 return NULL;
537 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000538 /* Return a float. This works because we know that
539 this calls float_pow() which converts its
540 arguments to double. */
541 return PyFloat_Type.tp_as_number->nb_power(
542 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000543 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000545 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000546 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000548 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000549 return NULL;
550 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000551 }
552 /*
553 * XXX: The original exponentiation code stopped looping
554 * when temp hit zero; this code will continue onwards
555 * unnecessarily, but at least it won't cause any errors.
556 * Hopefully the speed improvement from the fast exponentiation
557 * will compensate for the slight inefficiency.
558 * XXX: Better handling of overflows is desperately needed.
559 */
560 temp = iv;
561 ix = 1;
562 while (iw > 0) {
563 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000564 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000565 ix = ix*temp;
566 if (temp == 0)
567 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000568 if (ix / temp != prev) {
569 if (err_ovf("integer exponentiation"))
570 return NULL;
571 return PyLong_Type.tp_as_number->nb_power(
572 (PyObject *)v,
573 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000574 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000575 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000576 }
577 iw >>= 1; /* Shift exponent down by 1 bit */
578 if (iw==0) break;
579 prev = temp;
580 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000581 if (prev!=0 && temp/prev!=prev) {
582 if (err_ovf("integer exponentiation"))
583 return NULL;
584 return PyLong_Type.tp_as_number->nb_power(
585 (PyObject *)v, (PyObject *)w, (PyObject *)z);
586 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000587 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000588 /* If we did a multiplication, perform a modulo */
589 ix = ix % iz;
590 temp = temp % iz;
591 }
592 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000593 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000594 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000595 switch (i_divmod(ix, iz, &div, &mod)) {
596 case DIVMOD_OK:
597 ix = mod;
598 break;
599 case DIVMOD_OVERFLOW:
600 return PyLong_Type.tp_as_number->nb_power(
601 (PyObject *)v, (PyObject *)w, (PyObject *)z);
602 default:
603 return NULL;
604 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000605 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000607}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000609static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000610int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000611{
612 register long a, x;
613 a = v->ob_ival;
614 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000615 if (a < 0 && x < 0) {
616 if (err_ovf("integer negation"))
617 return NULL;
618 return PyNumber_Negative(PyLong_FromLong(a));
619 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000621}
622
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000623static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000624int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000626 if (PyInt_CheckExact(v)) {
627 Py_INCREF(v);
628 return (PyObject *)v;
629 }
630 else
631 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000632}
633
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000635int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000636{
637 if (v->ob_ival >= 0)
638 return int_pos(v);
639 else
640 return int_neg(v);
641}
642
Guido van Rossum0bff0151991-05-14 12:05:32 +0000643static int
Fred Drakea2f55112000-07-09 15:16:51 +0000644int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000645{
646 return v->ob_ival != 0;
647}
648
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000650int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000651{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000653}
654
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000656int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000657{
Guido van Rossum078151d2002-08-11 04:24:12 +0000658 long a, b, c;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000659 CONVERT_TO_LONG(v, a);
660 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000661 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000662 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000663 return NULL;
664 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000665 if (a == 0 || b == 0)
666 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000667 if (b >= LONG_BIT) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000668 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000669 "x<<y losing bits or changing sign "
670 "will return a long in Python 2.4 and up") < 0)
671 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000673 }
Tim Petersda1a2212002-08-11 17:54:42 +0000674 c = a << b;
675 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000676 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000677 "x<<y losing bits or changing sign "
678 "will return a long in Python 2.4 and up") < 0)
679 return NULL;
680 }
681 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000682}
683
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000685int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000686{
687 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000688 CONVERT_TO_LONG(v, a);
689 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000690 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000691 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000692 return NULL;
693 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000694 if (a == 0 || b == 0)
695 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000696 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000697 if (a < 0)
698 a = -1;
699 else
700 a = 0;
701 }
702 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000703 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000704 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000705 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000706}
707
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000709int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000710{
711 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000712 CONVERT_TO_LONG(v, a);
713 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000715}
716
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000717static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000718int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000719{
720 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000721 CONVERT_TO_LONG(v, a);
722 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000723 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000724}
725
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000727int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000728{
729 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000730 CONVERT_TO_LONG(v, a);
731 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000733}
734
Guido van Rossum1952e382001-09-19 01:25:16 +0000735static int
736int_coerce(PyObject **pv, PyObject **pw)
737{
738 if (PyInt_Check(*pw)) {
739 Py_INCREF(*pv);
740 Py_INCREF(*pw);
741 return 0;
742 }
743 return 1; /* Can't do it */
744}
745
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000746static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000747int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000748{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000749 Py_INCREF(v);
750 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000751}
752
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000754int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000755{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000757}
758
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000759static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000760int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000761{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000762 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000763}
764
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000766int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000767{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000768 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000769 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000770 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000771 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000772 "hex()/oct() of negative int will return "
773 "a signed string in Python 2.4 and up") < 0)
774 return NULL;
775 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000776 if (x == 0)
777 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000778 else
Barry Warsaw61975092001-11-28 20:55:34 +0000779 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000780 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000781}
782
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000784int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000785{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000786 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000787 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000788 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000789 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000790 "hex()/oct() of negative int will return "
791 "a signed string in Python 2.4 and up") < 0)
792 return NULL;
793 }
Barry Warsaw61975092001-11-28 20:55:34 +0000794 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000796}
797
Jeremy Hylton938ace62002-07-17 16:30:39 +0000798static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000799int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
800
Tim Peters6d6c1a32001-08-02 04:15:00 +0000801static PyObject *
802int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
803{
804 PyObject *x = NULL;
805 int base = -909;
806 static char *kwlist[] = {"x", "base", 0};
807
Guido van Rossumbef14172001-08-29 15:47:46 +0000808 if (type != &PyInt_Type)
809 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
811 &x, &base))
812 return NULL;
813 if (x == NULL)
814 return PyInt_FromLong(0L);
815 if (base == -909)
816 return PyNumber_Int(x);
817 if (PyString_Check(x))
818 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000819#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000820 if (PyUnicode_Check(x))
821 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
822 PyUnicode_GET_SIZE(x),
823 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000824#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000825 PyErr_SetString(PyExc_TypeError,
826 "int() can't convert non-string with explicit base");
827 return NULL;
828}
829
Guido van Rossumbef14172001-08-29 15:47:46 +0000830/* Wimpy, slow approach to tp_new calls for subtypes of int:
831 first create a regular int from whatever arguments we got,
832 then allocate a subtype instance and initialize its ob_ival
833 from the regular int. The regular int is then thrown away.
834*/
835static PyObject *
836int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
837{
838 PyObject *tmp, *new;
839
840 assert(PyType_IsSubtype(type, &PyInt_Type));
841 tmp = int_new(&PyInt_Type, args, kwds);
842 if (tmp == NULL)
843 return NULL;
844 assert(PyInt_Check(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000845 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000846 if (new == NULL)
847 return NULL;
848 ((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
849 Py_DECREF(tmp);
850 return new;
851}
852
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000853PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000854"int(x[, base]) -> integer\n\
855\n\
856Convert a string or number to an integer, if possible. A floating point\n\
857argument will be truncated towards zero (this does not include a string\n\
858representation of a floating point number!) When converting a string, use\n\
859the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +0000860non-string. If the argument is outside the integer range a long object\n\
861will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000862
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000863static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000864 (binaryfunc)int_add, /*nb_add*/
865 (binaryfunc)int_sub, /*nb_subtract*/
866 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000867 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000868 (binaryfunc)int_mod, /*nb_remainder*/
869 (binaryfunc)int_divmod, /*nb_divmod*/
870 (ternaryfunc)int_pow, /*nb_power*/
871 (unaryfunc)int_neg, /*nb_negative*/
872 (unaryfunc)int_pos, /*nb_positive*/
873 (unaryfunc)int_abs, /*nb_absolute*/
874 (inquiry)int_nonzero, /*nb_nonzero*/
875 (unaryfunc)int_invert, /*nb_invert*/
876 (binaryfunc)int_lshift, /*nb_lshift*/
877 (binaryfunc)int_rshift, /*nb_rshift*/
878 (binaryfunc)int_and, /*nb_and*/
879 (binaryfunc)int_xor, /*nb_xor*/
880 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +0000881 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000882 (unaryfunc)int_int, /*nb_int*/
883 (unaryfunc)int_long, /*nb_long*/
884 (unaryfunc)int_float, /*nb_float*/
885 (unaryfunc)int_oct, /*nb_oct*/
886 (unaryfunc)int_hex, /*nb_hex*/
887 0, /*nb_inplace_add*/
888 0, /*nb_inplace_subtract*/
889 0, /*nb_inplace_multiply*/
890 0, /*nb_inplace_divide*/
891 0, /*nb_inplace_remainder*/
892 0, /*nb_inplace_power*/
893 0, /*nb_inplace_lshift*/
894 0, /*nb_inplace_rshift*/
895 0, /*nb_inplace_and*/
896 0, /*nb_inplace_xor*/
897 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000898 (binaryfunc)int_div, /* nb_floor_divide */
899 int_true_divide, /* nb_true_divide */
900 0, /* nb_inplace_floor_divide */
901 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000902};
903
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000904PyTypeObject PyInt_Type = {
905 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000906 0,
907 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000909 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000910 (destructor)int_dealloc, /* tp_dealloc */
911 (printfunc)int_print, /* tp_print */
912 0, /* tp_getattr */
913 0, /* tp_setattr */
914 (cmpfunc)int_compare, /* tp_compare */
915 (reprfunc)int_repr, /* tp_repr */
916 &int_as_number, /* tp_as_number */
917 0, /* tp_as_sequence */
918 0, /* tp_as_mapping */
919 (hashfunc)int_hash, /* tp_hash */
920 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +0000921 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000922 PyObject_GenericGetAttr, /* tp_getattro */
923 0, /* tp_setattro */
924 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000925 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
926 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000927 int_doc, /* tp_doc */
928 0, /* tp_traverse */
929 0, /* tp_clear */
930 0, /* tp_richcompare */
931 0, /* tp_weaklistoffset */
932 0, /* tp_iter */
933 0, /* tp_iternext */
934 0, /* tp_methods */
935 0, /* tp_members */
936 0, /* tp_getset */
937 0, /* tp_base */
938 0, /* tp_dict */
939 0, /* tp_descr_get */
940 0, /* tp_descr_set */
941 0, /* tp_dictoffset */
942 0, /* tp_init */
943 0, /* tp_alloc */
944 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +0000945 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000946};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000947
948void
Fred Drakea2f55112000-07-09 15:16:51 +0000949PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000950{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000951 PyIntObject *p;
952 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000953 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000954 int bc, bf; /* block count, number of freed blocks */
955 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000956
Guido van Rossumda084ed1999-03-10 22:55:24 +0000957#if NSMALLNEGINTS + NSMALLPOSINTS > 0
958 PyIntObject **q;
959
960 i = NSMALLNEGINTS + NSMALLPOSINTS;
961 q = small_ints;
962 while (--i >= 0) {
963 Py_XDECREF(*q);
964 *q++ = NULL;
965 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000966#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000967 bc = 0;
968 bf = 0;
969 isum = 0;
970 list = block_list;
971 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000972 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000973 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000974 bc++;
975 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000976 for (i = 0, p = &list->objects[0];
977 i < N_INTOBJECTS;
978 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000979 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +0000980 irem++;
981 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000982 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000983 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000984 list->next = block_list;
985 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000986 for (i = 0, p = &list->objects[0];
987 i < N_INTOBJECTS;
988 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000989 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +0000990 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +0000991 p->ob_type = (struct _typeobject *)
992 free_list;
993 free_list = p;
994 }
995#if NSMALLNEGINTS + NSMALLPOSINTS > 0
996 else if (-NSMALLNEGINTS <= p->ob_ival &&
997 p->ob_ival < NSMALLPOSINTS &&
998 small_ints[p->ob_ival +
999 NSMALLNEGINTS] == NULL) {
1000 Py_INCREF(p);
1001 small_ints[p->ob_ival +
1002 NSMALLNEGINTS] = p;
1003 }
1004#endif
1005 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001006 }
1007 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001008 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001009 bf++;
1010 }
1011 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001012 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001013 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001014 if (!Py_VerboseFlag)
1015 return;
1016 fprintf(stderr, "# cleanup ints");
1017 if (!isum) {
1018 fprintf(stderr, "\n");
1019 }
1020 else {
1021 fprintf(stderr,
1022 ": %d unfreed int%s in %d out of %d block%s\n",
1023 isum, isum == 1 ? "" : "s",
1024 bc - bf, bc, bc == 1 ? "" : "s");
1025 }
1026 if (Py_VerboseFlag > 1) {
1027 list = block_list;
1028 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001029 for (i = 0, p = &list->objects[0];
1030 i < N_INTOBJECTS;
1031 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001032 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001033 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001034 "# <int at %p, refcnt=%d, val=%ld>\n",
1035 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001036 }
1037 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001038 }
1039 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001040}