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> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 7 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 8 | PyInt_GetMax(void) |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 9 | { |
| 10 | return LONG_MAX; /* To initialize sys.maxint */ |
| 11 | } |
| 12 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | /* Integers are quite normal objects, to make object handling uniform. |
| 14 | (Using odd pointers to represent integers would save much space |
| 15 | but require extra checks for this special case throughout the code.) |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 16 | Since a typical Python program spends much of its time allocating |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 17 | and deallocating integers, these operations should be very fast. |
| 18 | Therefore we use a dedicated allocation scheme with a much lower |
| 19 | overhead (in space and time) than straight malloc(): a simple |
| 20 | dedicated free list, filled when necessary with memory from malloc(). |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 21 | |
| 22 | block_list is a singly-linked list of all PyIntBlocks ever allocated, |
| 23 | linked via their next members. PyIntBlocks are never returned to the |
| 24 | system before shutdown (PyInt_Fini). |
| 25 | |
| 26 | free_list is a singly-linked list of available PyIntObjects, linked |
| 27 | via abuse of their ob_type members. |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 31 | #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
| 32 | #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject)) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 34 | struct _intblock { |
| 35 | struct _intblock *next; |
| 36 | PyIntObject objects[N_INTOBJECTS]; |
| 37 | }; |
| 38 | |
| 39 | typedef struct _intblock PyIntBlock; |
| 40 | |
| 41 | static PyIntBlock *block_list = NULL; |
| 42 | static PyIntObject *free_list = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 44 | static PyIntObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 45 | fill_free_list(void) |
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 | PyIntObject *p, *q; |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 48 | /* Python's object allocator isn't appropriate for large blocks. */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 49 | p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 50 | if (p == NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 51 | return (PyIntObject *) PyErr_NoMemory(); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 52 | ((PyIntBlock *)p)->next = block_list; |
| 53 | block_list = (PyIntBlock *)p; |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 54 | /* Link the int objects together, from rear to front, then return |
| 55 | the address of the last int object in the block. */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 56 | p = &((PyIntBlock *)p)->objects[0]; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 57 | q = p + N_INTOBJECTS; |
| 58 | while (--q > p) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 59 | q->ob_type = (struct _typeobject *)(q-1); |
| 60 | q->ob_type = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 61 | return p + N_INTOBJECTS - 1; |
| 62 | } |
| 63 | |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 64 | #ifndef NSMALLPOSINTS |
Georg Brandl | 418a1ef | 2006-02-22 11:30:06 +0000 | [diff] [blame] | 65 | #define NSMALLPOSINTS 257 |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 66 | #endif |
| 67 | #ifndef NSMALLNEGINTS |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 68 | #define NSMALLNEGINTS 5 |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 69 | #endif |
| 70 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 71 | /* References to small integers are saved in this array so that they |
| 72 | can be shared. |
| 73 | The integers that are saved are those in the range |
| 74 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
| 75 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 76 | static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 77 | #endif |
| 78 | #ifdef COUNT_ALLOCS |
| 79 | int quick_int_allocs, quick_neg_int_allocs; |
| 80 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 82 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 83 | PyInt_FromLong(long ival) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 84 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 85 | register PyIntObject *v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 86 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 87 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 88 | v = small_ints[ival + NSMALLNEGINTS]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 89 | Py_INCREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 90 | #ifdef COUNT_ALLOCS |
| 91 | if (ival >= 0) |
| 92 | quick_int_allocs++; |
| 93 | else |
| 94 | quick_neg_int_allocs++; |
| 95 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 96 | return (PyObject *) v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 97 | } |
| 98 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 99 | if (free_list == NULL) { |
| 100 | if ((free_list = fill_free_list()) == NULL) |
| 101 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | } |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 103 | /* Inline PyObject_New */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 104 | v = free_list; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 105 | free_list = (PyIntObject *)v->ob_type; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 106 | PyObject_INIT(v, &PyInt_Type); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 107 | v->ob_ival = ival; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 108 | return (PyObject *) v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 111 | PyObject * |
| 112 | PyInt_FromSize_t(size_t ival) |
| 113 | { |
| 114 | if (ival <= LONG_MAX) |
| 115 | return PyInt_FromLong((long)ival); |
| 116 | return _PyLong_FromSize_t(ival); |
| 117 | } |
| 118 | |
| 119 | PyObject * |
| 120 | PyInt_FromSsize_t(Py_ssize_t ival) |
| 121 | { |
| 122 | if (ival >= LONG_MIN && ival <= LONG_MAX) |
| 123 | return PyInt_FromLong((long)ival); |
| 124 | return _PyLong_FromSsize_t(ival); |
| 125 | } |
| 126 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 127 | static void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 128 | int_dealloc(PyIntObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 129 | { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 130 | if (PyInt_CheckExact(v)) { |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 131 | v->ob_type = (struct _typeobject *)free_list; |
| 132 | free_list = v; |
| 133 | } |
| 134 | else |
| 135 | v->ob_type->tp_free((PyObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Guido van Rossum | 9364698 | 2002-04-26 00:53:34 +0000 | [diff] [blame] | 138 | static void |
| 139 | int_free(PyIntObject *v) |
| 140 | { |
| 141 | v->ob_type = (struct _typeobject *)free_list; |
| 142 | free_list = v; |
| 143 | } |
| 144 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 145 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 146 | PyInt_AsLong(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 148 | PyNumberMethods *nb; |
| 149 | PyIntObject *io; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 150 | long val; |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 151 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 152 | if (op && PyInt_Check(op)) |
| 153 | return PyInt_AS_LONG((PyIntObject*) op); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 155 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 156 | nb->nb_int == NULL) { |
Guido van Rossum | c18a6f4 | 2000-05-09 14:27:48 +0000 | [diff] [blame] | 157 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 158 | return -1; |
| 159 | } |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 161 | io = (PyIntObject*) (*nb->nb_int) (op); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 162 | if (io == NULL) |
| 163 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 164 | if (!PyInt_Check(io)) { |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 165 | if (PyLong_Check(io)) { |
| 166 | /* got a long? => retry int conversion */ |
| 167 | val = PyLong_AsLong((PyObject *)io); |
Thomas Heller | 850566b | 2003-02-20 20:32:11 +0000 | [diff] [blame] | 168 | Py_DECREF(io); |
| 169 | if ((val == -1) && PyErr_Occurred()) |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 170 | return -1; |
Thomas Heller | 850566b | 2003-02-20 20:32:11 +0000 | [diff] [blame] | 171 | return val; |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 172 | } |
| 173 | else |
| 174 | { |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 175 | Py_DECREF(io); |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 176 | PyErr_SetString(PyExc_TypeError, |
| 177 | "nb_int should return int object"); |
| 178 | return -1; |
| 179 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 180 | } |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 182 | val = PyInt_AS_LONG(io); |
| 183 | Py_DECREF(io); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 185 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 188 | Py_ssize_t |
| 189 | PyInt_AsSsize_t(register PyObject *op) |
| 190 | { |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 191 | #if SIZEOF_SIZE_T != SIZEOF_LONG |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 192 | PyNumberMethods *nb; |
| 193 | PyIntObject *io; |
| 194 | Py_ssize_t val; |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 195 | #endif |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 196 | if (op && !PyInt_CheckExact(op) && PyLong_Check(op)) |
| 197 | return _PyLong_AsSsize_t(op); |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 198 | #if SIZEOF_SIZE_T == SIZEOF_LONG |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 199 | return PyInt_AsLong(op); |
| 200 | #else |
| 201 | |
| 202 | if (op && PyInt_Check(op)) |
| 203 | return PyInt_AS_LONG((PyIntObject*) op); |
| 204 | |
| 205 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 206 | (nb->nb_int == NULL && nb->nb_long == 0)) { |
| 207 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | if (nb->nb_long != 0) { |
| 212 | io = (PyIntObject*) (*nb->nb_long) (op); |
| 213 | } else { |
| 214 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 215 | } |
| 216 | if (io == NULL) |
| 217 | return -1; |
| 218 | if (!PyInt_Check(io)) { |
| 219 | if (PyLong_Check(io)) { |
| 220 | /* got a long? => retry int conversion */ |
| 221 | val = _PyLong_AsSsize_t((PyObject *)io); |
| 222 | Py_DECREF(io); |
| 223 | if ((val == -1) && PyErr_Occurred()) |
| 224 | return -1; |
| 225 | return val; |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | Py_DECREF(io); |
| 230 | PyErr_SetString(PyExc_TypeError, |
| 231 | "nb_int should return int object"); |
| 232 | return -1; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | val = PyInt_AS_LONG(io); |
| 237 | Py_DECREF(io); |
| 238 | |
| 239 | return val; |
| 240 | #endif |
| 241 | } |
| 242 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 243 | unsigned long |
| 244 | PyInt_AsUnsignedLongMask(register PyObject *op) |
| 245 | { |
| 246 | PyNumberMethods *nb; |
| 247 | PyIntObject *io; |
| 248 | unsigned long val; |
| 249 | |
| 250 | if (op && PyInt_Check(op)) |
| 251 | return PyInt_AS_LONG((PyIntObject*) op); |
| 252 | if (op && PyLong_Check(op)) |
| 253 | return PyLong_AsUnsignedLongMask(op); |
| 254 | |
| 255 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 256 | nb->nb_int == NULL) { |
| 257 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 262 | if (io == NULL) |
| 263 | return -1; |
| 264 | if (!PyInt_Check(io)) { |
| 265 | if (PyLong_Check(io)) { |
| 266 | val = PyLong_AsUnsignedLongMask((PyObject *)io); |
| 267 | Py_DECREF(io); |
| 268 | if (PyErr_Occurred()) |
| 269 | return -1; |
| 270 | return val; |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | Py_DECREF(io); |
| 275 | PyErr_SetString(PyExc_TypeError, |
| 276 | "nb_int should return int object"); |
| 277 | return -1; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | val = PyInt_AS_LONG(io); |
| 282 | Py_DECREF(io); |
| 283 | |
| 284 | return val; |
| 285 | } |
| 286 | |
| 287 | #ifdef HAVE_LONG_LONG |
| 288 | unsigned PY_LONG_LONG |
| 289 | PyInt_AsUnsignedLongLongMask(register PyObject *op) |
| 290 | { |
| 291 | PyNumberMethods *nb; |
| 292 | PyIntObject *io; |
| 293 | unsigned PY_LONG_LONG val; |
| 294 | |
| 295 | if (op && PyInt_Check(op)) |
| 296 | return PyInt_AS_LONG((PyIntObject*) op); |
| 297 | if (op && PyLong_Check(op)) |
| 298 | return PyLong_AsUnsignedLongLongMask(op); |
| 299 | |
| 300 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 301 | nb->nb_int == NULL) { |
| 302 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 303 | return -1; |
| 304 | } |
| 305 | |
| 306 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 307 | if (io == NULL) |
| 308 | return -1; |
| 309 | if (!PyInt_Check(io)) { |
| 310 | if (PyLong_Check(io)) { |
| 311 | val = PyLong_AsUnsignedLongLongMask((PyObject *)io); |
| 312 | Py_DECREF(io); |
| 313 | if (PyErr_Occurred()) |
| 314 | return -1; |
| 315 | return val; |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | Py_DECREF(io); |
| 320 | PyErr_SetString(PyExc_TypeError, |
| 321 | "nb_int should return int object"); |
| 322 | return -1; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | val = PyInt_AS_LONG(io); |
| 327 | Py_DECREF(io); |
| 328 | |
| 329 | return val; |
| 330 | } |
| 331 | #endif |
| 332 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 333 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 334 | PyInt_FromString(char *s, char **pend, int base) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 335 | { |
| 336 | char *end; |
| 337 | long x; |
| 338 | char buffer[256]; /* For errors */ |
| 339 | |
| 340 | if ((base != 0 && base < 2) || base > 36) { |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 341 | PyErr_SetString(PyExc_ValueError, |
| 342 | "int() base must be >= 2 and <= 36"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | while (*s && isspace(Py_CHARMASK(*s))) |
| 347 | s++; |
| 348 | errno = 0; |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 349 | if (base == 0 && s[0] == '0') { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 350 | x = (long) PyOS_strtoul(s, &end, base); |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 351 | if (x < 0) |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 352 | return PyLong_FromString(s, pend, base); |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 353 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 354 | else |
| 355 | x = PyOS_strtol(s, &end, base); |
Martin v. Löwis | 2b6727b | 2001-03-06 12:12:02 +0000 | [diff] [blame] | 356 | if (end == s || !isalnum(Py_CHARMASK(end[-1]))) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 357 | goto bad; |
| 358 | while (*end && isspace(Py_CHARMASK(*end))) |
| 359 | end++; |
| 360 | if (*end != '\0') { |
| 361 | bad: |
Barry Warsaw | 6197509 | 2001-11-28 20:55:34 +0000 | [diff] [blame] | 362 | PyOS_snprintf(buffer, sizeof(buffer), |
| 363 | "invalid literal for int(): %.200s", s); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 364 | PyErr_SetString(PyExc_ValueError, buffer); |
| 365 | return NULL; |
| 366 | } |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 367 | else if (errno != 0) |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 368 | return PyLong_FromString(s, pend, base); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 369 | if (pend) |
| 370 | *pend = end; |
| 371 | return PyInt_FromLong(x); |
| 372 | } |
| 373 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 374 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 375 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 376 | PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 377 | { |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 378 | PyObject *result; |
| 379 | char *buffer = PyMem_MALLOC(length+1); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 380 | |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 381 | if (buffer == NULL) |
| 382 | return NULL; |
| 383 | |
| 384 | if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) { |
| 385 | PyMem_FREE(buffer); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 386 | return NULL; |
| 387 | } |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 388 | result = PyInt_FromString(buffer, NULL, base); |
| 389 | PyMem_FREE(buffer); |
| 390 | return result; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 391 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 392 | #endif |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 393 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 394 | /* Methods */ |
| 395 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 396 | /* Integers are seen as the "smallest" of all numeric types and thus |
| 397 | don't have any knowledge about conversion of other types to |
| 398 | integers. */ |
| 399 | |
| 400 | #define CONVERT_TO_LONG(obj, lng) \ |
| 401 | if (PyInt_Check(obj)) { \ |
| 402 | lng = PyInt_AS_LONG(obj); \ |
| 403 | } \ |
| 404 | else { \ |
| 405 | Py_INCREF(Py_NotImplemented); \ |
| 406 | return Py_NotImplemented; \ |
| 407 | } |
| 408 | |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 409 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 410 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 411 | int_print(PyIntObject *v, FILE *fp, int flags) |
| 412 | /* flags -- not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 413 | { |
| 414 | fprintf(fp, "%ld", v->ob_ival); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 415 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 418 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 419 | int_repr(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 420 | { |
Tim Peters | 4222104 | 2001-12-01 02:52:56 +0000 | [diff] [blame] | 421 | char buf[64]; |
Barry Warsaw | 6197509 | 2001-11-28 20:55:34 +0000 | [diff] [blame] | 422 | PyOS_snprintf(buf, sizeof(buf), "%ld", v->ob_ival); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 423 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 427 | int_compare(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 428 | { |
| 429 | register long i = v->ob_ival; |
| 430 | register long j = w->ob_ival; |
| 431 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 432 | } |
| 433 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 434 | static long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 435 | int_hash(PyIntObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 436 | { |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 437 | /* XXX If this is changed, you also need to change the way |
| 438 | Python's long, float and complex types are hashed. */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 439 | long x = v -> ob_ival; |
| 440 | if (x == -1) |
| 441 | x = -2; |
| 442 | return x; |
| 443 | } |
| 444 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 445 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 446 | int_add(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 447 | { |
| 448 | register long a, b, x; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 449 | CONVERT_TO_LONG(v, a); |
| 450 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 451 | x = a + b; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 452 | if ((x^a) >= 0 || (x^b) >= 0) |
| 453 | return PyInt_FromLong(x); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 454 | 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] | 455 | } |
| 456 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 457 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 458 | int_sub(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 459 | { |
| 460 | register long a, b, x; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 461 | CONVERT_TO_LONG(v, a); |
| 462 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 463 | x = a - b; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 464 | if ((x^a) >= 0 || (x^~b) >= 0) |
| 465 | return PyInt_FromLong(x); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 466 | return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v, |
| 467 | (PyObject *)w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 470 | /* |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 471 | Integer overflow checking for * is painful: Python tried a couple ways, but |
| 472 | they didn't work on all platforms, or failed in endcases (a product of |
| 473 | -sys.maxint-1 has been a particular pain). |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 474 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 475 | Here's another way: |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 476 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 477 | The native long product x*y is either exactly right or *way* off, being |
| 478 | just the last n bits of the true product, where n is the number of bits |
| 479 | in a long (the delivered product is the true product plus i*2**n for |
| 480 | some integer i). |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 481 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 482 | The native double product (double)x * (double)y is subject to three |
| 483 | rounding errors: on a sizeof(long)==8 box, each cast to double can lose |
| 484 | info, and even on a sizeof(long)==4 box, the multiplication can lose info. |
| 485 | But, unlike the native long product, it's not in *range* trouble: even |
| 486 | if sizeof(long)==32 (256-bit longs), the product easily fits in the |
| 487 | dynamic range of a double. So the leading 50 (or so) bits of the double |
| 488 | product are correct. |
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 | We check these two ways against each other, and declare victory if they're |
| 491 | approximately the same. Else, because the native long product is the only |
| 492 | one that can lose catastrophic amounts of information, it's the native long |
| 493 | product that must have overflowed. |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 494 | */ |
| 495 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 496 | static PyObject * |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 497 | int_mul(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 498 | { |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 499 | long a, b; |
| 500 | long longprod; /* a*b in native long arithmetic */ |
| 501 | double doubled_longprod; /* (double)longprod */ |
| 502 | double doubleprod; /* (double)a * (double)b */ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 503 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 504 | CONVERT_TO_LONG(v, a); |
| 505 | CONVERT_TO_LONG(w, b); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 506 | longprod = a * b; |
| 507 | doubleprod = (double)a * (double)b; |
| 508 | doubled_longprod = (double)longprod; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 509 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 510 | /* Fast path for normal case: small multiplicands, and no info |
| 511 | is lost in either method. */ |
| 512 | if (doubled_longprod == doubleprod) |
| 513 | return PyInt_FromLong(longprod); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 514 | |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 515 | /* Somebody somewhere lost info. Close enough, or way off? Note |
| 516 | that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0). |
| 517 | The difference either is or isn't significant compared to the |
| 518 | true value (of which doubleprod is a good approximation). |
| 519 | */ |
| 520 | { |
| 521 | const double diff = doubled_longprod - doubleprod; |
| 522 | const double absdiff = diff >= 0.0 ? diff : -diff; |
| 523 | const double absprod = doubleprod >= 0.0 ? doubleprod : |
| 524 | -doubleprod; |
| 525 | /* absdiff/absprod <= 1/32 iff |
| 526 | 32 * absdiff <= absprod -- 5 good bits is "close enough" */ |
| 527 | if (32.0 * absdiff <= absprod) |
| 528 | return PyInt_FromLong(longprod); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 529 | else |
| 530 | return PyLong_Type.tp_as_number->nb_multiply(v, w); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 531 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 534 | /* Return type of i_divmod */ |
| 535 | enum divmod_result { |
| 536 | DIVMOD_OK, /* Correct result */ |
| 537 | DIVMOD_OVERFLOW, /* Overflow, try again using longs */ |
| 538 | DIVMOD_ERROR /* Exception raised */ |
| 539 | }; |
| 540 | |
| 541 | static enum divmod_result |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 542 | i_divmod(register long x, register long y, |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 543 | long *p_xdivy, long *p_xmody) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 544 | { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 545 | long xdivy, xmody; |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 546 | |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 547 | if (y == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 548 | PyErr_SetString(PyExc_ZeroDivisionError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 549 | "integer division or modulo by zero"); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 550 | return DIVMOD_ERROR; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 551 | } |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 552 | /* (-sys.maxint-1)/-1 is the only overflow case. */ |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 553 | if (y == -1 && x < 0 && x == -x) |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 554 | return DIVMOD_OVERFLOW; |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 555 | xdivy = x / y; |
| 556 | xmody = x - xdivy * y; |
| 557 | /* If the signs of x and y differ, and the remainder is non-0, |
| 558 | * C89 doesn't define whether xdivy is now the floor or the |
| 559 | * ceiling of the infinitely precise quotient. We want the floor, |
| 560 | * and we have it iff the remainder's sign matches y's. |
| 561 | */ |
| 562 | if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) { |
| 563 | xmody += y; |
| 564 | --xdivy; |
| 565 | assert(xmody && ((y ^ xmody) >= 0)); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 566 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 567 | *p_xdivy = xdivy; |
| 568 | *p_xmody = xmody; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 569 | return DIVMOD_OK; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 572 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 573 | int_div(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 574 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 575 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 576 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 577 | CONVERT_TO_LONG(x, xi); |
| 578 | CONVERT_TO_LONG(y, yi); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 579 | switch (i_divmod(xi, yi, &d, &m)) { |
| 580 | case DIVMOD_OK: |
| 581 | return PyInt_FromLong(d); |
| 582 | case DIVMOD_OVERFLOW: |
| 583 | return PyLong_Type.tp_as_number->nb_divide((PyObject *)x, |
| 584 | (PyObject *)y); |
| 585 | default: |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 586 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 587 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 590 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 591 | int_classic_div(PyIntObject *x, PyIntObject *y) |
| 592 | { |
| 593 | long xi, yi; |
| 594 | long d, m; |
| 595 | CONVERT_TO_LONG(x, xi); |
| 596 | CONVERT_TO_LONG(y, yi); |
| 597 | if (Py_DivisionWarningFlag && |
| 598 | PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0) |
| 599 | return NULL; |
| 600 | switch (i_divmod(xi, yi, &d, &m)) { |
| 601 | case DIVMOD_OK: |
| 602 | return PyInt_FromLong(d); |
| 603 | case DIVMOD_OVERFLOW: |
| 604 | return PyLong_Type.tp_as_number->nb_divide((PyObject *)x, |
| 605 | (PyObject *)y); |
| 606 | default: |
| 607 | return NULL; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | static PyObject * |
Tim Peters | 9c1d7fd | 2001-09-04 05:52:47 +0000 | [diff] [blame] | 612 | int_true_divide(PyObject *v, PyObject *w) |
| 613 | { |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 614 | /* If they aren't both ints, give someone else a chance. In |
| 615 | particular, this lets int/long get handled by longs, which |
| 616 | underflows to 0 gracefully if the long is too big to convert |
| 617 | to float. */ |
| 618 | if (PyInt_Check(v) && PyInt_Check(w)) |
| 619 | return PyFloat_Type.tp_as_number->nb_true_divide(v, w); |
| 620 | Py_INCREF(Py_NotImplemented); |
| 621 | return Py_NotImplemented; |
Tim Peters | 9c1d7fd | 2001-09-04 05:52:47 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 625 | int_mod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 626 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 627 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 628 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 629 | CONVERT_TO_LONG(x, xi); |
| 630 | CONVERT_TO_LONG(y, yi); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 631 | switch (i_divmod(xi, yi, &d, &m)) { |
| 632 | case DIVMOD_OK: |
| 633 | return PyInt_FromLong(m); |
| 634 | case DIVMOD_OVERFLOW: |
| 635 | return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x, |
| 636 | (PyObject *)y); |
| 637 | default: |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 638 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 639 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 642 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 643 | int_divmod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 644 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 645 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 646 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 647 | CONVERT_TO_LONG(x, xi); |
| 648 | CONVERT_TO_LONG(y, yi); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 649 | switch (i_divmod(xi, yi, &d, &m)) { |
| 650 | case DIVMOD_OK: |
| 651 | return Py_BuildValue("(ll)", d, m); |
| 652 | case DIVMOD_OVERFLOW: |
| 653 | return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x, |
| 654 | (PyObject *)y); |
| 655 | default: |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 656 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 657 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 660 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 661 | int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 662 | { |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 663 | register long iv, iw, iz=0, ix, temp, prev; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 664 | CONVERT_TO_LONG(v, iv); |
| 665 | CONVERT_TO_LONG(w, iw); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 666 | if (iw < 0) { |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 667 | if ((PyObject *)z != Py_None) { |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 668 | PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " |
| 669 | "cannot be negative when 3rd argument specified"); |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 670 | return NULL; |
| 671 | } |
Guido van Rossum | b82fedc | 2001-07-12 11:19:45 +0000 | [diff] [blame] | 672 | /* Return a float. This works because we know that |
| 673 | this calls float_pow() which converts its |
| 674 | arguments to double. */ |
| 675 | return PyFloat_Type.tp_as_number->nb_power( |
| 676 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 677 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 678 | if ((PyObject *)z != Py_None) { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 679 | CONVERT_TO_LONG(z, iz); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 680 | if (iz == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 681 | PyErr_SetString(PyExc_ValueError, |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 682 | "pow() 3rd argument cannot be 0"); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 683 | return NULL; |
| 684 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 685 | } |
| 686 | /* |
| 687 | * XXX: The original exponentiation code stopped looping |
| 688 | * when temp hit zero; this code will continue onwards |
| 689 | * unnecessarily, but at least it won't cause any errors. |
| 690 | * Hopefully the speed improvement from the fast exponentiation |
| 691 | * will compensate for the slight inefficiency. |
| 692 | * XXX: Better handling of overflows is desperately needed. |
| 693 | */ |
| 694 | temp = iv; |
| 695 | ix = 1; |
| 696 | while (iw > 0) { |
| 697 | prev = ix; /* Save value for overflow check */ |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 698 | if (iw & 1) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 699 | ix = ix*temp; |
| 700 | if (temp == 0) |
| 701 | break; /* Avoid ix / 0 */ |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 702 | if (ix / temp != prev) { |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 703 | return PyLong_Type.tp_as_number->nb_power( |
| 704 | (PyObject *)v, |
| 705 | (PyObject *)w, |
Tim Peters | 31960db | 2001-08-23 21:28:33 +0000 | [diff] [blame] | 706 | (PyObject *)z); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 707 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 708 | } |
| 709 | iw >>= 1; /* Shift exponent down by 1 bit */ |
| 710 | if (iw==0) break; |
| 711 | prev = temp; |
| 712 | temp *= temp; /* Square the value of temp */ |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 713 | if (prev != 0 && temp / prev != prev) { |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 714 | return PyLong_Type.tp_as_number->nb_power( |
| 715 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
| 716 | } |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 717 | if (iz) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 718 | /* If we did a multiplication, perform a modulo */ |
| 719 | ix = ix % iz; |
| 720 | temp = temp % iz; |
| 721 | } |
| 722 | } |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 723 | if (iz) { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 724 | long div, mod; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 725 | switch (i_divmod(ix, iz, &div, &mod)) { |
| 726 | case DIVMOD_OK: |
| 727 | ix = mod; |
| 728 | break; |
| 729 | case DIVMOD_OVERFLOW: |
| 730 | return PyLong_Type.tp_as_number->nb_power( |
| 731 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
| 732 | default: |
| 733 | return NULL; |
| 734 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 735 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 736 | return PyInt_FromLong(ix); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 737 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 738 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 739 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 740 | int_neg(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 741 | { |
| 742 | register long a, x; |
| 743 | a = v->ob_ival; |
| 744 | x = -a; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 745 | if (a < 0 && x < 0) { |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 746 | PyObject *o = PyLong_FromLong(a); |
Neal Norwitz | fa56e2d | 2003-01-19 15:40:09 +0000 | [diff] [blame] | 747 | if (o != NULL) { |
| 748 | PyObject *result = PyNumber_Negative(o); |
| 749 | Py_DECREF(o); |
| 750 | return result; |
| 751 | } |
| 752 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 753 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 754 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 757 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 758 | int_pos(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 759 | { |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 760 | if (PyInt_CheckExact(v)) { |
| 761 | Py_INCREF(v); |
| 762 | return (PyObject *)v; |
| 763 | } |
| 764 | else |
| 765 | return PyInt_FromLong(v->ob_ival); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 768 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 769 | int_abs(PyIntObject *v) |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 770 | { |
| 771 | if (v->ob_ival >= 0) |
| 772 | return int_pos(v); |
| 773 | else |
| 774 | return int_neg(v); |
| 775 | } |
| 776 | |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 777 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 778 | int_nonzero(PyIntObject *v) |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 779 | { |
| 780 | return v->ob_ival != 0; |
| 781 | } |
| 782 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 783 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 784 | int_invert(PyIntObject *v) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 785 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 786 | return PyInt_FromLong(~v->ob_ival); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 789 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 790 | int_lshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 791 | { |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 792 | long a, b, c; |
Raymond Hettinger | a006c37 | 2004-06-26 23:22:57 +0000 | [diff] [blame] | 793 | PyObject *vv, *ww, *result; |
| 794 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 795 | CONVERT_TO_LONG(v, a); |
| 796 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 797 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 798 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 799 | return NULL; |
| 800 | } |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 801 | if (a == 0 || b == 0) |
| 802 | return int_pos(v); |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 803 | if (b >= LONG_BIT) { |
Raymond Hettinger | a006c37 | 2004-06-26 23:22:57 +0000 | [diff] [blame] | 804 | vv = PyLong_FromLong(PyInt_AS_LONG(v)); |
| 805 | if (vv == NULL) |
| 806 | return NULL; |
| 807 | ww = PyLong_FromLong(PyInt_AS_LONG(w)); |
| 808 | if (ww == NULL) { |
| 809 | Py_DECREF(vv); |
| 810 | return NULL; |
| 811 | } |
| 812 | result = PyNumber_Lshift(vv, ww); |
| 813 | Py_DECREF(vv); |
| 814 | Py_DECREF(ww); |
| 815 | return result; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 816 | } |
Tim Peters | da1a221 | 2002-08-11 17:54:42 +0000 | [diff] [blame] | 817 | c = a << b; |
| 818 | if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) { |
Raymond Hettinger | a006c37 | 2004-06-26 23:22:57 +0000 | [diff] [blame] | 819 | vv = PyLong_FromLong(PyInt_AS_LONG(v)); |
| 820 | if (vv == NULL) |
| 821 | return NULL; |
| 822 | ww = PyLong_FromLong(PyInt_AS_LONG(w)); |
| 823 | if (ww == NULL) { |
| 824 | Py_DECREF(vv); |
| 825 | return NULL; |
| 826 | } |
| 827 | result = PyNumber_Lshift(vv, ww); |
| 828 | Py_DECREF(vv); |
| 829 | Py_DECREF(ww); |
| 830 | return result; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 831 | } |
| 832 | return PyInt_FromLong(c); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 835 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 836 | int_rshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 837 | { |
| 838 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 839 | CONVERT_TO_LONG(v, a); |
| 840 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 841 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 842 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 843 | return NULL; |
| 844 | } |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 845 | if (a == 0 || b == 0) |
| 846 | return int_pos(v); |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 847 | if (b >= LONG_BIT) { |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 848 | if (a < 0) |
| 849 | a = -1; |
| 850 | else |
| 851 | a = 0; |
| 852 | } |
| 853 | else { |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 854 | a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 855 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 856 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 859 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 860 | int_and(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 861 | { |
| 862 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 863 | CONVERT_TO_LONG(v, a); |
| 864 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 865 | return PyInt_FromLong(a & b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 868 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 869 | int_xor(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 870 | { |
| 871 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 872 | CONVERT_TO_LONG(v, a); |
| 873 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 874 | return PyInt_FromLong(a ^ b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 877 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 878 | int_or(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 879 | { |
| 880 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 881 | CONVERT_TO_LONG(v, a); |
| 882 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 883 | return PyInt_FromLong(a | b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 886 | static int |
| 887 | int_coerce(PyObject **pv, PyObject **pw) |
| 888 | { |
| 889 | if (PyInt_Check(*pw)) { |
| 890 | Py_INCREF(*pv); |
| 891 | Py_INCREF(*pw); |
| 892 | return 0; |
| 893 | } |
| 894 | return 1; /* Can't do it */ |
| 895 | } |
| 896 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 897 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 898 | int_int(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 899 | { |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 900 | if (PyInt_CheckExact(v)) |
| 901 | Py_INCREF(v); |
| 902 | else |
| 903 | v = (PyIntObject *)PyInt_FromLong(v->ob_ival); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 904 | return (PyObject *)v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 907 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 908 | int_long(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 909 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 910 | return PyLong_FromLong((v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 913 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 914 | int_float(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 915 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 916 | return PyFloat_FromDouble((double)(v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 919 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 920 | int_oct(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 921 | { |
Guido van Rossum | 6f72f97 | 1997-01-14 15:43:41 +0000 | [diff] [blame] | 922 | char buf[100]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 923 | long x = v -> ob_ival; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 924 | if (x < 0) |
| 925 | PyOS_snprintf(buf, sizeof(buf), "-0%lo", -x); |
| 926 | else if (x == 0) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 927 | strcpy(buf, "0"); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 928 | else |
Barry Warsaw | 6197509 | 2001-11-28 20:55:34 +0000 | [diff] [blame] | 929 | PyOS_snprintf(buf, sizeof(buf), "0%lo", x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 930 | return PyString_FromString(buf); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 933 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 934 | int_hex(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 935 | { |
Guido van Rossum | 6f72f97 | 1997-01-14 15:43:41 +0000 | [diff] [blame] | 936 | char buf[100]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 937 | long x = v -> ob_ival; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 938 | if (x < 0) |
| 939 | PyOS_snprintf(buf, sizeof(buf), "-0x%lx", -x); |
| 940 | else |
| 941 | PyOS_snprintf(buf, sizeof(buf), "0x%lx", x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 942 | return PyString_FromString(buf); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 945 | static PyObject * |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 946 | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 947 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 948 | static PyObject * |
| 949 | int_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 950 | { |
| 951 | PyObject *x = NULL; |
| 952 | int base = -909; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 953 | static char *kwlist[] = {"x", "base", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 954 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 955 | if (type != &PyInt_Type) |
| 956 | return int_subtype_new(type, args, kwds); /* Wimp out */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 957 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, |
| 958 | &x, &base)) |
| 959 | return NULL; |
| 960 | if (x == NULL) |
| 961 | return PyInt_FromLong(0L); |
| 962 | if (base == -909) |
| 963 | return PyNumber_Int(x); |
| 964 | if (PyString_Check(x)) |
| 965 | return PyInt_FromString(PyString_AS_STRING(x), NULL, base); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 966 | #ifdef Py_USING_UNICODE |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 967 | if (PyUnicode_Check(x)) |
| 968 | return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x), |
| 969 | PyUnicode_GET_SIZE(x), |
| 970 | base); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 971 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 972 | PyErr_SetString(PyExc_TypeError, |
| 973 | "int() can't convert non-string with explicit base"); |
| 974 | return NULL; |
| 975 | } |
| 976 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 977 | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
| 978 | first create a regular int from whatever arguments we got, |
| 979 | then allocate a subtype instance and initialize its ob_ival |
| 980 | from the regular int. The regular int is then thrown away. |
| 981 | */ |
| 982 | static PyObject * |
| 983 | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 984 | { |
| 985 | PyObject *tmp, *new; |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 986 | long ival; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 987 | |
| 988 | assert(PyType_IsSubtype(type, &PyInt_Type)); |
| 989 | tmp = int_new(&PyInt_Type, args, kwds); |
| 990 | if (tmp == NULL) |
| 991 | return NULL; |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 992 | if (!PyInt_Check(tmp)) { |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 993 | ival = PyLong_AsLong(tmp); |
Michael W. Hudson | 71665dc | 2003-08-11 17:32:02 +0000 | [diff] [blame] | 994 | if (ival == -1 && PyErr_Occurred()) { |
| 995 | Py_DECREF(tmp); |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 996 | return NULL; |
Michael W. Hudson | 71665dc | 2003-08-11 17:32:02 +0000 | [diff] [blame] | 997 | } |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 998 | } else { |
| 999 | ival = ((PyIntObject *)tmp)->ob_ival; |
| 1000 | } |
| 1001 | |
Guido van Rossum | d93dce1 | 2001-08-30 03:09:31 +0000 | [diff] [blame] | 1002 | new = type->tp_alloc(type, 0); |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 1003 | if (new == NULL) { |
| 1004 | Py_DECREF(tmp); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1005 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 1006 | } |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1007 | ((PyIntObject *)new)->ob_ival = ival; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1008 | Py_DECREF(tmp); |
| 1009 | return new; |
| 1010 | } |
| 1011 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1012 | static PyObject * |
| 1013 | int_getnewargs(PyIntObject *v) |
| 1014 | { |
| 1015 | return Py_BuildValue("(l)", v->ob_ival); |
| 1016 | } |
| 1017 | |
| 1018 | static PyMethodDef int_methods[] = { |
| 1019 | {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS}, |
| 1020 | {NULL, NULL} /* sentinel */ |
| 1021 | }; |
| 1022 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1023 | PyDoc_STRVAR(int_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1024 | "int(x[, base]) -> integer\n\ |
| 1025 | \n\ |
| 1026 | Convert a string or number to an integer, if possible. A floating point\n\ |
| 1027 | argument will be truncated towards zero (this does not include a string\n\ |
| 1028 | representation of a floating point number!) When converting a string, use\n\ |
| 1029 | the optional base. It is an error to supply a base when converting a\n\ |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 1030 | non-string. If the argument is outside the integer range a long object\n\ |
| 1031 | will be returned instead."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1032 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1033 | static PyNumberMethods int_as_number = { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1034 | (binaryfunc)int_add, /*nb_add*/ |
| 1035 | (binaryfunc)int_sub, /*nb_subtract*/ |
| 1036 | (binaryfunc)int_mul, /*nb_multiply*/ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 1037 | (binaryfunc)int_classic_div, /*nb_divide*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1038 | (binaryfunc)int_mod, /*nb_remainder*/ |
| 1039 | (binaryfunc)int_divmod, /*nb_divmod*/ |
| 1040 | (ternaryfunc)int_pow, /*nb_power*/ |
| 1041 | (unaryfunc)int_neg, /*nb_negative*/ |
| 1042 | (unaryfunc)int_pos, /*nb_positive*/ |
| 1043 | (unaryfunc)int_abs, /*nb_absolute*/ |
| 1044 | (inquiry)int_nonzero, /*nb_nonzero*/ |
| 1045 | (unaryfunc)int_invert, /*nb_invert*/ |
| 1046 | (binaryfunc)int_lshift, /*nb_lshift*/ |
| 1047 | (binaryfunc)int_rshift, /*nb_rshift*/ |
| 1048 | (binaryfunc)int_and, /*nb_and*/ |
| 1049 | (binaryfunc)int_xor, /*nb_xor*/ |
| 1050 | (binaryfunc)int_or, /*nb_or*/ |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 1051 | int_coerce, /*nb_coerce*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1052 | (unaryfunc)int_int, /*nb_int*/ |
| 1053 | (unaryfunc)int_long, /*nb_long*/ |
| 1054 | (unaryfunc)int_float, /*nb_float*/ |
| 1055 | (unaryfunc)int_oct, /*nb_oct*/ |
| 1056 | (unaryfunc)int_hex, /*nb_hex*/ |
| 1057 | 0, /*nb_inplace_add*/ |
| 1058 | 0, /*nb_inplace_subtract*/ |
| 1059 | 0, /*nb_inplace_multiply*/ |
| 1060 | 0, /*nb_inplace_divide*/ |
| 1061 | 0, /*nb_inplace_remainder*/ |
| 1062 | 0, /*nb_inplace_power*/ |
| 1063 | 0, /*nb_inplace_lshift*/ |
| 1064 | 0, /*nb_inplace_rshift*/ |
| 1065 | 0, /*nb_inplace_and*/ |
| 1066 | 0, /*nb_inplace_xor*/ |
| 1067 | 0, /*nb_inplace_or*/ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1068 | (binaryfunc)int_div, /* nb_floor_divide */ |
| 1069 | int_true_divide, /* nb_true_divide */ |
| 1070 | 0, /* nb_inplace_floor_divide */ |
| 1071 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1072 | }; |
| 1073 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1074 | PyTypeObject PyInt_Type = { |
| 1075 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1076 | 0, |
| 1077 | "int", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1078 | sizeof(PyIntObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1079 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1080 | (destructor)int_dealloc, /* tp_dealloc */ |
| 1081 | (printfunc)int_print, /* tp_print */ |
| 1082 | 0, /* tp_getattr */ |
| 1083 | 0, /* tp_setattr */ |
| 1084 | (cmpfunc)int_compare, /* tp_compare */ |
| 1085 | (reprfunc)int_repr, /* tp_repr */ |
| 1086 | &int_as_number, /* tp_as_number */ |
| 1087 | 0, /* tp_as_sequence */ |
| 1088 | 0, /* tp_as_mapping */ |
| 1089 | (hashfunc)int_hash, /* tp_hash */ |
| 1090 | 0, /* tp_call */ |
Guido van Rossum | e75bfde | 2002-02-01 15:34:10 +0000 | [diff] [blame] | 1091 | (reprfunc)int_repr, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1092 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1093 | 0, /* tp_setattro */ |
| 1094 | 0, /* tp_as_buffer */ |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1095 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 1096 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1097 | int_doc, /* tp_doc */ |
| 1098 | 0, /* tp_traverse */ |
| 1099 | 0, /* tp_clear */ |
| 1100 | 0, /* tp_richcompare */ |
| 1101 | 0, /* tp_weaklistoffset */ |
| 1102 | 0, /* tp_iter */ |
| 1103 | 0, /* tp_iternext */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1104 | int_methods, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1105 | 0, /* tp_members */ |
| 1106 | 0, /* tp_getset */ |
| 1107 | 0, /* tp_base */ |
| 1108 | 0, /* tp_dict */ |
| 1109 | 0, /* tp_descr_get */ |
| 1110 | 0, /* tp_descr_set */ |
| 1111 | 0, /* tp_dictoffset */ |
| 1112 | 0, /* tp_init */ |
| 1113 | 0, /* tp_alloc */ |
| 1114 | int_new, /* tp_new */ |
Guido van Rossum | 9364698 | 2002-04-26 00:53:34 +0000 | [diff] [blame] | 1115 | (freefunc)int_free, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1116 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1117 | |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1118 | int |
Neal Norwitz | b2501f4 | 2002-12-31 03:42:13 +0000 | [diff] [blame] | 1119 | _PyInt_Init(void) |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1120 | { |
| 1121 | PyIntObject *v; |
| 1122 | int ival; |
| 1123 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 1124 | for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) { |
Raymond Hettinger | b32e640 | 2004-02-08 18:54:37 +0000 | [diff] [blame] | 1125 | if (!free_list && (free_list = fill_free_list()) == NULL) |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1126 | return 0; |
| 1127 | /* PyObject_New is inlined */ |
| 1128 | v = free_list; |
| 1129 | free_list = (PyIntObject *)v->ob_type; |
| 1130 | PyObject_INIT(v, &PyInt_Type); |
| 1131 | v->ob_ival = ival; |
| 1132 | small_ints[ival + NSMALLNEGINTS] = v; |
| 1133 | } |
| 1134 | #endif |
| 1135 | return 1; |
| 1136 | } |
| 1137 | |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1138 | void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 1139 | PyInt_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1140 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1141 | PyIntObject *p; |
| 1142 | PyIntBlock *list, *next; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1143 | int i; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1144 | int bc, bf; /* block count, number of freed blocks */ |
| 1145 | int irem, isum; /* remaining unfreed ints per block, total */ |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1146 | |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1147 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 1148 | PyIntObject **q; |
| 1149 | |
| 1150 | i = NSMALLNEGINTS + NSMALLPOSINTS; |
| 1151 | q = small_ints; |
| 1152 | while (--i >= 0) { |
| 1153 | Py_XDECREF(*q); |
| 1154 | *q++ = NULL; |
| 1155 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1156 | #endif |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1157 | bc = 0; |
| 1158 | bf = 0; |
| 1159 | isum = 0; |
| 1160 | list = block_list; |
| 1161 | block_list = NULL; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1162 | free_list = NULL; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1163 | while (list != NULL) { |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1164 | bc++; |
| 1165 | irem = 0; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1166 | for (i = 0, p = &list->objects[0]; |
| 1167 | i < N_INTOBJECTS; |
| 1168 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1169 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1170 | irem++; |
| 1171 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1172 | next = list->next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1173 | if (irem) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1174 | list->next = block_list; |
| 1175 | block_list = list; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1176 | for (i = 0, p = &list->objects[0]; |
| 1177 | i < N_INTOBJECTS; |
| 1178 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1179 | if (!PyInt_CheckExact(p) || |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1180 | p->ob_refcnt == 0) { |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1181 | p->ob_type = (struct _typeobject *) |
| 1182 | free_list; |
| 1183 | free_list = p; |
| 1184 | } |
| 1185 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 1186 | else if (-NSMALLNEGINTS <= p->ob_ival && |
| 1187 | p->ob_ival < NSMALLPOSINTS && |
| 1188 | small_ints[p->ob_ival + |
| 1189 | NSMALLNEGINTS] == NULL) { |
| 1190 | Py_INCREF(p); |
| 1191 | small_ints[p->ob_ival + |
| 1192 | NSMALLNEGINTS] = p; |
| 1193 | } |
| 1194 | #endif |
| 1195 | } |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1196 | } |
| 1197 | else { |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 1198 | PyMem_FREE(list); |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1199 | bf++; |
| 1200 | } |
| 1201 | isum += irem; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1202 | list = next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1203 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1204 | if (!Py_VerboseFlag) |
| 1205 | return; |
| 1206 | fprintf(stderr, "# cleanup ints"); |
| 1207 | if (!isum) { |
| 1208 | fprintf(stderr, "\n"); |
| 1209 | } |
| 1210 | else { |
| 1211 | fprintf(stderr, |
| 1212 | ": %d unfreed int%s in %d out of %d block%s\n", |
| 1213 | isum, isum == 1 ? "" : "s", |
| 1214 | bc - bf, bc, bc == 1 ? "" : "s"); |
| 1215 | } |
| 1216 | if (Py_VerboseFlag > 1) { |
| 1217 | list = block_list; |
| 1218 | while (list != NULL) { |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1219 | for (i = 0, p = &list->objects[0]; |
| 1220 | i < N_INTOBJECTS; |
| 1221 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1222 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1223 | fprintf(stderr, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 1224 | "# <int at %p, refcnt=%d, val=%ld>\n", |
| 1225 | p, p->ob_refcnt, p->ob_ival); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1226 | } |
| 1227 | list = list->next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1228 | } |
| 1229 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1230 | } |