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