blob: d4532f49a67bb38c7e31aadf266151599e4e37bc [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
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00007static PyObject *int_int(PyIntObject *v);
8
Guido van Rossum2e1d4331993-12-24 10:22:45 +00009long
Fred Drakea2f55112000-07-09 15:16:51 +000010PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +000011{
12 return LONG_MAX; /* To initialize sys.maxint */
13}
14
Guido van Rossum3f5da241990-12-20 15:06:42 +000015/* Integers are quite normal objects, to make object handling uniform.
16 (Using odd pointers to represent integers would save much space
17 but require extra checks for this special case throughout the code.)
Tim Peters29c0afc2002-04-28 16:57:34 +000018 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000019 and deallocating integers, these operations should be very fast.
20 Therefore we use a dedicated allocation scheme with a much lower
21 overhead (in space and time) than straight malloc(): a simple
22 dedicated free list, filled when necessary with memory from malloc().
Tim Peters29c0afc2002-04-28 16:57:34 +000023
24 block_list is a singly-linked list of all PyIntBlocks ever allocated,
25 linked via their next members. PyIntBlocks are never returned to the
26 system before shutdown (PyInt_Fini).
27
28 free_list is a singly-linked list of available PyIntObjects, linked
29 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000030*/
31
32#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000033#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
34#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000035
Guido van Rossum3fce8831999-03-12 19:43:17 +000036struct _intblock {
37 struct _intblock *next;
38 PyIntObject objects[N_INTOBJECTS];
39};
40
41typedef struct _intblock PyIntBlock;
42
43static PyIntBlock *block_list = NULL;
44static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000045
Guido van Rossumc0b618a1997-05-02 03:12:38 +000046static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000047fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000048{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000049 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000050 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000051 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000052 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000053 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000054 ((PyIntBlock *)p)->next = block_list;
55 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000056 /* Link the int objects together, from rear to front, then return
57 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000058 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000059 q = p + N_INTOBJECTS;
60 while (--q > p)
Christian Heimese93237d2007-12-19 02:37:44 +000061 Py_TYPE(q) = (struct _typeobject *)(q-1);
62 Py_TYPE(q) = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000063 return p + N_INTOBJECTS - 1;
64}
65
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000066#ifndef NSMALLPOSINTS
Georg Brandl418a1ef2006-02-22 11:30:06 +000067#define NSMALLPOSINTS 257
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000068#endif
69#ifndef NSMALLNEGINTS
Neal Norwitzc91ed402002-12-30 22:29:22 +000070#define NSMALLNEGINTS 5
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000071#endif
72#if NSMALLNEGINTS + NSMALLPOSINTS > 0
73/* References to small integers are saved in this array so that they
74 can be shared.
75 The integers that are saved are those in the range
76 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
77*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000079#endif
80#ifdef COUNT_ALLOCS
Martin v. Löwisb90304a2009-01-07 18:40:40 +000081Py_ssize_t quick_int_allocs;
82Py_ssize_t quick_neg_int_allocs;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000083#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000084
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000086PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000089#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Neal Norwitzc91ed402002-12-30 22:29:22 +000090 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
91 v = small_ints[ival + NSMALLNEGINTS];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000093#ifdef COUNT_ALLOCS
94 if (ival >= 0)
95 quick_int_allocs++;
96 else
97 quick_neg_int_allocs++;
98#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000100 }
101#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000102 if (free_list == NULL) {
103 if ((free_list = fill_free_list()) == NULL)
104 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000106 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107 v = free_list;
Christian Heimese93237d2007-12-19 02:37:44 +0000108 free_list = (PyIntObject *)Py_TYPE(v);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000109 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000111 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112}
113
Martin v. Löwis18e16552006-02-15 17:27:45 +0000114PyObject *
115PyInt_FromSize_t(size_t ival)
116{
117 if (ival <= LONG_MAX)
118 return PyInt_FromLong((long)ival);
119 return _PyLong_FromSize_t(ival);
120}
121
122PyObject *
123PyInt_FromSsize_t(Py_ssize_t ival)
124{
125 if (ival >= LONG_MIN && ival <= LONG_MAX)
126 return PyInt_FromLong((long)ival);
127 return _PyLong_FromSsize_t(ival);
128}
129
Guido van Rossum3f5da241990-12-20 15:06:42 +0000130static void
Fred Drakea2f55112000-07-09 15:16:51 +0000131int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000132{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000133 if (PyInt_CheckExact(v)) {
Christian Heimese93237d2007-12-19 02:37:44 +0000134 Py_TYPE(v) = (struct _typeobject *)free_list;
Guido van Rossumbef14172001-08-29 15:47:46 +0000135 free_list = v;
136 }
137 else
Christian Heimese93237d2007-12-19 02:37:44 +0000138 Py_TYPE(v)->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139}
140
Guido van Rossum93646982002-04-26 00:53:34 +0000141static void
142int_free(PyIntObject *v)
143{
Christian Heimese93237d2007-12-19 02:37:44 +0000144 Py_TYPE(v) = (struct _typeobject *)free_list;
Guido van Rossum93646982002-04-26 00:53:34 +0000145 free_list = v;
146}
147
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000148long
Fred Drakea2f55112000-07-09 15:16:51 +0000149PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000151 PyNumberMethods *nb;
152 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000153 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000154
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155 if (op && PyInt_Check(op))
156 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000157
Christian Heimese93237d2007-12-19 02:37:44 +0000158 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000159 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000160 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161 return -1;
162 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000163
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000164 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000165 if (io == NULL)
166 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000167 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000168 if (PyLong_Check(io)) {
169 /* got a long? => retry int conversion */
170 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000171 Py_DECREF(io);
172 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000173 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000174 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000175 }
176 else
177 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000178 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000179 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
Martin v. Löwis18e16552006-02-15 17:27:45 +0000191Py_ssize_t
192PyInt_AsSsize_t(register PyObject *op)
193{
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000194#if SIZEOF_SIZE_T != SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000195 PyNumberMethods *nb;
196 PyIntObject *io;
197 Py_ssize_t val;
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000198#endif
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000199
200 if (op == NULL) {
201 PyErr_SetString(PyExc_TypeError, "an integer is required");
202 return -1;
203 }
204
205 if (PyInt_Check(op))
206 return PyInt_AS_LONG((PyIntObject*) op);
207 if (PyLong_Check(op))
Martin v. Löwis18e16552006-02-15 17:27:45 +0000208 return _PyLong_AsSsize_t(op);
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000209#if SIZEOF_SIZE_T == SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000210 return PyInt_AsLong(op);
211#else
212
Christian Heimese93237d2007-12-19 02:37:44 +0000213 if ((nb = Py_TYPE(op)->tp_as_number) == NULL ||
Martin v. Löwis18e16552006-02-15 17:27:45 +0000214 (nb->nb_int == NULL && nb->nb_long == 0)) {
215 PyErr_SetString(PyExc_TypeError, "an integer is required");
216 return -1;
217 }
218
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +0000219 if (nb->nb_long != 0)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000220 io = (PyIntObject*) (*nb->nb_long) (op);
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +0000221 else
Martin v. Löwis18e16552006-02-15 17:27:45 +0000222 io = (PyIntObject*) (*nb->nb_int) (op);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000223 if (io == NULL)
224 return -1;
225 if (!PyInt_Check(io)) {
226 if (PyLong_Check(io)) {
227 /* got a long? => retry int conversion */
228 val = _PyLong_AsSsize_t((PyObject *)io);
229 Py_DECREF(io);
230 if ((val == -1) && PyErr_Occurred())
231 return -1;
232 return val;
233 }
234 else
235 {
236 Py_DECREF(io);
237 PyErr_SetString(PyExc_TypeError,
238 "nb_int should return int object");
239 return -1;
240 }
241 }
242
243 val = PyInt_AS_LONG(io);
244 Py_DECREF(io);
245
246 return val;
247#endif
248}
249
Thomas Hellera4ea6032003-04-17 18:55:45 +0000250unsigned long
251PyInt_AsUnsignedLongMask(register PyObject *op)
252{
253 PyNumberMethods *nb;
254 PyIntObject *io;
255 unsigned long val;
256
257 if (op && PyInt_Check(op))
258 return PyInt_AS_LONG((PyIntObject*) op);
259 if (op && PyLong_Check(op))
260 return PyLong_AsUnsignedLongMask(op);
261
Christian Heimese93237d2007-12-19 02:37:44 +0000262 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
Thomas Hellera4ea6032003-04-17 18:55:45 +0000263 nb->nb_int == NULL) {
264 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000265 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000266 }
267
268 io = (PyIntObject*) (*nb->nb_int) (op);
269 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000270 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000271 if (!PyInt_Check(io)) {
272 if (PyLong_Check(io)) {
273 val = PyLong_AsUnsignedLongMask((PyObject *)io);
274 Py_DECREF(io);
275 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000276 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000277 return val;
278 }
279 else
280 {
281 Py_DECREF(io);
282 PyErr_SetString(PyExc_TypeError,
283 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000284 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000285 }
286 }
287
288 val = PyInt_AS_LONG(io);
289 Py_DECREF(io);
290
291 return val;
292}
293
294#ifdef HAVE_LONG_LONG
295unsigned PY_LONG_LONG
296PyInt_AsUnsignedLongLongMask(register PyObject *op)
297{
298 PyNumberMethods *nb;
299 PyIntObject *io;
300 unsigned PY_LONG_LONG val;
301
302 if (op && PyInt_Check(op))
303 return PyInt_AS_LONG((PyIntObject*) op);
304 if (op && PyLong_Check(op))
305 return PyLong_AsUnsignedLongLongMask(op);
306
Christian Heimese93237d2007-12-19 02:37:44 +0000307 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
Thomas Hellera4ea6032003-04-17 18:55:45 +0000308 nb->nb_int == NULL) {
309 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000310 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000311 }
312
313 io = (PyIntObject*) (*nb->nb_int) (op);
314 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000315 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000316 if (!PyInt_Check(io)) {
317 if (PyLong_Check(io)) {
318 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
319 Py_DECREF(io);
320 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000321 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000322 return val;
323 }
324 else
325 {
326 Py_DECREF(io);
327 PyErr_SetString(PyExc_TypeError,
328 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000329 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000330 }
331 }
332
333 val = PyInt_AS_LONG(io);
334 Py_DECREF(io);
335
336 return val;
337}
338#endif
339
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000340PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000341PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000342{
343 char *end;
344 long x;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000345 Py_ssize_t slen;
346 PyObject *sobj, *srepr;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000347
348 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000349 PyErr_SetString(PyExc_ValueError,
350 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000351 return NULL;
352 }
353
354 while (*s && isspace(Py_CHARMASK(*s)))
355 s++;
356 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000357 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000358 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000359 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000360 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000361 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000362 else
363 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000364 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000365 goto bad;
366 while (*end && isspace(Py_CHARMASK(*end)))
367 end++;
368 if (*end != '\0') {
369 bad:
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000370 slen = strlen(s) < 200 ? strlen(s) : 200;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000371 sobj = PyString_FromStringAndSize(s, slen);
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000372 if (sobj == NULL)
373 return NULL;
374 srepr = PyObject_Repr(sobj);
375 Py_DECREF(sobj);
376 if (srepr == NULL)
377 return NULL;
378 PyErr_Format(PyExc_ValueError,
379 "invalid literal for int() with base %d: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000380 base, PyString_AS_STRING(srepr));
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000381 Py_DECREF(srepr);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000382 return NULL;
383 }
Tim Petersc8854432004-08-25 02:14:08 +0000384 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000385 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000386 if (pend)
387 *pend = end;
388 return PyInt_FromLong(x);
389}
390
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000391#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000392PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000393PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000394{
Walter Dörwald07e14762002-11-06 16:15:14 +0000395 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +0000396 char *buffer = (char *)PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000397
Walter Dörwald07e14762002-11-06 16:15:14 +0000398 if (buffer == NULL)
Neal Norwitzd501d1f2007-05-16 04:33:50 +0000399 return PyErr_NoMemory();
Walter Dörwald07e14762002-11-06 16:15:14 +0000400
401 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
402 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000403 return NULL;
404 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000405 result = PyInt_FromString(buffer, NULL, base);
406 PyMem_FREE(buffer);
407 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000408}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000409#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000410
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411/* Methods */
412
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000413/* Integers are seen as the "smallest" of all numeric types and thus
414 don't have any knowledge about conversion of other types to
415 integers. */
416
417#define CONVERT_TO_LONG(obj, lng) \
418 if (PyInt_Check(obj)) { \
419 lng = PyInt_AS_LONG(obj); \
420 } \
421 else { \
422 Py_INCREF(Py_NotImplemented); \
423 return Py_NotImplemented; \
424 }
425
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000426/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000427static int
Fred Drakea2f55112000-07-09 15:16:51 +0000428int_print(PyIntObject *v, FILE *fp, int flags)
429 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430{
Brett Cannon01531592007-09-17 03:28:34 +0000431 long int_val = v->ob_ival;
432 Py_BEGIN_ALLOW_THREADS
433 fprintf(fp, "%ld", int_val);
434 Py_END_ALLOW_THREADS
Guido van Rossum90933611991-06-07 16:10:43 +0000435 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436}
437
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000439int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440{
Eric Smith5e527eb2008-02-10 01:36:53 +0000441 return _PyInt_Format(v, 10, 0);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442}
443
444static int
Fred Drakea2f55112000-07-09 15:16:51 +0000445int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446{
447 register long i = v->ob_ival;
448 register long j = w->ob_ival;
449 return (i < j) ? -1 : (i > j) ? 1 : 0;
450}
451
Guido van Rossum9bfef441993-03-29 10:43:31 +0000452static long
Fred Drakea2f55112000-07-09 15:16:51 +0000453int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000454{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000455 /* XXX If this is changed, you also need to change the way
456 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000457 long x = v -> ob_ival;
458 if (x == -1)
459 x = -2;
460 return x;
461}
462
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000464int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465{
466 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000467 CONVERT_TO_LONG(v, a);
468 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469 x = a + b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000470 if ((x^a) >= 0 || (x^b) >= 0)
471 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000472 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473}
474
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000476int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477{
478 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000479 CONVERT_TO_LONG(v, a);
480 CONVERT_TO_LONG(w, b);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481 x = a - b;
Guido van Rossume27f7952001-08-23 02:59:04 +0000482 if ((x^a) >= 0 || (x^~b) >= 0)
483 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000484 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
485 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486}
487
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000488/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000489Integer overflow checking for * is painful: Python tried a couple ways, but
490they didn't work on all platforms, or failed in endcases (a product of
491-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000492
Tim Petersa3c01ce2001-12-04 23:05:10 +0000493Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000494
Tim Petersa3c01ce2001-12-04 23:05:10 +0000495The native long product x*y is either exactly right or *way* off, being
496just the last n bits of the true product, where n is the number of bits
497in a long (the delivered product is the true product plus i*2**n for
498some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000499
Tim Petersa3c01ce2001-12-04 23:05:10 +0000500The native double product (double)x * (double)y is subject to three
501rounding errors: on a sizeof(long)==8 box, each cast to double can lose
502info, and even on a sizeof(long)==4 box, the multiplication can lose info.
503But, unlike the native long product, it's not in *range* trouble: even
504if sizeof(long)==32 (256-bit longs), the product easily fits in the
505dynamic range of a double. So the leading 50 (or so) bits of the double
506product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000507
Tim Petersa3c01ce2001-12-04 23:05:10 +0000508We check these two ways against each other, and declare victory if they're
509approximately the same. Else, because the native long product is the only
510one that can lose catastrophic amounts of information, it's the native long
511product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000512*/
513
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000514static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000515int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000516{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000517 long a, b;
518 long longprod; /* a*b in native long arithmetic */
519 double doubled_longprod; /* (double)longprod */
520 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000521
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000522 CONVERT_TO_LONG(v, a);
523 CONVERT_TO_LONG(w, b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000524 longprod = a * b;
525 doubleprod = (double)a * (double)b;
526 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000527
Tim Petersa3c01ce2001-12-04 23:05:10 +0000528 /* Fast path for normal case: small multiplicands, and no info
529 is lost in either method. */
530 if (doubled_longprod == doubleprod)
531 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000532
Tim Petersa3c01ce2001-12-04 23:05:10 +0000533 /* Somebody somewhere lost info. Close enough, or way off? Note
534 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
535 The difference either is or isn't significant compared to the
536 true value (of which doubleprod is a good approximation).
537 */
538 {
539 const double diff = doubled_longprod - doubleprod;
540 const double absdiff = diff >= 0.0 ? diff : -diff;
541 const double absprod = doubleprod >= 0.0 ? doubleprod :
542 -doubleprod;
543 /* absdiff/absprod <= 1/32 iff
544 32 * absdiff <= absprod -- 5 good bits is "close enough" */
545 if (32.0 * absdiff <= absprod)
546 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000547 else
548 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000549 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550}
551
Armin Rigo7ccbca92006-10-04 12:17:45 +0000552/* Integer overflow checking for unary negation: on a 2's-complement
553 * box, -x overflows iff x is the most negative long. In this case we
554 * get -x == x. However, -x is undefined (by C) if x /is/ the most
555 * negative long (it's a signed overflow case), and some compilers care.
556 * So we cast x to unsigned long first. However, then other compilers
557 * warn about applying unary minus to an unsigned operand. Hence the
558 * weird "0-".
559 */
560#define UNARY_NEG_WOULD_OVERFLOW(x) \
561 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
562
Guido van Rossume27f7952001-08-23 02:59:04 +0000563/* Return type of i_divmod */
564enum divmod_result {
565 DIVMOD_OK, /* Correct result */
566 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
567 DIVMOD_ERROR /* Exception raised */
568};
569
570static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000571i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000572 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000574 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000575
Tim Peters1dad6a82001-06-18 19:21:11 +0000576 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000577 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000578 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000579 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000580 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000581 /* (-sys.maxint-1)/-1 is the only overflow case. */
Armin Rigo7ccbca92006-10-04 12:17:45 +0000582 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
Guido van Rossume27f7952001-08-23 02:59:04 +0000583 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000584 xdivy = x / y;
585 xmody = x - xdivy * y;
586 /* If the signs of x and y differ, and the remainder is non-0,
587 * C89 doesn't define whether xdivy is now the floor or the
588 * ceiling of the infinitely precise quotient. We want the floor,
589 * and we have it iff the remainder's sign matches y's.
590 */
591 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
592 xmody += y;
593 --xdivy;
594 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000595 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000596 *p_xdivy = xdivy;
597 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000598 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000599}
600
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000601static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000602int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000603{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000604 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000605 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000606 CONVERT_TO_LONG(x, xi);
607 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000608 switch (i_divmod(xi, yi, &d, &m)) {
609 case DIVMOD_OK:
610 return PyInt_FromLong(d);
611 case DIVMOD_OVERFLOW:
612 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
613 (PyObject *)y);
614 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000615 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000616 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000617}
618
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000619static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000620int_classic_div(PyIntObject *x, PyIntObject *y)
621{
622 long xi, yi;
623 long d, m;
624 CONVERT_TO_LONG(x, xi);
625 CONVERT_TO_LONG(y, yi);
626 if (Py_DivisionWarningFlag &&
627 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
628 return NULL;
629 switch (i_divmod(xi, yi, &d, &m)) {
630 case DIVMOD_OK:
631 return PyInt_FromLong(d);
632 case DIVMOD_OVERFLOW:
633 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
634 (PyObject *)y);
635 default:
636 return NULL;
637 }
638}
639
640static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000641int_true_divide(PyObject *v, PyObject *w)
642{
Tim Peterse2a60002001-09-04 06:17:36 +0000643 /* If they aren't both ints, give someone else a chance. In
644 particular, this lets int/long get handled by longs, which
645 underflows to 0 gracefully if the long is too big to convert
646 to float. */
647 if (PyInt_Check(v) && PyInt_Check(w))
648 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
649 Py_INCREF(Py_NotImplemented);
650 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000651}
652
653static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000654int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000655{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000656 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000657 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000658 CONVERT_TO_LONG(x, xi);
659 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000660 switch (i_divmod(xi, yi, &d, &m)) {
661 case DIVMOD_OK:
662 return PyInt_FromLong(m);
663 case DIVMOD_OVERFLOW:
664 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
665 (PyObject *)y);
666 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000667 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000668 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000669}
670
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000671static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000672int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000673{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000674 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000675 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000676 CONVERT_TO_LONG(x, xi);
677 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000678 switch (i_divmod(xi, yi, &d, &m)) {
679 case DIVMOD_OK:
680 return Py_BuildValue("(ll)", d, m);
681 case DIVMOD_OVERFLOW:
682 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
683 (PyObject *)y);
684 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000685 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000686 }
Guido van Rossum00466951991-05-05 20:08:27 +0000687}
688
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000689static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000690int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000691{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000692 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000693 CONVERT_TO_LONG(v, iv);
694 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000695 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000696 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000697 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
698 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000699 return NULL;
700 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000701 /* Return a float. This works because we know that
702 this calls float_pow() which converts its
703 arguments to double. */
704 return PyFloat_Type.tp_as_number->nb_power(
705 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000706 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000708 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000709 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000710 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000711 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000712 return NULL;
713 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000714 }
715 /*
716 * XXX: The original exponentiation code stopped looping
717 * when temp hit zero; this code will continue onwards
718 * unnecessarily, but at least it won't cause any errors.
719 * Hopefully the speed improvement from the fast exponentiation
720 * will compensate for the slight inefficiency.
721 * XXX: Better handling of overflows is desperately needed.
722 */
723 temp = iv;
724 ix = 1;
725 while (iw > 0) {
726 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000727 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000728 ix = ix*temp;
729 if (temp == 0)
730 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000731 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000732 return PyLong_Type.tp_as_number->nb_power(
733 (PyObject *)v,
734 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000735 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000736 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000737 }
738 iw >>= 1; /* Shift exponent down by 1 bit */
739 if (iw==0) break;
740 prev = temp;
741 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000742 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000743 return PyLong_Type.tp_as_number->nb_power(
744 (PyObject *)v, (PyObject *)w, (PyObject *)z);
745 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000746 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000747 /* If we did a multiplication, perform a modulo */
748 ix = ix % iz;
749 temp = temp % iz;
750 }
751 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000752 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000753 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000754 switch (i_divmod(ix, iz, &div, &mod)) {
755 case DIVMOD_OK:
756 ix = mod;
757 break;
758 case DIVMOD_OVERFLOW:
759 return PyLong_Type.tp_as_number->nb_power(
760 (PyObject *)v, (PyObject *)w, (PyObject *)z);
761 default:
762 return NULL;
763 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000764 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000766}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000767
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000768static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000769int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770{
Martin v. Löwis820d6ac2006-10-04 05:47:34 +0000771 register long a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772 a = v->ob_ival;
Armin Rigo7ccbca92006-10-04 12:17:45 +0000773 /* check for overflow */
774 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
Tim Petersc8854432004-08-25 02:14:08 +0000775 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000776 if (o != NULL) {
777 PyObject *result = PyNumber_Negative(o);
778 Py_DECREF(o);
779 return result;
780 }
781 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000782 }
Martin v. Löwis820d6ac2006-10-04 05:47:34 +0000783 return PyInt_FromLong(-a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000784}
785
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000786static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000787int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000788{
789 if (v->ob_ival >= 0)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000790 return int_int(v);
Guido van Rossum00466951991-05-05 20:08:27 +0000791 else
792 return int_neg(v);
793}
794
Guido van Rossum0bff0151991-05-14 12:05:32 +0000795static int
Fred Drakea2f55112000-07-09 15:16:51 +0000796int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000797{
798 return v->ob_ival != 0;
799}
800
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000801static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000802int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000803{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000804 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000805}
806
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000807static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000808int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000809{
Guido van Rossum078151d2002-08-11 04:24:12 +0000810 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000811 PyObject *vv, *ww, *result;
812
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000813 CONVERT_TO_LONG(v, a);
814 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000815 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000816 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000817 return NULL;
818 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000819 if (a == 0 || b == 0)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000820 return int_int(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000821 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000822 vv = PyLong_FromLong(PyInt_AS_LONG(v));
823 if (vv == NULL)
824 return NULL;
825 ww = PyLong_FromLong(PyInt_AS_LONG(w));
826 if (ww == NULL) {
827 Py_DECREF(vv);
828 return NULL;
829 }
830 result = PyNumber_Lshift(vv, ww);
831 Py_DECREF(vv);
832 Py_DECREF(ww);
833 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000834 }
Tim Petersda1a2212002-08-11 17:54:42 +0000835 c = a << b;
836 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000837 vv = PyLong_FromLong(PyInt_AS_LONG(v));
838 if (vv == NULL)
839 return NULL;
840 ww = PyLong_FromLong(PyInt_AS_LONG(w));
841 if (ww == NULL) {
842 Py_DECREF(vv);
843 return NULL;
844 }
845 result = PyNumber_Lshift(vv, ww);
846 Py_DECREF(vv);
847 Py_DECREF(ww);
848 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000849 }
850 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000851}
852
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000853static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000854int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000855{
856 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000857 CONVERT_TO_LONG(v, a);
858 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000859 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000860 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000861 return NULL;
862 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000863 if (a == 0 || b == 0)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000864 return int_int(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000865 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000866 if (a < 0)
867 a = -1;
868 else
869 a = 0;
870 }
871 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000872 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000873 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000874 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000875}
876
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000877static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000878int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000879{
880 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000881 CONVERT_TO_LONG(v, a);
882 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000883 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000884}
885
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000886static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000887int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000888{
889 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000890 CONVERT_TO_LONG(v, a);
891 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000892 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000893}
894
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000895static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000896int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000897{
898 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000899 CONVERT_TO_LONG(v, a);
900 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000901 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000902}
903
Guido van Rossum1952e382001-09-19 01:25:16 +0000904static int
905int_coerce(PyObject **pv, PyObject **pw)
906{
907 if (PyInt_Check(*pw)) {
908 Py_INCREF(*pv);
909 Py_INCREF(*pw);
910 return 0;
911 }
912 return 1; /* Can't do it */
913}
914
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000915static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000916int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000917{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000918 if (PyInt_CheckExact(v))
919 Py_INCREF(v);
920 else
921 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000922 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000923}
924
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000925static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000926int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000927{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000928 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000929}
930
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000931static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000932int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000933{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000934 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000935}
936
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000937static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000938int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000939{
Eric Smith5e527eb2008-02-10 01:36:53 +0000940 return _PyInt_Format(v, 8, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000941}
942
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000943static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000944int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000945{
Eric Smith5e527eb2008-02-10 01:36:53 +0000946 return _PyInt_Format(v, 16, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000947}
948
Jeremy Hylton938ace62002-07-17 16:30:39 +0000949static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000950int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
951
Tim Peters6d6c1a32001-08-02 04:15:00 +0000952static PyObject *
953int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
954{
955 PyObject *x = NULL;
956 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000957 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000958
Guido van Rossumbef14172001-08-29 15:47:46 +0000959 if (type != &PyInt_Type)
960 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000961 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
962 &x, &base))
963 return NULL;
964 if (x == NULL)
965 return PyInt_FromLong(0L);
966 if (base == -909)
967 return PyNumber_Int(x);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000968 if (PyString_Check(x)) {
Georg Brandl2c1375c2006-10-12 11:27:59 +0000969 /* Since PyInt_FromString doesn't have a length parameter,
970 * check here for possible NULs in the string. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000971 char *string = PyString_AS_STRING(x);
972 if (strlen(string) != PyString_Size(x)) {
Georg Brandl2c1375c2006-10-12 11:27:59 +0000973 /* create a repr() of the input string,
974 * just like PyInt_FromString does */
975 PyObject *srepr;
976 srepr = PyObject_Repr(x);
977 if (srepr == NULL)
978 return NULL;
979 PyErr_Format(PyExc_ValueError,
980 "invalid literal for int() with base %d: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000981 base, PyString_AS_STRING(srepr));
Georg Brandl2c1375c2006-10-12 11:27:59 +0000982 Py_DECREF(srepr);
983 return NULL;
984 }
985 return PyInt_FromString(string, NULL, base);
986 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000987#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000988 if (PyUnicode_Check(x))
989 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
990 PyUnicode_GET_SIZE(x),
991 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000992#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +0000993 PyErr_SetString(PyExc_TypeError,
994 "int() can't convert non-string with explicit base");
995 return NULL;
996}
997
Guido van Rossumbef14172001-08-29 15:47:46 +0000998/* Wimpy, slow approach to tp_new calls for subtypes of int:
999 first create a regular int from whatever arguments we got,
1000 then allocate a subtype instance and initialize its ob_ival
1001 from the regular int. The regular int is then thrown away.
1002*/
1003static PyObject *
1004int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1005{
Anthony Baxter377be112006-04-11 06:54:30 +00001006 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001007 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001008
1009 assert(PyType_IsSubtype(type, &PyInt_Type));
1010 tmp = int_new(&PyInt_Type, args, kwds);
1011 if (tmp == NULL)
1012 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001013 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001014 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001015 if (ival == -1 && PyErr_Occurred()) {
1016 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001017 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001018 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001019 } else {
1020 ival = ((PyIntObject *)tmp)->ob_ival;
1021 }
1022
Anthony Baxter377be112006-04-11 06:54:30 +00001023 newobj = type->tp_alloc(type, 0);
1024 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001025 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001026 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001027 }
Anthony Baxter377be112006-04-11 06:54:30 +00001028 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001029 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00001030 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001031}
1032
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001033static PyObject *
1034int_getnewargs(PyIntObject *v)
1035{
1036 return Py_BuildValue("(l)", v->ob_ival);
1037}
1038
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001039static PyObject *
1040int_getN(PyIntObject *v, void *context) {
Jeffrey Yasskin5de250e2008-03-18 01:09:59 +00001041 return PyInt_FromLong((Py_intptr_t)context);
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001042}
1043
Eric Smith5e527eb2008-02-10 01:36:53 +00001044/* Convert an integer to the given base. Returns a string.
1045 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'.
1046 If newstyle is zero, then use the pre-2.6 behavior of octal having
1047 a leading "0" */
1048PyAPI_FUNC(PyObject*)
1049_PyInt_Format(PyIntObject *v, int base, int newstyle)
1050{
1051 /* There are no doubt many, many ways to optimize this, using code
1052 similar to _PyLong_Format */
1053 long n = v->ob_ival;
1054 int negative = n < 0;
1055 int is_zero = n == 0;
1056
1057 /* For the reasoning behind this size, see
1058 http://c-faq.com/misc/hexio.html. Then, add a few bytes for
1059 the possible sign and prefix "0[box]" */
1060 char buf[sizeof(n)*CHAR_BIT+6];
1061
1062 /* Start by pointing to the end of the buffer. We fill in from
1063 the back forward. */
1064 char* p = &buf[sizeof(buf)];
1065
1066 assert(base >= 2 && base <= 36);
1067
1068 do {
1069 /* I'd use i_divmod, except it doesn't produce the results
1070 I want when n is negative. So just duplicate the salient
1071 part here. */
1072 long div = n / base;
1073 long mod = n - div * base;
1074
1075 /* convert abs(mod) to the right character in [0-9, a-z] */
1076 char cdigit = (char)(mod < 0 ? -mod : mod);
1077 cdigit += (cdigit < 10) ? '0' : 'a'-10;
1078 *--p = cdigit;
1079
1080 n = div;
1081 } while(n);
1082
1083 if (base == 2) {
1084 *--p = 'b';
1085 *--p = '0';
1086 }
1087 else if (base == 8) {
1088 if (newstyle) {
1089 *--p = 'o';
1090 *--p = '0';
1091 }
1092 else
1093 if (!is_zero)
1094 *--p = '0';
1095 }
1096 else if (base == 16) {
1097 *--p = 'x';
1098 *--p = '0';
1099 }
1100 else if (base != 10) {
1101 *--p = '#';
1102 *--p = '0' + base%10;
1103 if (base > 10)
1104 *--p = '0' + base/10;
1105 }
1106 if (negative)
1107 *--p = '-';
1108
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001109 return PyString_FromStringAndSize(p, &buf[sizeof(buf)] - p);
Eric Smith5e527eb2008-02-10 01:36:53 +00001110}
1111
Eric Smitha9f7d622008-02-17 19:46:49 +00001112static PyObject *
1113int__format__(PyObject *self, PyObject *args)
1114{
1115 PyObject *format_spec;
1116
1117 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
1118 return NULL;
Christian Heimes593daf52008-05-26 12:51:38 +00001119 if (PyBytes_Check(format_spec))
Eric Smithdc13b792008-05-30 18:10:04 +00001120 return _PyInt_FormatAdvanced(self,
1121 PyBytes_AS_STRING(format_spec),
1122 PyBytes_GET_SIZE(format_spec));
Eric Smitha9f7d622008-02-17 19:46:49 +00001123 if (PyUnicode_Check(format_spec)) {
1124 /* Convert format_spec to a str */
Eric Smithdc13b792008-05-30 18:10:04 +00001125 PyObject *result;
1126 PyObject *str_spec = PyObject_Str(format_spec);
Eric Smitha9f7d622008-02-17 19:46:49 +00001127
Eric Smithdc13b792008-05-30 18:10:04 +00001128 if (str_spec == NULL)
1129 return NULL;
Eric Smitha9f7d622008-02-17 19:46:49 +00001130
Eric Smithdc13b792008-05-30 18:10:04 +00001131 result = _PyInt_FormatAdvanced(self,
1132 PyBytes_AS_STRING(str_spec),
1133 PyBytes_GET_SIZE(str_spec));
Eric Smitha9f7d622008-02-17 19:46:49 +00001134
Eric Smithdc13b792008-05-30 18:10:04 +00001135 Py_DECREF(str_spec);
Eric Smitha9f7d622008-02-17 19:46:49 +00001136 return result;
1137 }
1138 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
1139 return NULL;
1140}
1141
Mark Dickinson1a707982008-12-17 16:14:37 +00001142static const unsigned char BitLengthTable[32] = {
1143 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
1144 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
1145};
1146
1147static PyObject *
1148int_bit_length(PyIntObject *v)
1149{
1150 unsigned long n;
1151 long r = 0;
1152
1153 if (v->ob_ival < 0)
1154 /* avoid undefined behaviour when v->ob_ival == -LONG_MAX-1 */
1155 n = 0U-(unsigned long)v->ob_ival;
1156 else
1157 n = (unsigned long)v->ob_ival;
1158
1159 while (n >= 32) {
1160 r += 6;
1161 n >>= 6;
1162 }
1163 r += (long)(BitLengthTable[n]);
1164 return PyInt_FromLong(r);
1165}
1166
1167PyDoc_STRVAR(int_bit_length_doc,
1168"int.bit_length() -> int\n\
1169\n\
1170Number of bits necessary to represent self in binary.\n\
1171>>> bin(37)\n\
1172'0b100101'\n\
1173>>> (37).bit_length()\n\
11746");
1175
Christian Heimes6f341092008-04-18 23:13:07 +00001176#if 0
1177static PyObject *
1178int_is_finite(PyObject *v)
1179{
1180 Py_RETURN_TRUE;
1181}
1182#endif
1183
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001184static PyMethodDef int_methods[] = {
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001185 {"conjugate", (PyCFunction)int_int, METH_NOARGS,
1186 "Returns self, the complex conjugate of any int."},
Mark Dickinson1a707982008-12-17 16:14:37 +00001187 {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS,
1188 int_bit_length_doc},
Christian Heimes6f341092008-04-18 23:13:07 +00001189#if 0
1190 {"is_finite", (PyCFunction)int_is_finite, METH_NOARGS,
1191 "Returns always True."},
1192#endif
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001193 {"__trunc__", (PyCFunction)int_int, METH_NOARGS,
1194 "Truncating an Integral returns itself."},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001195 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
Eric Smitha9f7d622008-02-17 19:46:49 +00001196 {"__format__", (PyCFunction)int__format__, METH_VARARGS},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001197 {NULL, NULL} /* sentinel */
1198};
1199
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001200static PyGetSetDef int_getset[] = {
1201 {"real",
1202 (getter)int_int, (setter)NULL,
1203 "the real part of a complex number",
1204 NULL},
1205 {"imag",
1206 (getter)int_getN, (setter)NULL,
1207 "the imaginary part of a complex number",
1208 (void*)0},
1209 {"numerator",
1210 (getter)int_int, (setter)NULL,
1211 "the numerator of a rational number in lowest terms",
1212 NULL},
1213 {"denominator",
1214 (getter)int_getN, (setter)NULL,
1215 "the denominator of a rational number in lowest terms",
1216 (void*)1},
1217 {NULL} /* Sentinel */
1218};
1219
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001220PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001221"int(x[, base]) -> integer\n\
1222\n\
1223Convert a string or number to an integer, if possible. A floating point\n\
1224argument will be truncated towards zero (this does not include a string\n\
1225representation of a floating point number!) When converting a string, use\n\
1226the optional base. It is an error to supply a base when converting a\n\
Gustavo Niemeyer37e65022007-01-10 16:15:48 +00001227non-string. If base is zero, the proper base is guessed based on the\n\
Gustavo Niemeyera443bc82007-01-10 16:13:40 +00001228string content. If the argument is outside the integer range a\n\
1229long object will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001230
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001231static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001232 (binaryfunc)int_add, /*nb_add*/
1233 (binaryfunc)int_sub, /*nb_subtract*/
1234 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00001235 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001236 (binaryfunc)int_mod, /*nb_remainder*/
1237 (binaryfunc)int_divmod, /*nb_divmod*/
1238 (ternaryfunc)int_pow, /*nb_power*/
1239 (unaryfunc)int_neg, /*nb_negative*/
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001240 (unaryfunc)int_int, /*nb_positive*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001241 (unaryfunc)int_abs, /*nb_absolute*/
1242 (inquiry)int_nonzero, /*nb_nonzero*/
1243 (unaryfunc)int_invert, /*nb_invert*/
1244 (binaryfunc)int_lshift, /*nb_lshift*/
1245 (binaryfunc)int_rshift, /*nb_rshift*/
1246 (binaryfunc)int_and, /*nb_and*/
1247 (binaryfunc)int_xor, /*nb_xor*/
1248 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +00001249 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001250 (unaryfunc)int_int, /*nb_int*/
1251 (unaryfunc)int_long, /*nb_long*/
1252 (unaryfunc)int_float, /*nb_float*/
1253 (unaryfunc)int_oct, /*nb_oct*/
1254 (unaryfunc)int_hex, /*nb_hex*/
1255 0, /*nb_inplace_add*/
1256 0, /*nb_inplace_subtract*/
1257 0, /*nb_inplace_multiply*/
1258 0, /*nb_inplace_divide*/
1259 0, /*nb_inplace_remainder*/
1260 0, /*nb_inplace_power*/
1261 0, /*nb_inplace_lshift*/
1262 0, /*nb_inplace_rshift*/
1263 0, /*nb_inplace_and*/
1264 0, /*nb_inplace_xor*/
1265 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001266 (binaryfunc)int_div, /* nb_floor_divide */
1267 int_true_divide, /* nb_true_divide */
1268 0, /* nb_inplace_floor_divide */
1269 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001270 (unaryfunc)int_int, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001271};
1272
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001273PyTypeObject PyInt_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001274 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001275 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001276 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001277 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001278 (destructor)int_dealloc, /* tp_dealloc */
1279 (printfunc)int_print, /* tp_print */
1280 0, /* tp_getattr */
1281 0, /* tp_setattr */
1282 (cmpfunc)int_compare, /* tp_compare */
1283 (reprfunc)int_repr, /* tp_repr */
1284 &int_as_number, /* tp_as_number */
1285 0, /* tp_as_sequence */
1286 0, /* tp_as_mapping */
1287 (hashfunc)int_hash, /* tp_hash */
1288 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001289 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001290 PyObject_GenericGetAttr, /* tp_getattro */
1291 0, /* tp_setattro */
1292 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001293 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00001294 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001295 int_doc, /* tp_doc */
1296 0, /* tp_traverse */
1297 0, /* tp_clear */
1298 0, /* tp_richcompare */
1299 0, /* tp_weaklistoffset */
1300 0, /* tp_iter */
1301 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001302 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001303 0, /* tp_members */
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001304 int_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001305 0, /* tp_base */
1306 0, /* tp_dict */
1307 0, /* tp_descr_get */
1308 0, /* tp_descr_set */
1309 0, /* tp_dictoffset */
1310 0, /* tp_init */
1311 0, /* tp_alloc */
1312 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001313 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001314};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001315
Neal Norwitzc91ed402002-12-30 22:29:22 +00001316int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001317_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001318{
1319 PyIntObject *v;
1320 int ival;
1321#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1322 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001323 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001324 return 0;
1325 /* PyObject_New is inlined */
1326 v = free_list;
Christian Heimese93237d2007-12-19 02:37:44 +00001327 free_list = (PyIntObject *)Py_TYPE(v);
Neal Norwitzc91ed402002-12-30 22:29:22 +00001328 PyObject_INIT(v, &PyInt_Type);
1329 v->ob_ival = ival;
1330 small_ints[ival + NSMALLNEGINTS] = v;
1331 }
1332#endif
1333 return 1;
1334}
1335
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001336int
1337PyInt_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001338{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001339 PyIntObject *p;
1340 PyIntBlock *list, *next;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001341 int i;
1342 int u; /* remaining unfreed ints per block */
1343 int freelist_size = 0;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001344
Guido van Rossumda084ed1999-03-10 22:55:24 +00001345 list = block_list;
1346 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001347 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001348 while (list != NULL) {
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001349 u = 0;
1350 for (i = 0, p = &list->objects[0];
1351 i < N_INTOBJECTS;
1352 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001353 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001354 u++;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001355 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001356 next = list->next;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001357 if (u) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001358 list->next = block_list;
1359 block_list = list;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001360 for (i = 0, p = &list->objects[0];
1361 i < N_INTOBJECTS;
1362 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001363 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001364 p->ob_refcnt == 0) {
Christian Heimese93237d2007-12-19 02:37:44 +00001365 Py_TYPE(p) = (struct _typeobject *)
Guido van Rossum51288bc1999-03-19 20:30:39 +00001366 free_list;
1367 free_list = p;
1368 }
1369#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1370 else if (-NSMALLNEGINTS <= p->ob_ival &&
1371 p->ob_ival < NSMALLPOSINTS &&
1372 small_ints[p->ob_ival +
1373 NSMALLNEGINTS] == NULL) {
1374 Py_INCREF(p);
1375 small_ints[p->ob_ival +
1376 NSMALLNEGINTS] = p;
1377 }
1378#endif
1379 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001380 }
1381 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001382 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001383 }
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001384 freelist_size += u;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001385 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001386 }
Christian Heimes422051a2008-02-04 18:00:12 +00001387
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001388 return freelist_size;
Christian Heimes422051a2008-02-04 18:00:12 +00001389}
1390
1391void
1392PyInt_Fini(void)
1393{
1394 PyIntObject *p;
1395 PyIntBlock *list;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001396 int i;
1397 int u; /* total unfreed ints per block */
Christian Heimes422051a2008-02-04 18:00:12 +00001398
1399#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Christian Heimes422051a2008-02-04 18:00:12 +00001400 PyIntObject **q;
1401
1402 i = NSMALLNEGINTS + NSMALLPOSINTS;
1403 q = small_ints;
1404 while (--i >= 0) {
1405 Py_XDECREF(*q);
1406 *q++ = NULL;
1407 }
1408#endif
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001409 u = PyInt_ClearFreeList();
Guido van Rossum3fce8831999-03-12 19:43:17 +00001410 if (!Py_VerboseFlag)
1411 return;
1412 fprintf(stderr, "# cleanup ints");
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001413 if (!u) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001414 fprintf(stderr, "\n");
1415 }
1416 else {
1417 fprintf(stderr,
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001418 ": %d unfreed int%s\n",
1419 u, u == 1 ? "" : "s");
Guido van Rossum3fce8831999-03-12 19:43:17 +00001420 }
1421 if (Py_VerboseFlag > 1) {
1422 list = block_list;
1423 while (list != NULL) {
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001424 for (i = 0, p = &list->objects[0];
1425 i < N_INTOBJECTS;
1426 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001427 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001428 /* XXX(twouters) cast refcount to
1429 long until %zd is universally
1430 available
1431 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001432 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001433 "# <int at %p, refcnt=%ld, val=%ld>\n",
1434 p, (long)p->ob_refcnt,
1435 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001436 }
1437 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001438 }
1439 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001440}