Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Integer object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 5 | #include <ctype.h> |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 6 | #include <float.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 8 | static PyObject *int_int(PyIntObject *v); |
| 9 | |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 10 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 11 | PyInt_GetMax(void) |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 12 | { |
| 13 | return LONG_MAX; /* To initialize sys.maxint */ |
| 14 | } |
| 15 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | /* 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 Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 19 | Since a typical Python program spends much of its time allocating |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 20 | 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 Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 24 | |
| 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 34 | #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
| 35 | #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject)) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 37 | struct _intblock { |
| 38 | struct _intblock *next; |
| 39 | PyIntObject objects[N_INTOBJECTS]; |
| 40 | }; |
| 41 | |
| 42 | typedef struct _intblock PyIntBlock; |
| 43 | |
| 44 | static PyIntBlock *block_list = NULL; |
| 45 | static PyIntObject *free_list = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 47 | static PyIntObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 48 | fill_free_list(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 50 | PyIntObject *p, *q; |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 51 | /* Python's object allocator isn't appropriate for large blocks. */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 52 | p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 53 | if (p == NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 54 | return (PyIntObject *) PyErr_NoMemory(); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 55 | ((PyIntBlock *)p)->next = block_list; |
| 56 | block_list = (PyIntBlock *)p; |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 57 | /* Link the int objects together, from rear to front, then return |
| 58 | the address of the last int object in the block. */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 59 | p = &((PyIntBlock *)p)->objects[0]; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 60 | q = p + N_INTOBJECTS; |
| 61 | while (--q > p) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 62 | Py_TYPE(q) = (struct _typeobject *)(q-1); |
| 63 | Py_TYPE(q) = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 64 | return p + N_INTOBJECTS - 1; |
| 65 | } |
| 66 | |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 67 | #ifndef NSMALLPOSINTS |
Georg Brandl | 418a1ef | 2006-02-22 11:30:06 +0000 | [diff] [blame] | 68 | #define NSMALLPOSINTS 257 |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 69 | #endif |
| 70 | #ifndef NSMALLNEGINTS |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 71 | #define NSMALLNEGINTS 5 |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 72 | #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 Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 79 | static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 80 | #endif |
| 81 | #ifdef COUNT_ALLOCS |
Martin v. Löwis | b90304a | 2009-01-07 18:40:40 +0000 | [diff] [blame] | 82 | Py_ssize_t quick_int_allocs; |
| 83 | Py_ssize_t quick_neg_int_allocs; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 84 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 86 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 87 | PyInt_FromLong(long ival) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 89 | register PyIntObject *v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 90 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 91 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 92 | v = small_ints[ival + NSMALLNEGINTS]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | Py_INCREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 94 | #ifdef COUNT_ALLOCS |
| 95 | if (ival >= 0) |
| 96 | quick_int_allocs++; |
| 97 | else |
| 98 | quick_neg_int_allocs++; |
| 99 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 100 | return (PyObject *) v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 101 | } |
| 102 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 103 | if (free_list == NULL) { |
| 104 | if ((free_list = fill_free_list()) == NULL) |
| 105 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | } |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 107 | /* Inline PyObject_New */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 108 | v = free_list; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 109 | free_list = (PyIntObject *)Py_TYPE(v); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 110 | PyObject_INIT(v, &PyInt_Type); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 111 | v->ob_ival = ival; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 112 | return (PyObject *) v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 115 | PyObject * |
| 116 | PyInt_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 | |
| 123 | PyObject * |
| 124 | PyInt_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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 131 | static void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 132 | int_dealloc(PyIntObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 133 | { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 134 | if (PyInt_CheckExact(v)) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 135 | Py_TYPE(v) = (struct _typeobject *)free_list; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 136 | free_list = v; |
| 137 | } |
| 138 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 139 | Py_TYPE(v)->tp_free((PyObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Guido van Rossum | 9364698 | 2002-04-26 00:53:34 +0000 | [diff] [blame] | 142 | static void |
| 143 | int_free(PyIntObject *v) |
| 144 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 145 | Py_TYPE(v) = (struct _typeobject *)free_list; |
Guido van Rossum | 9364698 | 2002-04-26 00:53:34 +0000 | [diff] [blame] | 146 | free_list = v; |
| 147 | } |
| 148 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 150 | PyInt_AsLong(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 151 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 152 | PyNumberMethods *nb; |
| 153 | PyIntObject *io; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 154 | long val; |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 156 | if (op && PyInt_Check(op)) |
| 157 | return PyInt_AS_LONG((PyIntObject*) op); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 158 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 159 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 160 | nb->nb_int == NULL) { |
Guido van Rossum | c18a6f4 | 2000-05-09 14:27:48 +0000 | [diff] [blame] | 161 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 162 | return -1; |
| 163 | } |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 165 | io = (PyIntObject*) (*nb->nb_int) (op); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 166 | if (io == NULL) |
| 167 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 168 | if (!PyInt_Check(io)) { |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 169 | if (PyLong_Check(io)) { |
| 170 | /* got a long? => retry int conversion */ |
| 171 | val = PyLong_AsLong((PyObject *)io); |
Thomas Heller | 850566b | 2003-02-20 20:32:11 +0000 | [diff] [blame] | 172 | Py_DECREF(io); |
| 173 | if ((val == -1) && PyErr_Occurred()) |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 174 | return -1; |
Thomas Heller | 850566b | 2003-02-20 20:32:11 +0000 | [diff] [blame] | 175 | return val; |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 179 | Py_DECREF(io); |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 180 | PyErr_SetString(PyExc_TypeError, |
| 181 | "nb_int should return int object"); |
| 182 | return -1; |
| 183 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 184 | } |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 185 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 186 | val = PyInt_AS_LONG(io); |
| 187 | Py_DECREF(io); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 189 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 192 | Py_ssize_t |
| 193 | PyInt_AsSsize_t(register PyObject *op) |
| 194 | { |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 195 | #if SIZEOF_SIZE_T != SIZEOF_LONG |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 196 | PyNumberMethods *nb; |
| 197 | PyIntObject *io; |
| 198 | Py_ssize_t val; |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 199 | #endif |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 200 | |
| 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öwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 209 | return _PyLong_AsSsize_t(op); |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 210 | #if SIZEOF_SIZE_T == SIZEOF_LONG |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 211 | return PyInt_AsLong(op); |
| 212 | #else |
| 213 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 214 | if ((nb = Py_TYPE(op)->tp_as_number) == NULL || |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 215 | (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ónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 220 | if (nb->nb_long != 0) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 221 | io = (PyIntObject*) (*nb->nb_long) (op); |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 222 | else |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 223 | io = (PyIntObject*) (*nb->nb_int) (op); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 224 | 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 Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 251 | unsigned long |
| 252 | PyInt_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 Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 263 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 264 | nb->nb_int == NULL) { |
| 265 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 266 | return (unsigned long)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 270 | if (io == NULL) |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 271 | return (unsigned long)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 272 | if (!PyInt_Check(io)) { |
| 273 | if (PyLong_Check(io)) { |
| 274 | val = PyLong_AsUnsignedLongMask((PyObject *)io); |
| 275 | Py_DECREF(io); |
| 276 | if (PyErr_Occurred()) |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 277 | return (unsigned long)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 278 | return val; |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | Py_DECREF(io); |
| 283 | PyErr_SetString(PyExc_TypeError, |
| 284 | "nb_int should return int object"); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 285 | return (unsigned long)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | val = PyInt_AS_LONG(io); |
| 290 | Py_DECREF(io); |
| 291 | |
| 292 | return val; |
| 293 | } |
| 294 | |
| 295 | #ifdef HAVE_LONG_LONG |
| 296 | unsigned PY_LONG_LONG |
| 297 | PyInt_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 Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 308 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 309 | nb->nb_int == NULL) { |
| 310 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 311 | return (unsigned PY_LONG_LONG)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 315 | if (io == NULL) |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 316 | return (unsigned PY_LONG_LONG)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 317 | if (!PyInt_Check(io)) { |
| 318 | if (PyLong_Check(io)) { |
| 319 | val = PyLong_AsUnsignedLongLongMask((PyObject *)io); |
| 320 | Py_DECREF(io); |
| 321 | if (PyErr_Occurred()) |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 322 | return (unsigned PY_LONG_LONG)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 323 | return val; |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | Py_DECREF(io); |
| 328 | PyErr_SetString(PyExc_TypeError, |
| 329 | "nb_int should return int object"); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 330 | return (unsigned PY_LONG_LONG)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
| 334 | val = PyInt_AS_LONG(io); |
| 335 | Py_DECREF(io); |
| 336 | |
| 337 | return val; |
| 338 | } |
| 339 | #endif |
| 340 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 341 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 342 | PyInt_FromString(char *s, char **pend, int base) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 343 | { |
| 344 | char *end; |
| 345 | long x; |
Thomas Wouters | 9cb28bea | 2006-04-11 23:50:33 +0000 | [diff] [blame] | 346 | Py_ssize_t slen; |
| 347 | PyObject *sobj, *srepr; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 348 | |
| 349 | if ((base != 0 && base < 2) || base > 36) { |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 350 | PyErr_SetString(PyExc_ValueError, |
| 351 | "int() base must be >= 2 and <= 36"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | while (*s && isspace(Py_CHARMASK(*s))) |
| 356 | s++; |
| 357 | errno = 0; |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 358 | if (base == 0 && s[0] == '0') { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 359 | x = (long) PyOS_strtoul(s, &end, base); |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 360 | if (x < 0) |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 361 | return PyLong_FromString(s, pend, base); |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 362 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 363 | else |
| 364 | x = PyOS_strtol(s, &end, base); |
Martin v. Löwis | 2b6727b | 2001-03-06 12:12:02 +0000 | [diff] [blame] | 365 | if (end == s || !isalnum(Py_CHARMASK(end[-1]))) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 366 | goto bad; |
| 367 | while (*end && isspace(Py_CHARMASK(*end))) |
| 368 | end++; |
| 369 | if (*end != '\0') { |
| 370 | bad: |
Thomas Wouters | 9cb28bea | 2006-04-11 23:50:33 +0000 | [diff] [blame] | 371 | slen = strlen(s) < 200 ? strlen(s) : 200; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 372 | sobj = PyString_FromStringAndSize(s, slen); |
Thomas Wouters | 9cb28bea | 2006-04-11 23:50:33 +0000 | [diff] [blame] | 373 | 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. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 381 | base, PyString_AS_STRING(srepr)); |
Thomas Wouters | 9cb28bea | 2006-04-11 23:50:33 +0000 | [diff] [blame] | 382 | Py_DECREF(srepr); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 383 | return NULL; |
| 384 | } |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 385 | else if (errno != 0) |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 386 | return PyLong_FromString(s, pend, base); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 387 | if (pend) |
| 388 | *pend = end; |
| 389 | return PyInt_FromLong(x); |
| 390 | } |
| 391 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 392 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 393 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 394 | PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 395 | { |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 396 | PyObject *result; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 397 | char *buffer = (char *)PyMem_MALLOC(length+1); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 398 | |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 399 | if (buffer == NULL) |
Neal Norwitz | d501d1f | 2007-05-16 04:33:50 +0000 | [diff] [blame] | 400 | return PyErr_NoMemory(); |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 401 | |
| 402 | if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) { |
| 403 | PyMem_FREE(buffer); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 404 | return NULL; |
| 405 | } |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 406 | result = PyInt_FromString(buffer, NULL, base); |
| 407 | PyMem_FREE(buffer); |
| 408 | return result; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 409 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 410 | #endif |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 411 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 412 | /* Methods */ |
| 413 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 414 | /* 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 Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 427 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 428 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 429 | int_print(PyIntObject *v, FILE *fp, int flags) |
| 430 | /* flags -- not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 431 | { |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 432 | long int_val = v->ob_ival; |
| 433 | Py_BEGIN_ALLOW_THREADS |
| 434 | fprintf(fp, "%ld", int_val); |
| 435 | Py_END_ALLOW_THREADS |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 436 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 439 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 440 | int_compare(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 441 | { |
| 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 Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 447 | static long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 448 | int_hash(PyIntObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 449 | { |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 450 | /* XXX If this is changed, you also need to change the way |
| 451 | Python's long, float and complex types are hashed. */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 452 | long x = v -> ob_ival; |
| 453 | if (x == -1) |
| 454 | x = -2; |
| 455 | return x; |
| 456 | } |
| 457 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 458 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 459 | int_add(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 460 | { |
| 461 | register long a, b, x; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 462 | CONVERT_TO_LONG(v, a); |
| 463 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 464 | x = a + b; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 465 | if ((x^a) >= 0 || (x^b) >= 0) |
| 466 | return PyInt_FromLong(x); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 467 | return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 470 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 471 | int_sub(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 472 | { |
| 473 | register long a, b, x; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 474 | CONVERT_TO_LONG(v, a); |
| 475 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 476 | x = a - b; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 477 | if ((x^a) >= 0 || (x^~b) >= 0) |
| 478 | return PyInt_FromLong(x); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 479 | return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v, |
| 480 | (PyObject *)w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 483 | /* |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 484 | Integer overflow checking for * is painful: Python tried a couple ways, but |
| 485 | they didn't work on all platforms, or failed in endcases (a product of |
| 486 | -sys.maxint-1 has been a particular pain). |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 487 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 488 | Here's another way: |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 489 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 490 | The native long product x*y is either exactly right or *way* off, being |
| 491 | just the last n bits of the true product, where n is the number of bits |
| 492 | in a long (the delivered product is the true product plus i*2**n for |
| 493 | some integer i). |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 494 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 495 | The native double product (double)x * (double)y is subject to three |
| 496 | rounding errors: on a sizeof(long)==8 box, each cast to double can lose |
| 497 | info, and even on a sizeof(long)==4 box, the multiplication can lose info. |
| 498 | But, unlike the native long product, it's not in *range* trouble: even |
| 499 | if sizeof(long)==32 (256-bit longs), the product easily fits in the |
| 500 | dynamic range of a double. So the leading 50 (or so) bits of the double |
| 501 | product are correct. |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 502 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 503 | We check these two ways against each other, and declare victory if they're |
| 504 | approximately the same. Else, because the native long product is the only |
| 505 | one that can lose catastrophic amounts of information, it's the native long |
| 506 | product that must have overflowed. |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 507 | */ |
| 508 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 509 | static PyObject * |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 510 | int_mul(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 511 | { |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 512 | long a, b; |
| 513 | long longprod; /* a*b in native long arithmetic */ |
| 514 | double doubled_longprod; /* (double)longprod */ |
| 515 | double doubleprod; /* (double)a * (double)b */ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 516 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 517 | CONVERT_TO_LONG(v, a); |
| 518 | CONVERT_TO_LONG(w, b); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 519 | longprod = a * b; |
| 520 | doubleprod = (double)a * (double)b; |
| 521 | doubled_longprod = (double)longprod; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 522 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 523 | /* Fast path for normal case: small multiplicands, and no info |
| 524 | is lost in either method. */ |
| 525 | if (doubled_longprod == doubleprod) |
| 526 | return PyInt_FromLong(longprod); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 527 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 528 | /* Somebody somewhere lost info. Close enough, or way off? Note |
| 529 | that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0). |
| 530 | The difference either is or isn't significant compared to the |
| 531 | true value (of which doubleprod is a good approximation). |
| 532 | */ |
| 533 | { |
| 534 | const double diff = doubled_longprod - doubleprod; |
| 535 | const double absdiff = diff >= 0.0 ? diff : -diff; |
| 536 | const double absprod = doubleprod >= 0.0 ? doubleprod : |
| 537 | -doubleprod; |
| 538 | /* absdiff/absprod <= 1/32 iff |
| 539 | 32 * absdiff <= absprod -- 5 good bits is "close enough" */ |
| 540 | if (32.0 * absdiff <= absprod) |
| 541 | return PyInt_FromLong(longprod); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 542 | else |
| 543 | return PyLong_Type.tp_as_number->nb_multiply(v, w); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 544 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 547 | /* Integer overflow checking for unary negation: on a 2's-complement |
| 548 | * box, -x overflows iff x is the most negative long. In this case we |
| 549 | * get -x == x. However, -x is undefined (by C) if x /is/ the most |
| 550 | * negative long (it's a signed overflow case), and some compilers care. |
| 551 | * So we cast x to unsigned long first. However, then other compilers |
| 552 | * warn about applying unary minus to an unsigned operand. Hence the |
| 553 | * weird "0-". |
| 554 | */ |
| 555 | #define UNARY_NEG_WOULD_OVERFLOW(x) \ |
| 556 | ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x)) |
| 557 | |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 558 | /* Return type of i_divmod */ |
| 559 | enum divmod_result { |
| 560 | DIVMOD_OK, /* Correct result */ |
| 561 | DIVMOD_OVERFLOW, /* Overflow, try again using longs */ |
| 562 | DIVMOD_ERROR /* Exception raised */ |
| 563 | }; |
| 564 | |
| 565 | static enum divmod_result |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 566 | i_divmod(register long x, register long y, |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 567 | long *p_xdivy, long *p_xmody) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 568 | { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 569 | long xdivy, xmody; |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 570 | |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 571 | if (y == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 572 | PyErr_SetString(PyExc_ZeroDivisionError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 573 | "integer division or modulo by zero"); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 574 | return DIVMOD_ERROR; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 575 | } |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 576 | /* (-sys.maxint-1)/-1 is the only overflow case. */ |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 577 | if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x)) |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 578 | return DIVMOD_OVERFLOW; |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 579 | xdivy = x / y; |
| 580 | xmody = x - xdivy * y; |
| 581 | /* If the signs of x and y differ, and the remainder is non-0, |
| 582 | * C89 doesn't define whether xdivy is now the floor or the |
| 583 | * ceiling of the infinitely precise quotient. We want the floor, |
| 584 | * and we have it iff the remainder's sign matches y's. |
| 585 | */ |
| 586 | if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) { |
| 587 | xmody += y; |
| 588 | --xdivy; |
| 589 | assert(xmody && ((y ^ xmody) >= 0)); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 590 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 591 | *p_xdivy = xdivy; |
| 592 | *p_xmody = xmody; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 593 | return DIVMOD_OK; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 596 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 597 | int_div(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 598 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 599 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 600 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 601 | CONVERT_TO_LONG(x, xi); |
| 602 | CONVERT_TO_LONG(y, yi); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 603 | switch (i_divmod(xi, yi, &d, &m)) { |
| 604 | case DIVMOD_OK: |
| 605 | return PyInt_FromLong(d); |
| 606 | case DIVMOD_OVERFLOW: |
| 607 | return PyLong_Type.tp_as_number->nb_divide((PyObject *)x, |
| 608 | (PyObject *)y); |
| 609 | default: |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 610 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 611 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 614 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 615 | int_classic_div(PyIntObject *x, PyIntObject *y) |
| 616 | { |
| 617 | long xi, yi; |
| 618 | long d, m; |
| 619 | CONVERT_TO_LONG(x, xi); |
| 620 | CONVERT_TO_LONG(y, yi); |
| 621 | if (Py_DivisionWarningFlag && |
| 622 | PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0) |
| 623 | return NULL; |
| 624 | switch (i_divmod(xi, yi, &d, &m)) { |
| 625 | case DIVMOD_OK: |
| 626 | return PyInt_FromLong(d); |
| 627 | case DIVMOD_OVERFLOW: |
| 628 | return PyLong_Type.tp_as_number->nb_divide((PyObject *)x, |
| 629 | (PyObject *)y); |
| 630 | default: |
| 631 | return NULL; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | static PyObject * |
Tim Peters | 9c1d7fd | 2001-09-04 05:52:47 +0000 | [diff] [blame] | 636 | int_true_divide(PyObject *v, PyObject *w) |
| 637 | { |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 638 | /* If they aren't both ints, give someone else a chance. In |
| 639 | particular, this lets int/long get handled by longs, which |
| 640 | underflows to 0 gracefully if the long is too big to convert |
| 641 | to float. */ |
| 642 | if (PyInt_Check(v) && PyInt_Check(w)) |
| 643 | return PyFloat_Type.tp_as_number->nb_true_divide(v, w); |
| 644 | Py_INCREF(Py_NotImplemented); |
| 645 | return Py_NotImplemented; |
Tim Peters | 9c1d7fd | 2001-09-04 05:52:47 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 649 | int_mod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 650 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 651 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 652 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 653 | CONVERT_TO_LONG(x, xi); |
| 654 | CONVERT_TO_LONG(y, yi); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 655 | switch (i_divmod(xi, yi, &d, &m)) { |
| 656 | case DIVMOD_OK: |
| 657 | return PyInt_FromLong(m); |
| 658 | case DIVMOD_OVERFLOW: |
| 659 | return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x, |
| 660 | (PyObject *)y); |
| 661 | default: |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 662 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 663 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 666 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 667 | int_divmod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 668 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 669 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 670 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 671 | CONVERT_TO_LONG(x, xi); |
| 672 | CONVERT_TO_LONG(y, yi); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 673 | switch (i_divmod(xi, yi, &d, &m)) { |
| 674 | case DIVMOD_OK: |
| 675 | return Py_BuildValue("(ll)", d, m); |
| 676 | case DIVMOD_OVERFLOW: |
| 677 | return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x, |
| 678 | (PyObject *)y); |
| 679 | default: |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 680 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 681 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 684 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 685 | int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 686 | { |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 687 | register long iv, iw, iz=0, ix, temp, prev; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 688 | CONVERT_TO_LONG(v, iv); |
| 689 | CONVERT_TO_LONG(w, iw); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 690 | if (iw < 0) { |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 691 | if ((PyObject *)z != Py_None) { |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 692 | PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " |
| 693 | "cannot be negative when 3rd argument specified"); |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 694 | return NULL; |
| 695 | } |
Guido van Rossum | b82fedc | 2001-07-12 11:19:45 +0000 | [diff] [blame] | 696 | /* Return a float. This works because we know that |
| 697 | this calls float_pow() which converts its |
| 698 | arguments to double. */ |
| 699 | return PyFloat_Type.tp_as_number->nb_power( |
| 700 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 701 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 702 | if ((PyObject *)z != Py_None) { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 703 | CONVERT_TO_LONG(z, iz); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 704 | if (iz == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 705 | PyErr_SetString(PyExc_ValueError, |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 706 | "pow() 3rd argument cannot be 0"); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 707 | return NULL; |
| 708 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 709 | } |
| 710 | /* |
| 711 | * XXX: The original exponentiation code stopped looping |
| 712 | * when temp hit zero; this code will continue onwards |
| 713 | * unnecessarily, but at least it won't cause any errors. |
| 714 | * Hopefully the speed improvement from the fast exponentiation |
| 715 | * will compensate for the slight inefficiency. |
| 716 | * XXX: Better handling of overflows is desperately needed. |
| 717 | */ |
| 718 | temp = iv; |
| 719 | ix = 1; |
| 720 | while (iw > 0) { |
| 721 | prev = ix; /* Save value for overflow check */ |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 722 | if (iw & 1) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 723 | ix = ix*temp; |
| 724 | if (temp == 0) |
| 725 | break; /* Avoid ix / 0 */ |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 726 | if (ix / temp != prev) { |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 727 | return PyLong_Type.tp_as_number->nb_power( |
| 728 | (PyObject *)v, |
| 729 | (PyObject *)w, |
Tim Peters | 31960db | 2001-08-23 21:28:33 +0000 | [diff] [blame] | 730 | (PyObject *)z); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 731 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 732 | } |
| 733 | iw >>= 1; /* Shift exponent down by 1 bit */ |
| 734 | if (iw==0) break; |
| 735 | prev = temp; |
| 736 | temp *= temp; /* Square the value of temp */ |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 737 | if (prev != 0 && temp / prev != prev) { |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 738 | return PyLong_Type.tp_as_number->nb_power( |
| 739 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
| 740 | } |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 741 | if (iz) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 742 | /* If we did a multiplication, perform a modulo */ |
| 743 | ix = ix % iz; |
| 744 | temp = temp % iz; |
| 745 | } |
| 746 | } |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 747 | if (iz) { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 748 | long div, mod; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 749 | switch (i_divmod(ix, iz, &div, &mod)) { |
| 750 | case DIVMOD_OK: |
| 751 | ix = mod; |
| 752 | break; |
| 753 | case DIVMOD_OVERFLOW: |
| 754 | return PyLong_Type.tp_as_number->nb_power( |
| 755 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
| 756 | default: |
| 757 | return NULL; |
| 758 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 759 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 760 | return PyInt_FromLong(ix); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 761 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 762 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 763 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 764 | int_neg(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 765 | { |
Martin v. Löwis | 820d6ac | 2006-10-04 05:47:34 +0000 | [diff] [blame] | 766 | register long a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 767 | a = v->ob_ival; |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 768 | /* check for overflow */ |
| 769 | if (UNARY_NEG_WOULD_OVERFLOW(a)) { |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 770 | PyObject *o = PyLong_FromLong(a); |
Neal Norwitz | fa56e2d | 2003-01-19 15:40:09 +0000 | [diff] [blame] | 771 | if (o != NULL) { |
| 772 | PyObject *result = PyNumber_Negative(o); |
| 773 | Py_DECREF(o); |
| 774 | return result; |
| 775 | } |
| 776 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 777 | } |
Martin v. Löwis | 820d6ac | 2006-10-04 05:47:34 +0000 | [diff] [blame] | 778 | return PyInt_FromLong(-a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 781 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 782 | int_abs(PyIntObject *v) |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 783 | { |
| 784 | if (v->ob_ival >= 0) |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 785 | return int_int(v); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 786 | else |
| 787 | return int_neg(v); |
| 788 | } |
| 789 | |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 790 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 791 | int_nonzero(PyIntObject *v) |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 792 | { |
| 793 | return v->ob_ival != 0; |
| 794 | } |
| 795 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 796 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 797 | int_invert(PyIntObject *v) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 798 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 799 | return PyInt_FromLong(~v->ob_ival); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 802 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 803 | int_lshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 804 | { |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 805 | long a, b, c; |
Raymond Hettinger | a006c37 | 2004-06-26 23:22:57 +0000 | [diff] [blame] | 806 | PyObject *vv, *ww, *result; |
| 807 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 808 | CONVERT_TO_LONG(v, a); |
| 809 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 810 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 811 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 812 | return NULL; |
| 813 | } |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 814 | if (a == 0 || b == 0) |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 815 | return int_int(v); |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 816 | if (b >= LONG_BIT) { |
Raymond Hettinger | a006c37 | 2004-06-26 23:22:57 +0000 | [diff] [blame] | 817 | vv = PyLong_FromLong(PyInt_AS_LONG(v)); |
| 818 | if (vv == NULL) |
| 819 | return NULL; |
| 820 | ww = PyLong_FromLong(PyInt_AS_LONG(w)); |
| 821 | if (ww == NULL) { |
| 822 | Py_DECREF(vv); |
| 823 | return NULL; |
| 824 | } |
| 825 | result = PyNumber_Lshift(vv, ww); |
| 826 | Py_DECREF(vv); |
| 827 | Py_DECREF(ww); |
| 828 | return result; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 829 | } |
Tim Peters | da1a221 | 2002-08-11 17:54:42 +0000 | [diff] [blame] | 830 | c = a << b; |
| 831 | if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) { |
Raymond Hettinger | a006c37 | 2004-06-26 23:22:57 +0000 | [diff] [blame] | 832 | vv = PyLong_FromLong(PyInt_AS_LONG(v)); |
| 833 | if (vv == NULL) |
| 834 | return NULL; |
| 835 | ww = PyLong_FromLong(PyInt_AS_LONG(w)); |
| 836 | if (ww == NULL) { |
| 837 | Py_DECREF(vv); |
| 838 | return NULL; |
| 839 | } |
| 840 | result = PyNumber_Lshift(vv, ww); |
| 841 | Py_DECREF(vv); |
| 842 | Py_DECREF(ww); |
| 843 | return result; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 844 | } |
| 845 | return PyInt_FromLong(c); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 848 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 849 | int_rshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 850 | { |
| 851 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 852 | CONVERT_TO_LONG(v, a); |
| 853 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 854 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 855 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 856 | return NULL; |
| 857 | } |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 858 | if (a == 0 || b == 0) |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 859 | return int_int(v); |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 860 | if (b >= LONG_BIT) { |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 861 | if (a < 0) |
| 862 | a = -1; |
| 863 | else |
| 864 | a = 0; |
| 865 | } |
| 866 | else { |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 867 | a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 868 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 869 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 872 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 873 | int_and(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 874 | { |
| 875 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 876 | CONVERT_TO_LONG(v, a); |
| 877 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 878 | return PyInt_FromLong(a & b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 881 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 882 | int_xor(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 883 | { |
| 884 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 885 | CONVERT_TO_LONG(v, a); |
| 886 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 887 | return PyInt_FromLong(a ^ b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 890 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 891 | int_or(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 892 | { |
| 893 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 894 | CONVERT_TO_LONG(v, a); |
| 895 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 896 | return PyInt_FromLong(a | b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 899 | static int |
| 900 | int_coerce(PyObject **pv, PyObject **pw) |
| 901 | { |
| 902 | if (PyInt_Check(*pw)) { |
| 903 | Py_INCREF(*pv); |
| 904 | Py_INCREF(*pw); |
| 905 | return 0; |
| 906 | } |
| 907 | return 1; /* Can't do it */ |
| 908 | } |
| 909 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 910 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 911 | int_int(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 912 | { |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 913 | if (PyInt_CheckExact(v)) |
| 914 | Py_INCREF(v); |
| 915 | else |
| 916 | v = (PyIntObject *)PyInt_FromLong(v->ob_ival); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 917 | return (PyObject *)v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 920 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 921 | int_long(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 922 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 923 | return PyLong_FromLong((v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 926 | static const unsigned char BitLengthTable[32] = { |
| 927 | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, |
| 928 | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 |
| 929 | }; |
| 930 | |
| 931 | static int |
| 932 | bits_in_ulong(unsigned long d) |
| 933 | { |
| 934 | int d_bits = 0; |
| 935 | while (d >= 32) { |
| 936 | d_bits += 6; |
| 937 | d >>= 6; |
| 938 | } |
| 939 | d_bits += (int)BitLengthTable[d]; |
| 940 | return d_bits; |
| 941 | } |
| 942 | |
| 943 | #if 8*SIZEOF_LONG-1 <= DBL_MANT_DIG |
| 944 | /* Every Python int can be exactly represented as a float. */ |
| 945 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 946 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 947 | int_float(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 948 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 949 | return PyFloat_FromDouble((double)(v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 952 | #else |
| 953 | /* Here not all Python ints are exactly representable as floats, so we may |
| 954 | have to round. We do this manually, since the C standards don't specify |
| 955 | whether converting an integer to a float rounds up or down */ |
| 956 | |
| 957 | static PyObject * |
| 958 | int_float(PyIntObject *v) |
| 959 | { |
| 960 | unsigned long abs_ival, lsb; |
| 961 | int round_up; |
| 962 | |
| 963 | if (v->ob_ival < 0) |
| 964 | abs_ival = 0U-(unsigned long)v->ob_ival; |
| 965 | else |
| 966 | abs_ival = (unsigned long)v->ob_ival; |
| 967 | if (abs_ival < (1L << DBL_MANT_DIG)) |
| 968 | /* small integer; no need to round */ |
| 969 | return PyFloat_FromDouble((double)v->ob_ival); |
| 970 | |
| 971 | /* Round abs_ival to MANT_DIG significant bits, using the |
| 972 | round-half-to-even rule. abs_ival & lsb picks out the 'rounding' |
| 973 | bit: the first bit after the most significant MANT_DIG bits of |
| 974 | abs_ival. We round up if this bit is set, provided that either: |
| 975 | |
| 976 | (1) abs_ival isn't exactly halfway between two floats, in which |
| 977 | case at least one of the bits following the rounding bit must be |
| 978 | set; i.e., abs_ival & lsb-1 != 0, or: |
| 979 | |
| 980 | (2) the resulting rounded value has least significant bit 0; or |
| 981 | in other words the bit above the rounding bit is set (this is the |
| 982 | 'to-even' bit of round-half-to-even); i.e., abs_ival & 2*lsb != 0 |
| 983 | |
| 984 | The condition "(1) or (2)" equates to abs_ival & 3*lsb-1 != 0. */ |
| 985 | |
| 986 | lsb = 1L << (bits_in_ulong(abs_ival)-DBL_MANT_DIG-1); |
| 987 | round_up = (abs_ival & lsb) && (abs_ival & (3*lsb-1)); |
| 988 | abs_ival &= -2*lsb; |
| 989 | if (round_up) |
| 990 | abs_ival += 2*lsb; |
| 991 | return PyFloat_FromDouble(v->ob_ival < 0 ? |
| 992 | -(double)abs_ival : |
| 993 | (double)abs_ival); |
| 994 | } |
| 995 | |
| 996 | #endif |
| 997 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 998 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 999 | int_oct(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1000 | { |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1001 | return _PyInt_Format(v, 8, 0); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1004 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1005 | int_hex(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1006 | { |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1007 | return _PyInt_Format(v, 16, 0); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1010 | static PyObject * |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1011 | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 1012 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1013 | static PyObject * |
| 1014 | int_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1015 | { |
| 1016 | PyObject *x = NULL; |
| 1017 | int base = -909; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1018 | static char *kwlist[] = {"x", "base", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1019 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1020 | if (type != &PyInt_Type) |
| 1021 | return int_subtype_new(type, args, kwds); /* Wimp out */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1022 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, |
| 1023 | &x, &base)) |
| 1024 | return NULL; |
| 1025 | if (x == NULL) |
| 1026 | return PyInt_FromLong(0L); |
| 1027 | if (base == -909) |
| 1028 | return PyNumber_Int(x); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1029 | if (PyString_Check(x)) { |
Georg Brandl | 2c1375c | 2006-10-12 11:27:59 +0000 | [diff] [blame] | 1030 | /* Since PyInt_FromString doesn't have a length parameter, |
| 1031 | * check here for possible NULs in the string. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1032 | char *string = PyString_AS_STRING(x); |
| 1033 | if (strlen(string) != PyString_Size(x)) { |
Georg Brandl | 2c1375c | 2006-10-12 11:27:59 +0000 | [diff] [blame] | 1034 | /* create a repr() of the input string, |
| 1035 | * just like PyInt_FromString does */ |
| 1036 | PyObject *srepr; |
| 1037 | srepr = PyObject_Repr(x); |
| 1038 | if (srepr == NULL) |
| 1039 | return NULL; |
| 1040 | PyErr_Format(PyExc_ValueError, |
| 1041 | "invalid literal for int() with base %d: %s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1042 | base, PyString_AS_STRING(srepr)); |
Georg Brandl | 2c1375c | 2006-10-12 11:27:59 +0000 | [diff] [blame] | 1043 | Py_DECREF(srepr); |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | return PyInt_FromString(string, NULL, base); |
| 1047 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1048 | #ifdef Py_USING_UNICODE |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1049 | if (PyUnicode_Check(x)) |
| 1050 | return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x), |
| 1051 | PyUnicode_GET_SIZE(x), |
| 1052 | base); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1053 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1054 | PyErr_SetString(PyExc_TypeError, |
| 1055 | "int() can't convert non-string with explicit base"); |
| 1056 | return NULL; |
| 1057 | } |
| 1058 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1059 | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
| 1060 | first create a regular int from whatever arguments we got, |
| 1061 | then allocate a subtype instance and initialize its ob_ival |
| 1062 | from the regular int. The regular int is then thrown away. |
| 1063 | */ |
| 1064 | static PyObject * |
| 1065 | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1066 | { |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1067 | PyObject *tmp, *newobj; |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1068 | long ival; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1069 | |
| 1070 | assert(PyType_IsSubtype(type, &PyInt_Type)); |
| 1071 | tmp = int_new(&PyInt_Type, args, kwds); |
| 1072 | if (tmp == NULL) |
| 1073 | return NULL; |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1074 | if (!PyInt_Check(tmp)) { |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1075 | ival = PyLong_AsLong(tmp); |
Michael W. Hudson | 71665dc | 2003-08-11 17:32:02 +0000 | [diff] [blame] | 1076 | if (ival == -1 && PyErr_Occurred()) { |
| 1077 | Py_DECREF(tmp); |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1078 | return NULL; |
Michael W. Hudson | 71665dc | 2003-08-11 17:32:02 +0000 | [diff] [blame] | 1079 | } |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1080 | } else { |
| 1081 | ival = ((PyIntObject *)tmp)->ob_ival; |
| 1082 | } |
| 1083 | |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1084 | newobj = type->tp_alloc(type, 0); |
| 1085 | if (newobj == NULL) { |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 1086 | Py_DECREF(tmp); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1087 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 1088 | } |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1089 | ((PyIntObject *)newobj)->ob_ival = ival; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1090 | Py_DECREF(tmp); |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1091 | return newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1094 | static PyObject * |
| 1095 | int_getnewargs(PyIntObject *v) |
| 1096 | { |
| 1097 | return Py_BuildValue("(l)", v->ob_ival); |
| 1098 | } |
| 1099 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1100 | static PyObject * |
Mark Dickinson | 85e269b | 2009-05-03 20:39:06 +0000 | [diff] [blame] | 1101 | int_get0(PyIntObject *v, void *context) { |
| 1102 | return PyInt_FromLong(0L); |
| 1103 | } |
| 1104 | |
| 1105 | static PyObject * |
| 1106 | int_get1(PyIntObject *v, void *context) { |
| 1107 | return PyInt_FromLong(1L); |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Mark Dickinson | 4b9d473 | 2009-09-27 16:05:21 +0000 | [diff] [blame^] | 1110 | /* Convert an integer to a decimal string. On many platforms, this |
| 1111 | will be significantly faster than the general arbitrary-base |
| 1112 | conversion machinery in _PyInt_Format, thanks to optimization |
| 1113 | opportunities offered by division by a compile-time constant. */ |
| 1114 | static PyObject * |
| 1115 | int_to_decimal_string(PyIntObject *v) { |
| 1116 | char buf[sizeof(long)*CHAR_BIT/3+6], *p, *bufend; |
| 1117 | long n = v->ob_ival; |
| 1118 | unsigned long absn; |
| 1119 | p = bufend = buf + sizeof(buf); |
| 1120 | absn = n < 0 ? -(unsigned long)n : n; |
| 1121 | do { |
| 1122 | *--p = '0' + absn % 10; |
| 1123 | absn /= 10; |
| 1124 | } while (absn); |
| 1125 | if (n < 0) |
| 1126 | *--p = '-'; |
| 1127 | return PyString_FromStringAndSize(p, bufend - p); |
| 1128 | } |
| 1129 | |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1130 | /* Convert an integer to the given base. Returns a string. |
| 1131 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. |
| 1132 | If newstyle is zero, then use the pre-2.6 behavior of octal having |
| 1133 | a leading "0" */ |
| 1134 | PyAPI_FUNC(PyObject*) |
| 1135 | _PyInt_Format(PyIntObject *v, int base, int newstyle) |
| 1136 | { |
| 1137 | /* There are no doubt many, many ways to optimize this, using code |
| 1138 | similar to _PyLong_Format */ |
| 1139 | long n = v->ob_ival; |
| 1140 | int negative = n < 0; |
| 1141 | int is_zero = n == 0; |
| 1142 | |
| 1143 | /* For the reasoning behind this size, see |
| 1144 | http://c-faq.com/misc/hexio.html. Then, add a few bytes for |
| 1145 | the possible sign and prefix "0[box]" */ |
| 1146 | char buf[sizeof(n)*CHAR_BIT+6]; |
| 1147 | |
| 1148 | /* Start by pointing to the end of the buffer. We fill in from |
| 1149 | the back forward. */ |
| 1150 | char* p = &buf[sizeof(buf)]; |
| 1151 | |
| 1152 | assert(base >= 2 && base <= 36); |
| 1153 | |
Mark Dickinson | 4b9d473 | 2009-09-27 16:05:21 +0000 | [diff] [blame^] | 1154 | /* Special case base 10, for speed */ |
| 1155 | if (base == 10) |
| 1156 | return int_to_decimal_string(v); |
| 1157 | |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1158 | do { |
| 1159 | /* I'd use i_divmod, except it doesn't produce the results |
| 1160 | I want when n is negative. So just duplicate the salient |
| 1161 | part here. */ |
| 1162 | long div = n / base; |
| 1163 | long mod = n - div * base; |
| 1164 | |
| 1165 | /* convert abs(mod) to the right character in [0-9, a-z] */ |
| 1166 | char cdigit = (char)(mod < 0 ? -mod : mod); |
| 1167 | cdigit += (cdigit < 10) ? '0' : 'a'-10; |
| 1168 | *--p = cdigit; |
| 1169 | |
| 1170 | n = div; |
| 1171 | } while(n); |
| 1172 | |
| 1173 | if (base == 2) { |
| 1174 | *--p = 'b'; |
| 1175 | *--p = '0'; |
| 1176 | } |
| 1177 | else if (base == 8) { |
| 1178 | if (newstyle) { |
| 1179 | *--p = 'o'; |
| 1180 | *--p = '0'; |
| 1181 | } |
| 1182 | else |
| 1183 | if (!is_zero) |
| 1184 | *--p = '0'; |
| 1185 | } |
| 1186 | else if (base == 16) { |
| 1187 | *--p = 'x'; |
| 1188 | *--p = '0'; |
| 1189 | } |
Mark Dickinson | 4b9d473 | 2009-09-27 16:05:21 +0000 | [diff] [blame^] | 1190 | else { |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1191 | *--p = '#'; |
| 1192 | *--p = '0' + base%10; |
| 1193 | if (base > 10) |
| 1194 | *--p = '0' + base/10; |
| 1195 | } |
| 1196 | if (negative) |
| 1197 | *--p = '-'; |
| 1198 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1199 | return PyString_FromStringAndSize(p, &buf[sizeof(buf)] - p); |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1202 | static PyObject * |
| 1203 | int__format__(PyObject *self, PyObject *args) |
| 1204 | { |
| 1205 | PyObject *format_spec; |
| 1206 | |
| 1207 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 1208 | return NULL; |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 1209 | if (PyBytes_Check(format_spec)) |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1210 | return _PyInt_FormatAdvanced(self, |
| 1211 | PyBytes_AS_STRING(format_spec), |
| 1212 | PyBytes_GET_SIZE(format_spec)); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1213 | if (PyUnicode_Check(format_spec)) { |
| 1214 | /* Convert format_spec to a str */ |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1215 | PyObject *result; |
| 1216 | PyObject *str_spec = PyObject_Str(format_spec); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1217 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1218 | if (str_spec == NULL) |
| 1219 | return NULL; |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1220 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1221 | result = _PyInt_FormatAdvanced(self, |
| 1222 | PyBytes_AS_STRING(str_spec), |
| 1223 | PyBytes_GET_SIZE(str_spec)); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1224 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1225 | Py_DECREF(str_spec); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1226 | return result; |
| 1227 | } |
| 1228 | PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode"); |
| 1229 | return NULL; |
| 1230 | } |
| 1231 | |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 1232 | static PyObject * |
| 1233 | int_bit_length(PyIntObject *v) |
| 1234 | { |
| 1235 | unsigned long n; |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 1236 | |
| 1237 | if (v->ob_ival < 0) |
| 1238 | /* avoid undefined behaviour when v->ob_ival == -LONG_MAX-1 */ |
| 1239 | n = 0U-(unsigned long)v->ob_ival; |
| 1240 | else |
| 1241 | n = (unsigned long)v->ob_ival; |
| 1242 | |
Mark Dickinson | 6736cf8 | 2009-04-20 21:13:33 +0000 | [diff] [blame] | 1243 | return PyInt_FromLong(bits_in_ulong(n)); |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | PyDoc_STRVAR(int_bit_length_doc, |
| 1247 | "int.bit_length() -> int\n\ |
| 1248 | \n\ |
| 1249 | Number of bits necessary to represent self in binary.\n\ |
| 1250 | >>> bin(37)\n\ |
| 1251 | '0b100101'\n\ |
| 1252 | >>> (37).bit_length()\n\ |
| 1253 | 6"); |
| 1254 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1255 | #if 0 |
| 1256 | static PyObject * |
| 1257 | int_is_finite(PyObject *v) |
| 1258 | { |
| 1259 | Py_RETURN_TRUE; |
| 1260 | } |
| 1261 | #endif |
| 1262 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1263 | static PyMethodDef int_methods[] = { |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1264 | {"conjugate", (PyCFunction)int_int, METH_NOARGS, |
| 1265 | "Returns self, the complex conjugate of any int."}, |
Mark Dickinson | 1a70798 | 2008-12-17 16:14:37 +0000 | [diff] [blame] | 1266 | {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS, |
| 1267 | int_bit_length_doc}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1268 | #if 0 |
| 1269 | {"is_finite", (PyCFunction)int_is_finite, METH_NOARGS, |
| 1270 | "Returns always True."}, |
| 1271 | #endif |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1272 | {"__trunc__", (PyCFunction)int_int, METH_NOARGS, |
| 1273 | "Truncating an Integral returns itself."}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1274 | {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1275 | {"__format__", (PyCFunction)int__format__, METH_VARARGS}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1276 | {NULL, NULL} /* sentinel */ |
| 1277 | }; |
| 1278 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1279 | static PyGetSetDef int_getset[] = { |
Mark Dickinson | 85e269b | 2009-05-03 20:39:06 +0000 | [diff] [blame] | 1280 | {"real", |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1281 | (getter)int_int, (setter)NULL, |
| 1282 | "the real part of a complex number", |
| 1283 | NULL}, |
Mark Dickinson | 85e269b | 2009-05-03 20:39:06 +0000 | [diff] [blame] | 1284 | {"imag", |
| 1285 | (getter)int_get0, (setter)NULL, |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1286 | "the imaginary part of a complex number", |
Mark Dickinson | 85e269b | 2009-05-03 20:39:06 +0000 | [diff] [blame] | 1287 | NULL}, |
| 1288 | {"numerator", |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1289 | (getter)int_int, (setter)NULL, |
| 1290 | "the numerator of a rational number in lowest terms", |
| 1291 | NULL}, |
Mark Dickinson | 85e269b | 2009-05-03 20:39:06 +0000 | [diff] [blame] | 1292 | {"denominator", |
| 1293 | (getter)int_get1, (setter)NULL, |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1294 | "the denominator of a rational number in lowest terms", |
Mark Dickinson | 85e269b | 2009-05-03 20:39:06 +0000 | [diff] [blame] | 1295 | NULL}, |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1296 | {NULL} /* Sentinel */ |
| 1297 | }; |
| 1298 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1299 | PyDoc_STRVAR(int_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1300 | "int(x[, base]) -> integer\n\ |
| 1301 | \n\ |
| 1302 | Convert a string or number to an integer, if possible. A floating point\n\ |
| 1303 | argument will be truncated towards zero (this does not include a string\n\ |
| 1304 | representation of a floating point number!) When converting a string, use\n\ |
| 1305 | the optional base. It is an error to supply a base when converting a\n\ |
Gustavo Niemeyer | 37e6502 | 2007-01-10 16:15:48 +0000 | [diff] [blame] | 1306 | non-string. If base is zero, the proper base is guessed based on the\n\ |
Gustavo Niemeyer | a443bc8 | 2007-01-10 16:13:40 +0000 | [diff] [blame] | 1307 | string content. If the argument is outside the integer range a\n\ |
| 1308 | long object will be returned instead."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1309 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1310 | static PyNumberMethods int_as_number = { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1311 | (binaryfunc)int_add, /*nb_add*/ |
| 1312 | (binaryfunc)int_sub, /*nb_subtract*/ |
| 1313 | (binaryfunc)int_mul, /*nb_multiply*/ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 1314 | (binaryfunc)int_classic_div, /*nb_divide*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1315 | (binaryfunc)int_mod, /*nb_remainder*/ |
| 1316 | (binaryfunc)int_divmod, /*nb_divmod*/ |
| 1317 | (ternaryfunc)int_pow, /*nb_power*/ |
| 1318 | (unaryfunc)int_neg, /*nb_negative*/ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1319 | (unaryfunc)int_int, /*nb_positive*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1320 | (unaryfunc)int_abs, /*nb_absolute*/ |
| 1321 | (inquiry)int_nonzero, /*nb_nonzero*/ |
| 1322 | (unaryfunc)int_invert, /*nb_invert*/ |
| 1323 | (binaryfunc)int_lshift, /*nb_lshift*/ |
| 1324 | (binaryfunc)int_rshift, /*nb_rshift*/ |
| 1325 | (binaryfunc)int_and, /*nb_and*/ |
| 1326 | (binaryfunc)int_xor, /*nb_xor*/ |
| 1327 | (binaryfunc)int_or, /*nb_or*/ |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 1328 | int_coerce, /*nb_coerce*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1329 | (unaryfunc)int_int, /*nb_int*/ |
| 1330 | (unaryfunc)int_long, /*nb_long*/ |
| 1331 | (unaryfunc)int_float, /*nb_float*/ |
| 1332 | (unaryfunc)int_oct, /*nb_oct*/ |
| 1333 | (unaryfunc)int_hex, /*nb_hex*/ |
| 1334 | 0, /*nb_inplace_add*/ |
| 1335 | 0, /*nb_inplace_subtract*/ |
| 1336 | 0, /*nb_inplace_multiply*/ |
| 1337 | 0, /*nb_inplace_divide*/ |
| 1338 | 0, /*nb_inplace_remainder*/ |
| 1339 | 0, /*nb_inplace_power*/ |
| 1340 | 0, /*nb_inplace_lshift*/ |
| 1341 | 0, /*nb_inplace_rshift*/ |
| 1342 | 0, /*nb_inplace_and*/ |
| 1343 | 0, /*nb_inplace_xor*/ |
| 1344 | 0, /*nb_inplace_or*/ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1345 | (binaryfunc)int_div, /* nb_floor_divide */ |
| 1346 | int_true_divide, /* nb_true_divide */ |
| 1347 | 0, /* nb_inplace_floor_divide */ |
| 1348 | 0, /* nb_inplace_true_divide */ |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1349 | (unaryfunc)int_int, /* nb_index */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1350 | }; |
| 1351 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1352 | PyTypeObject PyInt_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1353 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1354 | "int", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1355 | sizeof(PyIntObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1356 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1357 | (destructor)int_dealloc, /* tp_dealloc */ |
| 1358 | (printfunc)int_print, /* tp_print */ |
| 1359 | 0, /* tp_getattr */ |
| 1360 | 0, /* tp_setattr */ |
| 1361 | (cmpfunc)int_compare, /* tp_compare */ |
Mark Dickinson | 4b9d473 | 2009-09-27 16:05:21 +0000 | [diff] [blame^] | 1362 | (reprfunc)int_to_decimal_string, /* tp_repr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1363 | &int_as_number, /* tp_as_number */ |
| 1364 | 0, /* tp_as_sequence */ |
| 1365 | 0, /* tp_as_mapping */ |
| 1366 | (hashfunc)int_hash, /* tp_hash */ |
Mark Dickinson | 4b9d473 | 2009-09-27 16:05:21 +0000 | [diff] [blame^] | 1367 | 0, /* tp_call */ |
| 1368 | (reprfunc)int_to_decimal_string, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1369 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1370 | 0, /* tp_setattro */ |
| 1371 | 0, /* tp_as_buffer */ |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1372 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 1373 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1374 | int_doc, /* tp_doc */ |
| 1375 | 0, /* tp_traverse */ |
| 1376 | 0, /* tp_clear */ |
| 1377 | 0, /* tp_richcompare */ |
| 1378 | 0, /* tp_weaklistoffset */ |
| 1379 | 0, /* tp_iter */ |
| 1380 | 0, /* tp_iternext */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1381 | int_methods, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1382 | 0, /* tp_members */ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1383 | int_getset, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1384 | 0, /* tp_base */ |
| 1385 | 0, /* tp_dict */ |
| 1386 | 0, /* tp_descr_get */ |
| 1387 | 0, /* tp_descr_set */ |
| 1388 | 0, /* tp_dictoffset */ |
| 1389 | 0, /* tp_init */ |
| 1390 | 0, /* tp_alloc */ |
| 1391 | int_new, /* tp_new */ |
Guido van Rossum | 9364698 | 2002-04-26 00:53:34 +0000 | [diff] [blame] | 1392 | (freefunc)int_free, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1393 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1394 | |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1395 | int |
Neal Norwitz | b2501f4 | 2002-12-31 03:42:13 +0000 | [diff] [blame] | 1396 | _PyInt_Init(void) |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1397 | { |
| 1398 | PyIntObject *v; |
| 1399 | int ival; |
| 1400 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 1401 | for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) { |
Raymond Hettinger | b32e640 | 2004-02-08 18:54:37 +0000 | [diff] [blame] | 1402 | if (!free_list && (free_list = fill_free_list()) == NULL) |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1403 | return 0; |
| 1404 | /* PyObject_New is inlined */ |
| 1405 | v = free_list; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1406 | free_list = (PyIntObject *)Py_TYPE(v); |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1407 | PyObject_INIT(v, &PyInt_Type); |
| 1408 | v->ob_ival = ival; |
| 1409 | small_ints[ival + NSMALLNEGINTS] = v; |
| 1410 | } |
| 1411 | #endif |
| 1412 | return 1; |
| 1413 | } |
| 1414 | |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1415 | int |
| 1416 | PyInt_ClearFreeList(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1417 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1418 | PyIntObject *p; |
| 1419 | PyIntBlock *list, *next; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1420 | int i; |
| 1421 | int u; /* remaining unfreed ints per block */ |
| 1422 | int freelist_size = 0; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1423 | |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1424 | list = block_list; |
| 1425 | block_list = NULL; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1426 | free_list = NULL; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1427 | while (list != NULL) { |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1428 | u = 0; |
| 1429 | for (i = 0, p = &list->objects[0]; |
| 1430 | i < N_INTOBJECTS; |
| 1431 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1432 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1433 | u++; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1434 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1435 | next = list->next; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1436 | if (u) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1437 | list->next = block_list; |
| 1438 | block_list = list; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1439 | for (i = 0, p = &list->objects[0]; |
| 1440 | i < N_INTOBJECTS; |
| 1441 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1442 | if (!PyInt_CheckExact(p) || |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1443 | p->ob_refcnt == 0) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1444 | Py_TYPE(p) = (struct _typeobject *) |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1445 | free_list; |
| 1446 | free_list = p; |
| 1447 | } |
| 1448 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 1449 | else if (-NSMALLNEGINTS <= p->ob_ival && |
| 1450 | p->ob_ival < NSMALLPOSINTS && |
| 1451 | small_ints[p->ob_ival + |
| 1452 | NSMALLNEGINTS] == NULL) { |
| 1453 | Py_INCREF(p); |
| 1454 | small_ints[p->ob_ival + |
| 1455 | NSMALLNEGINTS] = p; |
| 1456 | } |
| 1457 | #endif |
| 1458 | } |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1459 | } |
| 1460 | else { |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 1461 | PyMem_FREE(list); |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1462 | } |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1463 | freelist_size += u; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1464 | list = next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1465 | } |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1466 | |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1467 | return freelist_size; |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | void |
| 1471 | PyInt_Fini(void) |
| 1472 | { |
| 1473 | PyIntObject *p; |
| 1474 | PyIntBlock *list; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1475 | int i; |
| 1476 | int u; /* total unfreed ints per block */ |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1477 | |
| 1478 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1479 | PyIntObject **q; |
| 1480 | |
| 1481 | i = NSMALLNEGINTS + NSMALLPOSINTS; |
| 1482 | q = small_ints; |
| 1483 | while (--i >= 0) { |
| 1484 | Py_XDECREF(*q); |
| 1485 | *q++ = NULL; |
| 1486 | } |
| 1487 | #endif |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1488 | u = PyInt_ClearFreeList(); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1489 | if (!Py_VerboseFlag) |
| 1490 | return; |
| 1491 | fprintf(stderr, "# cleanup ints"); |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1492 | if (!u) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1493 | fprintf(stderr, "\n"); |
| 1494 | } |
| 1495 | else { |
| 1496 | fprintf(stderr, |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1497 | ": %d unfreed int%s\n", |
| 1498 | u, u == 1 ? "" : "s"); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1499 | } |
| 1500 | if (Py_VerboseFlag > 1) { |
| 1501 | list = block_list; |
| 1502 | while (list != NULL) { |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame] | 1503 | for (i = 0, p = &list->objects[0]; |
| 1504 | i < N_INTOBJECTS; |
| 1505 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1506 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
Thomas Wouters | 8b87a0b | 2006-03-01 05:41:20 +0000 | [diff] [blame] | 1507 | /* XXX(twouters) cast refcount to |
| 1508 | long until %zd is universally |
| 1509 | available |
| 1510 | */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1511 | fprintf(stderr, |
Thomas Wouters | 8b87a0b | 2006-03-01 05:41:20 +0000 | [diff] [blame] | 1512 | "# <int at %p, refcnt=%ld, val=%ld>\n", |
| 1513 | p, (long)p->ob_refcnt, |
| 1514 | p->ob_ival); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1515 | } |
| 1516 | list = list->next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1519 | } |