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