blob: b03ef703cbcc60bb132e8ed6bc3e38f154aa2f5e [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>
Mark Dickinson6736cf82009-04-20 21:13:33 +00006#include <float.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00008static PyObject *int_int(PyIntObject *v);
9
Guido van Rossum2e1d4331993-12-24 10:22:45 +000010long
Fred Drakea2f55112000-07-09 15:16:51 +000011PyInt_GetMax(void)
Guido van Rossum2e1d4331993-12-24 10:22:45 +000012{
13 return LONG_MAX; /* To initialize sys.maxint */
14}
15
Guido van Rossum3f5da241990-12-20 15:06:42 +000016/* Integers are quite normal objects, to make object handling uniform.
17 (Using odd pointers to represent integers would save much space
18 but require extra checks for this special case throughout the code.)
Tim Peters29c0afc2002-04-28 16:57:34 +000019 Since a typical Python program spends much of its time allocating
Guido van Rossum3f5da241990-12-20 15:06:42 +000020 and deallocating integers, these operations should be very fast.
21 Therefore we use a dedicated allocation scheme with a much lower
22 overhead (in space and time) than straight malloc(): a simple
23 dedicated free list, filled when necessary with memory from malloc().
Tim Peters29c0afc2002-04-28 16:57:34 +000024
25 block_list is a singly-linked list of all PyIntBlocks ever allocated,
26 linked via their next members. PyIntBlocks are never returned to the
27 system before shutdown (PyInt_Fini).
28
29 free_list is a singly-linked list of available PyIntObjects, linked
30 via abuse of their ob_type members.
Guido van Rossum3f5da241990-12-20 15:06:42 +000031*/
32
33#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
Guido van Rossum3fce8831999-03-12 19:43:17 +000034#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
35#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
Guido van Rossumda084ed1999-03-10 22:55:24 +000036
Guido van Rossum3fce8831999-03-12 19:43:17 +000037struct _intblock {
38 struct _intblock *next;
39 PyIntObject objects[N_INTOBJECTS];
40};
41
42typedef struct _intblock PyIntBlock;
43
44static PyIntBlock *block_list = NULL;
45static PyIntObject *free_list = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000046
Guido van Rossumc0b618a1997-05-02 03:12:38 +000047static PyIntObject *
Fred Drakea2f55112000-07-09 15:16:51 +000048fill_free_list(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +000049{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000050 PyIntObject *p, *q;
Tim Peters29c0afc2002-04-28 16:57:34 +000051 /* Python's object allocator isn't appropriate for large blocks. */
Guido van Rossumb18618d2000-05-03 23:44:39 +000052 p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
Guido van Rossum3f5da241990-12-20 15:06:42 +000053 if (p == NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +000054 return (PyIntObject *) PyErr_NoMemory();
Guido van Rossum3fce8831999-03-12 19:43:17 +000055 ((PyIntBlock *)p)->next = block_list;
56 block_list = (PyIntBlock *)p;
Tim Peters29c0afc2002-04-28 16:57:34 +000057 /* Link the int objects together, from rear to front, then return
58 the address of the last int object in the block. */
Guido van Rossum3fce8831999-03-12 19:43:17 +000059 p = &((PyIntBlock *)p)->objects[0];
Guido van Rossum3f5da241990-12-20 15:06:42 +000060 q = p + N_INTOBJECTS;
61 while (--q > p)
Christian Heimese93237d2007-12-19 02:37:44 +000062 Py_TYPE(q) = (struct _typeobject *)(q-1);
63 Py_TYPE(q) = NULL;
Guido van Rossum3f5da241990-12-20 15:06:42 +000064 return p + N_INTOBJECTS - 1;
65}
66
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000067#ifndef NSMALLPOSINTS
Georg Brandl418a1ef2006-02-22 11:30:06 +000068#define NSMALLPOSINTS 257
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000069#endif
70#ifndef NSMALLNEGINTS
Neal Norwitzc91ed402002-12-30 22:29:22 +000071#define NSMALLNEGINTS 5
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000072#endif
73#if NSMALLNEGINTS + NSMALLPOSINTS > 0
74/* References to small integers are saved in this array so that they
75 can be shared.
76 The integers that are saved are those in the range
77 -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
78*/
Guido van Rossumc0b618a1997-05-02 03:12:38 +000079static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000080#endif
81#ifdef COUNT_ALLOCS
Martin v. Löwisb90304a2009-01-07 18:40:40 +000082Py_ssize_t quick_int_allocs;
83Py_ssize_t quick_neg_int_allocs;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000084#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000085
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +000087PyInt_FromLong(long ival)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089 register PyIntObject *v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000090#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Neal Norwitzc91ed402002-12-30 22:29:22 +000091 if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
92 v = small_ints[ival + NSMALLNEGINTS];
Guido van Rossumc0b618a1997-05-02 03:12:38 +000093 Py_INCREF(v);
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +000094#ifdef COUNT_ALLOCS
95 if (ival >= 0)
96 quick_int_allocs++;
97 else
98 quick_neg_int_allocs++;
99#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 return (PyObject *) v;
Sjoerd Mullender842d2cc1993-10-15 16:18:48 +0000101 }
102#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +0000103 if (free_list == NULL) {
104 if ((free_list = fill_free_list()) == NULL)
105 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000106 }
Guido van Rossume3a8e7e2002-08-19 19:26:42 +0000107 /* Inline PyObject_New */
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108 v = free_list;
Christian Heimese93237d2007-12-19 02:37:44 +0000109 free_list = (PyIntObject *)Py_TYPE(v);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000110 PyObject_INIT(v, &PyInt_Type);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111 v->ob_ival = ival;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 return (PyObject *) v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113}
114
Martin v. Löwis18e16552006-02-15 17:27:45 +0000115PyObject *
116PyInt_FromSize_t(size_t ival)
117{
118 if (ival <= LONG_MAX)
119 return PyInt_FromLong((long)ival);
120 return _PyLong_FromSize_t(ival);
121}
122
123PyObject *
124PyInt_FromSsize_t(Py_ssize_t ival)
125{
126 if (ival >= LONG_MIN && ival <= LONG_MAX)
127 return PyInt_FromLong((long)ival);
128 return _PyLong_FromSsize_t(ival);
129}
130
Guido van Rossum3f5da241990-12-20 15:06:42 +0000131static void
Fred Drakea2f55112000-07-09 15:16:51 +0000132int_dealloc(PyIntObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000133{
Guido van Rossumdea6ef92001-09-11 16:13:52 +0000134 if (PyInt_CheckExact(v)) {
Christian Heimese93237d2007-12-19 02:37:44 +0000135 Py_TYPE(v) = (struct _typeobject *)free_list;
Guido van Rossumbef14172001-08-29 15:47:46 +0000136 free_list = v;
137 }
138 else
Christian Heimese93237d2007-12-19 02:37:44 +0000139 Py_TYPE(v)->tp_free((PyObject *)v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140}
141
Guido van Rossum93646982002-04-26 00:53:34 +0000142static void
143int_free(PyIntObject *v)
144{
Christian Heimese93237d2007-12-19 02:37:44 +0000145 Py_TYPE(v) = (struct _typeobject *)free_list;
Guido van Rossum93646982002-04-26 00:53:34 +0000146 free_list = v;
147}
148
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149long
Fred Drakea2f55112000-07-09 15:16:51 +0000150PyInt_AsLong(register PyObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000152 PyNumberMethods *nb;
153 PyIntObject *io;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000154 long val;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000155
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000156 if (op && PyInt_Check(op))
157 return PyInt_AS_LONG((PyIntObject*) op);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000158
Christian Heimese93237d2007-12-19 02:37:44 +0000159 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000160 nb->nb_int == NULL) {
Guido van Rossumc18a6f42000-05-09 14:27:48 +0000161 PyErr_SetString(PyExc_TypeError, "an integer is required");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162 return -1;
163 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000164
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000165 io = (PyIntObject*) (*nb->nb_int) (op);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000166 if (io == NULL)
167 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000168 if (!PyInt_Check(io)) {
Walter Dörwaldf1715402002-11-19 20:49:15 +0000169 if (PyLong_Check(io)) {
170 /* got a long? => retry int conversion */
171 val = PyLong_AsLong((PyObject *)io);
Thomas Heller850566b2003-02-20 20:32:11 +0000172 Py_DECREF(io);
173 if ((val == -1) && PyErr_Occurred())
Walter Dörwaldf1715402002-11-19 20:49:15 +0000174 return -1;
Thomas Heller850566b2003-02-20 20:32:11 +0000175 return val;
Walter Dörwaldf1715402002-11-19 20:49:15 +0000176 }
177 else
178 {
Thomas Hellera4ea6032003-04-17 18:55:45 +0000179 Py_DECREF(io);
Walter Dörwaldf1715402002-11-19 20:49:15 +0000180 PyErr_SetString(PyExc_TypeError,
181 "nb_int should return int object");
182 return -1;
183 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000184 }
Tim Petersa3c01ce2001-12-04 23:05:10 +0000185
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000186 val = PyInt_AS_LONG(io);
187 Py_DECREF(io);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000188
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000189 return val;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000190}
191
Martin v. Löwis18e16552006-02-15 17:27:45 +0000192Py_ssize_t
193PyInt_AsSsize_t(register PyObject *op)
194{
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000195#if SIZEOF_SIZE_T != SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000196 PyNumberMethods *nb;
197 PyIntObject *io;
198 Py_ssize_t val;
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000199#endif
Neal Norwitz8a87f5d2006-08-12 17:03:09 +0000200
201 if (op == NULL) {
202 PyErr_SetString(PyExc_TypeError, "an integer is required");
203 return -1;
204 }
205
206 if (PyInt_Check(op))
207 return PyInt_AS_LONG((PyIntObject*) op);
208 if (PyLong_Check(op))
Martin v. Löwis18e16552006-02-15 17:27:45 +0000209 return _PyLong_AsSsize_t(op);
Thomas Woutersb1410fb2006-02-15 23:08:56 +0000210#if SIZEOF_SIZE_T == SIZEOF_LONG
Martin v. Löwis18e16552006-02-15 17:27:45 +0000211 return PyInt_AsLong(op);
212#else
213
Christian Heimese93237d2007-12-19 02:37:44 +0000214 if ((nb = Py_TYPE(op)->tp_as_number) == NULL ||
Martin v. Löwis18e16552006-02-15 17:27:45 +0000215 (nb->nb_int == NULL && nb->nb_long == 0)) {
216 PyErr_SetString(PyExc_TypeError, "an integer is required");
217 return -1;
218 }
219
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +0000220 if (nb->nb_long != 0)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000221 io = (PyIntObject*) (*nb->nb_long) (op);
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +0000222 else
Martin v. Löwis18e16552006-02-15 17:27:45 +0000223 io = (PyIntObject*) (*nb->nb_int) (op);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000224 if (io == NULL)
225 return -1;
226 if (!PyInt_Check(io)) {
227 if (PyLong_Check(io)) {
228 /* got a long? => retry int conversion */
229 val = _PyLong_AsSsize_t((PyObject *)io);
230 Py_DECREF(io);
231 if ((val == -1) && PyErr_Occurred())
232 return -1;
233 return val;
234 }
235 else
236 {
237 Py_DECREF(io);
238 PyErr_SetString(PyExc_TypeError,
239 "nb_int should return int object");
240 return -1;
241 }
242 }
243
244 val = PyInt_AS_LONG(io);
245 Py_DECREF(io);
246
247 return val;
248#endif
249}
250
Thomas Hellera4ea6032003-04-17 18:55:45 +0000251unsigned long
252PyInt_AsUnsignedLongMask(register PyObject *op)
253{
254 PyNumberMethods *nb;
255 PyIntObject *io;
256 unsigned long val;
257
258 if (op && PyInt_Check(op))
259 return PyInt_AS_LONG((PyIntObject*) op);
260 if (op && PyLong_Check(op))
261 return PyLong_AsUnsignedLongMask(op);
262
Christian Heimese93237d2007-12-19 02:37:44 +0000263 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
Thomas Hellera4ea6032003-04-17 18:55:45 +0000264 nb->nb_int == NULL) {
265 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000266 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000267 }
268
269 io = (PyIntObject*) (*nb->nb_int) (op);
270 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000271 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000272 if (!PyInt_Check(io)) {
273 if (PyLong_Check(io)) {
274 val = PyLong_AsUnsignedLongMask((PyObject *)io);
275 Py_DECREF(io);
276 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000277 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000278 return val;
279 }
280 else
281 {
282 Py_DECREF(io);
283 PyErr_SetString(PyExc_TypeError,
284 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000285 return (unsigned long)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000286 }
287 }
288
289 val = PyInt_AS_LONG(io);
290 Py_DECREF(io);
291
292 return val;
293}
294
295#ifdef HAVE_LONG_LONG
296unsigned PY_LONG_LONG
297PyInt_AsUnsignedLongLongMask(register PyObject *op)
298{
299 PyNumberMethods *nb;
300 PyIntObject *io;
301 unsigned PY_LONG_LONG val;
302
303 if (op && PyInt_Check(op))
304 return PyInt_AS_LONG((PyIntObject*) op);
305 if (op && PyLong_Check(op))
306 return PyLong_AsUnsignedLongLongMask(op);
307
Christian Heimese93237d2007-12-19 02:37:44 +0000308 if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL ||
Thomas Hellera4ea6032003-04-17 18:55:45 +0000309 nb->nb_int == NULL) {
310 PyErr_SetString(PyExc_TypeError, "an integer is required");
Skip Montanaro429433b2006-04-18 00:35:43 +0000311 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000312 }
313
314 io = (PyIntObject*) (*nb->nb_int) (op);
315 if (io == NULL)
Skip Montanaro429433b2006-04-18 00:35:43 +0000316 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000317 if (!PyInt_Check(io)) {
318 if (PyLong_Check(io)) {
319 val = PyLong_AsUnsignedLongLongMask((PyObject *)io);
320 Py_DECREF(io);
321 if (PyErr_Occurred())
Skip Montanaro429433b2006-04-18 00:35:43 +0000322 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000323 return val;
324 }
325 else
326 {
327 Py_DECREF(io);
328 PyErr_SetString(PyExc_TypeError,
329 "nb_int should return int object");
Skip Montanaro429433b2006-04-18 00:35:43 +0000330 return (unsigned PY_LONG_LONG)-1;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000331 }
332 }
333
334 val = PyInt_AS_LONG(io);
335 Py_DECREF(io);
336
337 return val;
338}
339#endif
340
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000341PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000342PyInt_FromString(char *s, char **pend, int base)
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000343{
344 char *end;
345 long x;
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000346 Py_ssize_t slen;
347 PyObject *sobj, *srepr;
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000348
349 if ((base != 0 && base < 2) || base > 36) {
Guido van Rossum47710652003-02-12 20:48:22 +0000350 PyErr_SetString(PyExc_ValueError,
351 "int() base must be >= 2 and <= 36");
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000352 return NULL;
353 }
354
355 while (*s && isspace(Py_CHARMASK(*s)))
356 s++;
357 errno = 0;
Guido van Rossum47710652003-02-12 20:48:22 +0000358 if (base == 0 && s[0] == '0') {
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000359 x = (long) PyOS_strtoul(s, &end, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000360 if (x < 0)
Guido van Rossum6c9e1302003-11-29 23:52:13 +0000361 return PyLong_FromString(s, pend, base);
Guido van Rossum47710652003-02-12 20:48:22 +0000362 }
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000363 else
364 x = PyOS_strtol(s, &end, base);
Martin v. Löwis2b6727b2001-03-06 12:12:02 +0000365 if (end == s || !isalnum(Py_CHARMASK(end[-1])))
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000366 goto bad;
367 while (*end && isspace(Py_CHARMASK(*end)))
368 end++;
369 if (*end != '\0') {
370 bad:
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000371 slen = strlen(s) < 200 ? strlen(s) : 200;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000372 sobj = PyString_FromStringAndSize(s, slen);
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000373 if (sobj == NULL)
374 return NULL;
375 srepr = PyObject_Repr(sobj);
376 Py_DECREF(sobj);
377 if (srepr == NULL)
378 return NULL;
379 PyErr_Format(PyExc_ValueError,
380 "invalid literal for int() with base %d: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000381 base, PyString_AS_STRING(srepr));
Thomas Wouters9cb28bea2006-04-11 23:50:33 +0000382 Py_DECREF(srepr);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000383 return NULL;
384 }
Tim Petersc8854432004-08-25 02:14:08 +0000385 else if (errno != 0)
Walter Dörwald07e14762002-11-06 16:15:14 +0000386 return PyLong_FromString(s, pend, base);
Barry Warsaw226ae6c1999-10-12 19:54:53 +0000387 if (pend)
388 *pend = end;
389 return PyInt_FromLong(x);
390}
391
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000392#ifdef Py_USING_UNICODE
Guido van Rossum9e896b32000-04-05 20:11:21 +0000393PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000394PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base)
Guido van Rossum9e896b32000-04-05 20:11:21 +0000395{
Walter Dörwald07e14762002-11-06 16:15:14 +0000396 PyObject *result;
Anthony Baxter377be112006-04-11 06:54:30 +0000397 char *buffer = (char *)PyMem_MALLOC(length+1);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000398
Walter Dörwald07e14762002-11-06 16:15:14 +0000399 if (buffer == NULL)
Neal Norwitzd501d1f2007-05-16 04:33:50 +0000400 return PyErr_NoMemory();
Walter Dörwald07e14762002-11-06 16:15:14 +0000401
402 if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) {
403 PyMem_FREE(buffer);
Guido van Rossum9e896b32000-04-05 20:11:21 +0000404 return NULL;
405 }
Walter Dörwald07e14762002-11-06 16:15:14 +0000406 result = PyInt_FromString(buffer, NULL, base);
407 PyMem_FREE(buffer);
408 return result;
Guido van Rossum9e896b32000-04-05 20:11:21 +0000409}
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000410#endif
Guido van Rossum9e896b32000-04-05 20:11:21 +0000411
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412/* Methods */
413
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000414/* Integers are seen as the "smallest" of all numeric types and thus
415 don't have any knowledge about conversion of other types to
416 integers. */
417
418#define CONVERT_TO_LONG(obj, lng) \
419 if (PyInt_Check(obj)) { \
420 lng = PyInt_AS_LONG(obj); \
421 } \
422 else { \
423 Py_INCREF(Py_NotImplemented); \
424 return Py_NotImplemented; \
425 }
426
Guido van Rossum719f5fa1992-03-27 17:31:02 +0000427/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000428static int
Fred Drakea2f55112000-07-09 15:16:51 +0000429int_print(PyIntObject *v, FILE *fp, int flags)
430 /* flags -- not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000431{
Brett Cannon01531592007-09-17 03:28:34 +0000432 long int_val = v->ob_ival;
433 Py_BEGIN_ALLOW_THREADS
434 fprintf(fp, "%ld", int_val);
435 Py_END_ALLOW_THREADS
Guido van Rossum90933611991-06-07 16:10:43 +0000436 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000437}
438
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439static int
Fred Drakea2f55112000-07-09 15:16:51 +0000440int_compare(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441{
442 register long i = v->ob_ival;
443 register long j = w->ob_ival;
444 return (i < j) ? -1 : (i > j) ? 1 : 0;
445}
446
Guido van Rossum9bfef441993-03-29 10:43:31 +0000447static long
Fred Drakea2f55112000-07-09 15:16:51 +0000448int_hash(PyIntObject *v)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000449{
Guido van Rossum541cdd81997-01-06 22:53:20 +0000450 /* XXX If this is changed, you also need to change the way
451 Python's long, float and complex types are hashed. */
Guido van Rossum9bfef441993-03-29 10:43:31 +0000452 long x = v -> ob_ival;
453 if (x == -1)
454 x = -2;
455 return x;
456}
457
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000459int_add(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460{
461 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000462 CONVERT_TO_LONG(v, a);
463 CONVERT_TO_LONG(w, b);
Mark Dickinson34398182009-12-02 17:33:41 +0000464 /* casts in the line below avoid undefined behaviour on overflow */
465 x = (long)((unsigned long)a + b);
Guido van Rossume27f7952001-08-23 02:59:04 +0000466 if ((x^a) >= 0 || (x^b) >= 0)
467 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000468 return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469}
470
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000472int_sub(PyIntObject *v, PyIntObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000473{
474 register long a, b, x;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000475 CONVERT_TO_LONG(v, a);
476 CONVERT_TO_LONG(w, b);
Mark Dickinson34398182009-12-02 17:33:41 +0000477 /* casts in the line below avoid undefined behaviour on overflow */
478 x = (long)((unsigned long)a - b);
Guido van Rossume27f7952001-08-23 02:59:04 +0000479 if ((x^a) >= 0 || (x^~b) >= 0)
480 return PyInt_FromLong(x);
Guido van Rossume27f7952001-08-23 02:59:04 +0000481 return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v,
482 (PyObject *)w);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483}
484
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000485/*
Tim Petersa3c01ce2001-12-04 23:05:10 +0000486Integer overflow checking for * is painful: Python tried a couple ways, but
487they didn't work on all platforms, or failed in endcases (a product of
488-sys.maxint-1 has been a particular pain).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000489
Tim Petersa3c01ce2001-12-04 23:05:10 +0000490Here's another way:
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000491
Tim Petersa3c01ce2001-12-04 23:05:10 +0000492The native long product x*y is either exactly right or *way* off, being
493just the last n bits of the true product, where n is the number of bits
494in a long (the delivered product is the true product plus i*2**n for
495some integer i).
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000496
Tim Petersa3c01ce2001-12-04 23:05:10 +0000497The native double product (double)x * (double)y is subject to three
498rounding errors: on a sizeof(long)==8 box, each cast to double can lose
499info, and even on a sizeof(long)==4 box, the multiplication can lose info.
500But, unlike the native long product, it's not in *range* trouble: even
501if sizeof(long)==32 (256-bit longs), the product easily fits in the
502dynamic range of a double. So the leading 50 (or so) bits of the double
503product are correct.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000504
Tim Petersa3c01ce2001-12-04 23:05:10 +0000505We check these two ways against each other, and declare victory if they're
506approximately the same. Else, because the native long product is the only
507one that can lose catastrophic amounts of information, it's the native long
508product that must have overflowed.
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000509*/
510
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000511static PyObject *
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000512int_mul(PyObject *v, PyObject *w)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513{
Tim Petersa3c01ce2001-12-04 23:05:10 +0000514 long a, b;
515 long longprod; /* a*b in native long arithmetic */
516 double doubled_longprod; /* (double)longprod */
517 double doubleprod; /* (double)a * (double)b */
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000518
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000519 CONVERT_TO_LONG(v, a);
520 CONVERT_TO_LONG(w, b);
Mark Dickinson34398182009-12-02 17:33:41 +0000521 /* casts in the next line avoid undefined behaviour on overflow */
522 longprod = (long)((unsigned long)a * b);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000523 doubleprod = (double)a * (double)b;
524 doubled_longprod = (double)longprod;
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000525
Tim Petersa3c01ce2001-12-04 23:05:10 +0000526 /* Fast path for normal case: small multiplicands, and no info
527 is lost in either method. */
528 if (doubled_longprod == doubleprod)
529 return PyInt_FromLong(longprod);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000530
Tim Petersa3c01ce2001-12-04 23:05:10 +0000531 /* Somebody somewhere lost info. Close enough, or way off? Note
532 that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
533 The difference either is or isn't significant compared to the
534 true value (of which doubleprod is a good approximation).
535 */
536 {
537 const double diff = doubled_longprod - doubleprod;
538 const double absdiff = diff >= 0.0 ? diff : -diff;
539 const double absprod = doubleprod >= 0.0 ? doubleprod :
540 -doubleprod;
541 /* absdiff/absprod <= 1/32 iff
542 32 * absdiff <= absprod -- 5 good bits is "close enough" */
543 if (32.0 * absdiff <= absprod)
544 return PyInt_FromLong(longprod);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000545 else
546 return PyLong_Type.tp_as_number->nb_multiply(v, w);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000547 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000548}
549
Armin Rigo7ccbca92006-10-04 12:17:45 +0000550/* Integer overflow checking for unary negation: on a 2's-complement
551 * box, -x overflows iff x is the most negative long. In this case we
552 * get -x == x. However, -x is undefined (by C) if x /is/ the most
553 * negative long (it's a signed overflow case), and some compilers care.
554 * So we cast x to unsigned long first. However, then other compilers
555 * warn about applying unary minus to an unsigned operand. Hence the
556 * weird "0-".
557 */
558#define UNARY_NEG_WOULD_OVERFLOW(x) \
559 ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x))
560
Guido van Rossume27f7952001-08-23 02:59:04 +0000561/* Return type of i_divmod */
562enum divmod_result {
563 DIVMOD_OK, /* Correct result */
564 DIVMOD_OVERFLOW, /* Overflow, try again using longs */
565 DIVMOD_ERROR /* Exception raised */
566};
567
568static enum divmod_result
Tim Peters1dad6a82001-06-18 19:21:11 +0000569i_divmod(register long x, register long y,
Fred Drakea2f55112000-07-09 15:16:51 +0000570 long *p_xdivy, long *p_xmody)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000571{
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000572 long xdivy, xmody;
Tim Petersa3c01ce2001-12-04 23:05:10 +0000573
Tim Peters1dad6a82001-06-18 19:21:11 +0000574 if (y == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575 PyErr_SetString(PyExc_ZeroDivisionError,
Fred Drake661ea262000-10-24 19:57:45 +0000576 "integer division or modulo by zero");
Guido van Rossume27f7952001-08-23 02:59:04 +0000577 return DIVMOD_ERROR;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578 }
Tim Peters1dad6a82001-06-18 19:21:11 +0000579 /* (-sys.maxint-1)/-1 is the only overflow case. */
Armin Rigo7ccbca92006-10-04 12:17:45 +0000580 if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x))
Guido van Rossume27f7952001-08-23 02:59:04 +0000581 return DIVMOD_OVERFLOW;
Tim Peters1dad6a82001-06-18 19:21:11 +0000582 xdivy = x / y;
Mark Dickinson16910252009-12-04 11:24:38 +0000583 /* xdiv*y can overflow on platforms where x/y gives floor(x/y)
584 * for x and y with differing signs. (This is unusual
585 * behaviour, and C99 prohibits it, but it's allowed by C89;
586 * for an example of overflow, take x = LONG_MIN, y = 5 or x =
587 * LONG_MAX, y = -5.) However, x - xdivy*y is always
588 * representable as a long, since it lies strictly between
589 * -abs(y) and abs(y). We add casts to avoid intermediate
590 * overflow.
591 */
592 xmody = (long)(x - (unsigned long)xdivy * y);
Tim Peters1dad6a82001-06-18 19:21:11 +0000593 /* If the signs of x and y differ, and the remainder is non-0,
594 * C89 doesn't define whether xdivy is now the floor or the
595 * ceiling of the infinitely precise quotient. We want the floor,
596 * and we have it iff the remainder's sign matches y's.
597 */
598 if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
599 xmody += y;
600 --xdivy;
601 assert(xmody && ((y ^ xmody) >= 0));
Guido van Rossum00466951991-05-05 20:08:27 +0000602 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000603 *p_xdivy = xdivy;
604 *p_xmody = xmody;
Guido van Rossume27f7952001-08-23 02:59:04 +0000605 return DIVMOD_OK;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000606}
607
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000608static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000609int_div(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000610{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000611 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000612 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000613 CONVERT_TO_LONG(x, xi);
614 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000615 switch (i_divmod(xi, yi, &d, &m)) {
616 case DIVMOD_OK:
617 return PyInt_FromLong(d);
618 case DIVMOD_OVERFLOW:
619 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
620 (PyObject *)y);
621 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000622 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000623 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000624}
625
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626static PyObject *
Guido van Rossum393661d2001-08-31 17:40:15 +0000627int_classic_div(PyIntObject *x, PyIntObject *y)
628{
629 long xi, yi;
630 long d, m;
631 CONVERT_TO_LONG(x, xi);
632 CONVERT_TO_LONG(y, yi);
633 if (Py_DivisionWarningFlag &&
634 PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0)
635 return NULL;
636 switch (i_divmod(xi, yi, &d, &m)) {
637 case DIVMOD_OK:
638 return PyInt_FromLong(d);
639 case DIVMOD_OVERFLOW:
640 return PyLong_Type.tp_as_number->nb_divide((PyObject *)x,
641 (PyObject *)y);
642 default:
643 return NULL;
644 }
645}
646
647static PyObject *
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000648int_true_divide(PyObject *v, PyObject *w)
649{
Tim Peterse2a60002001-09-04 06:17:36 +0000650 /* If they aren't both ints, give someone else a chance. In
651 particular, this lets int/long get handled by longs, which
652 underflows to 0 gracefully if the long is too big to convert
653 to float. */
654 if (PyInt_Check(v) && PyInt_Check(w))
655 return PyFloat_Type.tp_as_number->nb_true_divide(v, w);
656 Py_INCREF(Py_NotImplemented);
657 return Py_NotImplemented;
Tim Peters9c1d7fd2001-09-04 05:52:47 +0000658}
659
660static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000661int_mod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000662{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000663 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000664 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000665 CONVERT_TO_LONG(x, xi);
666 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000667 switch (i_divmod(xi, yi, &d, &m)) {
668 case DIVMOD_OK:
669 return PyInt_FromLong(m);
670 case DIVMOD_OVERFLOW:
671 return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x,
672 (PyObject *)y);
673 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000674 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000675 }
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000676}
677
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000679int_divmod(PyIntObject *x, PyIntObject *y)
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000680{
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000681 long xi, yi;
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000682 long d, m;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000683 CONVERT_TO_LONG(x, xi);
684 CONVERT_TO_LONG(y, yi);
Guido van Rossume27f7952001-08-23 02:59:04 +0000685 switch (i_divmod(xi, yi, &d, &m)) {
686 case DIVMOD_OK:
687 return Py_BuildValue("(ll)", d, m);
688 case DIVMOD_OVERFLOW:
689 return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x,
690 (PyObject *)y);
691 default:
Guido van Rossum2b16a6f1992-01-19 16:28:51 +0000692 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000693 }
Guido van Rossum00466951991-05-05 20:08:27 +0000694}
695
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000697int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000698{
Guido van Rossum9478dd41996-12-06 20:14:43 +0000699 register long iv, iw, iz=0, ix, temp, prev;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000700 CONVERT_TO_LONG(v, iv);
701 CONVERT_TO_LONG(w, iw);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000702 if (iw < 0) {
Tim Peters32f453e2001-09-03 08:35:41 +0000703 if ((PyObject *)z != Py_None) {
Tim Peters4c483c42001-09-05 06:24:58 +0000704 PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "
705 "cannot be negative when 3rd argument specified");
Tim Peters32f453e2001-09-03 08:35:41 +0000706 return NULL;
707 }
Guido van Rossumb82fedc2001-07-12 11:19:45 +0000708 /* Return a float. This works because we know that
709 this calls float_pow() which converts its
710 arguments to double. */
711 return PyFloat_Type.tp_as_number->nb_power(
712 (PyObject *)v, (PyObject *)w, (PyObject *)z);
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000713 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714 if ((PyObject *)z != Py_None) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000715 CONVERT_TO_LONG(z, iz);
Guido van Rossum9478dd41996-12-06 20:14:43 +0000716 if (iz == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000717 PyErr_SetString(PyExc_ValueError,
Tim Peters4c483c42001-09-05 06:24:58 +0000718 "pow() 3rd argument cannot be 0");
Guido van Rossum9478dd41996-12-06 20:14:43 +0000719 return NULL;
720 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000721 }
722 /*
723 * XXX: The original exponentiation code stopped looping
724 * when temp hit zero; this code will continue onwards
725 * unnecessarily, but at least it won't cause any errors.
726 * Hopefully the speed improvement from the fast exponentiation
727 * will compensate for the slight inefficiency.
728 * XXX: Better handling of overflows is desperately needed.
729 */
730 temp = iv;
731 ix = 1;
732 while (iw > 0) {
733 prev = ix; /* Save value for overflow check */
Tim Petersa3c01ce2001-12-04 23:05:10 +0000734 if (iw & 1) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000735 ix = ix*temp;
736 if (temp == 0)
737 break; /* Avoid ix / 0 */
Guido van Rossume27f7952001-08-23 02:59:04 +0000738 if (ix / temp != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000739 return PyLong_Type.tp_as_number->nb_power(
740 (PyObject *)v,
741 (PyObject *)w,
Tim Peters31960db2001-08-23 21:28:33 +0000742 (PyObject *)z);
Guido van Rossume27f7952001-08-23 02:59:04 +0000743 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000744 }
745 iw >>= 1; /* Shift exponent down by 1 bit */
746 if (iw==0) break;
747 prev = temp;
748 temp *= temp; /* Square the value of temp */
Tim Petersc8854432004-08-25 02:14:08 +0000749 if (prev != 0 && temp / prev != prev) {
Guido van Rossume27f7952001-08-23 02:59:04 +0000750 return PyLong_Type.tp_as_number->nb_power(
751 (PyObject *)v, (PyObject *)w, (PyObject *)z);
752 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000753 if (iz) {
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000754 /* If we did a multiplication, perform a modulo */
755 ix = ix % iz;
756 temp = temp % iz;
757 }
758 }
Guido van Rossum9478dd41996-12-06 20:14:43 +0000759 if (iz) {
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000760 long div, mod;
Guido van Rossume27f7952001-08-23 02:59:04 +0000761 switch (i_divmod(ix, iz, &div, &mod)) {
762 case DIVMOD_OK:
763 ix = mod;
764 break;
765 case DIVMOD_OVERFLOW:
766 return PyLong_Type.tp_as_number->nb_power(
767 (PyObject *)v, (PyObject *)w, (PyObject *)z);
768 default:
769 return NULL;
770 }
Guido van Rossumbf8c0e31994-08-29 12:48:32 +0000771 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000772 return PyInt_FromLong(ix);
Tim Petersa3c01ce2001-12-04 23:05:10 +0000773}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000774
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000775static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000776int_neg(PyIntObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000777{
Martin v. Löwis820d6ac2006-10-04 05:47:34 +0000778 register long a;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000779 a = v->ob_ival;
Armin Rigo7ccbca92006-10-04 12:17:45 +0000780 /* check for overflow */
781 if (UNARY_NEG_WOULD_OVERFLOW(a)) {
Tim Petersc8854432004-08-25 02:14:08 +0000782 PyObject *o = PyLong_FromLong(a);
Neal Norwitzfa56e2d2003-01-19 15:40:09 +0000783 if (o != NULL) {
784 PyObject *result = PyNumber_Negative(o);
785 Py_DECREF(o);
786 return result;
787 }
788 return NULL;
Guido van Rossume27f7952001-08-23 02:59:04 +0000789 }
Martin v. Löwis820d6ac2006-10-04 05:47:34 +0000790 return PyInt_FromLong(-a);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000791}
792
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000793static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000794int_abs(PyIntObject *v)
Guido van Rossum00466951991-05-05 20:08:27 +0000795{
796 if (v->ob_ival >= 0)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000797 return int_int(v);
Guido van Rossum00466951991-05-05 20:08:27 +0000798 else
799 return int_neg(v);
800}
801
Guido van Rossum0bff0151991-05-14 12:05:32 +0000802static int
Fred Drakea2f55112000-07-09 15:16:51 +0000803int_nonzero(PyIntObject *v)
Guido van Rossum0bff0151991-05-14 12:05:32 +0000804{
805 return v->ob_ival != 0;
806}
807
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000808static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000809int_invert(PyIntObject *v)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000810{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000811 return PyInt_FromLong(~v->ob_ival);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000812}
813
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000814static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000815int_lshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000816{
Guido van Rossum078151d2002-08-11 04:24:12 +0000817 long a, b, c;
Raymond Hettingera006c372004-06-26 23:22:57 +0000818 PyObject *vv, *ww, *result;
819
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000820 CONVERT_TO_LONG(v, a);
821 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000822 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000824 return NULL;
825 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000826 if (a == 0 || b == 0)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000827 return int_int(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000828 if (b >= LONG_BIT) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000829 vv = PyLong_FromLong(PyInt_AS_LONG(v));
830 if (vv == NULL)
831 return NULL;
832 ww = PyLong_FromLong(PyInt_AS_LONG(w));
833 if (ww == NULL) {
834 Py_DECREF(vv);
835 return NULL;
836 }
837 result = PyNumber_Lshift(vv, ww);
838 Py_DECREF(vv);
839 Py_DECREF(ww);
840 return result;
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000841 }
Tim Petersda1a2212002-08-11 17:54:42 +0000842 c = a << b;
843 if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) {
Raymond Hettingera006c372004-06-26 23:22:57 +0000844 vv = PyLong_FromLong(PyInt_AS_LONG(v));
845 if (vv == NULL)
846 return NULL;
847 ww = PyLong_FromLong(PyInt_AS_LONG(w));
848 if (ww == NULL) {
849 Py_DECREF(vv);
850 return NULL;
851 }
852 result = PyNumber_Lshift(vv, ww);
853 Py_DECREF(vv);
854 Py_DECREF(ww);
855 return result;
Guido van Rossum078151d2002-08-11 04:24:12 +0000856 }
857 return PyInt_FromLong(c);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000858}
859
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000860static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000861int_rshift(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000862{
863 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000864 CONVERT_TO_LONG(v, a);
865 CONVERT_TO_LONG(w, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000866 if (b < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000867 PyErr_SetString(PyExc_ValueError, "negative shift count");
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000868 return NULL;
869 }
Tim Peters73a1dfe2001-09-11 21:44:14 +0000870 if (a == 0 || b == 0)
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +0000871 return int_int(v);
Guido van Rossum72481a31993-10-26 15:21:51 +0000872 if (b >= LONG_BIT) {
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000873 if (a < 0)
874 a = -1;
875 else
876 a = 0;
877 }
878 else {
Tim Peters7d3a5112000-07-08 04:17:21 +0000879 a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
Guido van Rossumf3b351f1992-01-14 18:33:22 +0000880 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000881 return PyInt_FromLong(a);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000882}
883
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000884static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000885int_and(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000886{
887 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000888 CONVERT_TO_LONG(v, a);
889 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000890 return PyInt_FromLong(a & b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000891}
892
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000893static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000894int_xor(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000895{
896 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000897 CONVERT_TO_LONG(v, a);
898 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899 return PyInt_FromLong(a ^ b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000900}
901
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000902static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000903int_or(PyIntObject *v, PyIntObject *w)
Guido van Rossum7928cd71991-10-24 14:59:31 +0000904{
905 register long a, b;
Neil Schemenauer139e72a2001-01-04 01:45:33 +0000906 CONVERT_TO_LONG(v, a);
907 CONVERT_TO_LONG(w, b);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000908 return PyInt_FromLong(a | b);
Guido van Rossum7928cd71991-10-24 14:59:31 +0000909}
910
Guido van Rossum1952e382001-09-19 01:25:16 +0000911static int
912int_coerce(PyObject **pv, PyObject **pw)
913{
914 if (PyInt_Check(*pw)) {
915 Py_INCREF(*pv);
916 Py_INCREF(*pw);
917 return 0;
918 }
919 return 1; /* Can't do it */
920}
921
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000922static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000923int_int(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000924{
Brett Cannonc3647ac2005-04-26 03:45:26 +0000925 if (PyInt_CheckExact(v))
926 Py_INCREF(v);
927 else
928 v = (PyIntObject *)PyInt_FromLong(v->ob_ival);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000929 return (PyObject *)v;
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000930}
931
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000932static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000933int_long(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000934{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935 return PyLong_FromLong((v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000936}
937
Mark Dickinson6736cf82009-04-20 21:13:33 +0000938static const unsigned char BitLengthTable[32] = {
939 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
940 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
941};
942
943static int
944bits_in_ulong(unsigned long d)
945{
946 int d_bits = 0;
947 while (d >= 32) {
948 d_bits += 6;
949 d >>= 6;
950 }
951 d_bits += (int)BitLengthTable[d];
952 return d_bits;
953}
954
955#if 8*SIZEOF_LONG-1 <= DBL_MANT_DIG
956/* Every Python int can be exactly represented as a float. */
957
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000958static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +0000959int_float(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000960{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000961 return PyFloat_FromDouble((double)(v -> ob_ival));
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000962}
963
Mark Dickinson6736cf82009-04-20 21:13:33 +0000964#else
965/* Here not all Python ints are exactly representable as floats, so we may
966 have to round. We do this manually, since the C standards don't specify
967 whether converting an integer to a float rounds up or down */
968
969static PyObject *
970int_float(PyIntObject *v)
971{
972 unsigned long abs_ival, lsb;
973 int round_up;
974
975 if (v->ob_ival < 0)
976 abs_ival = 0U-(unsigned long)v->ob_ival;
977 else
978 abs_ival = (unsigned long)v->ob_ival;
979 if (abs_ival < (1L << DBL_MANT_DIG))
980 /* small integer; no need to round */
981 return PyFloat_FromDouble((double)v->ob_ival);
982
983 /* Round abs_ival to MANT_DIG significant bits, using the
984 round-half-to-even rule. abs_ival & lsb picks out the 'rounding'
985 bit: the first bit after the most significant MANT_DIG bits of
986 abs_ival. We round up if this bit is set, provided that either:
987
988 (1) abs_ival isn't exactly halfway between two floats, in which
989 case at least one of the bits following the rounding bit must be
990 set; i.e., abs_ival & lsb-1 != 0, or:
991
992 (2) the resulting rounded value has least significant bit 0; or
993 in other words the bit above the rounding bit is set (this is the
994 'to-even' bit of round-half-to-even); i.e., abs_ival & 2*lsb != 0
995
996 The condition "(1) or (2)" equates to abs_ival & 3*lsb-1 != 0. */
997
998 lsb = 1L << (bits_in_ulong(abs_ival)-DBL_MANT_DIG-1);
999 round_up = (abs_ival & lsb) && (abs_ival & (3*lsb-1));
1000 abs_ival &= -2*lsb;
1001 if (round_up)
1002 abs_ival += 2*lsb;
1003 return PyFloat_FromDouble(v->ob_ival < 0 ?
1004 -(double)abs_ival :
1005 (double)abs_ival);
1006}
1007
1008#endif
1009
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001010static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001011int_oct(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001012{
Eric Smith5e527eb2008-02-10 01:36:53 +00001013 return _PyInt_Format(v, 8, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001014}
1015
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001016static PyObject *
Fred Drakea2f55112000-07-09 15:16:51 +00001017int_hex(PyIntObject *v)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001018{
Eric Smith5e527eb2008-02-10 01:36:53 +00001019 return _PyInt_Format(v, 16, 0);
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001020}
1021
Jeremy Hylton938ace62002-07-17 16:30:39 +00001022static PyObject *
Guido van Rossumbef14172001-08-29 15:47:46 +00001023int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
1024
Tim Peters6d6c1a32001-08-02 04:15:00 +00001025static PyObject *
1026int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1027{
1028 PyObject *x = NULL;
1029 int base = -909;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001030 static char *kwlist[] = {"x", "base", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001031
Guido van Rossumbef14172001-08-29 15:47:46 +00001032 if (type != &PyInt_Type)
1033 return int_subtype_new(type, args, kwds); /* Wimp out */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001034 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
1035 &x, &base))
1036 return NULL;
1037 if (x == NULL)
1038 return PyInt_FromLong(0L);
1039 if (base == -909)
1040 return PyNumber_Int(x);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001041 if (PyString_Check(x)) {
Georg Brandl2c1375c2006-10-12 11:27:59 +00001042 /* Since PyInt_FromString doesn't have a length parameter,
1043 * check here for possible NULs in the string. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001044 char *string = PyString_AS_STRING(x);
1045 if (strlen(string) != PyString_Size(x)) {
Georg Brandl2c1375c2006-10-12 11:27:59 +00001046 /* create a repr() of the input string,
1047 * just like PyInt_FromString does */
1048 PyObject *srepr;
1049 srepr = PyObject_Repr(x);
1050 if (srepr == NULL)
1051 return NULL;
1052 PyErr_Format(PyExc_ValueError,
1053 "invalid literal for int() with base %d: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001054 base, PyString_AS_STRING(srepr));
Georg Brandl2c1375c2006-10-12 11:27:59 +00001055 Py_DECREF(srepr);
1056 return NULL;
1057 }
1058 return PyInt_FromString(string, NULL, base);
1059 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001060#ifdef Py_USING_UNICODE
Tim Peters6d6c1a32001-08-02 04:15:00 +00001061 if (PyUnicode_Check(x))
1062 return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x),
1063 PyUnicode_GET_SIZE(x),
1064 base);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001065#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00001066 PyErr_SetString(PyExc_TypeError,
1067 "int() can't convert non-string with explicit base");
1068 return NULL;
1069}
1070
Guido van Rossumbef14172001-08-29 15:47:46 +00001071/* Wimpy, slow approach to tp_new calls for subtypes of int:
1072 first create a regular int from whatever arguments we got,
1073 then allocate a subtype instance and initialize its ob_ival
1074 from the regular int. The regular int is then thrown away.
1075*/
1076static PyObject *
1077int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1078{
Anthony Baxter377be112006-04-11 06:54:30 +00001079 PyObject *tmp, *newobj;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001080 long ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001081
1082 assert(PyType_IsSubtype(type, &PyInt_Type));
1083 tmp = int_new(&PyInt_Type, args, kwds);
1084 if (tmp == NULL)
1085 return NULL;
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001086 if (!PyInt_Check(tmp)) {
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001087 ival = PyLong_AsLong(tmp);
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001088 if (ival == -1 && PyErr_Occurred()) {
1089 Py_DECREF(tmp);
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001090 return NULL;
Michael W. Hudson71665dc2003-08-11 17:32:02 +00001091 }
Neal Norwitzde8b94c2003-02-10 02:12:43 +00001092 } else {
1093 ival = ((PyIntObject *)tmp)->ob_ival;
1094 }
1095
Anthony Baxter377be112006-04-11 06:54:30 +00001096 newobj = type->tp_alloc(type, 0);
1097 if (newobj == NULL) {
Raymond Hettingerf4667932003-06-28 20:04:25 +00001098 Py_DECREF(tmp);
Guido van Rossumbef14172001-08-29 15:47:46 +00001099 return NULL;
Raymond Hettingerf4667932003-06-28 20:04:25 +00001100 }
Anthony Baxter377be112006-04-11 06:54:30 +00001101 ((PyIntObject *)newobj)->ob_ival = ival;
Guido van Rossumbef14172001-08-29 15:47:46 +00001102 Py_DECREF(tmp);
Anthony Baxter377be112006-04-11 06:54:30 +00001103 return newobj;
Guido van Rossumbef14172001-08-29 15:47:46 +00001104}
1105
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001106static PyObject *
1107int_getnewargs(PyIntObject *v)
1108{
1109 return Py_BuildValue("(l)", v->ob_ival);
1110}
1111
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001112static PyObject *
Mark Dickinson85e269b2009-05-03 20:39:06 +00001113int_get0(PyIntObject *v, void *context) {
1114 return PyInt_FromLong(0L);
1115}
1116
1117static PyObject *
1118int_get1(PyIntObject *v, void *context) {
1119 return PyInt_FromLong(1L);
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001120}
1121
Mark Dickinson4b9d4732009-09-27 16:05:21 +00001122/* Convert an integer to a decimal string. On many platforms, this
1123 will be significantly faster than the general arbitrary-base
1124 conversion machinery in _PyInt_Format, thanks to optimization
1125 opportunities offered by division by a compile-time constant. */
1126static PyObject *
1127int_to_decimal_string(PyIntObject *v) {
1128 char buf[sizeof(long)*CHAR_BIT/3+6], *p, *bufend;
1129 long n = v->ob_ival;
1130 unsigned long absn;
1131 p = bufend = buf + sizeof(buf);
Mark Dickinson3d6790e2009-11-16 19:17:16 +00001132 absn = n < 0 ? 0UL - n : n;
Mark Dickinson4b9d4732009-09-27 16:05:21 +00001133 do {
1134 *--p = '0' + absn % 10;
1135 absn /= 10;
1136 } while (absn);
1137 if (n < 0)
1138 *--p = '-';
1139 return PyString_FromStringAndSize(p, bufend - p);
1140}
1141
Eric Smith5e527eb2008-02-10 01:36:53 +00001142/* Convert an integer to the given base. Returns a string.
1143 If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'.
1144 If newstyle is zero, then use the pre-2.6 behavior of octal having
1145 a leading "0" */
1146PyAPI_FUNC(PyObject*)
1147_PyInt_Format(PyIntObject *v, int base, int newstyle)
1148{
1149 /* There are no doubt many, many ways to optimize this, using code
1150 similar to _PyLong_Format */
1151 long n = v->ob_ival;
1152 int negative = n < 0;
1153 int is_zero = n == 0;
1154
1155 /* For the reasoning behind this size, see
1156 http://c-faq.com/misc/hexio.html. Then, add a few bytes for
1157 the possible sign and prefix "0[box]" */
1158 char buf[sizeof(n)*CHAR_BIT+6];
1159
1160 /* Start by pointing to the end of the buffer. We fill in from
1161 the back forward. */
1162 char* p = &buf[sizeof(buf)];
1163
1164 assert(base >= 2 && base <= 36);
1165
Mark Dickinson4b9d4732009-09-27 16:05:21 +00001166 /* Special case base 10, for speed */
1167 if (base == 10)
1168 return int_to_decimal_string(v);
1169
Eric Smith5e527eb2008-02-10 01:36:53 +00001170 do {
1171 /* I'd use i_divmod, except it doesn't produce the results
1172 I want when n is negative. So just duplicate the salient
1173 part here. */
1174 long div = n / base;
1175 long mod = n - div * base;
1176
1177 /* convert abs(mod) to the right character in [0-9, a-z] */
1178 char cdigit = (char)(mod < 0 ? -mod : mod);
1179 cdigit += (cdigit < 10) ? '0' : 'a'-10;
1180 *--p = cdigit;
1181
1182 n = div;
1183 } while(n);
1184
1185 if (base == 2) {
1186 *--p = 'b';
1187 *--p = '0';
1188 }
1189 else if (base == 8) {
1190 if (newstyle) {
1191 *--p = 'o';
1192 *--p = '0';
1193 }
1194 else
1195 if (!is_zero)
1196 *--p = '0';
1197 }
1198 else if (base == 16) {
1199 *--p = 'x';
1200 *--p = '0';
1201 }
Mark Dickinson4b9d4732009-09-27 16:05:21 +00001202 else {
Eric Smith5e527eb2008-02-10 01:36:53 +00001203 *--p = '#';
1204 *--p = '0' + base%10;
1205 if (base > 10)
1206 *--p = '0' + base/10;
1207 }
1208 if (negative)
1209 *--p = '-';
1210
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001211 return PyString_FromStringAndSize(p, &buf[sizeof(buf)] - p);
Eric Smith5e527eb2008-02-10 01:36:53 +00001212}
1213
Eric Smitha9f7d622008-02-17 19:46:49 +00001214static PyObject *
1215int__format__(PyObject *self, PyObject *args)
1216{
1217 PyObject *format_spec;
1218
1219 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
1220 return NULL;
Christian Heimes593daf52008-05-26 12:51:38 +00001221 if (PyBytes_Check(format_spec))
Eric Smithdc13b792008-05-30 18:10:04 +00001222 return _PyInt_FormatAdvanced(self,
1223 PyBytes_AS_STRING(format_spec),
1224 PyBytes_GET_SIZE(format_spec));
Eric Smitha9f7d622008-02-17 19:46:49 +00001225 if (PyUnicode_Check(format_spec)) {
1226 /* Convert format_spec to a str */
Eric Smithdc13b792008-05-30 18:10:04 +00001227 PyObject *result;
1228 PyObject *str_spec = PyObject_Str(format_spec);
Eric Smitha9f7d622008-02-17 19:46:49 +00001229
Eric Smithdc13b792008-05-30 18:10:04 +00001230 if (str_spec == NULL)
1231 return NULL;
Eric Smitha9f7d622008-02-17 19:46:49 +00001232
Eric Smithdc13b792008-05-30 18:10:04 +00001233 result = _PyInt_FormatAdvanced(self,
1234 PyBytes_AS_STRING(str_spec),
1235 PyBytes_GET_SIZE(str_spec));
Eric Smitha9f7d622008-02-17 19:46:49 +00001236
Eric Smithdc13b792008-05-30 18:10:04 +00001237 Py_DECREF(str_spec);
Eric Smitha9f7d622008-02-17 19:46:49 +00001238 return result;
1239 }
1240 PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode");
1241 return NULL;
1242}
1243
Mark Dickinson1a707982008-12-17 16:14:37 +00001244static PyObject *
1245int_bit_length(PyIntObject *v)
1246{
1247 unsigned long n;
Mark Dickinson1a707982008-12-17 16:14:37 +00001248
1249 if (v->ob_ival < 0)
1250 /* avoid undefined behaviour when v->ob_ival == -LONG_MAX-1 */
1251 n = 0U-(unsigned long)v->ob_ival;
1252 else
1253 n = (unsigned long)v->ob_ival;
1254
Mark Dickinson6736cf82009-04-20 21:13:33 +00001255 return PyInt_FromLong(bits_in_ulong(n));
Mark Dickinson1a707982008-12-17 16:14:37 +00001256}
1257
1258PyDoc_STRVAR(int_bit_length_doc,
1259"int.bit_length() -> int\n\
1260\n\
1261Number of bits necessary to represent self in binary.\n\
1262>>> bin(37)\n\
1263'0b100101'\n\
1264>>> (37).bit_length()\n\
12656");
1266
Christian Heimes6f341092008-04-18 23:13:07 +00001267#if 0
1268static PyObject *
1269int_is_finite(PyObject *v)
1270{
1271 Py_RETURN_TRUE;
1272}
1273#endif
1274
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001275static PyMethodDef int_methods[] = {
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001276 {"conjugate", (PyCFunction)int_int, METH_NOARGS,
1277 "Returns self, the complex conjugate of any int."},
Mark Dickinson1a707982008-12-17 16:14:37 +00001278 {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS,
1279 int_bit_length_doc},
Christian Heimes6f341092008-04-18 23:13:07 +00001280#if 0
1281 {"is_finite", (PyCFunction)int_is_finite, METH_NOARGS,
1282 "Returns always True."},
1283#endif
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001284 {"__trunc__", (PyCFunction)int_int, METH_NOARGS,
1285 "Truncating an Integral returns itself."},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001286 {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS},
Eric Smitha9f7d622008-02-17 19:46:49 +00001287 {"__format__", (PyCFunction)int__format__, METH_VARARGS},
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001288 {NULL, NULL} /* sentinel */
1289};
1290
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001291static PyGetSetDef int_getset[] = {
Mark Dickinson85e269b2009-05-03 20:39:06 +00001292 {"real",
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001293 (getter)int_int, (setter)NULL,
1294 "the real part of a complex number",
1295 NULL},
Mark Dickinson85e269b2009-05-03 20:39:06 +00001296 {"imag",
1297 (getter)int_get0, (setter)NULL,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001298 "the imaginary part of a complex number",
Mark Dickinson85e269b2009-05-03 20:39:06 +00001299 NULL},
1300 {"numerator",
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001301 (getter)int_int, (setter)NULL,
1302 "the numerator of a rational number in lowest terms",
1303 NULL},
Mark Dickinson85e269b2009-05-03 20:39:06 +00001304 {"denominator",
1305 (getter)int_get1, (setter)NULL,
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001306 "the denominator of a rational number in lowest terms",
Mark Dickinson85e269b2009-05-03 20:39:06 +00001307 NULL},
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001308 {NULL} /* Sentinel */
1309};
1310
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001311PyDoc_STRVAR(int_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001312"int(x[, base]) -> integer\n\
1313\n\
1314Convert a string or number to an integer, if possible. A floating point\n\
1315argument will be truncated towards zero (this does not include a string\n\
1316representation of a floating point number!) When converting a string, use\n\
1317the optional base. It is an error to supply a base when converting a\n\
Gustavo Niemeyer37e65022007-01-10 16:15:48 +00001318non-string. If base is zero, the proper base is guessed based on the\n\
Gustavo Niemeyera443bc82007-01-10 16:13:40 +00001319string content. If the argument is outside the integer range a\n\
1320long object will be returned instead.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001321
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001322static PyNumberMethods int_as_number = {
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001323 (binaryfunc)int_add, /*nb_add*/
1324 (binaryfunc)int_sub, /*nb_subtract*/
1325 (binaryfunc)int_mul, /*nb_multiply*/
Guido van Rossum393661d2001-08-31 17:40:15 +00001326 (binaryfunc)int_classic_div, /*nb_divide*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001327 (binaryfunc)int_mod, /*nb_remainder*/
1328 (binaryfunc)int_divmod, /*nb_divmod*/
1329 (ternaryfunc)int_pow, /*nb_power*/
1330 (unaryfunc)int_neg, /*nb_negative*/
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001331 (unaryfunc)int_int, /*nb_positive*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001332 (unaryfunc)int_abs, /*nb_absolute*/
1333 (inquiry)int_nonzero, /*nb_nonzero*/
1334 (unaryfunc)int_invert, /*nb_invert*/
1335 (binaryfunc)int_lshift, /*nb_lshift*/
1336 (binaryfunc)int_rshift, /*nb_rshift*/
1337 (binaryfunc)int_and, /*nb_and*/
1338 (binaryfunc)int_xor, /*nb_xor*/
1339 (binaryfunc)int_or, /*nb_or*/
Guido van Rossum1952e382001-09-19 01:25:16 +00001340 int_coerce, /*nb_coerce*/
Neil Schemenauer139e72a2001-01-04 01:45:33 +00001341 (unaryfunc)int_int, /*nb_int*/
1342 (unaryfunc)int_long, /*nb_long*/
1343 (unaryfunc)int_float, /*nb_float*/
1344 (unaryfunc)int_oct, /*nb_oct*/
1345 (unaryfunc)int_hex, /*nb_hex*/
1346 0, /*nb_inplace_add*/
1347 0, /*nb_inplace_subtract*/
1348 0, /*nb_inplace_multiply*/
1349 0, /*nb_inplace_divide*/
1350 0, /*nb_inplace_remainder*/
1351 0, /*nb_inplace_power*/
1352 0, /*nb_inplace_lshift*/
1353 0, /*nb_inplace_rshift*/
1354 0, /*nb_inplace_and*/
1355 0, /*nb_inplace_xor*/
1356 0, /*nb_inplace_or*/
Guido van Rossum4668b002001-08-08 05:00:18 +00001357 (binaryfunc)int_div, /* nb_floor_divide */
1358 int_true_divide, /* nb_true_divide */
1359 0, /* nb_inplace_floor_divide */
1360 0, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001361 (unaryfunc)int_int, /* nb_index */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001362};
1363
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001364PyTypeObject PyInt_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001365 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001366 "int",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001367 sizeof(PyIntObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001368 0,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001369 (destructor)int_dealloc, /* tp_dealloc */
1370 (printfunc)int_print, /* tp_print */
1371 0, /* tp_getattr */
1372 0, /* tp_setattr */
1373 (cmpfunc)int_compare, /* tp_compare */
Mark Dickinson4b9d4732009-09-27 16:05:21 +00001374 (reprfunc)int_to_decimal_string, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001375 &int_as_number, /* tp_as_number */
1376 0, /* tp_as_sequence */
1377 0, /* tp_as_mapping */
1378 (hashfunc)int_hash, /* tp_hash */
Mark Dickinson4b9d4732009-09-27 16:05:21 +00001379 0, /* tp_call */
1380 (reprfunc)int_to_decimal_string, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001381 PyObject_GenericGetAttr, /* tp_getattro */
1382 0, /* tp_setattro */
1383 0, /* tp_as_buffer */
Guido van Rossumbef14172001-08-29 15:47:46 +00001384 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Neal Norwitzee3a1b52007-02-25 19:44:48 +00001385 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001386 int_doc, /* tp_doc */
1387 0, /* tp_traverse */
1388 0, /* tp_clear */
1389 0, /* tp_richcompare */
1390 0, /* tp_weaklistoffset */
1391 0, /* tp_iter */
1392 0, /* tp_iternext */
Guido van Rossum5d9113d2003-01-29 17:58:45 +00001393 int_methods, /* tp_methods */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001394 0, /* tp_members */
Jeffrey Yasskin2f3c16b2008-01-03 02:21:52 +00001395 int_getset, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001396 0, /* tp_base */
1397 0, /* tp_dict */
1398 0, /* tp_descr_get */
1399 0, /* tp_descr_set */
1400 0, /* tp_dictoffset */
1401 0, /* tp_init */
1402 0, /* tp_alloc */
1403 int_new, /* tp_new */
Guido van Rossum93646982002-04-26 00:53:34 +00001404 (freefunc)int_free, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001405};
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001406
Neal Norwitzc91ed402002-12-30 22:29:22 +00001407int
Neal Norwitzb2501f42002-12-31 03:42:13 +00001408_PyInt_Init(void)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001409{
1410 PyIntObject *v;
1411 int ival;
1412#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1413 for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
Raymond Hettingerb32e6402004-02-08 18:54:37 +00001414 if (!free_list && (free_list = fill_free_list()) == NULL)
Neal Norwitzc91ed402002-12-30 22:29:22 +00001415 return 0;
1416 /* PyObject_New is inlined */
1417 v = free_list;
Christian Heimese93237d2007-12-19 02:37:44 +00001418 free_list = (PyIntObject *)Py_TYPE(v);
Neal Norwitzc91ed402002-12-30 22:29:22 +00001419 PyObject_INIT(v, &PyInt_Type);
1420 v->ob_ival = ival;
1421 small_ints[ival + NSMALLNEGINTS] = v;
1422 }
1423#endif
1424 return 1;
1425}
1426
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001427int
1428PyInt_ClearFreeList(void)
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001429{
Guido van Rossum3fce8831999-03-12 19:43:17 +00001430 PyIntObject *p;
1431 PyIntBlock *list, *next;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001432 int i;
1433 int u; /* remaining unfreed ints per block */
1434 int freelist_size = 0;
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001435
Guido van Rossumda084ed1999-03-10 22:55:24 +00001436 list = block_list;
1437 block_list = NULL;
Guido van Rossum51288bc1999-03-19 20:30:39 +00001438 free_list = NULL;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001439 while (list != NULL) {
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001440 u = 0;
1441 for (i = 0, p = &list->objects[0];
1442 i < N_INTOBJECTS;
1443 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001444 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001445 u++;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001446 }
Guido van Rossum3fce8831999-03-12 19:43:17 +00001447 next = list->next;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001448 if (u) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001449 list->next = block_list;
1450 block_list = list;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001451 for (i = 0, p = &list->objects[0];
1452 i < N_INTOBJECTS;
1453 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001454 if (!PyInt_CheckExact(p) ||
Guido van Rossumbef14172001-08-29 15:47:46 +00001455 p->ob_refcnt == 0) {
Christian Heimese93237d2007-12-19 02:37:44 +00001456 Py_TYPE(p) = (struct _typeobject *)
Guido van Rossum51288bc1999-03-19 20:30:39 +00001457 free_list;
1458 free_list = p;
1459 }
1460#if NSMALLNEGINTS + NSMALLPOSINTS > 0
1461 else if (-NSMALLNEGINTS <= p->ob_ival &&
1462 p->ob_ival < NSMALLPOSINTS &&
1463 small_ints[p->ob_ival +
1464 NSMALLNEGINTS] == NULL) {
1465 Py_INCREF(p);
1466 small_ints[p->ob_ival +
1467 NSMALLNEGINTS] = p;
1468 }
1469#endif
1470 }
Guido van Rossumda084ed1999-03-10 22:55:24 +00001471 }
1472 else {
Tim Peters29c0afc2002-04-28 16:57:34 +00001473 PyMem_FREE(list);
Guido van Rossumda084ed1999-03-10 22:55:24 +00001474 }
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001475 freelist_size += u;
Guido van Rossum3fce8831999-03-12 19:43:17 +00001476 list = next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001477 }
Christian Heimes422051a2008-02-04 18:00:12 +00001478
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001479 return freelist_size;
Christian Heimes422051a2008-02-04 18:00:12 +00001480}
1481
1482void
1483PyInt_Fini(void)
1484{
1485 PyIntObject *p;
1486 PyIntBlock *list;
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001487 int i;
1488 int u; /* total unfreed ints per block */
Christian Heimes422051a2008-02-04 18:00:12 +00001489
1490#if NSMALLNEGINTS + NSMALLPOSINTS > 0
Christian Heimes422051a2008-02-04 18:00:12 +00001491 PyIntObject **q;
1492
1493 i = NSMALLNEGINTS + NSMALLPOSINTS;
1494 q = small_ints;
1495 while (--i >= 0) {
1496 Py_XDECREF(*q);
1497 *q++ = NULL;
1498 }
1499#endif
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001500 u = PyInt_ClearFreeList();
Guido van Rossum3fce8831999-03-12 19:43:17 +00001501 if (!Py_VerboseFlag)
1502 return;
1503 fprintf(stderr, "# cleanup ints");
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001504 if (!u) {
Guido van Rossum3fce8831999-03-12 19:43:17 +00001505 fprintf(stderr, "\n");
1506 }
1507 else {
1508 fprintf(stderr,
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001509 ": %d unfreed int%s\n",
1510 u, u == 1 ? "" : "s");
Guido van Rossum3fce8831999-03-12 19:43:17 +00001511 }
1512 if (Py_VerboseFlag > 1) {
1513 list = block_list;
1514 while (list != NULL) {
Gregory P. Smith2fe77062008-07-06 03:35:58 +00001515 for (i = 0, p = &list->objects[0];
1516 i < N_INTOBJECTS;
1517 i++, p++) {
Guido van Rossumdea6ef92001-09-11 16:13:52 +00001518 if (PyInt_CheckExact(p) && p->ob_refcnt != 0)
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001519 /* XXX(twouters) cast refcount to
1520 long until %zd is universally
1521 available
1522 */
Guido van Rossum3fce8831999-03-12 19:43:17 +00001523 fprintf(stderr,
Thomas Wouters8b87a0b2006-03-01 05:41:20 +00001524 "# <int at %p, refcnt=%ld, val=%ld>\n",
1525 p, (long)p->ob_refcnt,
1526 p->ob_ival);
Guido van Rossum3fce8831999-03-12 19:43:17 +00001527 }
1528 list = list->next;
Guido van Rossumda084ed1999-03-10 22:55:24 +00001529 }
1530 }
Guido van Rossumfbbd57e1997-08-05 02:16:08 +00001531}