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