blob: 406a45afefad1a28110fcfc6734c3b599eb26c3a [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
81int quick_int_allocs, quick_neg_int_allocs;
82#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000083
Guido van Rossumc0b618a1997-05-02 03:12:38 +000084PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000085PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000087 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000088#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Neal Norwitzc91ed402002-12-30 22:29:22 +000089 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
90 v = small_ints[ival + NSMALLNEGINTS];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000092#ifdef COUNT_ALLOCS
93 if (ival >= 0)
94 quick_int_allocs++;
95 else
96 quick_neg_int_allocs++;
97#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +000098 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000099 }
100#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000101 if (free_list == NULL) {
102 if ((free_list = fill_free_list()) == NULL)
103 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000105 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000106 v = free_list;
Christian Heimese93237d2007-12-19 02:37:44 +0000107 free_list = (PyIntObject *)Py_TYPE(v);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000108 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000109 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111}
112
Martin v. Löwis18e16552006-02-15 17:27:45 +0000113PyObject *
114PyInt_FromSize_t(size_t ival)
115{
116 if (ival <= LONG_MAX)
117 return PyInt_FromLong((long)ival);
118 return _PyLong_FromSize_t(ival);
119}
120
121PyObject *
122PyInt_FromSsize_t(Py_ssize_t ival)
123{
124 if (ival >= LONG_MIN && ival <= LONG_MAX)
125 return PyInt_FromLong((long)ival);
126 return _PyLong_FromSsize_t(ival);
127}
128
Guido van Rossum3f5da241990-12-20 15:06:42 +0000129static void
Fred Drakea2f55112000-07-09 15:16:51 +0000130int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000131{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000132 if (PyInt_CheckExact(v)) {
Christian Heimese93237d2007-12-19 02:37:44 +0000133 Py_TYPE(v) = (struct _typeobject *)free_list;
Guido van Rossumbef14172001-08-29 15:47:46 +0000134 free_list = v;
135 }
136 else
Christian Heimese93237d2007-12-19 02:37:44 +0000137 Py_TYPE(v)->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138}
139
Guido van Rossum93646982002-04-26 00:53:34 +0000140static void
141int_free(PyIntObject *v)
142{
Christian Heimese93237d2007-12-19 02:37:44 +0000143 Py_TYPE(v) = (struct _typeobject *)free_list;
Guido van Rossum93646982002-04-26 00:53:34 +0000144 free_list = v;
145}
146
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147long
Fred Drakea2f55112000-07-09 15:16:51 +0000148PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 PyNumberMethods *nb;
151 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000152 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000153
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000154 if (op && PyInt_Check(op))
155 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000156
Christian Heimese93237d2007-12-19 02:37:44 +0000157 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000158 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000159 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160 return -1;
161 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000162
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000164 if (io == NULL)
165 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000166 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000167 if (PyLong_Check(io)) {
168 /* got a long? => retry int conversion */
169 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000170 Py_DECREF(io);
171 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000172 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000173 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000174 }
175 else
176 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000177 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000178 PyErr_SetString(PyExc_TypeError,
179 "nb_int should return int object");
180 return -1;
181 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000182 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000183
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000184 val = PyInt_AS_LONG(io);
185 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000186
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000187 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000188}
189
Martin v. Löwis18e16552006-02-15 17:27:45 +0000190Py_ssize_t
191PyInt_AsSsize_t(register PyObject *op)
192{
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000193#if SIZEOF_SIZE_T != SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000194 PyNumberMethods *nb;
195 PyIntObject *io;
196 Py_ssize_t val;
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000197#endif
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000198
199 if (op == NULL) {
200 PyErr_SetString(PyExc_TypeError, "an integer is required");
201 return -1;
202 }
203
204 if (PyInt_Check(op))
205 return PyInt_AS_LONG((PyIntObject*) op);
206 if (PyLong_Check(op))
Martin v. Löwis18e16552006-02-15 17:27:45 +0000207 return _PyLong_AsSsize_t(op);
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000208#if SIZEOF_SIZE_T == SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000209 return PyInt_AsLong(op);
210#else
211
Christian Heimese93237d2007-12-19 02:37:44 +0000212 if ((nb = Py_TYPE(op)->tp_as_number) == NULL ||
Martin v. Löwis18e16552006-02-15 17:27:45 +0000213 (nb->nb_int == NULL && nb->nb_long == 0)) {
214 PyErr_SetString(PyExc_TypeError, "an integer is required");
215 return -1;
216 }
217
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +0000218 if (nb->nb_long != 0)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000219 io = (PyIntObject*) (*nb->nb_long) (op);
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +0000220 else
Martin v. Löwis18e16552006-02-15 17:27:45 +0000221 io = (PyIntObject*) (*nb->nb_int) (op);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000222 if (io == NULL)
223 return -1;
224 if (!PyInt_Check(io)) {
225 if (PyLong_Check(io)) {
226 /* got a long? => retry int conversion */
227 val = _PyLong_AsSsize_t((PyObject *)io);
228 Py_DECREF(io);
229 if ((val == -1) && PyErr_Occurred())
230 return -1;
231 return val;
232 }
233 else
234 {
235 Py_DECREF(io);
236 PyErr_SetString(PyExc_TypeError,
237 "nb_int should return int object");
238 return -1;
239 }
240 }
241
242 val = PyInt_AS_LONG(io);
243 Py_DECREF(io);
244
245 return val;
246#endif
247}
248
Thomas Hellera4ea6032003-04-17 18:55:45 +0000249unsigned long
250PyInt_AsUnsignedLongMask(register PyObject *op)
251{
252 PyNumberMethods *nb;
253 PyIntObject *io;
254 unsigned long val;
255
256 if (op && PyInt_Check(op))
257 return PyInt_AS_LONG((PyIntObject*) op);
258 if (op && PyLong_Check(op))
259 return PyLong_AsUnsignedLongMask(op);
260
Christian Heimese93237d2007-12-19 02:37:44 +0000261 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
Thomas Hellera4ea6032003-04-17 18:55:45 +0000262 nb->nb_int == NULL) {
263 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000264 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000265 }
266
267 io = (PyIntObject*) (*nb->nb_int) (op);
268 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000269 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000270 if (!PyInt_Check(io)) {
271 if (PyLong_Check(io)) {
272 val = PyLong_AsUnsignedLongMask((PyObject *)io);
273 Py_DECREF(io);
274 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000275 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000276 return val;
277 }
278 else
279 {
280 Py_DECREF(io);
281 PyErr_SetString(PyExc_TypeError,
282 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000283 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000284 }
285 }
286
287 val = PyInt_AS_LONG(io);
288 Py_DECREF(io);
289
290 return val;
291}
292
293#ifdef HAVE_LONG_LONG
294unsigned PY_LONG_LONG
295PyInt_AsUnsignedLongLongMask(register PyObject *op)
296{
297 PyNumberMethods *nb;
298 PyIntObject *io;
299 unsigned PY_LONG_LONG val;
300
301 if (op && PyInt_Check(op))
302 return PyInt_AS_LONG((PyIntObject*) op);
303 if (op && PyLong_Check(op))
304 return PyLong_AsUnsignedLongLongMask(op);
305
Christian Heimese93237d2007-12-19 02:37:44 +0000306 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
Thomas Hellera4ea6032003-04-17 18:55:45 +0000307 nb->nb_int == NULL) {
308 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000309 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000310 }
311
312 io = (PyIntObject*) (*nb->nb_int) (op);
313 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000314 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000315 if (!PyInt_Check(io)) {
316 if (PyLong_Check(io)) {
317 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
318 Py_DECREF(io);
319 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000320 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000321 return val;
322 }
323 else
324 {
325 Py_DECREF(io);
326 PyErr_SetString(PyExc_TypeError,
327 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000328 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000329 }
330 }
331
332 val = PyInt_AS_LONG(io);
333 Py_DECREF(io);
334
335 return val;
336}
337#endif
338
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000339PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000340PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000341{
342 char *end;
343 long x;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000344 Py_ssize_t slen;
345 PyObject *sobj, *srepr;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000346
347 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000348 PyErr_SetString(PyExc_ValueError,
349 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000350 return NULL;
351 }
352
353 while (*s && isspace(Py_CHARMASK(*s)))
354 s++;
355 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000356 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000357 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000358 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000359 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000360 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000361 else
362 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000363 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000364 goto bad;
365 while (*end && isspace(Py_CHARMASK(*end)))
366 end++;
367 if (*end != '\0') {
368 bad:
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000369 slen = strlen(s) < 200 ? strlen(s) : 200;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000370 sobj = PyString_FromStringAndSize(s, slen);
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000371 if (sobj == NULL)
372 return NULL;
373 srepr = PyObject_Repr(sobj);
374 Py_DECREF(sobj);
375 if (srepr == NULL)
376 return NULL;
377 PyErr_Format(PyExc_ValueError,
378 "invalid literal for int() with base %d: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000379 base, PyString_AS_STRING(srepr));
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000380 Py_DECREF(srepr);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000381 return NULL;
382 }
Tim Petersc8854432004-08-25 02:14:08 +0000383 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000384 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000385 if (pend)
386 *pend = end;
387 return PyInt_FromLong(x);
388}
389
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000390#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000391PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000392PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000393{
Walter Dörwald07e14762002-11-06 16:15:14 +0000394 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +0000395 char *buffer = (char *)PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000396
Walter Dörwald07e14762002-11-06 16:15:14 +0000397 if (buffer == NULL)
Neal Norwitzd501d1f2007-05-16 04:33:50 +0000398 return PyErr_NoMemory();
Walter Dörwald07e14762002-11-06 16:15:14 +0000399
400 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
401 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000402 return NULL;
403 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000404 result = PyInt_FromString(buffer, NULL, base);
405 PyMem_FREE(buffer);
406 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000407}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000408#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000409
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000410/* Methods */
411
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000412/* Integers are seen as the "smallest" of all numeric types and thus
413 don't have any knowledge about conversion of other types to
414 integers. */
415
416#define CONVERT_TO_LONG(obj, lng) \
417 if (PyInt_Check(obj)) { \
418 lng = PyInt_AS_LONG(obj); \
419 } \
420 else { \
421 Py_INCREF(Py_NotImplemented); \
422 return Py_NotImplemented; \
423 }
424
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000425/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000426static int
Fred Drakea2f55112000-07-09 15:16:51 +0000427int_print(PyIntObject *v, FILE *fp, int flags)
428 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000429{
Brett Cannon01531592007-09-17 03:28:34 +0000430 long int_val = v->ob_ival;
431 Py_BEGIN_ALLOW_THREADS
432 fprintf(fp, "%ld", int_val);
433 Py_END_ALLOW_THREADS
Guido van Rossum90933611991-06-07 16:10:43 +0000434 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000435}
436
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000437static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000438int_repr(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439{
Eric Smith5e527eb2008-02-10 01:36:53 +0000440 return _PyInt_Format(v, 10, 0);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441}
442
443static int
Fred Drakea2f55112000-07-09 15:16:51 +0000444int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000445{
446 register long i = v->ob_ival;
447 register long j = w->ob_ival;
448 return (i < j) ? -1 : (i > j) ? 1 : 0;
449}
450
Guido van Rossum9bfef441993-03-29 10:43:31 +0000451static long
Fred Drakea2f55112000-07-09 15:16:51 +0000452int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000453{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000454 /* XXX If this is changed, you also need to change the way
455 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000456 long x = v -> ob_ival;
457 if (x == -1)
458 x = -2;
459 return x;
460}
461
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000462static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000463int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464{
465 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000466 CONVERT_TO_LONG(v, a);
467 CONVERT_TO_LONG(w, b);
Mark Dickinson6df863c92009-12-02 17:36:34 +0000468 /* casts in the line below avoid undefined behaviour on overflow */
469 x = (long)((unsigned long)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);
Mark Dickinson6df863c92009-12-02 17:36:34 +0000481 /* casts in the line below avoid undefined behaviour on overflow */
482 x = (long)((unsigned long)a - b);
Guido van Rossume27f7952001-08-23 02:59:04 +0000483 if ((x^a) >= 0 || (x^~b) >= 0)
484 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000485 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
486 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487}
488
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000489/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000490Integer overflow checking for * is painful: Python tried a couple ways, but
491they didn't work on all platforms, or failed in endcases (a product of
492-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000493
Tim Petersa3c01ce2001-12-04 23:05:10 +0000494Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000495
Tim Petersa3c01ce2001-12-04 23:05:10 +0000496The native long product x*y is either exactly right or *way* off, being
497just the last n bits of the true product, where n is the number of bits
498in a long (the delivered product is the true product plus i*2**n for
499some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000500
Tim Petersa3c01ce2001-12-04 23:05:10 +0000501The native double product (double)x * (double)y is subject to three
502rounding errors: on a sizeof(long)==8 box, each cast to double can lose
503info, and even on a sizeof(long)==4 box, the multiplication can lose info.
504But, unlike the native long product, it's not in *range* trouble: even
505if sizeof(long)==32 (256-bit longs), the product easily fits in the
506dynamic range of a double. So the leading 50 (or so) bits of the double
507product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000508
Tim Petersa3c01ce2001-12-04 23:05:10 +0000509We check these two ways against each other, and declare victory if they're
510approximately the same. Else, because the native long product is the only
511one that can lose catastrophic amounts of information, it's the native long
512product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000513*/
514
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000515static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000516int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000518 long a, b;
519 long longprod; /* a*b in native long arithmetic */
520 double doubled_longprod; /* (double)longprod */
521 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000522
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000523 CONVERT_TO_LONG(v, a);
524 CONVERT_TO_LONG(w, b);
Mark Dickinson6df863c92009-12-02 17:36:34 +0000525 /* casts in the next line avoid undefined behaviour on overflow */
526 longprod = (long)((unsigned long)a * b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000527 doubleprod = (double)a * (double)b;
528 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000529
Tim Petersa3c01ce2001-12-04 23:05:10 +0000530 /* Fast path for normal case: small multiplicands, and no info
531 is lost in either method. */
532 if (doubled_longprod == doubleprod)
533 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000534
Tim Petersa3c01ce2001-12-04 23:05:10 +0000535 /* Somebody somewhere lost info. Close enough, or way off? Note
536 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
537 The difference either is or isn't significant compared to the
538 true value (of which doubleprod is a good approximation).
539 */
540 {
541 const double diff = doubled_longprod - doubleprod;
542 const double absdiff = diff >= 0.0 ? diff : -diff;
543 const double absprod = doubleprod >= 0.0 ? doubleprod :
544 -doubleprod;
545 /* absdiff/absprod <= 1/32 iff
546 32 * absdiff <= absprod -- 5 good bits is "close enough" */
547 if (32.0 * absdiff <= absprod)
548 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000549 else
550 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000551 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000552}
553
Armin Rigo7ccbca92006-10-04 12:17:45 +0000554/* Integer overflow checking for unary negation: on a 2's-complement
555 * box, -x overflows iff x is the most negative long. In this case we
556 * get -x == x. However, -x is undefined (by C) if x /is/ the most
557 * negative long (it's a signed overflow case), and some compilers care.
558 * So we cast x to unsigned long first. However, then other compilers
559 * warn about applying unary minus to an unsigned operand. Hence the
560 * weird "0-".
561 */
562#define UNARY_NEG_WOULD_OVERFLOW(x) \
563 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
564
Guido van Rossume27f7952001-08-23 02:59:04 +0000565/* Return type of i_divmod */
566enum divmod_result {
567 DIVMOD_OK, /* Correct result */
568 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
569 DIVMOD_ERROR /* Exception raised */
570};
571
572static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000573i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000574 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000576 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000577
Tim Peters1dad6a82001-06-18 19:21:11 +0000578 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000580 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000581 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000582 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000583 /* (-sys.maxint-1)/-1 is the only overflow case. */
Armin Rigo7ccbca92006-10-04 12:17:45 +0000584 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
Guido van Rossume27f7952001-08-23 02:59:04 +0000585 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000586 xdivy = x / y;
Mark Dickinson3f8707c2009-12-04 11:25:29 +0000587 /* xdiv*y can overflow on platforms where x/y gives floor(x/y)
588 * for x and y with differing signs. (This is unusual
589 * behaviour, and C99 prohibits it, but it's allowed by C89;
590 * for an example of overflow, take x = LONG_MIN, y = 5 or x =
591 * LONG_MAX, y = -5.) However, x - xdivy*y is always
592 * representable as a long, since it lies strictly between
593 * -abs(y) and abs(y). We add casts to avoid intermediate
594 * overflow.
595 */
596 xmody = (long)(x - (unsigned long)xdivy * y);
Tim Peters1dad6a82001-06-18 19:21:11 +0000597 /* If the signs of x and y differ, and the remainder is non-0,
598 * C89 doesn't define whether xdivy is now the floor or the
599 * ceiling of the infinitely precise quotient. We want the floor,
600 * and we have it iff the remainder's sign matches y's.
601 */
602 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
603 xmody += y;
604 --xdivy;
605 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000606 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000607 *p_xdivy = xdivy;
608 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000609 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000610}
611
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000612static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000613int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000614{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000615 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000616 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000617 CONVERT_TO_LONG(x, xi);
618 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000619 switch (i_divmod(xi, yi, &d, &m)) {
620 case DIVMOD_OK:
621 return PyInt_FromLong(d);
622 case DIVMOD_OVERFLOW:
623 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
624 (PyObject *)y);
625 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000626 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000627 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000628}
629
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000630static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000631int_classic_div(PyIntObject *x, PyIntObject *y)
632{
633 long xi, yi;
634 long d, m;
635 CONVERT_TO_LONG(x, xi);
636 CONVERT_TO_LONG(y, yi);
637 if (Py_DivisionWarningFlag &&
638 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
639 return NULL;
640 switch (i_divmod(xi, yi, &d, &m)) {
641 case DIVMOD_OK:
642 return PyInt_FromLong(d);
643 case DIVMOD_OVERFLOW:
644 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
645 (PyObject *)y);
646 default:
647 return NULL;
648 }
649}
650
651static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000652int_true_divide(PyObject *v, PyObject *w)
653{
Tim Peterse2a60002001-09-04 06:17:36 +0000654 /* If they aren't both ints, give someone else a chance. In
655 particular, this lets int/long get handled by longs, which
656 underflows to 0 gracefully if the long is too big to convert
657 to float. */
658 if (PyInt_Check(v) && PyInt_Check(w))
659 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
660 Py_INCREF(Py_NotImplemented);
661 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000662}
663
664static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000665int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000666{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000667 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000668 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000669 CONVERT_TO_LONG(x, xi);
670 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000671 switch (i_divmod(xi, yi, &d, &m)) {
672 case DIVMOD_OK:
673 return PyInt_FromLong(m);
674 case DIVMOD_OVERFLOW:
675 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
676 (PyObject *)y);
677 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000678 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000679 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000680}
681
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000683int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000684{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000685 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000686 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000687 CONVERT_TO_LONG(x, xi);
688 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000689 switch (i_divmod(xi, yi, &d, &m)) {
690 case DIVMOD_OK:
691 return Py_BuildValue("(ll)", d, m);
692 case DIVMOD_OVERFLOW:
693 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
694 (PyObject *)y);
695 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000696 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000697 }
Guido van Rossum00466951991-05-05 20:08:27 +0000698}
699
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000701int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000702{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000703 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000704 CONVERT_TO_LONG(v, iv);
705 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000706 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000707 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000708 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
709 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000710 return NULL;
711 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000712 /* Return a float. This works because we know that
713 this calls float_pow() which converts its
714 arguments to double. */
715 return PyFloat_Type.tp_as_number->nb_power(
716 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000717 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000719 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000720 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000721 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000722 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000723 return NULL;
724 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000725 }
726 /*
727 * XXX: The original exponentiation code stopped looping
728 * when temp hit zero; this code will continue onwards
729 * unnecessarily, but at least it won't cause any errors.
730 * Hopefully the speed improvement from the fast exponentiation
731 * will compensate for the slight inefficiency.
732 * XXX: Better handling of overflows is desperately needed.
733 */
734 temp = iv;
735 ix = 1;
736 while (iw > 0) {
737 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000738 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000739 ix = ix*temp;
740 if (temp == 0)
741 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000742 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000743 return PyLong_Type.tp_as_number->nb_power(
744 (PyObject *)v,
745 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000746 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000747 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000748 }
749 iw >>= 1; /* Shift exponent down by 1 bit */
750 if (iw==0) break;
751 prev = temp;
752 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000753 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000754 return PyLong_Type.tp_as_number->nb_power(
755 (PyObject *)v, (PyObject *)w, (PyObject *)z);
756 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000757 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000758 /* If we did a multiplication, perform a modulo */
759 ix = ix % iz;
760 temp = temp % iz;
761 }
762 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000763 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000764 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000765 switch (i_divmod(ix, iz, &div, &mod)) {
766 case DIVMOD_OK:
767 ix = mod;
768 break;
769 case DIVMOD_OVERFLOW:
770 return PyLong_Type.tp_as_number->nb_power(
771 (PyObject *)v, (PyObject *)w, (PyObject *)z);
772 default:
773 return NULL;
774 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000775 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000776 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000777}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000779static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000780int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000781{
Martin v. Löwis820d6ac2006-10-04 05:47:34 +0000782 register long a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000783 a = v->ob_ival;
Armin Rigo7ccbca92006-10-04 12:17:45 +0000784 /* check for overflow */
785 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
Tim Petersc8854432004-08-25 02:14:08 +0000786 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000787 if (o != NULL) {
788 PyObject *result = PyNumber_Negative(o);
789 Py_DECREF(o);
790 return result;
791 }
792 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000793 }
Martin v. Löwis820d6ac2006-10-04 05:47:34 +0000794 return PyInt_FromLong(-a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795}
796
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000798int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000799{
800 if (v->ob_ival >= 0)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000801 return int_int(v);
Guido van Rossum00466951991-05-05 20:08:27 +0000802 else
803 return int_neg(v);
804}
805
Guido van Rossum0bff0151991-05-14 12:05:32 +0000806static int
Fred Drakea2f55112000-07-09 15:16:51 +0000807int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000808{
809 return v->ob_ival != 0;
810}
811
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000812static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000813int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000814{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000816}
817
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000818static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000819int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000820{
Guido van Rossum078151d2002-08-11 04:24:12 +0000821 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000822 PyObject *vv, *ww, *result;
823
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000824 CONVERT_TO_LONG(v, a);
825 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000826 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000827 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000828 return NULL;
829 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000830 if (a == 0 || b == 0)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000831 return int_int(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000832 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000833 vv = PyLong_FromLong(PyInt_AS_LONG(v));
834 if (vv == NULL)
835 return NULL;
836 ww = PyLong_FromLong(PyInt_AS_LONG(w));
837 if (ww == NULL) {
838 Py_DECREF(vv);
839 return NULL;
840 }
841 result = PyNumber_Lshift(vv, ww);
842 Py_DECREF(vv);
843 Py_DECREF(ww);
844 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000845 }
Tim Petersda1a2212002-08-11 17:54:42 +0000846 c = a << b;
847 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000848 vv = PyLong_FromLong(PyInt_AS_LONG(v));
849 if (vv == NULL)
850 return NULL;
851 ww = PyLong_FromLong(PyInt_AS_LONG(w));
852 if (ww == NULL) {
853 Py_DECREF(vv);
854 return NULL;
855 }
856 result = PyNumber_Lshift(vv, ww);
857 Py_DECREF(vv);
858 Py_DECREF(ww);
859 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000860 }
861 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000862}
863
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000864static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000865int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000866{
867 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000868 CONVERT_TO_LONG(v, a);
869 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000870 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000872 return NULL;
873 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000874 if (a == 0 || b == 0)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000875 return int_int(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000876 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000877 if (a < 0)
878 a = -1;
879 else
880 a = 0;
881 }
882 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000883 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000884 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000885 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000886}
887
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000889int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000890{
891 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000892 CONVERT_TO_LONG(v, a);
893 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000894 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000895}
896
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000898int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000899{
900 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000901 CONVERT_TO_LONG(v, a);
902 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000903 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000904}
905
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000906static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000907int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000908{
909 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000910 CONVERT_TO_LONG(v, a);
911 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000912 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000913}
914
Guido van Rossum1952e382001-09-19 01:25:16 +0000915static int
916int_coerce(PyObject **pv, PyObject **pw)
917{
918 if (PyInt_Check(*pw)) {
919 Py_INCREF(*pv);
920 Py_INCREF(*pw);
921 return 0;
922 }
923 return 1; /* Can't do it */
924}
925
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000926static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000927int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000928{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000929 if (PyInt_CheckExact(v))
930 Py_INCREF(v);
931 else
932 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000933 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000934}
935
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000936static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000937int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000938{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000939 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000940}
941
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000942static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000943int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000944{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000945 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000946}
947
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000948static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000949int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000950{
Eric Smith5e527eb2008-02-10 01:36:53 +0000951 return _PyInt_Format(v, 8, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000952}
953
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000954static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000955int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000956{
Eric Smith5e527eb2008-02-10 01:36:53 +0000957 return _PyInt_Format(v, 16, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000958}
959
Jeremy Hylton938ace62002-07-17 16:30:39 +0000960static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +0000961int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
962
Tim Peters6d6c1a32001-08-02 04:15:00 +0000963static PyObject *
964int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
965{
966 PyObject *x = NULL;
967 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000968 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000969
Guido van Rossumbef14172001-08-29 15:47:46 +0000970 if (type != &PyInt_Type)
971 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000972 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
973 &x, &base))
974 return NULL;
975 if (x == NULL)
976 return PyInt_FromLong(0L);
977 if (base == -909)
978 return PyNumber_Int(x);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000979 if (PyString_Check(x)) {
Georg Brandl2c1375c2006-10-12 11:27:59 +0000980 /* Since PyInt_FromString doesn't have a length parameter,
981 * check here for possible NULs in the string. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000982 char *string = PyString_AS_STRING(x);
983 if (strlen(string) != PyString_Size(x)) {
Georg Brandl2c1375c2006-10-12 11:27:59 +0000984 /* create a repr() of the input string,
985 * just like PyInt_FromString does */
986 PyObject *srepr;
987 srepr = PyObject_Repr(x);
988 if (srepr == NULL)
989 return NULL;
990 PyErr_Format(PyExc_ValueError,
991 "invalid literal for int() with base %d: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000992 base, PyString_AS_STRING(srepr));
Georg Brandl2c1375c2006-10-12 11:27:59 +0000993 Py_DECREF(srepr);
994 return NULL;
995 }
996 return PyInt_FromString(string, NULL, base);
997 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000998#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +0000999 if (PyUnicode_Check(x))
1000 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
1001 PyUnicode_GET_SIZE(x),
1002 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001003#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00001004 PyErr_SetString(PyExc_TypeError,
1005 "int() can't convert non-string with explicit base");
1006 return NULL;
1007}
1008
Guido van Rossumbef14172001-08-29 15:47:46 +00001009/* Wimpy, slow approach to tp_new calls for subtypes of int:
1010 first create a regular int from whatever arguments we got,
1011 then allocate a subtype instance and initialize its ob_ival
1012 from the regular int. The regular int is then thrown away.
1013*/
1014static PyObject *
1015int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1016{
Anthony Baxter377be112006-04-11 06:54:30 +00001017 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001018 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001019
1020 assert(PyType_IsSubtype(type, &PyInt_Type));
1021 tmp = int_new(&PyInt_Type, args, kwds);
1022 if (tmp == NULL)
1023 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001024 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001025 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001026 if (ival == -1 && PyErr_Occurred()) {
1027 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001028 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001029 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001030 } else {
1031 ival = ((PyIntObject *)tmp)->ob_ival;
1032 }
1033
Anthony Baxter377be112006-04-11 06:54:30 +00001034 newobj = type->tp_alloc(type, 0);
1035 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001036 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001037 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001038 }
Anthony Baxter377be112006-04-11 06:54:30 +00001039 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001040 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00001041 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001042}
1043
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001044static PyObject *
1045int_getnewargs(PyIntObject *v)
1046{
1047 return Py_BuildValue("(l)", v->ob_ival);
1048}
1049
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001050static PyObject *
1051int_getN(PyIntObject *v, void *context) {
Jeffrey Yasskin5de250e2008-03-18 01:09:59 +00001052 return PyInt_FromLong((Py_intptr_t)context);
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001053}
1054
Eric Smith5e527eb2008-02-10 01:36:53 +00001055/* Convert an integer to the given base. Returns a string.
1056 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'.
1057 If newstyle is zero, then use the pre-2.6 behavior of octal having
1058 a leading "0" */
1059PyAPI_FUNC(PyObject*)
1060_PyInt_Format(PyIntObject *v, int base, int newstyle)
1061{
1062 /* There are no doubt many, many ways to optimize this, using code
1063 similar to _PyLong_Format */
1064 long n = v->ob_ival;
1065 int negative = n < 0;
1066 int is_zero = n == 0;
1067
1068 /* For the reasoning behind this size, see
1069 http://c-faq.com/misc/hexio.html. Then, add a few bytes for
1070 the possible sign and prefix "0[box]" */
1071 char buf[sizeof(n)*CHAR_BIT+6];
1072
1073 /* Start by pointing to the end of the buffer. We fill in from
1074 the back forward. */
1075 char* p = &buf[sizeof(buf)];
1076
1077 assert(base >= 2 && base <= 36);
1078
1079 do {
1080 /* I'd use i_divmod, except it doesn't produce the results
1081 I want when n is negative. So just duplicate the salient
1082 part here. */
1083 long div = n / base;
1084 long mod = n - div * base;
1085
1086 /* convert abs(mod) to the right character in [0-9, a-z] */
1087 char cdigit = (char)(mod < 0 ? -mod : mod);
1088 cdigit += (cdigit < 10) ? '0' : 'a'-10;
1089 *--p = cdigit;
1090
1091 n = div;
1092 } while(n);
1093
1094 if (base == 2) {
1095 *--p = 'b';
1096 *--p = '0';
1097 }
1098 else if (base == 8) {
1099 if (newstyle) {
1100 *--p = 'o';
1101 *--p = '0';
1102 }
1103 else
1104 if (!is_zero)
1105 *--p = '0';
1106 }
1107 else if (base == 16) {
1108 *--p = 'x';
1109 *--p = '0';
1110 }
1111 else if (base != 10) {
1112 *--p = '#';
1113 *--p = '0' + base%10;
1114 if (base > 10)
1115 *--p = '0' + base/10;
1116 }
1117 if (negative)
1118 *--p = '-';
1119
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001120 return PyString_FromStringAndSize(p, &buf[sizeof(buf)] - p);
Eric Smith5e527eb2008-02-10 01:36:53 +00001121}
1122
Eric Smitha9f7d622008-02-17 19:46:49 +00001123static PyObject *
1124int__format__(PyObject *self, PyObject *args)
1125{
1126 PyObject *format_spec;
1127
1128 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
1129 return NULL;
Christian Heimes593daf52008-05-26 12:51:38 +00001130 if (PyBytes_Check(format_spec))
Eric Smithdc13b792008-05-30 18:10:04 +00001131 return _PyInt_FormatAdvanced(self,
1132 PyBytes_AS_STRING(format_spec),
1133 PyBytes_GET_SIZE(format_spec));
Eric Smitha9f7d622008-02-17 19:46:49 +00001134 if (PyUnicode_Check(format_spec)) {
1135 /* Convert format_spec to a str */
Eric Smithdc13b792008-05-30 18:10:04 +00001136 PyObject *result;
1137 PyObject *str_spec = PyObject_Str(format_spec);
Eric Smitha9f7d622008-02-17 19:46:49 +00001138
Eric Smithdc13b792008-05-30 18:10:04 +00001139 if (str_spec == NULL)
1140 return NULL;
Eric Smitha9f7d622008-02-17 19:46:49 +00001141
Eric Smithdc13b792008-05-30 18:10:04 +00001142 result = _PyInt_FormatAdvanced(self,
1143 PyBytes_AS_STRING(str_spec),
1144 PyBytes_GET_SIZE(str_spec));
Eric Smitha9f7d622008-02-17 19:46:49 +00001145
Eric Smithdc13b792008-05-30 18:10:04 +00001146 Py_DECREF(str_spec);
Eric Smitha9f7d622008-02-17 19:46:49 +00001147 return result;
1148 }
1149 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
1150 return NULL;
1151}
1152
Christian Heimes6f341092008-04-18 23:13:07 +00001153#if 0
1154static PyObject *
1155int_is_finite(PyObject *v)
1156{
1157 Py_RETURN_TRUE;
1158}
1159#endif
1160
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001161static PyMethodDef int_methods[] = {
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001162 {"conjugate", (PyCFunction)int_int, METH_NOARGS,
1163 "Returns self, the complex conjugate of any int."},
Christian Heimes6f341092008-04-18 23:13:07 +00001164#if 0
1165 {"is_finite", (PyCFunction)int_is_finite, METH_NOARGS,
1166 "Returns always True."},
1167#endif
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001168 {"__trunc__", (PyCFunction)int_int, METH_NOARGS,
1169 "Truncating an Integral returns itself."},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001170 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
Eric Smitha9f7d622008-02-17 19:46:49 +00001171 {"__format__", (PyCFunction)int__format__, METH_VARARGS},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001172 {NULL, NULL} /* sentinel */
1173};
1174
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001175static PyGetSetDef int_getset[] = {
1176 {"real",
1177 (getter)int_int, (setter)NULL,
1178 "the real part of a complex number",
1179 NULL},
1180 {"imag",
1181 (getter)int_getN, (setter)NULL,
1182 "the imaginary part of a complex number",
1183 (void*)0},
1184 {"numerator",
1185 (getter)int_int, (setter)NULL,
1186 "the numerator of a rational number in lowest terms",
1187 NULL},
1188 {"denominator",
1189 (getter)int_getN, (setter)NULL,
1190 "the denominator of a rational number in lowest terms",
1191 (void*)1},
1192 {NULL} /* Sentinel */
1193};
1194
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001195PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001196"int(x[, base]) -> integer\n\
1197\n\
1198Convert a string or number to an integer, if possible. A floating point\n\
1199argument will be truncated towards zero (this does not include a string\n\
1200representation of a floating point number!) When converting a string, use\n\
1201the optional base. It is an error to supply a base when converting a\n\
Gustavo Niemeyer37e65022007-01-10 16:15:48 +00001202non-string. If base is zero, the proper base is guessed based on the\n\
Gustavo Niemeyera443bc82007-01-10 16:13:40 +00001203string content. If the argument is outside the integer range a\n\
1204long object will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001205
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001206static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001207 (binaryfunc)int_add, /*nb_add*/
1208 (binaryfunc)int_sub, /*nb_subtract*/
1209 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00001210 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001211 (binaryfunc)int_mod, /*nb_remainder*/
1212 (binaryfunc)int_divmod, /*nb_divmod*/
1213 (ternaryfunc)int_pow, /*nb_power*/
1214 (unaryfunc)int_neg, /*nb_negative*/
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001215 (unaryfunc)int_int, /*nb_positive*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001216 (unaryfunc)int_abs, /*nb_absolute*/
1217 (inquiry)int_nonzero, /*nb_nonzero*/
1218 (unaryfunc)int_invert, /*nb_invert*/
1219 (binaryfunc)int_lshift, /*nb_lshift*/
1220 (binaryfunc)int_rshift, /*nb_rshift*/
1221 (binaryfunc)int_and, /*nb_and*/
1222 (binaryfunc)int_xor, /*nb_xor*/
1223 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +00001224 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001225 (unaryfunc)int_int, /*nb_int*/
1226 (unaryfunc)int_long, /*nb_long*/
1227 (unaryfunc)int_float, /*nb_float*/
1228 (unaryfunc)int_oct, /*nb_oct*/
1229 (unaryfunc)int_hex, /*nb_hex*/
1230 0, /*nb_inplace_add*/
1231 0, /*nb_inplace_subtract*/
1232 0, /*nb_inplace_multiply*/
1233 0, /*nb_inplace_divide*/
1234 0, /*nb_inplace_remainder*/
1235 0, /*nb_inplace_power*/
1236 0, /*nb_inplace_lshift*/
1237 0, /*nb_inplace_rshift*/
1238 0, /*nb_inplace_and*/
1239 0, /*nb_inplace_xor*/
1240 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001241 (binaryfunc)int_div, /* nb_floor_divide */
1242 int_true_divide, /* nb_true_divide */
1243 0, /* nb_inplace_floor_divide */
1244 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001245 (unaryfunc)int_int, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001246};
1247
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001248PyTypeObject PyInt_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001249 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001250 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001251 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001252 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001253 (destructor)int_dealloc, /* tp_dealloc */
1254 (printfunc)int_print, /* tp_print */
1255 0, /* tp_getattr */
1256 0, /* tp_setattr */
1257 (cmpfunc)int_compare, /* tp_compare */
1258 (reprfunc)int_repr, /* tp_repr */
1259 &int_as_number, /* tp_as_number */
1260 0, /* tp_as_sequence */
1261 0, /* tp_as_mapping */
1262 (hashfunc)int_hash, /* tp_hash */
1263 0, /* tp_call */
Guido van Rossume75bfde2002-02-01 15:34:10 +00001264 (reprfunc)int_repr, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265 PyObject_GenericGetAttr, /* tp_getattro */
1266 0, /* tp_setattro */
1267 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001268 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00001269 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001270 int_doc, /* tp_doc */
1271 0, /* tp_traverse */
1272 0, /* tp_clear */
1273 0, /* tp_richcompare */
1274 0, /* tp_weaklistoffset */
1275 0, /* tp_iter */
1276 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001277 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001278 0, /* tp_members */
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001279 int_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001280 0, /* tp_base */
1281 0, /* tp_dict */
1282 0, /* tp_descr_get */
1283 0, /* tp_descr_set */
1284 0, /* tp_dictoffset */
1285 0, /* tp_init */
1286 0, /* tp_alloc */
1287 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001288 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001289};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001290
Neal Norwitzc91ed402002-12-30 22:29:22 +00001291int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001292_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001293{
1294 PyIntObject *v;
1295 int ival;
1296#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1297 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001298 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001299 return 0;
1300 /* PyObject_New is inlined */
1301 v = free_list;
Christian Heimese93237d2007-12-19 02:37:44 +00001302 free_list = (PyIntObject *)Py_TYPE(v);
Neal Norwitzc91ed402002-12-30 22:29:22 +00001303 PyObject_INIT(v, &PyInt_Type);
1304 v->ob_ival = ival;
1305 small_ints[ival + NSMALLNEGINTS] = v;
1306 }
1307#endif
1308 return 1;
1309}
1310
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001311int
1312PyInt_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001313{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001314 PyIntObject *p;
1315 PyIntBlock *list, *next;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001316 int i;
1317 int u; /* remaining unfreed ints per block */
1318 int freelist_size = 0;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001319
Guido van Rossumda084ed1999-03-10 22:55:24 +00001320 list = block_list;
1321 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001322 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001323 while (list != NULL) {
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001324 u = 0;
1325 for (i = 0, p = &list->objects[0];
1326 i < N_INTOBJECTS;
1327 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001328 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001329 u++;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001330 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001331 next = list->next;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001332 if (u) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001333 list->next = block_list;
1334 block_list = list;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001335 for (i = 0, p = &list->objects[0];
1336 i < N_INTOBJECTS;
1337 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001338 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001339 p->ob_refcnt == 0) {
Christian Heimese93237d2007-12-19 02:37:44 +00001340 Py_TYPE(p) = (struct _typeobject *)
Guido van Rossum51288bc1999-03-19 20:30:39 +00001341 free_list;
1342 free_list = p;
1343 }
1344#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1345 else if (-NSMALLNEGINTS <= p->ob_ival &&
1346 p->ob_ival < NSMALLPOSINTS &&
1347 small_ints[p->ob_ival +
1348 NSMALLNEGINTS] == NULL) {
1349 Py_INCREF(p);
1350 small_ints[p->ob_ival +
1351 NSMALLNEGINTS] = p;
1352 }
1353#endif
1354 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001355 }
1356 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001357 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001358 }
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001359 freelist_size += u;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001360 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001361 }
Christian Heimes422051a2008-02-04 18:00:12 +00001362
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001363 return freelist_size;
Christian Heimes422051a2008-02-04 18:00:12 +00001364}
1365
1366void
1367PyInt_Fini(void)
1368{
1369 PyIntObject *p;
1370 PyIntBlock *list;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001371 int i;
1372 int u; /* total unfreed ints per block */
Christian Heimes422051a2008-02-04 18:00:12 +00001373
1374#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Christian Heimes422051a2008-02-04 18:00:12 +00001375 PyIntObject **q;
1376
1377 i = NSMALLNEGINTS + NSMALLPOSINTS;
1378 q = small_ints;
1379 while (--i >= 0) {
1380 Py_XDECREF(*q);
1381 *q++ = NULL;
1382 }
1383#endif
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001384 u = PyInt_ClearFreeList();
Guido van Rossum3fce8831999-03-12 19:43:17 +00001385 if (!Py_VerboseFlag)
1386 return;
1387 fprintf(stderr, "# cleanup ints");
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001388 if (!u) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001389 fprintf(stderr, "\n");
1390 }
1391 else {
1392 fprintf(stderr,
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001393 ": %d unfreed int%s\n",
1394 u, u == 1 ? "" : "s");
Guido van Rossum3fce8831999-03-12 19:43:17 +00001395 }
1396 if (Py_VerboseFlag > 1) {
1397 list = block_list;
1398 while (list != NULL) {
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001399 for (i = 0, p = &list->objects[0];
1400 i < N_INTOBJECTS;
1401 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001402 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001403 /* XXX(twouters) cast refcount to
1404 long until %zd is universally
1405 available
1406 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001407 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001408 "# <int at %p, refcnt=%ld, val=%ld>\n",
1409 p, (long)p->ob_refcnt,
1410 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001411 }
1412 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001413 }
1414 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001415}