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 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 7 | static PyObject *int_int(PyIntObject *v); |
| 8 | |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 9 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 10 | PyInt_GetMax(void) |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 11 | { |
| 12 | return LONG_MAX; /* To initialize sys.maxint */ |
| 13 | } |
| 14 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 15 | /* Integers are quite normal objects, to make object handling uniform. |
| 16 | (Using odd pointers to represent integers would save much space |
| 17 | but require extra checks for this special case throughout the code.) |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 18 | Since a typical Python program spends much of its time allocating |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 19 | and deallocating integers, these operations should be very fast. |
| 20 | Therefore we use a dedicated allocation scheme with a much lower |
| 21 | overhead (in space and time) than straight malloc(): a simple |
| 22 | dedicated free list, filled when necessary with memory from malloc(). |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 23 | |
| 24 | block_list is a singly-linked list of all PyIntBlocks ever allocated, |
| 25 | linked via their next members. PyIntBlocks are never returned to the |
| 26 | system before shutdown (PyInt_Fini). |
| 27 | |
| 28 | free_list is a singly-linked list of available PyIntObjects, linked |
| 29 | via abuse of their ob_type members. |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 33 | #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
| 34 | #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject)) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 36 | struct _intblock { |
| 37 | struct _intblock *next; |
| 38 | PyIntObject objects[N_INTOBJECTS]; |
| 39 | }; |
| 40 | |
| 41 | typedef struct _intblock PyIntBlock; |
| 42 | |
| 43 | static PyIntBlock *block_list = NULL; |
| 44 | static PyIntObject *free_list = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 46 | static PyIntObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 47 | fill_free_list(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 48 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 49 | PyIntObject *p, *q; |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 50 | /* Python's object allocator isn't appropriate for large blocks. */ |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 51 | p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 52 | if (p == NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 53 | return (PyIntObject *) PyErr_NoMemory(); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 54 | ((PyIntBlock *)p)->next = block_list; |
| 55 | block_list = (PyIntBlock *)p; |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 56 | /* Link the int objects together, from rear to front, then return |
| 57 | the address of the last int object in the block. */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 58 | p = &((PyIntBlock *)p)->objects[0]; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 59 | q = p + N_INTOBJECTS; |
| 60 | while (--q > p) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 61 | Py_TYPE(q) = (struct _typeobject *)(q-1); |
| 62 | Py_TYPE(q) = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 63 | return p + N_INTOBJECTS - 1; |
| 64 | } |
| 65 | |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 66 | #ifndef NSMALLPOSINTS |
Georg Brandl | 418a1ef | 2006-02-22 11:30:06 +0000 | [diff] [blame] | 67 | #define NSMALLPOSINTS 257 |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 68 | #endif |
| 69 | #ifndef NSMALLNEGINTS |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 70 | #define NSMALLNEGINTS 5 |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 71 | #endif |
| 72 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 73 | /* References to small integers are saved in this array so that they |
| 74 | can be shared. |
| 75 | The integers that are saved are those in the range |
| 76 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
| 77 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 79 | #endif |
| 80 | #ifdef COUNT_ALLOCS |
| 81 | int quick_int_allocs, quick_neg_int_allocs; |
| 82 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 84 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 85 | PyInt_FromLong(long ival) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 87 | register PyIntObject *v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 88 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 89 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 90 | v = small_ints[ival + NSMALLNEGINTS]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 91 | Py_INCREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 92 | #ifdef COUNT_ALLOCS |
| 93 | if (ival >= 0) |
| 94 | quick_int_allocs++; |
| 95 | else |
| 96 | quick_neg_int_allocs++; |
| 97 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 98 | return (PyObject *) v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 99 | } |
| 100 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 101 | if (free_list == NULL) { |
| 102 | if ((free_list = fill_free_list()) == NULL) |
| 103 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | } |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 105 | /* Inline PyObject_New */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 106 | v = free_list; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 107 | free_list = (PyIntObject *)Py_TYPE(v); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 108 | PyObject_INIT(v, &PyInt_Type); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 109 | v->ob_ival = ival; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 110 | return (PyObject *) v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 113 | PyObject * |
| 114 | PyInt_FromSize_t(size_t ival) |
| 115 | { |
| 116 | if (ival <= LONG_MAX) |
| 117 | return PyInt_FromLong((long)ival); |
| 118 | return _PyLong_FromSize_t(ival); |
| 119 | } |
| 120 | |
| 121 | PyObject * |
| 122 | PyInt_FromSsize_t(Py_ssize_t ival) |
| 123 | { |
| 124 | if (ival >= LONG_MIN && ival <= LONG_MAX) |
| 125 | return PyInt_FromLong((long)ival); |
| 126 | return _PyLong_FromSsize_t(ival); |
| 127 | } |
| 128 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 129 | static void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 130 | int_dealloc(PyIntObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 131 | { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 132 | if (PyInt_CheckExact(v)) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 133 | Py_TYPE(v) = (struct _typeobject *)free_list; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 134 | free_list = v; |
| 135 | } |
| 136 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 137 | Py_TYPE(v)->tp_free((PyObject *)v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Guido van Rossum | 9364698 | 2002-04-26 00:53:34 +0000 | [diff] [blame] | 140 | static void |
| 141 | int_free(PyIntObject *v) |
| 142 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 143 | Py_TYPE(v) = (struct _typeobject *)free_list; |
Guido van Rossum | 9364698 | 2002-04-26 00:53:34 +0000 | [diff] [blame] | 144 | free_list = v; |
| 145 | } |
| 146 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 148 | PyInt_AsLong(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 150 | PyNumberMethods *nb; |
| 151 | PyIntObject *io; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 152 | long val; |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 154 | if (op && PyInt_Check(op)) |
| 155 | return PyInt_AS_LONG((PyIntObject*) op); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 156 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 157 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 158 | nb->nb_int == NULL) { |
Guido van Rossum | c18a6f4 | 2000-05-09 14:27:48 +0000 | [diff] [blame] | 159 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 160 | return -1; |
| 161 | } |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 163 | io = (PyIntObject*) (*nb->nb_int) (op); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 164 | if (io == NULL) |
| 165 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 166 | if (!PyInt_Check(io)) { |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 167 | if (PyLong_Check(io)) { |
| 168 | /* got a long? => retry int conversion */ |
| 169 | val = PyLong_AsLong((PyObject *)io); |
Thomas Heller | 850566b | 2003-02-20 20:32:11 +0000 | [diff] [blame] | 170 | Py_DECREF(io); |
| 171 | if ((val == -1) && PyErr_Occurred()) |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 172 | return -1; |
Thomas Heller | 850566b | 2003-02-20 20:32:11 +0000 | [diff] [blame] | 173 | return val; |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 174 | } |
| 175 | else |
| 176 | { |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 177 | Py_DECREF(io); |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 178 | PyErr_SetString(PyExc_TypeError, |
| 179 | "nb_int should return int object"); |
| 180 | return -1; |
| 181 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 182 | } |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 183 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 184 | val = PyInt_AS_LONG(io); |
| 185 | Py_DECREF(io); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 187 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 190 | Py_ssize_t |
| 191 | PyInt_AsSsize_t(register PyObject *op) |
| 192 | { |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 193 | #if SIZEOF_SIZE_T != SIZEOF_LONG |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 194 | PyNumberMethods *nb; |
| 195 | PyIntObject *io; |
| 196 | Py_ssize_t val; |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 197 | #endif |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 198 | |
| 199 | if (op == NULL) { |
| 200 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | if (PyInt_Check(op)) |
| 205 | return PyInt_AS_LONG((PyIntObject*) op); |
| 206 | if (PyLong_Check(op)) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 207 | return _PyLong_AsSsize_t(op); |
Thomas Wouters | b1410fb | 2006-02-15 23:08:56 +0000 | [diff] [blame] | 208 | #if SIZEOF_SIZE_T == SIZEOF_LONG |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 209 | return PyInt_AsLong(op); |
| 210 | #else |
| 211 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 212 | if ((nb = Py_TYPE(op)->tp_as_number) == NULL || |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 213 | (nb->nb_int == NULL && nb->nb_long == 0)) { |
| 214 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 215 | return -1; |
| 216 | } |
| 217 | |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 218 | if (nb->nb_long != 0) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 219 | io = (PyIntObject*) (*nb->nb_long) (op); |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 220 | else |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 221 | io = (PyIntObject*) (*nb->nb_int) (op); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 222 | if (io == NULL) |
| 223 | return -1; |
| 224 | if (!PyInt_Check(io)) { |
| 225 | if (PyLong_Check(io)) { |
| 226 | /* got a long? => retry int conversion */ |
| 227 | val = _PyLong_AsSsize_t((PyObject *)io); |
| 228 | Py_DECREF(io); |
| 229 | if ((val == -1) && PyErr_Occurred()) |
| 230 | return -1; |
| 231 | return val; |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | Py_DECREF(io); |
| 236 | PyErr_SetString(PyExc_TypeError, |
| 237 | "nb_int should return int object"); |
| 238 | return -1; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | val = PyInt_AS_LONG(io); |
| 243 | Py_DECREF(io); |
| 244 | |
| 245 | return val; |
| 246 | #endif |
| 247 | } |
| 248 | |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 249 | unsigned long |
| 250 | PyInt_AsUnsignedLongMask(register PyObject *op) |
| 251 | { |
| 252 | PyNumberMethods *nb; |
| 253 | PyIntObject *io; |
| 254 | unsigned long val; |
| 255 | |
| 256 | if (op && PyInt_Check(op)) |
| 257 | return PyInt_AS_LONG((PyIntObject*) op); |
| 258 | if (op && PyLong_Check(op)) |
| 259 | return PyLong_AsUnsignedLongMask(op); |
| 260 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 261 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 262 | nb->nb_int == NULL) { |
| 263 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 264 | return (unsigned long)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 268 | if (io == NULL) |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 269 | return (unsigned long)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 270 | if (!PyInt_Check(io)) { |
| 271 | if (PyLong_Check(io)) { |
| 272 | val = PyLong_AsUnsignedLongMask((PyObject *)io); |
| 273 | Py_DECREF(io); |
| 274 | if (PyErr_Occurred()) |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 275 | return (unsigned long)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 276 | return val; |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | Py_DECREF(io); |
| 281 | PyErr_SetString(PyExc_TypeError, |
| 282 | "nb_int should return int object"); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 283 | return (unsigned long)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | val = PyInt_AS_LONG(io); |
| 288 | Py_DECREF(io); |
| 289 | |
| 290 | return val; |
| 291 | } |
| 292 | |
| 293 | #ifdef HAVE_LONG_LONG |
| 294 | unsigned PY_LONG_LONG |
| 295 | PyInt_AsUnsignedLongLongMask(register PyObject *op) |
| 296 | { |
| 297 | PyNumberMethods *nb; |
| 298 | PyIntObject *io; |
| 299 | unsigned PY_LONG_LONG val; |
| 300 | |
| 301 | if (op && PyInt_Check(op)) |
| 302 | return PyInt_AS_LONG((PyIntObject*) op); |
| 303 | if (op && PyLong_Check(op)) |
| 304 | return PyLong_AsUnsignedLongLongMask(op); |
| 305 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 306 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 307 | nb->nb_int == NULL) { |
| 308 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 309 | return (unsigned PY_LONG_LONG)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | io = (PyIntObject*) (*nb->nb_int) (op); |
| 313 | if (io == NULL) |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 314 | return (unsigned PY_LONG_LONG)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 315 | if (!PyInt_Check(io)) { |
| 316 | if (PyLong_Check(io)) { |
| 317 | val = PyLong_AsUnsignedLongLongMask((PyObject *)io); |
| 318 | Py_DECREF(io); |
| 319 | if (PyErr_Occurred()) |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 320 | return (unsigned PY_LONG_LONG)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 321 | return val; |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | Py_DECREF(io); |
| 326 | PyErr_SetString(PyExc_TypeError, |
| 327 | "nb_int should return int object"); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 328 | return (unsigned PY_LONG_LONG)-1; |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | val = PyInt_AS_LONG(io); |
| 333 | Py_DECREF(io); |
| 334 | |
| 335 | return val; |
| 336 | } |
| 337 | #endif |
| 338 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 339 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 340 | PyInt_FromString(char *s, char **pend, int base) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 341 | { |
| 342 | char *end; |
| 343 | long x; |
Thomas Wouters | 9cb28bea | 2006-04-11 23:50:33 +0000 | [diff] [blame] | 344 | Py_ssize_t slen; |
| 345 | PyObject *sobj, *srepr; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 346 | |
| 347 | if ((base != 0 && base < 2) || base > 36) { |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 348 | PyErr_SetString(PyExc_ValueError, |
| 349 | "int() base must be >= 2 and <= 36"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | while (*s && isspace(Py_CHARMASK(*s))) |
| 354 | s++; |
| 355 | errno = 0; |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 356 | if (base == 0 && s[0] == '0') { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 357 | x = (long) PyOS_strtoul(s, &end, base); |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 358 | if (x < 0) |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 359 | return PyLong_FromString(s, pend, base); |
Guido van Rossum | 4771065 | 2003-02-12 20:48:22 +0000 | [diff] [blame] | 360 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 361 | else |
| 362 | x = PyOS_strtol(s, &end, base); |
Martin v. Löwis | 2b6727b | 2001-03-06 12:12:02 +0000 | [diff] [blame] | 363 | if (end == s || !isalnum(Py_CHARMASK(end[-1]))) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 364 | goto bad; |
| 365 | while (*end && isspace(Py_CHARMASK(*end))) |
| 366 | end++; |
| 367 | if (*end != '\0') { |
| 368 | bad: |
Thomas Wouters | 9cb28bea | 2006-04-11 23:50:33 +0000 | [diff] [blame] | 369 | slen = strlen(s) < 200 ? strlen(s) : 200; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 370 | sobj = PyString_FromStringAndSize(s, slen); |
Thomas Wouters | 9cb28bea | 2006-04-11 23:50:33 +0000 | [diff] [blame] | 371 | if (sobj == NULL) |
| 372 | return NULL; |
| 373 | srepr = PyObject_Repr(sobj); |
| 374 | Py_DECREF(sobj); |
| 375 | if (srepr == NULL) |
| 376 | return NULL; |
| 377 | PyErr_Format(PyExc_ValueError, |
| 378 | "invalid literal for int() with base %d: %s", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 379 | base, PyString_AS_STRING(srepr)); |
Thomas Wouters | 9cb28bea | 2006-04-11 23:50:33 +0000 | [diff] [blame] | 380 | Py_DECREF(srepr); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 381 | return NULL; |
| 382 | } |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 383 | else if (errno != 0) |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 384 | return PyLong_FromString(s, pend, base); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 385 | if (pend) |
| 386 | *pend = end; |
| 387 | return PyInt_FromLong(x); |
| 388 | } |
| 389 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 390 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 391 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 392 | PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 393 | { |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 394 | PyObject *result; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 395 | char *buffer = (char *)PyMem_MALLOC(length+1); |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 396 | |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 397 | if (buffer == NULL) |
Neal Norwitz | d501d1f | 2007-05-16 04:33:50 +0000 | [diff] [blame] | 398 | return PyErr_NoMemory(); |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 399 | |
| 400 | if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) { |
| 401 | PyMem_FREE(buffer); |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 402 | return NULL; |
| 403 | } |
Walter Dörwald | 07e1476 | 2002-11-06 16:15:14 +0000 | [diff] [blame] | 404 | result = PyInt_FromString(buffer, NULL, base); |
| 405 | PyMem_FREE(buffer); |
| 406 | return result; |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 407 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 408 | #endif |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 409 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 410 | /* Methods */ |
| 411 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 412 | /* Integers are seen as the "smallest" of all numeric types and thus |
| 413 | don't have any knowledge about conversion of other types to |
| 414 | integers. */ |
| 415 | |
| 416 | #define CONVERT_TO_LONG(obj, lng) \ |
| 417 | if (PyInt_Check(obj)) { \ |
| 418 | lng = PyInt_AS_LONG(obj); \ |
| 419 | } \ |
| 420 | else { \ |
| 421 | Py_INCREF(Py_NotImplemented); \ |
| 422 | return Py_NotImplemented; \ |
| 423 | } |
| 424 | |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 425 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 426 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 427 | int_print(PyIntObject *v, FILE *fp, int flags) |
| 428 | /* flags -- not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 429 | { |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 430 | long int_val = v->ob_ival; |
| 431 | Py_BEGIN_ALLOW_THREADS |
| 432 | fprintf(fp, "%ld", int_val); |
| 433 | Py_END_ALLOW_THREADS |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 434 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 437 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 438 | int_repr(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 439 | { |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 440 | return _PyInt_Format(v, 10, 0); |
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_abs(PyIntObject *v) |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 787 | { |
| 788 | if (v->ob_ival >= 0) |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 789 | return int_int(v); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 790 | else |
| 791 | return int_neg(v); |
| 792 | } |
| 793 | |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 794 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 795 | int_nonzero(PyIntObject *v) |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 796 | { |
| 797 | return v->ob_ival != 0; |
| 798 | } |
| 799 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 800 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 801 | int_invert(PyIntObject *v) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 802 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 803 | return PyInt_FromLong(~v->ob_ival); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 806 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 807 | int_lshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 808 | { |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 809 | long a, b, c; |
Raymond Hettinger | a006c37 | 2004-06-26 23:22:57 +0000 | [diff] [blame] | 810 | PyObject *vv, *ww, *result; |
| 811 | |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 812 | CONVERT_TO_LONG(v, a); |
| 813 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 814 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 815 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 816 | return NULL; |
| 817 | } |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 818 | if (a == 0 || b == 0) |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 819 | return int_int(v); |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 820 | if (b >= LONG_BIT) { |
Raymond Hettinger | a006c37 | 2004-06-26 23:22:57 +0000 | [diff] [blame] | 821 | vv = PyLong_FromLong(PyInt_AS_LONG(v)); |
| 822 | if (vv == NULL) |
| 823 | return NULL; |
| 824 | ww = PyLong_FromLong(PyInt_AS_LONG(w)); |
| 825 | if (ww == NULL) { |
| 826 | Py_DECREF(vv); |
| 827 | return NULL; |
| 828 | } |
| 829 | result = PyNumber_Lshift(vv, ww); |
| 830 | Py_DECREF(vv); |
| 831 | Py_DECREF(ww); |
| 832 | return result; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 833 | } |
Tim Peters | da1a221 | 2002-08-11 17:54:42 +0000 | [diff] [blame] | 834 | c = a << b; |
| 835 | if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) { |
Raymond Hettinger | a006c37 | 2004-06-26 23:22:57 +0000 | [diff] [blame] | 836 | vv = PyLong_FromLong(PyInt_AS_LONG(v)); |
| 837 | if (vv == NULL) |
| 838 | return NULL; |
| 839 | ww = PyLong_FromLong(PyInt_AS_LONG(w)); |
| 840 | if (ww == NULL) { |
| 841 | Py_DECREF(vv); |
| 842 | return NULL; |
| 843 | } |
| 844 | result = PyNumber_Lshift(vv, ww); |
| 845 | Py_DECREF(vv); |
| 846 | Py_DECREF(ww); |
| 847 | return result; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 848 | } |
| 849 | return PyInt_FromLong(c); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 852 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 853 | int_rshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 854 | { |
| 855 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 856 | CONVERT_TO_LONG(v, a); |
| 857 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 858 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 859 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 860 | return NULL; |
| 861 | } |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 862 | if (a == 0 || b == 0) |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 863 | return int_int(v); |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 864 | if (b >= LONG_BIT) { |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 865 | if (a < 0) |
| 866 | a = -1; |
| 867 | else |
| 868 | a = 0; |
| 869 | } |
| 870 | else { |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 871 | a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 872 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 873 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 876 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 877 | int_and(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 878 | { |
| 879 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 880 | CONVERT_TO_LONG(v, a); |
| 881 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 882 | return PyInt_FromLong(a & b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 885 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 886 | int_xor(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 887 | { |
| 888 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 889 | CONVERT_TO_LONG(v, a); |
| 890 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 891 | return PyInt_FromLong(a ^ b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 894 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 895 | int_or(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 896 | { |
| 897 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 898 | CONVERT_TO_LONG(v, a); |
| 899 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 900 | return PyInt_FromLong(a | b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 903 | static int |
| 904 | int_coerce(PyObject **pv, PyObject **pw) |
| 905 | { |
| 906 | if (PyInt_Check(*pw)) { |
| 907 | Py_INCREF(*pv); |
| 908 | Py_INCREF(*pw); |
| 909 | return 0; |
| 910 | } |
| 911 | return 1; /* Can't do it */ |
| 912 | } |
| 913 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 914 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 915 | int_int(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 916 | { |
Brett Cannon | c3647ac | 2005-04-26 03:45:26 +0000 | [diff] [blame] | 917 | if (PyInt_CheckExact(v)) |
| 918 | Py_INCREF(v); |
| 919 | else |
| 920 | v = (PyIntObject *)PyInt_FromLong(v->ob_ival); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 921 | return (PyObject *)v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 924 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 925 | int_long(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 926 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 927 | return PyLong_FromLong((v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 930 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 931 | int_float(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 932 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 933 | return PyFloat_FromDouble((double)(v -> ob_ival)); |
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_oct(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 938 | { |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 939 | return _PyInt_Format(v, 8, 0); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 942 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 943 | int_hex(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 944 | { |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 945 | return _PyInt_Format(v, 16, 0); |
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); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 967 | if (PyString_Check(x)) { |
Georg Brandl | 2c1375c | 2006-10-12 11:27:59 +0000 | [diff] [blame] | 968 | /* Since PyInt_FromString doesn't have a length parameter, |
| 969 | * check here for possible NULs in the string. */ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 970 | char *string = PyString_AS_STRING(x); |
| 971 | if (strlen(string) != PyString_Size(x)) { |
Georg Brandl | 2c1375c | 2006-10-12 11:27:59 +0000 | [diff] [blame] | 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", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 980 | base, PyString_AS_STRING(srepr)); |
Georg Brandl | 2c1375c | 2006-10-12 11:27:59 +0000 | [diff] [blame] | 981 | Py_DECREF(srepr); |
| 982 | return NULL; |
| 983 | } |
| 984 | return PyInt_FromString(string, NULL, base); |
| 985 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 986 | #ifdef Py_USING_UNICODE |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 987 | if (PyUnicode_Check(x)) |
| 988 | return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x), |
| 989 | PyUnicode_GET_SIZE(x), |
| 990 | base); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 991 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 992 | PyErr_SetString(PyExc_TypeError, |
| 993 | "int() can't convert non-string with explicit base"); |
| 994 | return NULL; |
| 995 | } |
| 996 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 997 | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
| 998 | first create a regular int from whatever arguments we got, |
| 999 | then allocate a subtype instance and initialize its ob_ival |
| 1000 | from the regular int. The regular int is then thrown away. |
| 1001 | */ |
| 1002 | static PyObject * |
| 1003 | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1004 | { |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1005 | PyObject *tmp, *newobj; |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1006 | long ival; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1007 | |
| 1008 | assert(PyType_IsSubtype(type, &PyInt_Type)); |
| 1009 | tmp = int_new(&PyInt_Type, args, kwds); |
| 1010 | if (tmp == NULL) |
| 1011 | return NULL; |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1012 | if (!PyInt_Check(tmp)) { |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1013 | ival = PyLong_AsLong(tmp); |
Michael W. Hudson | 71665dc | 2003-08-11 17:32:02 +0000 | [diff] [blame] | 1014 | if (ival == -1 && PyErr_Occurred()) { |
| 1015 | Py_DECREF(tmp); |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1016 | return NULL; |
Michael W. Hudson | 71665dc | 2003-08-11 17:32:02 +0000 | [diff] [blame] | 1017 | } |
Neal Norwitz | de8b94c | 2003-02-10 02:12:43 +0000 | [diff] [blame] | 1018 | } else { |
| 1019 | ival = ((PyIntObject *)tmp)->ob_ival; |
| 1020 | } |
| 1021 | |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1022 | newobj = type->tp_alloc(type, 0); |
| 1023 | if (newobj == NULL) { |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 1024 | Py_DECREF(tmp); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1025 | return NULL; |
Raymond Hettinger | f466793 | 2003-06-28 20:04:25 +0000 | [diff] [blame] | 1026 | } |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1027 | ((PyIntObject *)newobj)->ob_ival = ival; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1028 | Py_DECREF(tmp); |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 1029 | return newobj; |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1032 | static PyObject * |
| 1033 | int_getnewargs(PyIntObject *v) |
| 1034 | { |
| 1035 | return Py_BuildValue("(l)", v->ob_ival); |
| 1036 | } |
| 1037 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1038 | static PyObject * |
| 1039 | int_getN(PyIntObject *v, void *context) { |
Jeffrey Yasskin | 5de250e | 2008-03-18 01:09:59 +0000 | [diff] [blame] | 1040 | return PyInt_FromLong((Py_intptr_t)context); |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1043 | /* Convert an integer to the given base. Returns a string. |
| 1044 | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. |
| 1045 | If newstyle is zero, then use the pre-2.6 behavior of octal having |
| 1046 | a leading "0" */ |
| 1047 | PyAPI_FUNC(PyObject*) |
| 1048 | _PyInt_Format(PyIntObject *v, int base, int newstyle) |
| 1049 | { |
| 1050 | /* There are no doubt many, many ways to optimize this, using code |
| 1051 | similar to _PyLong_Format */ |
| 1052 | long n = v->ob_ival; |
| 1053 | int negative = n < 0; |
| 1054 | int is_zero = n == 0; |
| 1055 | |
| 1056 | /* For the reasoning behind this size, see |
| 1057 | http://c-faq.com/misc/hexio.html. Then, add a few bytes for |
| 1058 | the possible sign and prefix "0[box]" */ |
| 1059 | char buf[sizeof(n)*CHAR_BIT+6]; |
| 1060 | |
| 1061 | /* Start by pointing to the end of the buffer. We fill in from |
| 1062 | the back forward. */ |
| 1063 | char* p = &buf[sizeof(buf)]; |
| 1064 | |
| 1065 | assert(base >= 2 && base <= 36); |
| 1066 | |
| 1067 | do { |
| 1068 | /* I'd use i_divmod, except it doesn't produce the results |
| 1069 | I want when n is negative. So just duplicate the salient |
| 1070 | part here. */ |
| 1071 | long div = n / base; |
| 1072 | long mod = n - div * base; |
| 1073 | |
| 1074 | /* convert abs(mod) to the right character in [0-9, a-z] */ |
| 1075 | char cdigit = (char)(mod < 0 ? -mod : mod); |
| 1076 | cdigit += (cdigit < 10) ? '0' : 'a'-10; |
| 1077 | *--p = cdigit; |
| 1078 | |
| 1079 | n = div; |
| 1080 | } while(n); |
| 1081 | |
| 1082 | if (base == 2) { |
| 1083 | *--p = 'b'; |
| 1084 | *--p = '0'; |
| 1085 | } |
| 1086 | else if (base == 8) { |
| 1087 | if (newstyle) { |
| 1088 | *--p = 'o'; |
| 1089 | *--p = '0'; |
| 1090 | } |
| 1091 | else |
| 1092 | if (!is_zero) |
| 1093 | *--p = '0'; |
| 1094 | } |
| 1095 | else if (base == 16) { |
| 1096 | *--p = 'x'; |
| 1097 | *--p = '0'; |
| 1098 | } |
| 1099 | else if (base != 10) { |
| 1100 | *--p = '#'; |
| 1101 | *--p = '0' + base%10; |
| 1102 | if (base > 10) |
| 1103 | *--p = '0' + base/10; |
| 1104 | } |
| 1105 | if (negative) |
| 1106 | *--p = '-'; |
| 1107 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1108 | return PyString_FromStringAndSize(p, &buf[sizeof(buf)] - p); |
Eric Smith | 5e527eb | 2008-02-10 01:36:53 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1111 | static PyObject * |
| 1112 | int__format__(PyObject *self, PyObject *args) |
| 1113 | { |
| 1114 | PyObject *format_spec; |
| 1115 | |
| 1116 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
| 1117 | return NULL; |
Christian Heimes | 593daf5 | 2008-05-26 12:51:38 +0000 | [diff] [blame] | 1118 | if (PyBytes_Check(format_spec)) |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1119 | return _PyInt_FormatAdvanced(self, |
| 1120 | PyBytes_AS_STRING(format_spec), |
| 1121 | PyBytes_GET_SIZE(format_spec)); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1122 | if (PyUnicode_Check(format_spec)) { |
| 1123 | /* Convert format_spec to a str */ |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1124 | PyObject *result; |
| 1125 | PyObject *str_spec = PyObject_Str(format_spec); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1126 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1127 | if (str_spec == NULL) |
| 1128 | return NULL; |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1129 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1130 | result = _PyInt_FormatAdvanced(self, |
| 1131 | PyBytes_AS_STRING(str_spec), |
| 1132 | PyBytes_GET_SIZE(str_spec)); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1133 | |
Eric Smith | dc13b79 | 2008-05-30 18:10:04 +0000 | [diff] [blame] | 1134 | Py_DECREF(str_spec); |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1135 | return result; |
| 1136 | } |
| 1137 | PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode"); |
| 1138 | return NULL; |
| 1139 | } |
| 1140 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1141 | #if 0 |
| 1142 | static PyObject * |
| 1143 | int_is_finite(PyObject *v) |
| 1144 | { |
| 1145 | Py_RETURN_TRUE; |
| 1146 | } |
| 1147 | #endif |
| 1148 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1149 | static PyMethodDef int_methods[] = { |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1150 | {"conjugate", (PyCFunction)int_int, METH_NOARGS, |
| 1151 | "Returns self, the complex conjugate of any int."}, |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 1152 | #if 0 |
| 1153 | {"is_finite", (PyCFunction)int_is_finite, METH_NOARGS, |
| 1154 | "Returns always True."}, |
| 1155 | #endif |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1156 | {"__trunc__", (PyCFunction)int_int, METH_NOARGS, |
| 1157 | "Truncating an Integral returns itself."}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1158 | {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS}, |
Eric Smith | a9f7d62 | 2008-02-17 19:46:49 +0000 | [diff] [blame] | 1159 | {"__format__", (PyCFunction)int__format__, METH_VARARGS}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1160 | {NULL, NULL} /* sentinel */ |
| 1161 | }; |
| 1162 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1163 | static PyGetSetDef int_getset[] = { |
| 1164 | {"real", |
| 1165 | (getter)int_int, (setter)NULL, |
| 1166 | "the real part of a complex number", |
| 1167 | NULL}, |
| 1168 | {"imag", |
| 1169 | (getter)int_getN, (setter)NULL, |
| 1170 | "the imaginary part of a complex number", |
| 1171 | (void*)0}, |
| 1172 | {"numerator", |
| 1173 | (getter)int_int, (setter)NULL, |
| 1174 | "the numerator of a rational number in lowest terms", |
| 1175 | NULL}, |
| 1176 | {"denominator", |
| 1177 | (getter)int_getN, (setter)NULL, |
| 1178 | "the denominator of a rational number in lowest terms", |
| 1179 | (void*)1}, |
| 1180 | {NULL} /* Sentinel */ |
| 1181 | }; |
| 1182 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1183 | PyDoc_STRVAR(int_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1184 | "int(x[, base]) -> integer\n\ |
| 1185 | \n\ |
| 1186 | Convert a string or number to an integer, if possible. A floating point\n\ |
| 1187 | argument will be truncated towards zero (this does not include a string\n\ |
| 1188 | representation of a floating point number!) When converting a string, use\n\ |
| 1189 | 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] | 1190 | 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] | 1191 | string content. If the argument is outside the integer range a\n\ |
| 1192 | long object will be returned instead."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1193 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1194 | static PyNumberMethods int_as_number = { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1195 | (binaryfunc)int_add, /*nb_add*/ |
| 1196 | (binaryfunc)int_sub, /*nb_subtract*/ |
| 1197 | (binaryfunc)int_mul, /*nb_multiply*/ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 1198 | (binaryfunc)int_classic_div, /*nb_divide*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1199 | (binaryfunc)int_mod, /*nb_remainder*/ |
| 1200 | (binaryfunc)int_divmod, /*nb_divmod*/ |
| 1201 | (ternaryfunc)int_pow, /*nb_power*/ |
| 1202 | (unaryfunc)int_neg, /*nb_negative*/ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1203 | (unaryfunc)int_int, /*nb_positive*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1204 | (unaryfunc)int_abs, /*nb_absolute*/ |
| 1205 | (inquiry)int_nonzero, /*nb_nonzero*/ |
| 1206 | (unaryfunc)int_invert, /*nb_invert*/ |
| 1207 | (binaryfunc)int_lshift, /*nb_lshift*/ |
| 1208 | (binaryfunc)int_rshift, /*nb_rshift*/ |
| 1209 | (binaryfunc)int_and, /*nb_and*/ |
| 1210 | (binaryfunc)int_xor, /*nb_xor*/ |
| 1211 | (binaryfunc)int_or, /*nb_or*/ |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 1212 | int_coerce, /*nb_coerce*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 1213 | (unaryfunc)int_int, /*nb_int*/ |
| 1214 | (unaryfunc)int_long, /*nb_long*/ |
| 1215 | (unaryfunc)int_float, /*nb_float*/ |
| 1216 | (unaryfunc)int_oct, /*nb_oct*/ |
| 1217 | (unaryfunc)int_hex, /*nb_hex*/ |
| 1218 | 0, /*nb_inplace_add*/ |
| 1219 | 0, /*nb_inplace_subtract*/ |
| 1220 | 0, /*nb_inplace_multiply*/ |
| 1221 | 0, /*nb_inplace_divide*/ |
| 1222 | 0, /*nb_inplace_remainder*/ |
| 1223 | 0, /*nb_inplace_power*/ |
| 1224 | 0, /*nb_inplace_lshift*/ |
| 1225 | 0, /*nb_inplace_rshift*/ |
| 1226 | 0, /*nb_inplace_and*/ |
| 1227 | 0, /*nb_inplace_xor*/ |
| 1228 | 0, /*nb_inplace_or*/ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1229 | (binaryfunc)int_div, /* nb_floor_divide */ |
| 1230 | int_true_divide, /* nb_true_divide */ |
| 1231 | 0, /* nb_inplace_floor_divide */ |
| 1232 | 0, /* nb_inplace_true_divide */ |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1233 | (unaryfunc)int_int, /* nb_index */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1234 | }; |
| 1235 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1236 | PyTypeObject PyInt_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1237 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1238 | "int", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1239 | sizeof(PyIntObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1240 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1241 | (destructor)int_dealloc, /* tp_dealloc */ |
| 1242 | (printfunc)int_print, /* tp_print */ |
| 1243 | 0, /* tp_getattr */ |
| 1244 | 0, /* tp_setattr */ |
| 1245 | (cmpfunc)int_compare, /* tp_compare */ |
| 1246 | (reprfunc)int_repr, /* tp_repr */ |
| 1247 | &int_as_number, /* tp_as_number */ |
| 1248 | 0, /* tp_as_sequence */ |
| 1249 | 0, /* tp_as_mapping */ |
| 1250 | (hashfunc)int_hash, /* tp_hash */ |
| 1251 | 0, /* tp_call */ |
Guido van Rossum | e75bfde | 2002-02-01 15:34:10 +0000 | [diff] [blame] | 1252 | (reprfunc)int_repr, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1253 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 1254 | 0, /* tp_setattro */ |
| 1255 | 0, /* tp_as_buffer */ |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1256 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 1257 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1258 | int_doc, /* tp_doc */ |
| 1259 | 0, /* tp_traverse */ |
| 1260 | 0, /* tp_clear */ |
| 1261 | 0, /* tp_richcompare */ |
| 1262 | 0, /* tp_weaklistoffset */ |
| 1263 | 0, /* tp_iter */ |
| 1264 | 0, /* tp_iternext */ |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 1265 | int_methods, /* tp_methods */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1266 | 0, /* tp_members */ |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 1267 | int_getset, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1268 | 0, /* tp_base */ |
| 1269 | 0, /* tp_dict */ |
| 1270 | 0, /* tp_descr_get */ |
| 1271 | 0, /* tp_descr_set */ |
| 1272 | 0, /* tp_dictoffset */ |
| 1273 | 0, /* tp_init */ |
| 1274 | 0, /* tp_alloc */ |
| 1275 | int_new, /* tp_new */ |
Guido van Rossum | 9364698 | 2002-04-26 00:53:34 +0000 | [diff] [blame] | 1276 | (freefunc)int_free, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1277 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1278 | |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1279 | int |
Neal Norwitz | b2501f4 | 2002-12-31 03:42:13 +0000 | [diff] [blame] | 1280 | _PyInt_Init(void) |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1281 | { |
| 1282 | PyIntObject *v; |
| 1283 | int ival; |
| 1284 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 1285 | for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) { |
Raymond Hettinger | b32e640 | 2004-02-08 18:54:37 +0000 | [diff] [blame] | 1286 | if (!free_list && (free_list = fill_free_list()) == NULL) |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1287 | return 0; |
| 1288 | /* PyObject_New is inlined */ |
| 1289 | v = free_list; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1290 | free_list = (PyIntObject *)Py_TYPE(v); |
Neal Norwitz | c91ed40 | 2002-12-30 22:29:22 +0000 | [diff] [blame] | 1291 | PyObject_INIT(v, &PyInt_Type); |
| 1292 | v->ob_ival = ival; |
| 1293 | small_ints[ival + NSMALLNEGINTS] = v; |
| 1294 | } |
| 1295 | #endif |
| 1296 | return 1; |
| 1297 | } |
| 1298 | |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1299 | int |
| 1300 | PyInt_ClearFreeList(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1301 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1302 | PyIntObject *p; |
| 1303 | PyIntBlock *list, *next; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1304 | int i; |
| 1305 | int u; /* remaining unfreed ints per block */ |
| 1306 | int freelist_size = 0; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1307 | |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1308 | list = block_list; |
| 1309 | block_list = NULL; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1310 | free_list = NULL; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1311 | while (list != NULL) { |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1312 | u = 0; |
| 1313 | for (i = 0, p = &list->objects[0]; |
| 1314 | i < N_INTOBJECTS; |
| 1315 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1316 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1317 | u++; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1318 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1319 | next = list->next; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1320 | if (u) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1321 | list->next = block_list; |
| 1322 | block_list = list; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1323 | for (i = 0, p = &list->objects[0]; |
| 1324 | i < N_INTOBJECTS; |
| 1325 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1326 | if (!PyInt_CheckExact(p) || |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1327 | p->ob_refcnt == 0) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1328 | Py_TYPE(p) = (struct _typeobject *) |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1329 | free_list; |
| 1330 | free_list = p; |
| 1331 | } |
| 1332 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 1333 | else if (-NSMALLNEGINTS <= p->ob_ival && |
| 1334 | p->ob_ival < NSMALLPOSINTS && |
| 1335 | small_ints[p->ob_ival + |
| 1336 | NSMALLNEGINTS] == NULL) { |
| 1337 | Py_INCREF(p); |
| 1338 | small_ints[p->ob_ival + |
| 1339 | NSMALLNEGINTS] = p; |
| 1340 | } |
| 1341 | #endif |
| 1342 | } |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1343 | } |
| 1344 | else { |
Tim Peters | 29c0afc | 2002-04-28 16:57:34 +0000 | [diff] [blame] | 1345 | PyMem_FREE(list); |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1346 | } |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1347 | freelist_size += u; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1348 | list = next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1349 | } |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1350 | |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1351 | return freelist_size; |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | void |
| 1355 | PyInt_Fini(void) |
| 1356 | { |
| 1357 | PyIntObject *p; |
| 1358 | PyIntBlock *list; |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1359 | int i; |
| 1360 | int u; /* total unfreed ints per block */ |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1361 | |
| 1362 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 1363 | PyIntObject **q; |
| 1364 | |
| 1365 | i = NSMALLNEGINTS + NSMALLPOSINTS; |
| 1366 | q = small_ints; |
| 1367 | while (--i >= 0) { |
| 1368 | Py_XDECREF(*q); |
| 1369 | *q++ = NULL; |
| 1370 | } |
| 1371 | #endif |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1372 | u = PyInt_ClearFreeList(); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1373 | if (!Py_VerboseFlag) |
| 1374 | return; |
| 1375 | fprintf(stderr, "# cleanup ints"); |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1376 | if (!u) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1377 | fprintf(stderr, "\n"); |
| 1378 | } |
| 1379 | else { |
| 1380 | fprintf(stderr, |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1381 | ": %d unfreed int%s\n", |
| 1382 | u, u == 1 ? "" : "s"); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1383 | } |
| 1384 | if (Py_VerboseFlag > 1) { |
| 1385 | list = block_list; |
| 1386 | while (list != NULL) { |
Gregory P. Smith | 2fe7706 | 2008-07-06 03:35:58 +0000 | [diff] [blame^] | 1387 | for (i = 0, p = &list->objects[0]; |
| 1388 | i < N_INTOBJECTS; |
| 1389 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1390 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
Thomas Wouters | 8b87a0b | 2006-03-01 05:41:20 +0000 | [diff] [blame] | 1391 | /* XXX(twouters) cast refcount to |
| 1392 | long until %zd is universally |
| 1393 | available |
| 1394 | */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1395 | fprintf(stderr, |
Thomas Wouters | 8b87a0b | 2006-03-01 05:41:20 +0000 | [diff] [blame] | 1396 | "# <int at %p, refcnt=%ld, val=%ld>\n", |
| 1397 | p, (long)p->ob_refcnt, |
| 1398 | p->ob_ival); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1399 | } |
| 1400 | list = list->next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1401 | } |
| 1402 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1403 | } |