blob: e33908591cf945322a6b6d798d4d1aeffe98ce77 [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)) {
169 PyErr_SetString(PyExc_TypeError,
170 "nb_int should return int object");
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000171 return -1;
172 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000173
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 val = PyInt_AS_LONG(io);
175 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000176
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000177 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000178}
179
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000180PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000181PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000182{
183 char *end;
184 long x;
185 char buffer[256]; /* For errors */
186
187 if ((base != 0 && base < 2) || base > 36) {
Fred Drake661ea262000-10-24 19:57:45 +0000188 PyErr_SetString(PyExc_ValueError, "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000189 return NULL;
190 }
191
192 while (*s && isspace(Py_CHARMASK(*s)))
193 s++;
194 errno = 0;
195 if (base == 0 && s[0] == '0')
196 x = (long) PyOS_strtoul(s, &end, base);
197 else
198 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000199 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000200 goto bad;
201 while (*end && isspace(Py_CHARMASK(*end)))
202 end++;
203 if (*end != '\0') {
204 bad:
Barry Warsaw61975092001-11-28 20:55:34 +0000205 PyOS_snprintf(buffer, sizeof(buffer),
206 "invalid literal for int(): %.200s", s);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000207 PyErr_SetString(PyExc_ValueError, buffer);
208 return NULL;
209 }
210 else if (errno != 0) {
Walter Dörwald07e14762002-11-06 16:15:14 +0000211 if (err_ovf("string/unicode conversion"))
212 return NULL;
213 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000214 }
215 if (pend)
216 *pend = end;
217 return PyInt_FromLong(x);
218}
219
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000220#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000221PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000222PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000223{
Walter Dörwald07e14762002-11-06 16:15:14 +0000224 PyObject *result;
225 char *buffer = PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000226
Walter Dörwald07e14762002-11-06 16:15:14 +0000227 if (buffer == NULL)
228 return NULL;
229
230 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
231 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000232 return NULL;
233 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000234 result = PyInt_FromString(buffer, NULL, base);
235 PyMem_FREE(buffer);
236 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000237}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000238#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000239
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240/* Methods */
241
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000242/* Integers are seen as the "smallest" of all numeric types and thus
243 don't have any knowledge about conversion of other types to
244 integers. */
245
246#define CONVERT_TO_LONG(obj, lng) \
247 if (PyInt_Check(obj)) { \
248 lng = PyInt_AS_LONG(obj); \
249 } \
250 else { \
251 Py_INCREF(Py_NotImplemented); \
252 return Py_NotImplemented; \
253 }
254
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000255/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000256static int
Fred Drakea2f55112000-07-09 15:16:51 +0000257int_print(PyIntObject *v, FILE *fp, int flags)
258 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259{
260 fprintf(fp, "%ld", v->ob_ival);
Guido van Rossum90933611991-06-07 16:10:43 +0000261 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000264static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000265int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000266{
Tim Peters42221042001-12-01 02:52:56 +0000267 char buf[64];
Barry Warsaw61975092001-11-28 20:55:34 +0000268 PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000269 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270}
271
272static int
Fred Drakea2f55112000-07-09 15:16:51 +0000273int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000274{
275 register long i = v->ob_ival;
276 register long j = w->ob_ival;
277 return (i < j) ? -1 : (i > j) ? 1 : 0;
278}
279
Guido van Rossum9bfef441993-03-29 10:43:31 +0000280static long
Fred Drakea2f55112000-07-09 15:16:51 +0000281int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000282{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000283 /* XXX If this is changed, you also need to change the way
284 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000285 long x = v -> ob_ival;
286 if (x == -1)
287 x = -2;
288 return x;
289}
290
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000291static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000292int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000293{
294 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000295 CONVERT_TO_LONG(v, a);
296 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000298 if ((x^a) >= 0 || (x^b) >= 0)
299 return PyInt_FromLong(x);
300 if (err_ovf("integer addition"))
301 return NULL;
302 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303}
304
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000305static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000306int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307{
308 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000309 CONVERT_TO_LONG(v, a);
310 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000311 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000312 if ((x^a) >= 0 || (x^~b) >= 0)
313 return PyInt_FromLong(x);
314 if (err_ovf("integer subtraction"))
315 return NULL;
316 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
317 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318}
319
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000320/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000321Integer overflow checking for * is painful: Python tried a couple ways, but
322they didn't work on all platforms, or failed in endcases (a product of
323-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000324
Tim Petersa3c01ce2001-12-04 23:05:10 +0000325Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000326
Tim Petersa3c01ce2001-12-04 23:05:10 +0000327The native long product x*y is either exactly right or *way* off, being
328just the last n bits of the true product, where n is the number of bits
329in a long (the delivered product is the true product plus i*2**n for
330some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000331
Tim Petersa3c01ce2001-12-04 23:05:10 +0000332The native double product (double)x * (double)y is subject to three
333rounding errors: on a sizeof(long)==8 box, each cast to double can lose
334info, and even on a sizeof(long)==4 box, the multiplication can lose info.
335But, unlike the native long product, it's not in *range* trouble: even
336if sizeof(long)==32 (256-bit longs), the product easily fits in the
337dynamic range of a double. So the leading 50 (or so) bits of the double
338product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000339
Tim Petersa3c01ce2001-12-04 23:05:10 +0000340We check these two ways against each other, and declare victory if they're
341approximately the same. Else, because the native long product is the only
342one that can lose catastrophic amounts of information, it's the native long
343product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000344*/
345
Neil Schemenauer3bc3f282002-08-09 15:20:48 +0000346/* Return true if the sq_repeat method should be used */
347#define USE_SQ_REPEAT(o) (!PyInt_Check(o) && \
348 o->ob_type->tp_as_sequence && \
349 o->ob_type->tp_as_sequence->sq_repeat && \
Guido van Rossum4571e9d2002-08-13 10:05:56 +0000350 !(o->ob_type->tp_as_number && \
351 o->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES && \
352 o->ob_type->tp_as_number->nb_multiply))
353
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000354static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000355int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000356{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000357 long a, b;
358 long longprod; /* a*b in native long arithmetic */
359 double doubled_longprod; /* (double)longprod */
360 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000361
Neil Schemenauer3bc3f282002-08-09 15:20:48 +0000362 if (USE_SQ_REPEAT(v)) {
Guido van Rossum02fe6472002-09-11 19:00:52 +0000363 repeat:
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000364 /* sequence * int */
365 a = PyInt_AsLong(w);
Guido van Rossum02fe6472002-09-11 19:00:52 +0000366#if LONG_MAX != INT_MAX
367 if (a > INT_MAX) {
368 PyErr_SetString(PyExc_ValueError,
369 "sequence repeat count too large");
370 return NULL;
371 }
372 else if (a < INT_MIN)
373 a = INT_MIN;
374 /* XXX Why don't I either
375
376 - set a to -1 whenever it's negative (after all,
377 sequence repeat usually treats negative numbers
378 as zero(); or
379
380 - raise an exception when it's less than INT_MIN?
381
382 I'm thinking about a hypothetical use case where some
383 sequence type might use a negative value as a flag of
384 some kind. In those cases I don't want to break the
385 code by mapping all negative values to -1. But I also
386 don't want to break e.g. []*(-sys.maxint), which is
387 perfectly safe, returning []. As a compromise, I do
388 map out-of-range negative values.
389 */
390#endif
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000391 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a);
392 }
Neil Schemenauer3bc3f282002-08-09 15:20:48 +0000393 if (USE_SQ_REPEAT(w)) {
Guido van Rossum02fe6472002-09-11 19:00:52 +0000394 PyObject *tmp = v;
395 v = w;
396 w = tmp;
397 goto repeat;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000398 }
399
400 CONVERT_TO_LONG(v, a);
401 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000402 longprod = a * b;
403 doubleprod = (double)a * (double)b;
404 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000405
Tim Petersa3c01ce2001-12-04 23:05:10 +0000406 /* Fast path for normal case: small multiplicands, and no info
407 is lost in either method. */
408 if (doubled_longprod == doubleprod)
409 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000410
Tim Petersa3c01ce2001-12-04 23:05:10 +0000411 /* Somebody somewhere lost info. Close enough, or way off? Note
412 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
413 The difference either is or isn't significant compared to the
414 true value (of which doubleprod is a good approximation).
415 */
416 {
417 const double diff = doubled_longprod - doubleprod;
418 const double absdiff = diff >= 0.0 ? diff : -diff;
419 const double absprod = doubleprod >= 0.0 ? doubleprod :
420 -doubleprod;
421 /* absdiff/absprod <= 1/32 iff
422 32 * absdiff <= absprod -- 5 good bits is "close enough" */
423 if (32.0 * absdiff <= absprod)
424 return PyInt_FromLong(longprod);
425 else if (err_ovf("integer multiplication"))
426 return NULL;
427 else
428 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000429 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430}
431
Guido van Rossume27f7952001-08-23 02:59:04 +0000432/* Return type of i_divmod */
433enum divmod_result {
434 DIVMOD_OK, /* Correct result */
435 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
436 DIVMOD_ERROR /* Exception raised */
437};
438
439static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000440i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000441 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000443 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000444
Tim Peters1dad6a82001-06-18 19:21:11 +0000445 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000447 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000448 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000450 /* (-sys.maxint-1)/-1 is the only overflow case. */
451 if (y == -1 && x < 0 && x == -x) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000452 if (err_ovf("integer division"))
453 return DIVMOD_ERROR;
454 return DIVMOD_OVERFLOW;
Guido van Rossum00466951991-05-05 20:08:27 +0000455 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000456 xdivy = x / y;
457 xmody = x - xdivy * y;
458 /* If the signs of x and y differ, and the remainder is non-0,
459 * C89 doesn't define whether xdivy is now the floor or the
460 * ceiling of the infinitely precise quotient. We want the floor,
461 * and we have it iff the remainder's sign matches y's.
462 */
463 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
464 xmody += y;
465 --xdivy;
466 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000467 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000468 *p_xdivy = xdivy;
469 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000470 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000471}
472
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000474int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000475{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000476 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000477 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000478 CONVERT_TO_LONG(x, xi);
479 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000480 switch (i_divmod(xi, yi, &d, &m)) {
481 case DIVMOD_OK:
482 return PyInt_FromLong(d);
483 case DIVMOD_OVERFLOW:
484 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
485 (PyObject *)y);
486 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000487 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000488 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000489}
490
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000492int_classic_div(PyIntObject *x, PyIntObject *y)
493{
494 long xi, yi;
495 long d, m;
496 CONVERT_TO_LONG(x, xi);
497 CONVERT_TO_LONG(y, yi);
498 if (Py_DivisionWarningFlag &&
499 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
500 return NULL;
501 switch (i_divmod(xi, yi, &d, &m)) {
502 case DIVMOD_OK:
503 return PyInt_FromLong(d);
504 case DIVMOD_OVERFLOW:
505 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
506 (PyObject *)y);
507 default:
508 return NULL;
509 }
510}
511
512static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000513int_true_divide(PyObject *v, PyObject *w)
514{
Tim Peterse2a60002001-09-04 06:17:36 +0000515 /* If they aren't both ints, give someone else a chance. In
516 particular, this lets int/long get handled by longs, which
517 underflows to 0 gracefully if the long is too big to convert
518 to float. */
519 if (PyInt_Check(v) && PyInt_Check(w))
520 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
521 Py_INCREF(Py_NotImplemented);
522 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000523}
524
525static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000526int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000527{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000528 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000529 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000530 CONVERT_TO_LONG(x, xi);
531 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000532 switch (i_divmod(xi, yi, &d, &m)) {
533 case DIVMOD_OK:
534 return PyInt_FromLong(m);
535 case DIVMOD_OVERFLOW:
536 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
537 (PyObject *)y);
538 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000539 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000540 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000541}
542
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000544int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000545{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000546 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000547 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000548 CONVERT_TO_LONG(x, xi);
549 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000550 switch (i_divmod(xi, yi, &d, &m)) {
551 case DIVMOD_OK:
552 return Py_BuildValue("(ll)", d, m);
553 case DIVMOD_OVERFLOW:
554 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
555 (PyObject *)y);
556 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000557 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000558 }
Guido van Rossum00466951991-05-05 20:08:27 +0000559}
560
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000561static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000562int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000563{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000564 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000565 CONVERT_TO_LONG(v, iv);
566 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000567 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000568 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000569 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
570 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000571 return NULL;
572 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000573 /* Return a float. This works because we know that
574 this calls float_pow() which converts its
575 arguments to double. */
576 return PyFloat_Type.tp_as_number->nb_power(
577 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000578 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000580 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000581 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000582 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000583 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000584 return NULL;
585 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000586 }
587 /*
588 * XXX: The original exponentiation code stopped looping
589 * when temp hit zero; this code will continue onwards
590 * unnecessarily, but at least it won't cause any errors.
591 * Hopefully the speed improvement from the fast exponentiation
592 * will compensate for the slight inefficiency.
593 * XXX: Better handling of overflows is desperately needed.
594 */
595 temp = iv;
596 ix = 1;
597 while (iw > 0) {
598 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000599 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000600 ix = ix*temp;
601 if (temp == 0)
602 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000603 if (ix / temp != prev) {
604 if (err_ovf("integer exponentiation"))
605 return NULL;
606 return PyLong_Type.tp_as_number->nb_power(
607 (PyObject *)v,
608 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000609 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000610 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000611 }
612 iw >>= 1; /* Shift exponent down by 1 bit */
613 if (iw==0) break;
614 prev = temp;
615 temp *= temp; /* Square the value of temp */
Guido van Rossume27f7952001-08-23 02:59:04 +0000616 if (prev!=0 && temp/prev!=prev) {
617 if (err_ovf("integer exponentiation"))
618 return NULL;
619 return PyLong_Type.tp_as_number->nb_power(
620 (PyObject *)v, (PyObject *)w, (PyObject *)z);
621 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000622 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000623 /* If we did a multiplication, perform a modulo */
624 ix = ix % iz;
625 temp = temp % iz;
626 }
627 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000628 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000629 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000630 switch (i_divmod(ix, iz, &div, &mod)) {
631 case DIVMOD_OK:
632 ix = mod;
633 break;
634 case DIVMOD_OVERFLOW:
635 return PyLong_Type.tp_as_number->nb_power(
636 (PyObject *)v, (PyObject *)w, (PyObject *)z);
637 default:
638 return NULL;
639 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000640 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000641 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000642}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000645int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000646{
647 register long a, x;
648 a = v->ob_ival;
649 x = -a;
Guido van Rossume27f7952001-08-23 02:59:04 +0000650 if (a < 0 && x < 0) {
651 if (err_ovf("integer negation"))
652 return NULL;
653 return PyNumber_Negative(PyLong_FromLong(a));
654 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655 return PyInt_FromLong(x);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000656}
657
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000658static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000659int_pos(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660{
Tim Peters73a1dfe2001-09-11 21:44:14 +0000661 if (PyInt_CheckExact(v)) {
662 Py_INCREF(v);
663 return (PyObject *)v;
664 }
665 else
666 return PyInt_FromLong(v->ob_ival);
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_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000671{
672 if (v->ob_ival >= 0)
673 return int_pos(v);
674 else
675 return int_neg(v);
676}
677
Guido van Rossum0bff0151991-05-14 12:05:32 +0000678static int
Fred Drakea2f55112000-07-09 15:16:51 +0000679int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000680{
681 return v->ob_ival != 0;
682}
683
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000685int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000686{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000687 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000688}
689
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000691int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000692{
Guido van Rossum078151d2002-08-11 04:24:12 +0000693 long a, b, c;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000694 CONVERT_TO_LONG(v, a);
695 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000696 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000698 return NULL;
699 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000700 if (a == 0 || b == 0)
701 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000702 if (b >= LONG_BIT) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000703 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000704 "x<<y losing bits or changing sign "
705 "will return a long in Python 2.4 and up") < 0)
706 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707 return PyInt_FromLong(0L);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000708 }
Tim Petersda1a2212002-08-11 17:54:42 +0000709 c = a << b;
710 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000711 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000712 "x<<y losing bits or changing sign "
713 "will return a long in Python 2.4 and up") < 0)
714 return NULL;
715 }
716 return PyInt_FromLong(c);
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_rshift(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 Rossumf3b351f1992-01-14 18:33:22 +0000725 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000726 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000727 return NULL;
728 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000729 if (a == 0 || b == 0)
730 return int_pos(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000731 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000732 if (a < 0)
733 a = -1;
734 else
735 a = 0;
736 }
737 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000738 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000739 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000740 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000741}
742
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000743static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000744int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000745{
746 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000747 CONVERT_TO_LONG(v, a);
748 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000749 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000750}
751
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000752static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000753int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000754{
755 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000756 CONVERT_TO_LONG(v, a);
757 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000759}
760
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000761static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000762int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000763{
764 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000765 CONVERT_TO_LONG(v, a);
766 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000767 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000768}
769
Guido van Rossum1952e382001-09-19 01:25:16 +0000770static int
771int_coerce(PyObject **pv, PyObject **pw)
772{
773 if (PyInt_Check(*pw)) {
774 Py_INCREF(*pv);
775 Py_INCREF(*pw);
776 return 0;
777 }
778 return 1; /* Can't do it */
779}
780
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000781static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000782int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000783{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000784 Py_INCREF(v);
785 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000786}
787
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000789int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000790{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000791 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000792}
793
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000794static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000795int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000796{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000798}
799
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000800static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000801int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000802{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000803 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000804 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000805 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000806 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000807 "hex()/oct() of negative int will return "
808 "a signed string in Python 2.4 and up") < 0)
809 return NULL;
810 }
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000811 if (x == 0)
812 strcpy(buf, "0");
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000813 else
Barry Warsaw61975092001-11-28 20:55:34 +0000814 PyOS_snprintf(buf, sizeof(buf), "0%lo", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000816}
817
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000819int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000820{
Guido van Rossum6f72f971997-01-14 15:43:41 +0000821 char buf[100];
Guido van Rossum9bfef441993-03-29 10:43:31 +0000822 long x = v -> ob_ival;
Guido van Rossum078151d2002-08-11 04:24:12 +0000823 if (x < 0) {
Guido van Rossum54df53a2002-08-14 18:38:27 +0000824 if (PyErr_Warn(PyExc_FutureWarning,
Guido van Rossum078151d2002-08-11 04:24:12 +0000825 "hex()/oct() of negative int will return "
826 "a signed string in Python 2.4 and up") < 0)
827 return NULL;
828 }
Barry Warsaw61975092001-11-28 20:55:34 +0000829 PyOS_snprintf(buf, sizeof(buf), "0x%lx", x);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830 return PyString_FromString(buf);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000831}
832
Jeremy Hylton938ace62002-07-17 16:30:39 +0000833static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000834int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
835
Tim Peters6d6c1a32001-08-02 04:15:00 +0000836static PyObject *
837int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
838{
839 PyObject *x = NULL;
840 int base = -909;
841 static char *kwlist[] = {"x", "base", 0};
842
Guido van Rossumbef14172001-08-29 15:47:46 +0000843 if (type != &PyInt_Type)
844 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000845 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
846 &x, &base))
847 return NULL;
848 if (x == NULL)
849 return PyInt_FromLong(0L);
850 if (base == -909)
851 return PyNumber_Int(x);
852 if (PyString_Check(x))
853 return PyInt_FromString(PyString_AS_STRING(x), NULL, base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000854#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000855 if (PyUnicode_Check(x))
856 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
857 PyUnicode_GET_SIZE(x),
858 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000859#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000860 PyErr_SetString(PyExc_TypeError,
861 "int() can't convert non-string with explicit base");
862 return NULL;
863}
864
Guido van Rossumbef14172001-08-29 15:47:46 +0000865/* Wimpy, slow approach to tp_new calls for subtypes of int:
866 first create a regular int from whatever arguments we got,
867 then allocate a subtype instance and initialize its ob_ival
868 from the regular int. The regular int is then thrown away.
869*/
870static PyObject *
871int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
872{
873 PyObject *tmp, *new;
874
875 assert(PyType_IsSubtype(type, &PyInt_Type));
876 tmp = int_new(&PyInt_Type, args, kwds);
877 if (tmp == NULL)
878 return NULL;
879 assert(PyInt_Check(tmp));
Guido van Rossumd93dce12001-08-30 03:09:31 +0000880 new = type->tp_alloc(type, 0);
Guido van Rossumbef14172001-08-29 15:47:46 +0000881 if (new == NULL)
882 return NULL;
883 ((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
884 Py_DECREF(tmp);
885 return new;
886}
887
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000888PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000889"int(x[, base]) -> integer\n\
890\n\
891Convert a string or number to an integer, if possible. A floating point\n\
892argument will be truncated towards zero (this does not include a string\n\
893representation of a floating point number!) When converting a string, use\n\
894the optional base. It is an error to supply a base when converting a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000895non-string.");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000896
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000898 (binaryfunc)int_add, /*nb_add*/
899 (binaryfunc)int_sub, /*nb_subtract*/
900 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +0000901 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000902 (binaryfunc)int_mod, /*nb_remainder*/
903 (binaryfunc)int_divmod, /*nb_divmod*/
904 (ternaryfunc)int_pow, /*nb_power*/
905 (unaryfunc)int_neg, /*nb_negative*/
906 (unaryfunc)int_pos, /*nb_positive*/
907 (unaryfunc)int_abs, /*nb_absolute*/
908 (inquiry)int_nonzero, /*nb_nonzero*/
909 (unaryfunc)int_invert, /*nb_invert*/
910 (binaryfunc)int_lshift, /*nb_lshift*/
911 (binaryfunc)int_rshift, /*nb_rshift*/
912 (binaryfunc)int_and, /*nb_and*/
913 (binaryfunc)int_xor, /*nb_xor*/
914 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +0000915 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000916 (unaryfunc)int_int, /*nb_int*/
917 (unaryfunc)int_long, /*nb_long*/
918 (unaryfunc)int_float, /*nb_float*/
919 (unaryfunc)int_oct, /*nb_oct*/
920 (unaryfunc)int_hex, /*nb_hex*/
921 0, /*nb_inplace_add*/
922 0, /*nb_inplace_subtract*/
923 0, /*nb_inplace_multiply*/
924 0, /*nb_inplace_divide*/
925 0, /*nb_inplace_remainder*/
926 0, /*nb_inplace_power*/
927 0, /*nb_inplace_lshift*/
928 0, /*nb_inplace_rshift*/
929 0, /*nb_inplace_and*/
930 0, /*nb_inplace_xor*/
931 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +0000932 (binaryfunc)int_div, /* nb_floor_divide */
933 int_true_divide, /* nb_true_divide */
934 0, /* nb_inplace_floor_divide */
935 0, /* nb_inplace_true_divide */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000936};
937
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000938PyTypeObject PyInt_Type = {
939 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000940 0,
941 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000942 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000943 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000944 (destructor)int_dealloc, /* tp_dealloc */
945 (printfunc)int_print, /* tp_print */
946 0, /* tp_getattr */
947 0, /* tp_setattr */
948 (cmpfunc)int_compare, /* tp_compare */
949 (reprfunc)int_repr, /* tp_repr */
950 &int_as_number, /* tp_as_number */
951 0, /* tp_as_sequence */
952 0, /* tp_as_mapping */
953 (hashfunc)int_hash, /* tp_hash */
954 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +0000955 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000956 PyObject_GenericGetAttr, /* tp_getattro */
957 0, /* tp_setattro */
958 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +0000959 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
960 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000961 int_doc, /* tp_doc */
962 0, /* tp_traverse */
963 0, /* tp_clear */
964 0, /* tp_richcompare */
965 0, /* tp_weaklistoffset */
966 0, /* tp_iter */
967 0, /* tp_iternext */
968 0, /* tp_methods */
969 0, /* tp_members */
970 0, /* tp_getset */
971 0, /* tp_base */
972 0, /* tp_dict */
973 0, /* tp_descr_get */
974 0, /* tp_descr_set */
975 0, /* tp_dictoffset */
976 0, /* tp_init */
977 0, /* tp_alloc */
978 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +0000979 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000980};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000981
982void
Fred Drakea2f55112000-07-09 15:16:51 +0000983PyInt_Fini(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000984{
Guido van Rossum3fce8831999-03-12 19:43:17 +0000985 PyIntObject *p;
986 PyIntBlock *list, *next;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000987 int i;
Guido van Rossumda084ed1999-03-10 22:55:24 +0000988 int bc, bf; /* block count, number of freed blocks */
989 int irem, isum; /* remaining unfreed ints per block, total */
Guido van Rossumfbbd57e1997-08-05 02:16:08 +0000990
Guido van Rossumda084ed1999-03-10 22:55:24 +0000991#if NSMALLNEGINTS + NSMALLPOSINTS > 0
992 PyIntObject **q;
993
994 i = NSMALLNEGINTS + NSMALLPOSINTS;
995 q = small_ints;
996 while (--i >= 0) {
997 Py_XDECREF(*q);
998 *q++ = NULL;
999 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001000#endif
Guido van Rossumda084ed1999-03-10 22:55:24 +00001001 bc = 0;
1002 bf = 0;
1003 isum = 0;
1004 list = block_list;
1005 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001006 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001007 while (list != NULL) {
Guido van Rossumda084ed1999-03-10 22:55:24 +00001008 bc++;
1009 irem = 0;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001010 for (i = 0, p = &list->objects[0];
1011 i < N_INTOBJECTS;
1012 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001013 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossumda084ed1999-03-10 22:55:24 +00001014 irem++;
1015 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001016 next = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001017 if (irem) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001018 list->next = block_list;
1019 block_list = list;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001020 for (i = 0, p = &list->objects[0];
1021 i < N_INTOBJECTS;
1022 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001023 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001024 p->ob_refcnt == 0) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001025 p->ob_type = (struct _typeobject *)
1026 free_list;
1027 free_list = p;
1028 }
1029#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1030 else if (-NSMALLNEGINTS <= p->ob_ival &&
1031 p->ob_ival < NSMALLPOSINTS &&
1032 small_ints[p->ob_ival +
1033 NSMALLNEGINTS] == NULL) {
1034 Py_INCREF(p);
1035 small_ints[p->ob_ival +
1036 NSMALLNEGINTS] = p;
1037 }
1038#endif
1039 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001040 }
1041 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001042 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001043 bf++;
1044 }
1045 isum += irem;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001046 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001047 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001048 if (!Py_VerboseFlag)
1049 return;
1050 fprintf(stderr, "# cleanup ints");
1051 if (!isum) {
1052 fprintf(stderr, "\n");
1053 }
1054 else {
1055 fprintf(stderr,
1056 ": %d unfreed int%s in %d out of %d block%s\n",
1057 isum, isum == 1 ? "" : "s",
1058 bc - bf, bc, bc == 1 ? "" : "s");
1059 }
1060 if (Py_VerboseFlag > 1) {
1061 list = block_list;
1062 while (list != NULL) {
Guido van Rossum51288bc1999-03-19 20:30:39 +00001063 for (i = 0, p = &list->objects[0];
1064 i < N_INTOBJECTS;
1065 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001066 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Guido van Rossum3fce8831999-03-12 19:43:17 +00001067 fprintf(stderr,
Fred Drakea44d3532000-06-30 15:01:00 +00001068 "# <int at %p, refcnt=%d, val=%ld>\n",
1069 p, p->ob_refcnt, p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001070 }
1071 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001072 }
1073 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001074}