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