blob: 805f3b7a28add46e70abf00f19f41258345a1c15 [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
Neal Norwitzc91ed402002-12-30 22:29:22 +000081#define NSMALLNEGINTS 5
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000082#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
Neal Norwitzc91ed402002-12-30 22:29:22 +0000100 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
101 v = small_ints[ival + NSMALLNEGINTS];
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;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000121 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000122}
123
124static void
Fred Drakea2f55112000-07-09 15:16:51 +0000125int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000126{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000127 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000128 v->ob_type = (struct _typeobject *)free_list;
129 free_list = v;
130 }
131 else
132 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
Guido van Rossum93646982002-04-26 00:53:34 +0000135static void
136int_free(PyIntObject *v)
137{
138 v->ob_type = (struct _typeobject *)free_list;
139 free_list = v;
140}
141
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142long
Fred Drakea2f55112000-07-09 15:16:51 +0000143PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 PyNumberMethods *nb;
146 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000147 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000148
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000149 if (op && PyInt_Check(op))
150 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000151
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000152 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
153 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000154 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155 return -1;
156 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000157
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000158 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000159 if (io == NULL)
160 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000161 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000162 if (PyLong_Check(io)) {
163 /* got a long? => retry int conversion */
164 val = PyLong_AsLong((PyObject *)io);
165 if (PyErr_Occurred()) {
166 Py_DECREF(io);
167 return -1;
168 }
169 }
170 else
171 {
172 PyErr_SetString(PyExc_TypeError,
173 "nb_int should return int object");
174 return -1;
175 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000176 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000177
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 val = PyInt_AS_LONG(io);
179 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000180
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000181 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000182}
183
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000184PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000185PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000186{
187 char *end;
188 long x;
189 char buffer[256]; /* For errors */
190
191 if ((base != 0 && base < 2) || base > 36) {
Fred Drake661ea262000-10-24 19:57:45 +0000192 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000193 return NULL;
194 }
195
196 while (*s && isspace(Py_CHARMASK(*s)))
197 s++;
198 errno = 0;
199 if (base == 0 && s[0] == '0')
200 x = (long) PyOS_strtoul(s, &end, base);
201 else
202 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000203 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000204 goto bad;
205 while (*end && isspace(Py_CHARMASK(*end)))
206 end++;
207 if (*end != '\0') {
208 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000209 PyOS_snprintf(buffer, sizeof(buffer),
210 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000211 PyErr_SetString(PyExc_ValueError, buffer);
212 return NULL;
213 }
214 else if (errno != 0) {
Walter Dörwald07e14762002-11-06 16:15:14 +0000215 if (err_ovf("string/unicode conversion"))
216 return NULL;
217 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000218 }
219 if (pend)
220 *pend = end;
221 return PyInt_FromLong(x);
222}
223
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000224#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000225PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000226PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000227{
Walter Dörwald07e14762002-11-06 16:15:14 +0000228 PyObject *result;
229 char *buffer = PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000230
Walter Dörwald07e14762002-11-06 16:15:14 +0000231 if (buffer == NULL)
232 return NULL;
233
234 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
235 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000236 return NULL;
237 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000238 result = PyInt_FromString(buffer, NULL, base);
239 PyMem_FREE(buffer);
240 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000241}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000242#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000243
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244/* Methods */
245
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000246/* Integers are seen as the "smallest" of all numeric types and thus
247 don't have any knowledge about conversion of other types to
248 integers. */
249
250#define CONVERT_TO_LONG(obj, lng) \
251 if (PyInt_Check(obj)) { \
252 lng = PyInt_AS_LONG(obj); \
253 } \
254 else { \
255 Py_INCREF(Py_NotImplemented); \
256 return Py_NotImplemented; \
257 }
258
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000259/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000260static int
Fred Drakea2f55112000-07-09 15:16:51 +0000261int_print(PyIntObject *v, FILE *fp, int flags)
262 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263{
264 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000265 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266}
267
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000268static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000269int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270{
Tim Peters42221042001-12-01 02:52:56 +0000271 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000272 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000273 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274}
275
276static int
Fred Drakea2f55112000-07-09 15:16:51 +0000277int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278{
279 register long i = v->ob_ival;
280 register long j = w->ob_ival;
281 return (i < j) ? -1 : (i > j) ? 1 : 0;
282}
283
Guido van Rossum9bfef441993-03-29 10:43:31 +0000284static long
Fred Drakea2f55112000-07-09 15:16:51 +0000285int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000286{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000287 /* XXX If this is changed, you also need to change the way
288 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000289 long x = v -> ob_ival;
290 if (x == -1)
291 x = -2;
292 return x;
293}
294
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000295static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000296int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297{
298 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000299 CONVERT_TO_LONG(v, a);
300 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000302 if ((x^a) >= 0 || (x^b) >= 0)
303 return PyInt_FromLong(x);
304 if (err_ovf("integer addition"))
305 return NULL;
306 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307}
308
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000309static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000310int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311{
312 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000313 CONVERT_TO_LONG(v, a);
314 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000316 if ((x^a) >= 0 || (x^~b) >= 0)
317 return PyInt_FromLong(x);
318 if (err_ovf("integer subtraction"))
319 return NULL;
320 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
321 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322}
323
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000324/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000325Integer overflow checking for * is painful: Python tried a couple ways, but
326they didn't work on all platforms, or failed in endcases (a product of
327-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000328
Tim Petersa3c01ce2001-12-04 23:05:10 +0000329Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000330
Tim Petersa3c01ce2001-12-04 23:05:10 +0000331The native long product x*y is either exactly right or *way* off, being
332just the last n bits of the true product, where n is the number of bits
333in a long (the delivered product is the true product plus i*2**n for
334some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000335
Tim Petersa3c01ce2001-12-04 23:05:10 +0000336The native double product (double)x * (double)y is subject to three
337rounding errors: on a sizeof(long)==8 box, each cast to double can lose
338info, and even on a sizeof(long)==4 box, the multiplication can lose info.
339But, unlike the native long product, it's not in *range* trouble: even
340if sizeof(long)==32 (256-bit longs), the product easily fits in the
341dynamic range of a double. So the leading 50 (or so) bits of the double
342product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000343
Tim Petersa3c01ce2001-12-04 23:05:10 +0000344We check these two ways against each other, and declare victory if they're
345approximately the same. Else, because the native long product is the only
346one that can lose catastrophic amounts of information, it's the native long
347product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000348*/
349
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000350static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000351int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000353 long a, b;
354 long longprod; /* a*b in native long arithmetic */
355 double doubled_longprod; /* (double)longprod */
356 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000357
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000358 CONVERT_TO_LONG(v, a);
359 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000360 longprod = a * b;
361 doubleprod = (double)a * (double)b;
362 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000363
Tim Petersa3c01ce2001-12-04 23:05:10 +0000364 /* Fast path for normal case: small multiplicands, and no info
365 is lost in either method. */
366 if (doubled_longprod == doubleprod)
367 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000368
Tim Petersa3c01ce2001-12-04 23:05:10 +0000369 /* Somebody somewhere lost info. Close enough, or way off? Note
370 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
371 The difference either is or isn't significant compared to the
372 true value (of which doubleprod is a good approximation).
373 */
374 {
375 const double diff = doubled_longprod - doubleprod;
376 const double absdiff = diff >= 0.0 ? diff : -diff;
377 const double absprod = doubleprod >= 0.0 ? doubleprod :
378 -doubleprod;
379 /* absdiff/absprod <= 1/32 iff
380 32 * absdiff <= absprod -- 5 good bits is "close enough" */
381 if (32.0 * absdiff <= absprod)
382 return PyInt_FromLong(longprod);
383 else if (err_ovf("integer multiplication"))
384 return NULL;
385 else
386 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000387 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000388}
389
Guido van Rossume27f7952001-08-23 02:59:04 +0000390/* Return type of i_divmod */
391enum divmod_result {
392 DIVMOD_OK, /* Correct result */
393 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
394 DIVMOD_ERROR /* Exception raised */
395};
396
397static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000398i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000399 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000400{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000401 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000402
Tim Peters1dad6a82001-06-18 19:21:11 +0000403 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000405 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000406 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000408 /* (-sys.maxint-1)/-1 is the only overflow case. */
409 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000410 if (err_ovf("integer division"))
411 return DIVMOD_ERROR;
412 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000413 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000414 xdivy = x / y;
415 xmody = x - xdivy * y;
416 /* If the signs of x and y differ, and the remainder is non-0,
417 * C89 doesn't define whether xdivy is now the floor or the
418 * ceiling of the infinitely precise quotient. We want the floor,
419 * and we have it iff the remainder's sign matches y's.
420 */
421 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
422 xmody += y;
423 --xdivy;
424 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000425 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000426 *p_xdivy = xdivy;
427 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000428 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000429}
430
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000432int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000433{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000434 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000435 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000436 CONVERT_TO_LONG(x, xi);
437 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000438 switch (i_divmod(xi, yi, &d, &m)) {
439 case DIVMOD_OK:
440 return PyInt_FromLong(d);
441 case DIVMOD_OVERFLOW:
442 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
443 (PyObject *)y);
444 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000445 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000446 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000447}
448
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000449static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000450int_classic_div(PyIntObject *x, PyIntObject *y)
451{
452 long xi, yi;
453 long d, m;
454 CONVERT_TO_LONG(x, xi);
455 CONVERT_TO_LONG(y, yi);
456 if (Py_DivisionWarningFlag &&
457 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
458 return NULL;
459 switch (i_divmod(xi, yi, &d, &m)) {
460 case DIVMOD_OK:
461 return PyInt_FromLong(d);
462 case DIVMOD_OVERFLOW:
463 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
464 (PyObject *)y);
465 default:
466 return NULL;
467 }
468}
469
470static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000471int_true_divide(PyObject *v, PyObject *w)
472{
Tim Peterse2a60002001-09-04 06:17:36 +0000473 /* If they aren't both ints, give someone else a chance. In
474 particular, this lets int/long get handled by longs, which
475 underflows to 0 gracefully if the long is too big to convert
476 to float. */
477 if (PyInt_Check(v) && PyInt_Check(w))
478 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
479 Py_INCREF(Py_NotImplemented);
480 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000481}
482
483static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000484int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000485{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000486 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000487 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000488 CONVERT_TO_LONG(x, xi);
489 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000490 switch (i_divmod(xi, yi, &d, &m)) {
491 case DIVMOD_OK:
492 return PyInt_FromLong(m);
493 case DIVMOD_OVERFLOW:
494 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
495 (PyObject *)y);
496 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000497 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000498 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000499}
500
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000502int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000503{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000504 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000505 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000506 CONVERT_TO_LONG(x, xi);
507 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000508 switch (i_divmod(xi, yi, &d, &m)) {
509 case DIVMOD_OK:
510 return Py_BuildValue("(ll)", d, m);
511 case DIVMOD_OVERFLOW:
512 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
513 (PyObject *)y);
514 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000515 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000516 }
Guido van Rossum00466951991-05-05 20:08:27 +0000517}
518
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000519static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000520int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000522 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000523 CONVERT_TO_LONG(v, iv);
524 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000525 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000526 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000527 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
528 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000529 return NULL;
530 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000531 /* Return a float. This works because we know that
532 this calls float_pow() which converts its
533 arguments to double. */
534 return PyFloat_Type.tp_as_number->nb_power(
535 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000536 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000538 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000539 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000540 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000541 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000542 return NULL;
543 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000544 }
545 /*
546 * XXX: The original exponentiation code stopped looping
547 * when temp hit zero; this code will continue onwards
548 * unnecessarily, but at least it won't cause any errors.
549 * Hopefully the speed improvement from the fast exponentiation
550 * will compensate for the slight inefficiency.
551 * XXX: Better handling of overflows is desperately needed.
552 */
553 temp = iv;
554 ix = 1;
555 while (iw > 0) {
556 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000557 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000558 ix = ix*temp;
559 if (temp == 0)
560 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000561 if (ix / temp != prev) {
562 if (err_ovf("integer exponentiation"))
563 return NULL;
564 return PyLong_Type.tp_as_number->nb_power(
565 (PyObject *)v,
566 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000567 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000568 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000569 }
570 iw >>= 1; /* Shift exponent down by 1 bit */
571 if (iw==0) break;
572 prev = temp;
573 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000574 if (prev!=0 && temp/prev!=prev) {
575 if (err_ovf("integer exponentiation"))
576 return NULL;
577 return PyLong_Type.tp_as_number->nb_power(
578 (PyObject *)v, (PyObject *)w, (PyObject *)z);
579 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000580 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000581 /* If we did a multiplication, perform a modulo */
582 ix = ix % iz;
583 temp = temp % iz;
584 }
585 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000586 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000587 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000588 switch (i_divmod(ix, iz, &div, &mod)) {
589 case DIVMOD_OK:
590 ix = mod;
591 break;
592 case DIVMOD_OVERFLOW:
593 return PyLong_Type.tp_as_number->nb_power(
594 (PyObject *)v, (PyObject *)w, (PyObject *)z);
595 default:
596 return NULL;
597 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000598 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000600}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000603int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604{
605 register long a, x;
606 a = v->ob_ival;
607 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000608 if (a < 0 && x < 0) {
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000609 PyObject *o;
Guido van Rossume27f7952001-08-23 02:59:04 +0000610 if (err_ovf("integer negation"))
611 return NULL;
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000612 o = PyLong_FromLong(a);
613 if (o != NULL) {
614 PyObject *result = PyNumber_Negative(o);
615 Py_DECREF(o);
616 return result;
617 }
618 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000619 }
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
Neal Norwitzc91ed402002-12-30 22:29:22 +0000948int
Neal Norwitzb2501f42002-12-31 03:42:13 +0000949_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +0000950{
951 PyIntObject *v;
952 int ival;
953#if NSMALLNEGINTS + NSMALLPOSINTS > 0
954 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
955 if ((free_list = fill_free_list()) == NULL)
956 return 0;
957 /* PyObject_New is inlined */
958 v = free_list;
959 free_list = (PyIntObject *)v->ob_type;
960 PyObject_INIT(v, &PyInt_Type);
961 v->ob_ival = ival;
962 small_ints[ival + NSMALLNEGINTS] = v;
963 }
964#endif
965 return 1;
966}
967
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000968void
Fred Drakea2f55112000-07-09 15:16:51 +0000969PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000970{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000971 PyIntObject *p;
972 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000973 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000974 int bc, bf; /* block count, number of freed blocks */
975 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000976
Guido van Rossumda084ed1999-03-10 22:55:24 +0000977#if NSMALLNEGINTS + NSMALLPOSINTS > 0
978 PyIntObject **q;
979
980 i = NSMALLNEGINTS + NSMALLPOSINTS;
981 q = small_ints;
982 while (--i >= 0) {
983 Py_XDECREF(*q);
984 *q++ = NULL;
985 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000986#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000987 bc = 0;
988 bf = 0;
989 isum = 0;
990 list = block_list;
991 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000992 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000993 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000994 bc++;
995 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000996 for (i = 0, p = &list->objects[0];
997 i < N_INTOBJECTS;
998 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000999 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001000 irem++;
1001 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001002 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001003 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001004 list->next = block_list;
1005 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001006 for (i = 0, p = &list->objects[0];
1007 i < N_INTOBJECTS;
1008 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001009 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001010 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001011 p->ob_type = (struct _typeobject *)
1012 free_list;
1013 free_list = p;
1014 }
1015#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1016 else if (-NSMALLNEGINTS <= p->ob_ival &&
1017 p->ob_ival < NSMALLPOSINTS &&
1018 small_ints[p->ob_ival +
1019 NSMALLNEGINTS] == NULL) {
1020 Py_INCREF(p);
1021 small_ints[p->ob_ival +
1022 NSMALLNEGINTS] = p;
1023 }
1024#endif
1025 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001026 }
1027 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001028 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001029 bf++;
1030 }
1031 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001032 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001033 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001034 if (!Py_VerboseFlag)
1035 return;
1036 fprintf(stderr, "# cleanup ints");
1037 if (!isum) {
1038 fprintf(stderr, "\n");
1039 }
1040 else {
1041 fprintf(stderr,
1042 ": %d unfreed int%s in %d out of %d block%s\n",
1043 isum, isum == 1 ? "" : "s",
1044 bc - bf, bc, bc == 1 ? "" : "s");
1045 }
1046 if (Py_VerboseFlag > 1) {
1047 list = block_list;
1048 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001049 for (i = 0, p = &list->objects[0];
1050 i < N_INTOBJECTS;
1051 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001052 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001053 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001054 "# <int at %p, refcnt=%d, val=%ld>\n",
1055 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001056 }
1057 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001058 }
1059 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001060}