blob: 7242dd017061c8fecada3d41259c320a2d6bc37d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Integer object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Barry Warsaw226ae6c1999-10-12 19:54:53 +00005#include <ctype.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum2e1d4331993-12-24 10:22:45 +00007long
Fred Drakea2f55112000-07-09 15:16:51 +00008PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +00009{
10 return LONG_MAX; /* To initialize sys.maxint */
11}
12
Guido van Rossume27f7952001-08-23 02:59:04 +000013/* Return 1 if exception raised, 0 if caller should retry using longs */
14static int
Fred Drakea2f55112000-07-09 15:16:51 +000015err_ovf(char *msg)
Guido van Rossum165e67e1990-10-14 20:02:26 +000016{
Guido van Rossume27f7952001-08-23 02:59:04 +000017 if (PyErr_Warn(PyExc_OverflowWarning, msg) < 0) {
Guido van Rossum0b131162001-08-23 21:32:40 +000018 if (PyErr_ExceptionMatches(PyExc_OverflowWarning))
19 PyErr_SetString(PyExc_OverflowError, msg);
Guido van Rossume27f7952001-08-23 02:59:04 +000020 return 1;
21 }
22 else
23 return 0;
Guido van Rossum165e67e1990-10-14 20:02:26 +000024}
25
Guido van Rossum3f5da241990-12-20 15:06:42 +000026/* Integers are quite normal objects, to make object handling uniform.
27 (Using odd pointers to represent integers would save much space
28 but require extra checks for this special case throughout the code.)
Tim Peters29c0afc2002-04-28 16:57:34 +000029 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000030 and deallocating integers, these operations should be very fast.
31 Therefore we use a dedicated allocation scheme with a much lower
32 overhead (in space and time) than straight malloc(): a simple
33 dedicated free list, filled when necessary with memory from malloc().
Tim Peters29c0afc2002-04-28 16:57:34 +000034
35 block_list is a singly-linked list of all PyIntBlocks ever allocated,
36 linked via their next members. PyIntBlocks are never returned to the
37 system before shutdown (PyInt_Fini).
38
39 free_list is a singly-linked list of available PyIntObjects, linked
40 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000041*/
42
43#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000044#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
45#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000046
Guido van Rossum3fce8831999-03-12 19:43:17 +000047struct _intblock {
48 struct _intblock *next;
49 PyIntObject objects[N_INTOBJECTS];
50};
51
52typedef struct _intblock PyIntBlock;
53
54static PyIntBlock *block_list = NULL;
55static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000056
Guido van Rossumc0b618a1997-05-02 03:12:38 +000057static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000058fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000059{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000061 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000062 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000063 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000064 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000065 ((PyIntBlock *)p)->next = block_list;
66 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000067 /* Link the int objects together, from rear to front, then return
68 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000069 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000070 q = p + N_INTOBJECTS;
71 while (--q > p)
Guido van Rossumda084ed1999-03-10 22:55:24 +000072 q->ob_type = (struct _typeobject *)(q-1);
73 q->ob_type = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000074 return p + N_INTOBJECTS - 1;
75}
76
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000077#ifndef NSMALLPOSINTS
78#define NSMALLPOSINTS 100
79#endif
80#ifndef NSMALLNEGINTS
81#define NSMALLNEGINTS 1
82#endif
83#if NSMALLNEGINTS + NSMALLPOSINTS > 0
84/* References to small integers are saved in this array so that they
85 can be shared.
86 The integers that are saved are those in the range
87 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
88*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#endif
91#ifdef COUNT_ALLOCS
92int quick_int_allocs, quick_neg_int_allocs;
93#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000094
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000096PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000099#if NSMALLNEGINTS + NSMALLPOSINTS > 0
100 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS &&
101 (v = small_ints[ival + NSMALLNEGINTS]) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000103#ifdef COUNT_ALLOCS
104 if (ival >= 0)
105 quick_int_allocs++;
106 else
107 quick_neg_int_allocs++;
108#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000109 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000110 }
111#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112 if (free_list == NULL) {
113 if ((free_list = fill_free_list()) == NULL)
114 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000116 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 v = free_list;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000118 free_list = (PyIntObject *)v->ob_type;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000119 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000120 v->ob_ival = ival;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000121#if NSMALLNEGINTS + NSMALLPOSINTS > 0
122 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
123 /* save this one for a following allocation */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000125 small_ints[ival + NSMALLNEGINTS] = v;
126 }
127#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129}
130
131static void
Fred Drakea2f55112000-07-09 15:16:51 +0000132int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000133{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000134 if (PyInt_CheckExact(v)) {
Guido van Rossumbef14172001-08-29 15:47:46 +0000135 v->ob_type = (struct _typeobject *)free_list;
136 free_list = v;
137 }
138 else
139 v->ob_type->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140}
141
Guido van Rossum93646982002-04-26 00:53:34 +0000142static void
143int_free(PyIntObject *v)
144{
145 v->ob_type = (struct _typeobject *)free_list;
146 free_list = v;
147}
148
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149long
Fred Drakea2f55112000-07-09 15:16:51 +0000150PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 PyNumberMethods *nb;
153 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000154 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000155
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 if (op && PyInt_Check(op))
157 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000158
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000159 if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL ||
160 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000161 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162 return -1;
163 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000164
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000166 if (io == NULL)
167 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000169 if (PyLong_Check(io)) {
170 /* got a long? => retry int conversion */
171 val = PyLong_AsLong((PyObject *)io);
172 if (PyErr_Occurred()) {
173 Py_DECREF(io);
174 return -1;
175 }
176 }
177 else
178 {
179 PyErr_SetString(PyExc_TypeError,
180 "nb_int should return int object");
181 return -1;
182 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000183 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000184
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000185 val = PyInt_AS_LONG(io);
186 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000187
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000188 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189}
190
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000191PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000192PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000193{
194 char *end;
195 long x;
196 char buffer[256]; /* For errors */
197
198 if ((base != 0 && base < 2) || base > 36) {
Fred Drake661ea262000-10-24 19:57:45 +0000199 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000200 return NULL;
201 }
202
203 while (*s && isspace(Py_CHARMASK(*s)))
204 s++;
205 errno = 0;
206 if (base == 0 && s[0] == '0')
207 x = (long) PyOS_strtoul(s, &end, base);
208 else
209 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000210 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000211 goto bad;
212 while (*end && isspace(Py_CHARMASK(*end)))
213 end++;
214 if (*end != '\0') {
215 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000216 PyOS_snprintf(buffer, sizeof(buffer),
217 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000218 PyErr_SetString(PyExc_ValueError, buffer);
219 return NULL;
220 }
221 else if (errno != 0) {
Walter Dörwald07e14762002-11-06 16:15:14 +0000222 if (err_ovf("string/unicode conversion"))
223 return NULL;
224 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000225 }
226 if (pend)
227 *pend = end;
228 return PyInt_FromLong(x);
229}
230
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000231#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000232PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000233PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000234{
Walter Dörwald07e14762002-11-06 16:15:14 +0000235 PyObject *result;
236 char *buffer = PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000237
Walter Dörwald07e14762002-11-06 16:15:14 +0000238 if (buffer == NULL)
239 return NULL;
240
241 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
242 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000243 return NULL;
244 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000245 result = PyInt_FromString(buffer, NULL, base);
246 PyMem_FREE(buffer);
247 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000248}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000249#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000250
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000251/* Methods */
252
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000253/* Integers are seen as the "smallest" of all numeric types and thus
254 don't have any knowledge about conversion of other types to
255 integers. */
256
257#define CONVERT_TO_LONG(obj, lng) \
258 if (PyInt_Check(obj)) { \
259 lng = PyInt_AS_LONG(obj); \
260 } \
261 else { \
262 Py_INCREF(Py_NotImplemented); \
263 return Py_NotImplemented; \
264 }
265
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000266/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000267static int
Fred Drakea2f55112000-07-09 15:16:51 +0000268int_print(PyIntObject *v, FILE *fp, int flags)
269 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270{
271 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000272 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273}
274
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000276int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277{
Tim Peters42221042001-12-01 02:52:56 +0000278 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000279 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281}
282
283static int
Fred Drakea2f55112000-07-09 15:16:51 +0000284int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000285{
286 register long i = v->ob_ival;
287 register long j = w->ob_ival;
288 return (i < j) ? -1 : (i > j) ? 1 : 0;
289}
290
Guido van Rossum9bfef441993-03-29 10:43:31 +0000291static long
Fred Drakea2f55112000-07-09 15:16:51 +0000292int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000293{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000294 /* XXX If this is changed, you also need to change the way
295 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000296 long x = v -> ob_ival;
297 if (x == -1)
298 x = -2;
299 return x;
300}
301
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000302static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000303int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304{
305 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000306 CONVERT_TO_LONG(v, a);
307 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000309 if ((x^a) >= 0 || (x^b) >= 0)
310 return PyInt_FromLong(x);
311 if (err_ovf("integer addition"))
312 return NULL;
313 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314}
315
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000316static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000317int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318{
319 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000320 CONVERT_TO_LONG(v, a);
321 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000322 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000323 if ((x^a) >= 0 || (x^~b) >= 0)
324 return PyInt_FromLong(x);
325 if (err_ovf("integer subtraction"))
326 return NULL;
327 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
328 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329}
330
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000331/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000332Integer overflow checking for * is painful: Python tried a couple ways, but
333they didn't work on all platforms, or failed in endcases (a product of
334-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000335
Tim Petersa3c01ce2001-12-04 23:05:10 +0000336Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000337
Tim Petersa3c01ce2001-12-04 23:05:10 +0000338The native long product x*y is either exactly right or *way* off, being
339just the last n bits of the true product, where n is the number of bits
340in a long (the delivered product is the true product plus i*2**n for
341some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000342
Tim Petersa3c01ce2001-12-04 23:05:10 +0000343The native double product (double)x * (double)y is subject to three
344rounding errors: on a sizeof(long)==8 box, each cast to double can lose
345info, and even on a sizeof(long)==4 box, the multiplication can lose info.
346But, unlike the native long product, it's not in *range* trouble: even
347if sizeof(long)==32 (256-bit longs), the product easily fits in the
348dynamic range of a double. So the leading 50 (or so) bits of the double
349product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000350
Tim Petersa3c01ce2001-12-04 23:05:10 +0000351We check these two ways against each other, and declare victory if they're
352approximately the same. Else, because the native long product is the only
353one that can lose catastrophic amounts of information, it's the native long
354product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000355*/
356
Neil Schemenauer3bc3f282002-08-09 15:20:48 +0000357/* Return true if the sq_repeat method should be used */
358#define USE_SQ_REPEAT(o) (!PyInt_Check(o) && \
359 o->ob_type->tp_as_sequence && \
360 o->ob_type->tp_as_sequence->sq_repeat && \
Guido van Rossum4571e9d2002-08-13 10:05:56 +0000361 !(o->ob_type->tp_as_number && \
362 o->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES && \
363 o->ob_type->tp_as_number->nb_multiply))
364
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000365static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000366int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000367{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000368 long a, b;
369 long longprod; /* a*b in native long arithmetic */
370 double doubled_longprod; /* (double)longprod */
371 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000372
Neil Schemenauer3bc3f282002-08-09 15:20:48 +0000373 if (USE_SQ_REPEAT(v)) {
Guido van Rossum02fe6472002-09-11 19:00:52 +0000374 repeat:
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000375 /* sequence * int */
376 a = PyInt_AsLong(w);
Guido van Rossum02fe6472002-09-11 19:00:52 +0000377#if LONG_MAX != INT_MAX
378 if (a > INT_MAX) {
379 PyErr_SetString(PyExc_ValueError,
380 "sequence repeat count too large");
381 return NULL;
382 }
383 else if (a < INT_MIN)
384 a = INT_MIN;
385 /* XXX Why don't I either
386
387 - set a to -1 whenever it's negative (after all,
388 sequence repeat usually treats negative numbers
389 as zero(); or
390
391 - raise an exception when it's less than INT_MIN?
392
393 I'm thinking about a hypothetical use case where some
394 sequence type might use a negative value as a flag of
395 some kind. In those cases I don't want to break the
396 code by mapping all negative values to -1. But I also
397 don't want to break e.g. []*(-sys.maxint), which is
398 perfectly safe, returning []. As a compromise, I do
399 map out-of-range negative values.
400 */
401#endif
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000402 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
403 }
Neil Schemenauer3bc3f282002-08-09 15:20:48 +0000404 if (USE_SQ_REPEAT(w)) {
Guido van Rossum02fe6472002-09-11 19:00:52 +0000405 PyObject *tmp = v;
406 v = w;
407 w = tmp;
408 goto repeat;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000409 }
410
411 CONVERT_TO_LONG(v, a);
412 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000413 longprod = a * b;
414 doubleprod = (double)a * (double)b;
415 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000416
Tim Petersa3c01ce2001-12-04 23:05:10 +0000417 /* Fast path for normal case: small multiplicands, and no info
418 is lost in either method. */
419 if (doubled_longprod == doubleprod)
420 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000421
Tim Petersa3c01ce2001-12-04 23:05:10 +0000422 /* Somebody somewhere lost info. Close enough, or way off? Note
423 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
424 The difference either is or isn't significant compared to the
425 true value (of which doubleprod is a good approximation).
426 */
427 {
428 const double diff = doubled_longprod - doubleprod;
429 const double absdiff = diff >= 0.0 ? diff : -diff;
430 const double absprod = doubleprod >= 0.0 ? doubleprod :
431 -doubleprod;
432 /* absdiff/absprod <= 1/32 iff
433 32 * absdiff <= absprod -- 5 good bits is "close enough" */
434 if (32.0 * absdiff <= absprod)
435 return PyInt_FromLong(longprod);
436 else if (err_ovf("integer multiplication"))
437 return NULL;
438 else
439 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000440 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441}
442
Guido van Rossume27f7952001-08-23 02:59:04 +0000443/* Return type of i_divmod */
444enum divmod_result {
445 DIVMOD_OK, /* Correct result */
446 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
447 DIVMOD_ERROR /* Exception raised */
448};
449
450static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000451i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000452 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000454 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000455
Tim Peters1dad6a82001-06-18 19:21:11 +0000456 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000457 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000458 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000459 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000461 /* (-sys.maxint-1)/-1 is the only overflow case. */
462 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000463 if (err_ovf("integer division"))
464 return DIVMOD_ERROR;
465 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000466 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000467 xdivy = x / y;
468 xmody = x - xdivy * y;
469 /* If the signs of x and y differ, and the remainder is non-0,
470 * C89 doesn't define whether xdivy is now the floor or the
471 * ceiling of the infinitely precise quotient. We want the floor,
472 * and we have it iff the remainder's sign matches y's.
473 */
474 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
475 xmody += y;
476 --xdivy;
477 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000478 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000479 *p_xdivy = xdivy;
480 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000481 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000482}
483
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000484static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000485int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000486{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000487 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000488 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000489 CONVERT_TO_LONG(x, xi);
490 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000491 switch (i_divmod(xi, yi, &d, &m)) {
492 case DIVMOD_OK:
493 return PyInt_FromLong(d);
494 case DIVMOD_OVERFLOW:
495 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
496 (PyObject *)y);
497 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000498 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000499 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000500}
501
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000502static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000503int_classic_div(PyIntObject *x, PyIntObject *y)
504{
505 long xi, yi;
506 long d, m;
507 CONVERT_TO_LONG(x, xi);
508 CONVERT_TO_LONG(y, yi);
509 if (Py_DivisionWarningFlag &&
510 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
511 return NULL;
512 switch (i_divmod(xi, yi, &d, &m)) {
513 case DIVMOD_OK:
514 return PyInt_FromLong(d);
515 case DIVMOD_OVERFLOW:
516 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
517 (PyObject *)y);
518 default:
519 return NULL;
520 }
521}
522
523static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000524int_true_divide(PyObject *v, PyObject *w)
525{
Tim Peterse2a60002001-09-04 06:17:36 +0000526 /* If they aren't both ints, give someone else a chance. In
527 particular, this lets int/long get handled by longs, which
528 underflows to 0 gracefully if the long is too big to convert
529 to float. */
530 if (PyInt_Check(v) && PyInt_Check(w))
531 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
532 Py_INCREF(Py_NotImplemented);
533 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000534}
535
536static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000537int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000538{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000539 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000540 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000541 CONVERT_TO_LONG(x, xi);
542 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000543 switch (i_divmod(xi, yi, &d, &m)) {
544 case DIVMOD_OK:
545 return PyInt_FromLong(m);
546 case DIVMOD_OVERFLOW:
547 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
548 (PyObject *)y);
549 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000550 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000551 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000552}
553
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000555int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000556{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000557 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000558 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000559 CONVERT_TO_LONG(x, xi);
560 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000561 switch (i_divmod(xi, yi, &d, &m)) {
562 case DIVMOD_OK:
563 return Py_BuildValue("(ll)", d, m);
564 case DIVMOD_OVERFLOW:
565 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
566 (PyObject *)y);
567 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000568 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000569 }
Guido van Rossum00466951991-05-05 20:08:27 +0000570}
571
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000572static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000573int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000574{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000575 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000576 CONVERT_TO_LONG(v, iv);
577 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000578 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000579 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000580 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
581 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000582 return NULL;
583 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000584 /* Return a float. This works because we know that
585 this calls float_pow() which converts its
586 arguments to double. */
587 return PyFloat_Type.tp_as_number->nb_power(
588 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000589 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000590 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000591 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000592 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000594 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000595 return NULL;
596 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000597 }
598 /*
599 * XXX: The original exponentiation code stopped looping
600 * when temp hit zero; this code will continue onwards
601 * unnecessarily, but at least it won't cause any errors.
602 * Hopefully the speed improvement from the fast exponentiation
603 * will compensate for the slight inefficiency.
604 * XXX: Better handling of overflows is desperately needed.
605 */
606 temp = iv;
607 ix = 1;
608 while (iw > 0) {
609 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000610 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000611 ix = ix*temp;
612 if (temp == 0)
613 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000614 if (ix / temp != prev) {
615 if (err_ovf("integer exponentiation"))
616 return NULL;
617 return PyLong_Type.tp_as_number->nb_power(
618 (PyObject *)v,
619 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000620 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000621 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000622 }
623 iw >>= 1; /* Shift exponent down by 1 bit */
624 if (iw==0) break;
625 prev = temp;
626 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000627 if (prev!=0 && temp/prev!=prev) {
628 if (err_ovf("integer exponentiation"))
629 return NULL;
630 return PyLong_Type.tp_as_number->nb_power(
631 (PyObject *)v, (PyObject *)w, (PyObject *)z);
632 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000633 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000634 /* If we did a multiplication, perform a modulo */
635 ix = ix % iz;
636 temp = temp % iz;
637 }
638 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000639 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000640 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000641 switch (i_divmod(ix, iz, &div, &mod)) {
642 case DIVMOD_OK:
643 ix = mod;
644 break;
645 case DIVMOD_OVERFLOW:
646 return PyLong_Type.tp_as_number->nb_power(
647 (PyObject *)v, (PyObject *)w, (PyObject *)z);
648 default:
649 return NULL;
650 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000651 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000653}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000656int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000657{
658 register long a, x;
659 a = v->ob_ival;
660 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000661 if (a < 0 && x < 0) {
662 if (err_ovf("integer negation"))
663 return NULL;
664 return PyNumber_Negative(PyLong_FromLong(a));
665 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000666 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000667}
668
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000669static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000670int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000672 if (PyInt_CheckExact(v)) {
673 Py_INCREF(v);
674 return (PyObject *)v;
675 }
676 else
677 return PyInt_FromLong(v->ob_ival);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678}
679
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000680static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000681int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000682{
683 if (v->ob_ival >= 0)
684 return int_pos(v);
685 else
686 return int_neg(v);
687}
688
Guido van Rossum0bff0151991-05-14 12:05:32 +0000689static int
Fred Drakea2f55112000-07-09 15:16:51 +0000690int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000691{
692 return v->ob_ival != 0;
693}
694
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000696int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000697{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698 return PyInt_FromLong(~v->ob_ival);
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_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000703{
Guido van Rossum078151d2002-08-11 04:24:12 +0000704 long a, b, c;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000705 CONVERT_TO_LONG(v, a);
706 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000707 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000709 return NULL;
710 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000711 if (a == 0 || b == 0)
712 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000713 if (b >= LONG_BIT) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000714 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000715 "x<<y losing bits or changing sign "
716 "will return a long in Python 2.4 and up") < 0)
717 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000719 }
Tim Petersda1a2212002-08-11 17:54:42 +0000720 c = a << b;
721 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000722 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000723 "x<<y losing bits or changing sign "
724 "will return a long in Python 2.4 and up") < 0)
725 return NULL;
726 }
727 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000728}
729
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000730static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000731int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000732{
733 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000734 CONVERT_TO_LONG(v, a);
735 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000736 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000738 return NULL;
739 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000740 if (a == 0 || b == 0)
741 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000742 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000743 if (a < 0)
744 a = -1;
745 else
746 a = 0;
747 }
748 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000749 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000750 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000752}
753
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000755int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000756{
757 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000758 CONVERT_TO_LONG(v, a);
759 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000760 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000761}
762
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000764int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000765{
766 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000767 CONVERT_TO_LONG(v, a);
768 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000769 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000770}
771
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000772static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000773int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000774{
775 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000776 CONVERT_TO_LONG(v, a);
777 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000778 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000779}
780
Guido van Rossum1952e382001-09-19 01:25:16 +0000781static int
782int_coerce(PyObject **pv, PyObject **pw)
783{
784 if (PyInt_Check(*pw)) {
785 Py_INCREF(*pv);
786 Py_INCREF(*pw);
787 return 0;
788 }
789 return 1; /* Can't do it */
790}
791
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000792static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000793int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000794{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795 Py_INCREF(v);
796 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000797}
798
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000799static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000800int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000801{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000802 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000803}
804
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000806int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000807{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000808 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000809}
810
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000811static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000812int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000813{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000814 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000815 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000816 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000817 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000818 "hex()/oct() of negative int will return "
819 "a signed string in Python 2.4 and up") < 0)
820 return NULL;
821 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000822 if (x == 0)
823 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000824 else
Barry Warsaw61975092001-11-28 20:55:34 +0000825 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000826 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000827}
828
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000829static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000830int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000831{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000832 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000833 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000834 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000835 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000836 "hex()/oct() of negative int will return "
837 "a signed string in Python 2.4 and up") < 0)
838 return NULL;
839 }
Barry Warsaw61975092001-11-28 20:55:34 +0000840 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000841 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000842}
843
Jeremy Hylton938ace62002-07-17 16:30:39 +0000844static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000845int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
846
Tim Peters6d6c1a32001-08-02 04:15:00 +0000847static PyObject *
848int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
849{
850 PyObject *x = NULL;
851 int base = -909;
852 static char *kwlist[] = {"x", "base", 0};
853
Guido van Rossumbef14172001-08-29 15:47:46 +0000854 if (type != &PyInt_Type)
855 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000856 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
857 &x, &base))
858 return NULL;
859 if (x == NULL)
860 return PyInt_FromLong(0L);
861 if (base == -909)
862 return PyNumber_Int(x);
863 if (PyString_Check(x))
864 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000865#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000866 if (PyUnicode_Check(x))
867 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
868 PyUnicode_GET_SIZE(x),
869 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000870#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000871 PyErr_SetString(PyExc_TypeError,
872 "int() can't convert non-string with explicit base");
873 return NULL;
874}
875
Guido van Rossumbef14172001-08-29 15:47:46 +0000876/* Wimpy, slow approach to tp_new calls for subtypes of int:
877 first create a regular int from whatever arguments we got,
878 then allocate a subtype instance and initialize its ob_ival
879 from the regular int. The regular int is then thrown away.
880*/
881static PyObject *
882int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
883{
884 PyObject *tmp, *new;
885
886 assert(PyType_IsSubtype(type, &PyInt_Type));
887 tmp = int_new(&PyInt_Type, args, kwds);
888 if (tmp == NULL)
889 return NULL;
890 assert(PyInt_Check(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000891 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000892 if (new == NULL)
893 return NULL;
894 ((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
895 Py_DECREF(tmp);
896 return new;
897}
898
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000899PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000900"int(x[, base]) -> integer\n\
901\n\
902Convert a string or number to an integer, if possible. A floating point\n\
903argument will be truncated towards zero (this does not include a string\n\
904representation of a floating point number!) When converting a string, use\n\
905the optional base. It is an error to supply a base when converting a\n\
Walter Dörwaldf1715402002-11-19 20:49:15 +0000906non-string. If the argument is outside the integer range a long object\n\
907will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000908
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000909static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000910 (binaryfunc)int_add, /*nb_add*/
911 (binaryfunc)int_sub, /*nb_subtract*/
912 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000913 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000914 (binaryfunc)int_mod, /*nb_remainder*/
915 (binaryfunc)int_divmod, /*nb_divmod*/
916 (ternaryfunc)int_pow, /*nb_power*/
917 (unaryfunc)int_neg, /*nb_negative*/
918 (unaryfunc)int_pos, /*nb_positive*/
919 (unaryfunc)int_abs, /*nb_absolute*/
920 (inquiry)int_nonzero, /*nb_nonzero*/
921 (unaryfunc)int_invert, /*nb_invert*/
922 (binaryfunc)int_lshift, /*nb_lshift*/
923 (binaryfunc)int_rshift, /*nb_rshift*/
924 (binaryfunc)int_and, /*nb_and*/
925 (binaryfunc)int_xor, /*nb_xor*/
926 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +0000927 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000928 (unaryfunc)int_int, /*nb_int*/
929 (unaryfunc)int_long, /*nb_long*/
930 (unaryfunc)int_float, /*nb_float*/
931 (unaryfunc)int_oct, /*nb_oct*/
932 (unaryfunc)int_hex, /*nb_hex*/
933 0, /*nb_inplace_add*/
934 0, /*nb_inplace_subtract*/
935 0, /*nb_inplace_multiply*/
936 0, /*nb_inplace_divide*/
937 0, /*nb_inplace_remainder*/
938 0, /*nb_inplace_power*/
939 0, /*nb_inplace_lshift*/
940 0, /*nb_inplace_rshift*/
941 0, /*nb_inplace_and*/
942 0, /*nb_inplace_xor*/
943 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000944 (binaryfunc)int_div, /* nb_floor_divide */
945 int_true_divide, /* nb_true_divide */
946 0, /* nb_inplace_floor_divide */
947 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000948};
949
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000950PyTypeObject PyInt_Type = {
951 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000952 0,
953 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000954 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000955 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000956 (destructor)int_dealloc, /* tp_dealloc */
957 (printfunc)int_print, /* tp_print */
958 0, /* tp_getattr */
959 0, /* tp_setattr */
960 (cmpfunc)int_compare, /* tp_compare */
961 (reprfunc)int_repr, /* tp_repr */
962 &int_as_number, /* tp_as_number */
963 0, /* tp_as_sequence */
964 0, /* tp_as_mapping */
965 (hashfunc)int_hash, /* tp_hash */
966 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +0000967 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000968 PyObject_GenericGetAttr, /* tp_getattro */
969 0, /* tp_setattro */
970 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000971 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
972 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973 int_doc, /* tp_doc */
974 0, /* tp_traverse */
975 0, /* tp_clear */
976 0, /* tp_richcompare */
977 0, /* tp_weaklistoffset */
978 0, /* tp_iter */
979 0, /* tp_iternext */
980 0, /* tp_methods */
981 0, /* tp_members */
982 0, /* tp_getset */
983 0, /* tp_base */
984 0, /* tp_dict */
985 0, /* tp_descr_get */
986 0, /* tp_descr_set */
987 0, /* tp_dictoffset */
988 0, /* tp_init */
989 0, /* tp_alloc */
990 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +0000991 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000992};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000993
994void
Fred Drakea2f55112000-07-09 15:16:51 +0000995PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000996{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000997 PyIntObject *p;
998 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000999 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001000 int bc, bf; /* block count, number of freed blocks */
1001 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001002
Guido van Rossumda084ed1999-03-10 22:55:24 +00001003#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1004 PyIntObject **q;
1005
1006 i = NSMALLNEGINTS + NSMALLPOSINTS;
1007 q = small_ints;
1008 while (--i >= 0) {
1009 Py_XDECREF(*q);
1010 *q++ = NULL;
1011 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001012#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001013 bc = 0;
1014 bf = 0;
1015 isum = 0;
1016 list = block_list;
1017 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001018 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001019 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001020 bc++;
1021 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001022 for (i = 0, p = &list->objects[0];
1023 i < N_INTOBJECTS;
1024 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001025 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001026 irem++;
1027 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001028 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001029 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001030 list->next = block_list;
1031 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001032 for (i = 0, p = &list->objects[0];
1033 i < N_INTOBJECTS;
1034 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001035 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001036 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001037 p->ob_type = (struct _typeobject *)
1038 free_list;
1039 free_list = p;
1040 }
1041#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1042 else if (-NSMALLNEGINTS <= p->ob_ival &&
1043 p->ob_ival < NSMALLPOSINTS &&
1044 small_ints[p->ob_ival +
1045 NSMALLNEGINTS] == NULL) {
1046 Py_INCREF(p);
1047 small_ints[p->ob_ival +
1048 NSMALLNEGINTS] = p;
1049 }
1050#endif
1051 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001052 }
1053 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001054 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001055 bf++;
1056 }
1057 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001058 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001059 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001060 if (!Py_VerboseFlag)
1061 return;
1062 fprintf(stderr, "# cleanup ints");
1063 if (!isum) {
1064 fprintf(stderr, "\n");
1065 }
1066 else {
1067 fprintf(stderr,
1068 ": %d unfreed int%s in %d out of %d block%s\n",
1069 isum, isum == 1 ? "" : "s",
1070 bc - bf, bc, bc == 1 ? "" : "s");
1071 }
1072 if (Py_VerboseFlag > 1) {
1073 list = block_list;
1074 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001075 for (i = 0, p = &list->objects[0];
1076 i < N_INTOBJECTS;
1077 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001078 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001079 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001080 "# <int at %p, refcnt=%d, val=%ld>\n",
1081 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001082 }
1083 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001084 }
1085 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001086}