blob: 10587ac7bd84b10e4263a127fa26809c841ea02a [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) {
609 if (err_ovf("integer negation"))
610 return NULL;
611 return PyNumber_Negative(PyLong_FromLong(a));
612 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000613 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000614}
615
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000616static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000617int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000619 if (PyInt_CheckExact(v)) {
620 Py_INCREF(v);
621 return (PyObject *)v;
622 }
623 else
624 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000625}
626
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000628int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000629{
630 if (v->ob_ival >= 0)
631 return int_pos(v);
632 else
633 return int_neg(v);
634}
635
Guido van Rossum0bff0151991-05-14 12:05:32 +0000636static int
Fred Drakea2f55112000-07-09 15:16:51 +0000637int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000638{
639 return v->ob_ival != 0;
640}
641
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000643int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000644{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000646}
647
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000649int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000650{
Guido van Rossum078151d2002-08-11 04:24:12 +0000651 long a, b, c;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000652 CONVERT_TO_LONG(v, a);
653 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000654 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000656 return NULL;
657 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000658 if (a == 0 || b == 0)
659 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000660 if (b >= LONG_BIT) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000661 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000662 "x<<y losing bits or changing sign "
663 "will return a long in Python 2.4 and up") < 0)
664 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000666 }
Tim Petersda1a2212002-08-11 17:54:42 +0000667 c = a << b;
668 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000669 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000670 "x<<y losing bits or changing sign "
671 "will return a long in Python 2.4 and up") < 0)
672 return NULL;
673 }
674 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000675}
676
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000678int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000679{
680 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000681 CONVERT_TO_LONG(v, a);
682 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000683 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000685 return NULL;
686 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000687 if (a == 0 || b == 0)
688 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000689 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000690 if (a < 0)
691 a = -1;
692 else
693 a = 0;
694 }
695 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000696 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000697 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000699}
700
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000701static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000702int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000703{
704 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000705 CONVERT_TO_LONG(v, a);
706 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000708}
709
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000711int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000712{
713 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000714 CONVERT_TO_LONG(v, a);
715 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000716 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000717}
718
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000719static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000720int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000721{
722 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000723 CONVERT_TO_LONG(v, a);
724 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000725 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000726}
727
Guido van Rossum1952e382001-09-19 01:25:16 +0000728static int
729int_coerce(PyObject **pv, PyObject **pw)
730{
731 if (PyInt_Check(*pw)) {
732 Py_INCREF(*pv);
733 Py_INCREF(*pw);
734 return 0;
735 }
736 return 1; /* Can't do it */
737}
738
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000739static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000740int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000741{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000742 Py_INCREF(v);
743 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000744}
745
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000746static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000747int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000748{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000749 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000750}
751
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000752static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000753int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000754{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000756}
757
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000759int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000760{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000761 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000762 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000763 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000764 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000765 "hex()/oct() of negative int will return "
766 "a signed string in Python 2.4 and up") < 0)
767 return NULL;
768 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000769 if (x == 0)
770 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000771 else
Barry Warsaw61975092001-11-28 20:55:34 +0000772 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000774}
775
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000776static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000777int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000778{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000779 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000780 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000781 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000782 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000783 "hex()/oct() of negative int will return "
784 "a signed string in Python 2.4 and up") < 0)
785 return NULL;
786 }
Barry Warsaw61975092001-11-28 20:55:34 +0000787 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000789}
790
Jeremy Hylton938ace62002-07-17 16:30:39 +0000791static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000792int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
793
Tim Peters6d6c1a32001-08-02 04:15:00 +0000794static PyObject *
795int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
796{
797 PyObject *x = NULL;
798 int base = -909;
799 static char *kwlist[] = {"x", "base", 0};
800
Guido van Rossumbef14172001-08-29 15:47:46 +0000801 if (type != &PyInt_Type)
802 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000803 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
804 &x, &base))
805 return NULL;
806 if (x == NULL)
807 return PyInt_FromLong(0L);
808 if (base == -909)
809 return PyNumber_Int(x);
810 if (PyString_Check(x))
811 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000812#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000813 if (PyUnicode_Check(x))
814 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
815 PyUnicode_GET_SIZE(x),
816 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000817#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000818 PyErr_SetString(PyExc_TypeError,
819 "int() can't convert non-string with explicit base");
820 return NULL;
821}
822
Guido van Rossumbef14172001-08-29 15:47:46 +0000823/* Wimpy, slow approach to tp_new calls for subtypes of int:
824 first create a regular int from whatever arguments we got,
825 then allocate a subtype instance and initialize its ob_ival
826 from the regular int. The regular int is then thrown away.
827*/
828static PyObject *
829int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
830{
831 PyObject *tmp, *new;
832
833 assert(PyType_IsSubtype(type, &PyInt_Type));
834 tmp = int_new(&PyInt_Type, args, kwds);
835 if (tmp == NULL)
836 return NULL;
837 assert(PyInt_Check(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000838 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000839 if (new == NULL)
840 return NULL;
841 ((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
842 Py_DECREF(tmp);
843 return new;
844}
845
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000846PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000847"int(x[, base]) -> integer\n\
848\n\
849Convert a string or number to an integer, if possible. A floating point\n\
850argument will be truncated towards zero (this does not include a string\n\
851representation of a floating point number!) When converting a string, use\n\
852the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +0000853non-string. If the argument is outside the integer range a long object\n\
854will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000855
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000856static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000857 (binaryfunc)int_add, /*nb_add*/
858 (binaryfunc)int_sub, /*nb_subtract*/
859 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000860 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000861 (binaryfunc)int_mod, /*nb_remainder*/
862 (binaryfunc)int_divmod, /*nb_divmod*/
863 (ternaryfunc)int_pow, /*nb_power*/
864 (unaryfunc)int_neg, /*nb_negative*/
865 (unaryfunc)int_pos, /*nb_positive*/
866 (unaryfunc)int_abs, /*nb_absolute*/
867 (inquiry)int_nonzero, /*nb_nonzero*/
868 (unaryfunc)int_invert, /*nb_invert*/
869 (binaryfunc)int_lshift, /*nb_lshift*/
870 (binaryfunc)int_rshift, /*nb_rshift*/
871 (binaryfunc)int_and, /*nb_and*/
872 (binaryfunc)int_xor, /*nb_xor*/
873 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +0000874 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000875 (unaryfunc)int_int, /*nb_int*/
876 (unaryfunc)int_long, /*nb_long*/
877 (unaryfunc)int_float, /*nb_float*/
878 (unaryfunc)int_oct, /*nb_oct*/
879 (unaryfunc)int_hex, /*nb_hex*/
880 0, /*nb_inplace_add*/
881 0, /*nb_inplace_subtract*/
882 0, /*nb_inplace_multiply*/
883 0, /*nb_inplace_divide*/
884 0, /*nb_inplace_remainder*/
885 0, /*nb_inplace_power*/
886 0, /*nb_inplace_lshift*/
887 0, /*nb_inplace_rshift*/
888 0, /*nb_inplace_and*/
889 0, /*nb_inplace_xor*/
890 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000891 (binaryfunc)int_div, /* nb_floor_divide */
892 int_true_divide, /* nb_true_divide */
893 0, /* nb_inplace_floor_divide */
894 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000895};
896
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897PyTypeObject PyInt_Type = {
898 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000899 0,
900 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000901 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000902 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000903 (destructor)int_dealloc, /* tp_dealloc */
904 (printfunc)int_print, /* tp_print */
905 0, /* tp_getattr */
906 0, /* tp_setattr */
907 (cmpfunc)int_compare, /* tp_compare */
908 (reprfunc)int_repr, /* tp_repr */
909 &int_as_number, /* tp_as_number */
910 0, /* tp_as_sequence */
911 0, /* tp_as_mapping */
912 (hashfunc)int_hash, /* tp_hash */
913 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +0000914 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000915 PyObject_GenericGetAttr, /* tp_getattro */
916 0, /* tp_setattro */
917 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000918 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
919 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000920 int_doc, /* tp_doc */
921 0, /* tp_traverse */
922 0, /* tp_clear */
923 0, /* tp_richcompare */
924 0, /* tp_weaklistoffset */
925 0, /* tp_iter */
926 0, /* tp_iternext */
927 0, /* tp_methods */
928 0, /* tp_members */
929 0, /* tp_getset */
930 0, /* tp_base */
931 0, /* tp_dict */
932 0, /* tp_descr_get */
933 0, /* tp_descr_set */
934 0, /* tp_dictoffset */
935 0, /* tp_init */
936 0, /* tp_alloc */
937 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +0000938 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000939};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000940
Neal Norwitzc91ed402002-12-30 22:29:22 +0000941int
942PyInt_Init(void)
943{
944 PyIntObject *v;
945 int ival;
946#if NSMALLNEGINTS + NSMALLPOSINTS > 0
947 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
948 if ((free_list = fill_free_list()) == NULL)
949 return 0;
950 /* PyObject_New is inlined */
951 v = free_list;
952 free_list = (PyIntObject *)v->ob_type;
953 PyObject_INIT(v, &PyInt_Type);
954 v->ob_ival = ival;
955 small_ints[ival + NSMALLNEGINTS] = v;
956 }
957#endif
958 return 1;
959}
960
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000961void
Fred Drakea2f55112000-07-09 15:16:51 +0000962PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000963{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000964 PyIntObject *p;
965 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000966 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000967 int bc, bf; /* block count, number of freed blocks */
968 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000969
Guido van Rossumda084ed1999-03-10 22:55:24 +0000970#if NSMALLNEGINTS + NSMALLPOSINTS > 0
971 PyIntObject **q;
972
973 i = NSMALLNEGINTS + NSMALLPOSINTS;
974 q = small_ints;
975 while (--i >= 0) {
976 Py_XDECREF(*q);
977 *q++ = NULL;
978 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000979#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +0000980 bc = 0;
981 bf = 0;
982 isum = 0;
983 list = block_list;
984 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000985 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000986 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +0000987 bc++;
988 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000989 for (i = 0, p = &list->objects[0];
990 i < N_INTOBJECTS;
991 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000992 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +0000993 irem++;
994 }
Guido van Rossum3fce8831999-03-12 19:43:17 +0000995 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000996 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +0000997 list->next = block_list;
998 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +0000999 for (i = 0, p = &list->objects[0];
1000 i < N_INTOBJECTS;
1001 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001002 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001003 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001004 p->ob_type = (struct _typeobject *)
1005 free_list;
1006 free_list = p;
1007 }
1008#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1009 else if (-NSMALLNEGINTS <= p->ob_ival &&
1010 p->ob_ival < NSMALLPOSINTS &&
1011 small_ints[p->ob_ival +
1012 NSMALLNEGINTS] == NULL) {
1013 Py_INCREF(p);
1014 small_ints[p->ob_ival +
1015 NSMALLNEGINTS] = p;
1016 }
1017#endif
1018 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001019 }
1020 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001021 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001022 bf++;
1023 }
1024 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001025 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001026 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001027 if (!Py_VerboseFlag)
1028 return;
1029 fprintf(stderr, "# cleanup ints");
1030 if (!isum) {
1031 fprintf(stderr, "\n");
1032 }
1033 else {
1034 fprintf(stderr,
1035 ": %d unfreed int%s in %d out of %d block%s\n",
1036 isum, isum == 1 ? "" : "s",
1037 bc - bf, bc, bc == 1 ? "" : "s");
1038 }
1039 if (Py_VerboseFlag > 1) {
1040 list = block_list;
1041 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001042 for (i = 0, p = &list->objects[0];
1043 i < N_INTOBJECTS;
1044 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001045 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001046 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001047 "# <int at %p, refcnt=%d, val=%ld>\n",
1048 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001049 }
1050 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001051 }
1052 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001053}