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