| 1 | n/a | |
|---|
| 2 | n/a | /* Integer object implementation */ |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | #include "Python.h" |
|---|
| 5 | n/a | #include <ctype.h> |
|---|
| 6 | n/a | #include <float.h> |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | static PyObject *int_int(PyIntObject *v); |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | long |
|---|
| 11 | n/a | PyInt_GetMax(void) |
|---|
| 12 | 318 | { |
|---|
| 13 | 318 | return LONG_MAX; /* To initialize sys.maxint */ |
|---|
| 14 | n/a | } |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | /* Integers are quite normal objects, to make object handling uniform. |
|---|
| 17 | n/a | (Using odd pointers to represent integers would save much space |
|---|
| 18 | n/a | but require extra checks for this special case throughout the code.) |
|---|
| 19 | n/a | Since a typical Python program spends much of its time allocating |
|---|
| 20 | n/a | and deallocating integers, these operations should be very fast. |
|---|
| 21 | n/a | Therefore we use a dedicated allocation scheme with a much lower |
|---|
| 22 | n/a | overhead (in space and time) than straight malloc(): a simple |
|---|
| 23 | n/a | dedicated free list, filled when necessary with memory from malloc(). |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | block_list is a singly-linked list of all PyIntBlocks ever allocated, |
|---|
| 26 | n/a | linked via their next members. PyIntBlocks are never returned to the |
|---|
| 27 | n/a | system before shutdown (PyInt_Fini). |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | free_list is a singly-linked list of available PyIntObjects, linked |
|---|
| 30 | n/a | via abuse of their ob_type members. |
|---|
| 31 | n/a | */ |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
|---|
| 34 | n/a | #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
|---|
| 35 | n/a | #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject)) |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | struct _intblock { |
|---|
| 38 | n/a | struct _intblock *next; |
|---|
| 39 | n/a | PyIntObject objects[N_INTOBJECTS]; |
|---|
| 40 | n/a | }; |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | typedef struct _intblock PyIntBlock; |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | static PyIntBlock *block_list = NULL; |
|---|
| 45 | n/a | static PyIntObject *free_list = NULL; |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | static PyIntObject * |
|---|
| 48 | n/a | fill_free_list(void) |
|---|
| 49 | 134641 | { |
|---|
| 50 | n/a | PyIntObject *p, *q; |
|---|
| 51 | n/a | /* Python's object allocator isn't appropriate for large blocks. */ |
|---|
| 52 | 134641 | p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock)); |
|---|
| 53 | 134641 | if (p == NULL) |
|---|
| 54 | 0 | return (PyIntObject *) PyErr_NoMemory(); |
|---|
| 55 | 134641 | ((PyIntBlock *)p)->next = block_list; |
|---|
| 56 | 134641 | block_list = (PyIntBlock *)p; |
|---|
| 57 | n/a | /* Link the int objects together, from rear to front, then return |
|---|
| 58 | n/a | the address of the last int object in the block. */ |
|---|
| 59 | 134641 | p = &((PyIntBlock *)p)->objects[0]; |
|---|
| 60 | 134641 | q = p + N_INTOBJECTS; |
|---|
| 61 | 3366025 | while (--q > p) |
|---|
| 62 | 3096743 | Py_TYPE(q) = (struct _typeobject *)(q-1); |
|---|
| 63 | 134641 | Py_TYPE(q) = NULL; |
|---|
| 64 | 134641 | return p + N_INTOBJECTS - 1; |
|---|
| 65 | n/a | } |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | #ifndef NSMALLPOSINTS |
|---|
| 68 | n/a | #define NSMALLPOSINTS 257 |
|---|
| 69 | n/a | #endif |
|---|
| 70 | n/a | #ifndef NSMALLNEGINTS |
|---|
| 71 | n/a | #define NSMALLNEGINTS 5 |
|---|
| 72 | n/a | #endif |
|---|
| 73 | n/a | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
|---|
| 74 | n/a | /* References to small integers are saved in this array so that they |
|---|
| 75 | n/a | can be shared. |
|---|
| 76 | n/a | The integers that are saved are those in the range |
|---|
| 77 | n/a | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
|---|
| 78 | n/a | */ |
|---|
| 79 | n/a | static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
|---|
| 80 | n/a | #endif |
|---|
| 81 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 82 | n/a | Py_ssize_t quick_int_allocs; |
|---|
| 83 | n/a | Py_ssize_t quick_neg_int_allocs; |
|---|
| 84 | n/a | #endif |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | PyObject * |
|---|
| 87 | n/a | PyInt_FromLong(long ival) |
|---|
| 88 | 1005439293 | { |
|---|
| 89 | n/a | register PyIntObject *v; |
|---|
| 90 | n/a | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
|---|
| 91 | 1005439293 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
|---|
| 92 | 488688887 | v = small_ints[ival + NSMALLNEGINTS]; |
|---|
| 93 | 488688887 | Py_INCREF(v); |
|---|
| 94 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 95 | n/a | if (ival >= 0) |
|---|
| 96 | n/a | quick_int_allocs++; |
|---|
| 97 | n/a | else |
|---|
| 98 | n/a | quick_neg_int_allocs++; |
|---|
| 99 | n/a | #endif |
|---|
| 100 | 488688887 | return (PyObject *) v; |
|---|
| 101 | n/a | } |
|---|
| 102 | n/a | #endif |
|---|
| 103 | 516750406 | if (free_list == NULL) { |
|---|
| 104 | 131418 | if ((free_list = fill_free_list()) == NULL) |
|---|
| 105 | 0 | return NULL; |
|---|
| 106 | n/a | } |
|---|
| 107 | n/a | /* Inline PyObject_New */ |
|---|
| 108 | 516750406 | v = free_list; |
|---|
| 109 | 516750406 | free_list = (PyIntObject *)Py_TYPE(v); |
|---|
| 110 | 516750406 | PyObject_INIT(v, &PyInt_Type); |
|---|
| 111 | 516750406 | v->ob_ival = ival; |
|---|
| 112 | 516750406 | return (PyObject *) v; |
|---|
| 113 | n/a | } |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | PyObject * |
|---|
| 116 | n/a | PyInt_FromSize_t(size_t ival) |
|---|
| 117 | 199 | { |
|---|
| 118 | 199 | if (ival <= LONG_MAX) |
|---|
| 119 | 199 | return PyInt_FromLong((long)ival); |
|---|
| 120 | 0 | return _PyLong_FromSize_t(ival); |
|---|
| 121 | n/a | } |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | PyObject * |
|---|
| 124 | n/a | PyInt_FromSsize_t(Py_ssize_t ival) |
|---|
| 125 | 200708595 | { |
|---|
| 126 | n/a | if (ival >= LONG_MIN && ival <= LONG_MAX) |
|---|
| 127 | 200708595 | return PyInt_FromLong((long)ival); |
|---|
| 128 | n/a | return _PyLong_FromSsize_t(ival); |
|---|
| 129 | n/a | } |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | static void |
|---|
| 132 | n/a | int_dealloc(PyIntObject *v) |
|---|
| 133 | 516570063 | { |
|---|
| 134 | 516570063 | if (PyInt_CheckExact(v)) { |
|---|
| 135 | 516529818 | Py_TYPE(v) = (struct _typeobject *)free_list; |
|---|
| 136 | 516529818 | free_list = v; |
|---|
| 137 | n/a | } |
|---|
| 138 | n/a | else |
|---|
| 139 | 40245 | Py_TYPE(v)->tp_free((PyObject *)v); |
|---|
| 140 | 516570063 | } |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | static void |
|---|
| 143 | n/a | int_free(PyIntObject *v) |
|---|
| 144 | 0 | { |
|---|
| 145 | 0 | Py_TYPE(v) = (struct _typeobject *)free_list; |
|---|
| 146 | 0 | free_list = v; |
|---|
| 147 | 0 | } |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | long |
|---|
| 150 | n/a | PyInt_AsLong(register PyObject *op) |
|---|
| 151 | 27519985 | { |
|---|
| 152 | n/a | PyNumberMethods *nb; |
|---|
| 153 | n/a | PyIntObject *io; |
|---|
| 154 | n/a | long val; |
|---|
| 155 | n/a | |
|---|
| 156 | 27519985 | if (op && PyInt_Check(op)) |
|---|
| 157 | 27316499 | return PyInt_AS_LONG((PyIntObject*) op); |
|---|
| 158 | n/a | |
|---|
| 159 | 203486 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
|---|
| 160 | n/a | nb->nb_int == NULL) { |
|---|
| 161 | 142 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
|---|
| 162 | 142 | return -1; |
|---|
| 163 | n/a | } |
|---|
| 164 | n/a | |
|---|
| 165 | 203344 | io = (PyIntObject*) (*nb->nb_int) (op); |
|---|
| 166 | 203344 | if (io == NULL) |
|---|
| 167 | 1 | return -1; |
|---|
| 168 | 203343 | if (!PyInt_Check(io)) { |
|---|
| 169 | 15343 | if (PyLong_Check(io)) { |
|---|
| 170 | n/a | /* got a long? => retry int conversion */ |
|---|
| 171 | 15342 | val = PyLong_AsLong((PyObject *)io); |
|---|
| 172 | 15342 | Py_DECREF(io); |
|---|
| 173 | 15342 | if ((val == -1) && PyErr_Occurred()) |
|---|
| 174 | 15337 | return -1; |
|---|
| 175 | 5 | return val; |
|---|
| 176 | n/a | } |
|---|
| 177 | n/a | else |
|---|
| 178 | n/a | { |
|---|
| 179 | 1 | Py_DECREF(io); |
|---|
| 180 | 1 | PyErr_SetString(PyExc_TypeError, |
|---|
| 181 | n/a | "__int__ method should return an integer"); |
|---|
| 182 | 1 | return -1; |
|---|
| 183 | n/a | } |
|---|
| 184 | n/a | } |
|---|
| 185 | n/a | |
|---|
| 186 | 188000 | val = PyInt_AS_LONG(io); |
|---|
| 187 | 188000 | Py_DECREF(io); |
|---|
| 188 | n/a | |
|---|
| 189 | 188000 | return val; |
|---|
| 190 | n/a | } |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | Py_ssize_t |
|---|
| 193 | n/a | PyInt_AsSsize_t(register PyObject *op) |
|---|
| 194 | 98909717 | { |
|---|
| 195 | n/a | #if SIZEOF_SIZE_T != SIZEOF_LONG |
|---|
| 196 | n/a | PyNumberMethods *nb; |
|---|
| 197 | n/a | PyIntObject *io; |
|---|
| 198 | n/a | Py_ssize_t val; |
|---|
| 199 | n/a | #endif |
|---|
| 200 | n/a | |
|---|
| 201 | 98909717 | if (op == NULL) { |
|---|
| 202 | 0 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
|---|
| 203 | 0 | return -1; |
|---|
| 204 | n/a | } |
|---|
| 205 | n/a | |
|---|
| 206 | 98909717 | if (PyInt_Check(op)) |
|---|
| 207 | 98727072 | return PyInt_AS_LONG((PyIntObject*) op); |
|---|
| 208 | 182645 | if (PyLong_Check(op)) |
|---|
| 209 | 182632 | return _PyLong_AsSsize_t(op); |
|---|
| 210 | n/a | #if SIZEOF_SIZE_T == SIZEOF_LONG |
|---|
| 211 | 13 | return PyInt_AsLong(op); |
|---|
| 212 | n/a | #else |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | if ((nb = Py_TYPE(op)->tp_as_number) == NULL || |
|---|
| 215 | n/a | (nb->nb_int == NULL && nb->nb_long == 0)) { |
|---|
| 216 | n/a | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
|---|
| 217 | n/a | return -1; |
|---|
| 218 | n/a | } |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | if (nb->nb_long != 0) |
|---|
| 221 | n/a | io = (PyIntObject*) (*nb->nb_long) (op); |
|---|
| 222 | n/a | else |
|---|
| 223 | n/a | io = (PyIntObject*) (*nb->nb_int) (op); |
|---|
| 224 | n/a | if (io == NULL) |
|---|
| 225 | n/a | return -1; |
|---|
| 226 | n/a | if (!PyInt_Check(io)) { |
|---|
| 227 | n/a | if (PyLong_Check(io)) { |
|---|
| 228 | n/a | /* got a long? => retry int conversion */ |
|---|
| 229 | n/a | val = _PyLong_AsSsize_t((PyObject *)io); |
|---|
| 230 | n/a | Py_DECREF(io); |
|---|
| 231 | n/a | if ((val == -1) && PyErr_Occurred()) |
|---|
| 232 | n/a | return -1; |
|---|
| 233 | n/a | return val; |
|---|
| 234 | n/a | } |
|---|
| 235 | n/a | else |
|---|
| 236 | n/a | { |
|---|
| 237 | n/a | Py_DECREF(io); |
|---|
| 238 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 239 | n/a | "__int__ method should return an integer"); |
|---|
| 240 | n/a | return -1; |
|---|
| 241 | n/a | } |
|---|
| 242 | n/a | } |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | val = PyInt_AS_LONG(io); |
|---|
| 245 | n/a | Py_DECREF(io); |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | return val; |
|---|
| 248 | n/a | #endif |
|---|
| 249 | n/a | } |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | unsigned long |
|---|
| 252 | n/a | PyInt_AsUnsignedLongMask(register PyObject *op) |
|---|
| 253 | 181878 | { |
|---|
| 254 | n/a | PyNumberMethods *nb; |
|---|
| 255 | n/a | PyIntObject *io; |
|---|
| 256 | n/a | unsigned long val; |
|---|
| 257 | n/a | |
|---|
| 258 | 181878 | if (op && PyInt_Check(op)) |
|---|
| 259 | 180441 | return PyInt_AS_LONG((PyIntObject*) op); |
|---|
| 260 | 1437 | if (op && PyLong_Check(op)) |
|---|
| 261 | 1362 | return PyLong_AsUnsignedLongMask(op); |
|---|
| 262 | n/a | |
|---|
| 263 | 75 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
|---|
| 264 | n/a | nb->nb_int == NULL) { |
|---|
| 265 | 58 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
|---|
| 266 | 58 | return (unsigned long)-1; |
|---|
| 267 | n/a | } |
|---|
| 268 | n/a | |
|---|
| 269 | 17 | io = (PyIntObject*) (*nb->nb_int) (op); |
|---|
| 270 | 17 | if (io == NULL) |
|---|
| 271 | 0 | return (unsigned long)-1; |
|---|
| 272 | 17 | if (!PyInt_Check(io)) { |
|---|
| 273 | 3 | if (PyLong_Check(io)) { |
|---|
| 274 | 3 | val = PyLong_AsUnsignedLongMask((PyObject *)io); |
|---|
| 275 | 3 | Py_DECREF(io); |
|---|
| 276 | 3 | if (PyErr_Occurred()) |
|---|
| 277 | 0 | return (unsigned long)-1; |
|---|
| 278 | 3 | return val; |
|---|
| 279 | n/a | } |
|---|
| 280 | n/a | else |
|---|
| 281 | n/a | { |
|---|
| 282 | 0 | Py_DECREF(io); |
|---|
| 283 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 284 | n/a | "__int__ method should return an integer"); |
|---|
| 285 | 0 | return (unsigned long)-1; |
|---|
| 286 | n/a | } |
|---|
| 287 | n/a | } |
|---|
| 288 | n/a | |
|---|
| 289 | 14 | val = PyInt_AS_LONG(io); |
|---|
| 290 | 14 | Py_DECREF(io); |
|---|
| 291 | n/a | |
|---|
| 292 | 14 | return val; |
|---|
| 293 | n/a | } |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | #ifdef HAVE_LONG_LONG |
|---|
| 296 | n/a | unsigned PY_LONG_LONG |
|---|
| 297 | n/a | PyInt_AsUnsignedLongLongMask(register PyObject *op) |
|---|
| 298 | 0 | { |
|---|
| 299 | n/a | PyNumberMethods *nb; |
|---|
| 300 | n/a | PyIntObject *io; |
|---|
| 301 | n/a | unsigned PY_LONG_LONG val; |
|---|
| 302 | n/a | |
|---|
| 303 | 0 | if (op && PyInt_Check(op)) |
|---|
| 304 | 0 | return PyInt_AS_LONG((PyIntObject*) op); |
|---|
| 305 | 0 | if (op && PyLong_Check(op)) |
|---|
| 306 | 0 | return PyLong_AsUnsignedLongLongMask(op); |
|---|
| 307 | n/a | |
|---|
| 308 | 0 | if (op == NULL || (nb = Py_TYPE(op)->tp_as_number) == NULL || |
|---|
| 309 | n/a | nb->nb_int == NULL) { |
|---|
| 310 | 0 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
|---|
| 311 | 0 | return (unsigned PY_LONG_LONG)-1; |
|---|
| 312 | n/a | } |
|---|
| 313 | n/a | |
|---|
| 314 | 0 | io = (PyIntObject*) (*nb->nb_int) (op); |
|---|
| 315 | 0 | if (io == NULL) |
|---|
| 316 | 0 | return (unsigned PY_LONG_LONG)-1; |
|---|
| 317 | 0 | if (!PyInt_Check(io)) { |
|---|
| 318 | 0 | if (PyLong_Check(io)) { |
|---|
| 319 | 0 | val = PyLong_AsUnsignedLongLongMask((PyObject *)io); |
|---|
| 320 | 0 | Py_DECREF(io); |
|---|
| 321 | 0 | if (PyErr_Occurred()) |
|---|
| 322 | 0 | return (unsigned PY_LONG_LONG)-1; |
|---|
| 323 | 0 | return val; |
|---|
| 324 | n/a | } |
|---|
| 325 | n/a | else |
|---|
| 326 | n/a | { |
|---|
| 327 | 0 | Py_DECREF(io); |
|---|
| 328 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 329 | n/a | "__int__ method should return an integer"); |
|---|
| 330 | 0 | return (unsigned PY_LONG_LONG)-1; |
|---|
| 331 | n/a | } |
|---|
| 332 | n/a | } |
|---|
| 333 | n/a | |
|---|
| 334 | 0 | val = PyInt_AS_LONG(io); |
|---|
| 335 | 0 | Py_DECREF(io); |
|---|
| 336 | n/a | |
|---|
| 337 | 0 | return val; |
|---|
| 338 | n/a | } |
|---|
| 339 | n/a | #endif |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | PyObject * |
|---|
| 342 | n/a | PyInt_FromString(char *s, char **pend, int base) |
|---|
| 343 | 1174310 | { |
|---|
| 344 | n/a | char *end; |
|---|
| 345 | n/a | long x; |
|---|
| 346 | n/a | Py_ssize_t slen; |
|---|
| 347 | n/a | PyObject *sobj, *srepr; |
|---|
| 348 | n/a | |
|---|
| 349 | 1174310 | if ((base != 0 && base < 2) || base > 36) { |
|---|
| 350 | 1 | PyErr_SetString(PyExc_ValueError, |
|---|
| 351 | n/a | "int() base must be >= 2 and <= 36"); |
|---|
| 352 | 1 | return NULL; |
|---|
| 353 | n/a | } |
|---|
| 354 | n/a | |
|---|
| 355 | 2355106 | while (*s && isspace(Py_CHARMASK(*s))) |
|---|
| 356 | 6488 | s++; |
|---|
| 357 | 1174309 | errno = 0; |
|---|
| 358 | 1174325 | if (base == 0 && s[0] == '0') { |
|---|
| 359 | 16 | x = (long) PyOS_strtoul(s, &end, base); |
|---|
| 360 | 16 | if (x < 0) |
|---|
| 361 | 0 | return PyLong_FromString(s, pend, base); |
|---|
| 362 | n/a | } |
|---|
| 363 | n/a | else |
|---|
| 364 | 1174293 | x = PyOS_strtol(s, &end, base); |
|---|
| 365 | 1174309 | if (end == s || !isalnum(Py_CHARMASK(end[-1]))) |
|---|
| 366 | n/a | goto bad; |
|---|
| 367 | 2294303 | while (*end && isspace(Py_CHARMASK(*end))) |
|---|
| 368 | 35483 | end++; |
|---|
| 369 | 1129410 | if (*end != '\0') { |
|---|
| 370 | 45428 | bad: |
|---|
| 371 | 45428 | slen = strlen(s) < 200 ? strlen(s) : 200; |
|---|
| 372 | 45428 | sobj = PyString_FromStringAndSize(s, slen); |
|---|
| 373 | 45428 | if (sobj == NULL) |
|---|
| 374 | 0 | return NULL; |
|---|
| 375 | 45428 | srepr = PyObject_Repr(sobj); |
|---|
| 376 | 45428 | Py_DECREF(sobj); |
|---|
| 377 | 45428 | if (srepr == NULL) |
|---|
| 378 | 0 | return NULL; |
|---|
| 379 | 45428 | PyErr_Format(PyExc_ValueError, |
|---|
| 380 | n/a | "invalid literal for int() with base %d: %s", |
|---|
| 381 | n/a | base, PyString_AS_STRING(srepr)); |
|---|
| 382 | 45428 | Py_DECREF(srepr); |
|---|
| 383 | 45428 | return NULL; |
|---|
| 384 | n/a | } |
|---|
| 385 | 1128881 | else if (errno != 0) |
|---|
| 386 | 28557 | return PyLong_FromString(s, pend, base); |
|---|
| 387 | 1100324 | if (pend) |
|---|
| 388 | 629087 | *pend = end; |
|---|
| 389 | 1100324 | return PyInt_FromLong(x); |
|---|
| 390 | n/a | } |
|---|
| 391 | n/a | |
|---|
| 392 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 393 | n/a | PyObject * |
|---|
| 394 | n/a | PyInt_FromUnicode(Py_UNICODE *s, Py_ssize_t length, int base) |
|---|
| 395 | 700 | { |
|---|
| 396 | n/a | PyObject *result; |
|---|
| 397 | 700 | char *buffer = (char *)PyMem_MALLOC(length+1); |
|---|
| 398 | n/a | |
|---|
| 399 | 700 | if (buffer == NULL) |
|---|
| 400 | 0 | return PyErr_NoMemory(); |
|---|
| 401 | n/a | |
|---|
| 402 | 700 | if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) { |
|---|
| 403 | 13 | PyMem_FREE(buffer); |
|---|
| 404 | 13 | return NULL; |
|---|
| 405 | n/a | } |
|---|
| 406 | 687 | result = PyInt_FromString(buffer, NULL, base); |
|---|
| 407 | 687 | PyMem_FREE(buffer); |
|---|
| 408 | 687 | return result; |
|---|
| 409 | n/a | } |
|---|
| 410 | n/a | #endif |
|---|
| 411 | n/a | |
|---|
| 412 | n/a | /* Methods */ |
|---|
| 413 | n/a | |
|---|
| 414 | n/a | /* Integers are seen as the "smallest" of all numeric types and thus |
|---|
| 415 | n/a | don't have any knowledge about conversion of other types to |
|---|
| 416 | n/a | integers. */ |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | #define CONVERT_TO_LONG(obj, lng) \ |
|---|
| 419 | n/a | if (PyInt_Check(obj)) { \ |
|---|
| 420 | n/a | lng = PyInt_AS_LONG(obj); \ |
|---|
| 421 | n/a | } \ |
|---|
| 422 | n/a | else { \ |
|---|
| 423 | n/a | Py_INCREF(Py_NotImplemented); \ |
|---|
| 424 | n/a | return Py_NotImplemented; \ |
|---|
| 425 | n/a | } |
|---|
| 426 | n/a | |
|---|
| 427 | n/a | /* ARGSUSED */ |
|---|
| 428 | n/a | static int |
|---|
| 429 | n/a | int_print(PyIntObject *v, FILE *fp, int flags) |
|---|
| 430 | n/a | /* flags -- not used but required by interface */ |
|---|
| 431 | 639 | { |
|---|
| 432 | 639 | long int_val = v->ob_ival; |
|---|
| 433 | 639 | Py_BEGIN_ALLOW_THREADS |
|---|
| 434 | 639 | fprintf(fp, "%ld", int_val); |
|---|
| 435 | 639 | Py_END_ALLOW_THREADS |
|---|
| 436 | 639 | return 0; |
|---|
| 437 | n/a | } |
|---|
| 438 | n/a | |
|---|
| 439 | n/a | static int |
|---|
| 440 | n/a | int_compare(PyIntObject *v, PyIntObject *w) |
|---|
| 441 | 389097386 | { |
|---|
| 442 | 389097386 | register long i = v->ob_ival; |
|---|
| 443 | 389097386 | register long j = w->ob_ival; |
|---|
| 444 | 389097386 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
|---|
| 445 | n/a | } |
|---|
| 446 | n/a | |
|---|
| 447 | n/a | static long |
|---|
| 448 | n/a | int_hash(PyIntObject *v) |
|---|
| 449 | 620795085 | { |
|---|
| 450 | n/a | /* XXX If this is changed, you also need to change the way |
|---|
| 451 | n/a | Python's long, float and complex types are hashed. */ |
|---|
| 452 | 620795085 | long x = v -> ob_ival; |
|---|
| 453 | 620795085 | if (x == -1) |
|---|
| 454 | 42808 | x = -2; |
|---|
| 455 | 620795085 | return x; |
|---|
| 456 | n/a | } |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | static PyObject * |
|---|
| 459 | n/a | int_add(PyIntObject *v, PyIntObject *w) |
|---|
| 460 | 683174 | { |
|---|
| 461 | n/a | register long a, b, x; |
|---|
| 462 | 683277 | CONVERT_TO_LONG(v, a); |
|---|
| 463 | 1366142 | CONVERT_TO_LONG(w, b); |
|---|
| 464 | n/a | /* casts in the line below avoid undefined behaviour on overflow */ |
|---|
| 465 | 104393 | x = (long)((unsigned long)a + b); |
|---|
| 466 | 104822 | if ((x^a) >= 0 || (x^b) >= 0) |
|---|
| 467 | 104264 | return PyInt_FromLong(x); |
|---|
| 468 | 129 | return PyLong_Type.tp_as_number->nb_add((PyObject *)v, (PyObject *)w); |
|---|
| 469 | n/a | } |
|---|
| 470 | n/a | |
|---|
| 471 | n/a | static PyObject * |
|---|
| 472 | n/a | int_sub(PyIntObject *v, PyIntObject *w) |
|---|
| 473 | 49471 | { |
|---|
| 474 | n/a | register long a, b, x; |
|---|
| 475 | 49502 | CONVERT_TO_LONG(v, a); |
|---|
| 476 | 98880 | CONVERT_TO_LONG(w, b); |
|---|
| 477 | n/a | /* casts in the line below avoid undefined behaviour on overflow */ |
|---|
| 478 | 6699 | x = (long)((unsigned long)a - b); |
|---|
| 479 | 7042 | if ((x^a) >= 0 || (x^~b) >= 0) |
|---|
| 480 | 6630 | return PyInt_FromLong(x); |
|---|
| 481 | 69 | return PyLong_Type.tp_as_number->nb_subtract((PyObject *)v, |
|---|
| 482 | n/a | (PyObject *)w); |
|---|
| 483 | n/a | } |
|---|
| 484 | n/a | |
|---|
| 485 | n/a | /* |
|---|
| 486 | n/a | Integer overflow checking for * is painful: Python tried a couple ways, but |
|---|
| 487 | n/a | they didn't work on all platforms, or failed in endcases (a product of |
|---|
| 488 | n/a | -sys.maxint-1 has been a particular pain). |
|---|
| 489 | n/a | |
|---|
| 490 | n/a | Here's another way: |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | The native long product x*y is either exactly right or *way* off, being |
|---|
| 493 | n/a | just the last n bits of the true product, where n is the number of bits |
|---|
| 494 | n/a | in a long (the delivered product is the true product plus i*2**n for |
|---|
| 495 | n/a | some integer i). |
|---|
| 496 | n/a | |
|---|
| 497 | n/a | The native double product (double)x * (double)y is subject to three |
|---|
| 498 | n/a | rounding errors: on a sizeof(long)==8 box, each cast to double can lose |
|---|
| 499 | n/a | info, and even on a sizeof(long)==4 box, the multiplication can lose info. |
|---|
| 500 | n/a | But, unlike the native long product, it's not in *range* trouble: even |
|---|
| 501 | n/a | if sizeof(long)==32 (256-bit longs), the product easily fits in the |
|---|
| 502 | n/a | dynamic range of a double. So the leading 50 (or so) bits of the double |
|---|
| 503 | n/a | product are correct. |
|---|
| 504 | n/a | |
|---|
| 505 | n/a | We check these two ways against each other, and declare victory if they're |
|---|
| 506 | n/a | approximately the same. Else, because the native long product is the only |
|---|
| 507 | n/a | one that can lose catastrophic amounts of information, it's the native long |
|---|
| 508 | n/a | product that must have overflowed. |
|---|
| 509 | n/a | */ |
|---|
| 510 | n/a | |
|---|
| 511 | n/a | static PyObject * |
|---|
| 512 | n/a | int_mul(PyObject *v, PyObject *w) |
|---|
| 513 | 1884817 | { |
|---|
| 514 | n/a | long a, b; |
|---|
| 515 | n/a | long longprod; /* a*b in native long arithmetic */ |
|---|
| 516 | n/a | double doubled_longprod; /* (double)longprod */ |
|---|
| 517 | n/a | double doubleprod; /* (double)a * (double)b */ |
|---|
| 518 | n/a | |
|---|
| 519 | 2183294 | CONVERT_TO_LONG(v, a); |
|---|
| 520 | 3172680 | CONVERT_TO_LONG(w, b); |
|---|
| 521 | n/a | /* casts in the next line avoid undefined behaviour on overflow */ |
|---|
| 522 | 1181558 | longprod = (long)((unsigned long)a * b); |
|---|
| 523 | 1181558 | doubleprod = (double)a * (double)b; |
|---|
| 524 | 1181558 | doubled_longprod = (double)longprod; |
|---|
| 525 | n/a | |
|---|
| 526 | n/a | /* Fast path for normal case: small multiplicands, and no info |
|---|
| 527 | n/a | is lost in either method. */ |
|---|
| 528 | 1181558 | if (doubled_longprod == doubleprod) |
|---|
| 529 | 1175247 | return PyInt_FromLong(longprod); |
|---|
| 530 | n/a | |
|---|
| 531 | n/a | /* Somebody somewhere lost info. Close enough, or way off? Note |
|---|
| 532 | n/a | that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0). |
|---|
| 533 | n/a | The difference either is or isn't significant compared to the |
|---|
| 534 | n/a | true value (of which doubleprod is a good approximation). |
|---|
| 535 | n/a | */ |
|---|
| 536 | n/a | { |
|---|
| 537 | 6311 | const double diff = doubled_longprod - doubleprod; |
|---|
| 538 | 6311 | const double absdiff = diff >= 0.0 ? diff : -diff; |
|---|
| 539 | n/a | const double absprod = doubleprod >= 0.0 ? doubleprod : |
|---|
| 540 | 6311 | -doubleprod; |
|---|
| 541 | n/a | /* absdiff/absprod <= 1/32 iff |
|---|
| 542 | n/a | 32 * absdiff <= absprod -- 5 good bits is "close enough" */ |
|---|
| 543 | 6311 | if (32.0 * absdiff <= absprod) |
|---|
| 544 | 231 | return PyInt_FromLong(longprod); |
|---|
| 545 | n/a | else |
|---|
| 546 | 6080 | return PyLong_Type.tp_as_number->nb_multiply(v, w); |
|---|
| 547 | n/a | } |
|---|
| 548 | n/a | } |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | /* Integer overflow checking for unary negation: on a 2's-complement |
|---|
| 551 | n/a | * box, -x overflows iff x is the most negative long. In this case we |
|---|
| 552 | n/a | * get -x == x. However, -x is undefined (by C) if x /is/ the most |
|---|
| 553 | n/a | * negative long (it's a signed overflow case), and some compilers care. |
|---|
| 554 | n/a | * So we cast x to unsigned long first. However, then other compilers |
|---|
| 555 | n/a | * warn about applying unary minus to an unsigned operand. Hence the |
|---|
| 556 | n/a | * weird "0-". |
|---|
| 557 | n/a | */ |
|---|
| 558 | n/a | #define UNARY_NEG_WOULD_OVERFLOW(x) \ |
|---|
| 559 | n/a | ((x) < 0 && (unsigned long)(x) == 0-(unsigned long)(x)) |
|---|
| 560 | n/a | |
|---|
| 561 | n/a | /* Return type of i_divmod */ |
|---|
| 562 | n/a | enum divmod_result { |
|---|
| 563 | n/a | DIVMOD_OK, /* Correct result */ |
|---|
| 564 | n/a | DIVMOD_OVERFLOW, /* Overflow, try again using longs */ |
|---|
| 565 | n/a | DIVMOD_ERROR /* Exception raised */ |
|---|
| 566 | n/a | }; |
|---|
| 567 | n/a | |
|---|
| 568 | n/a | static enum divmod_result |
|---|
| 569 | n/a | i_divmod(register long x, register long y, |
|---|
| 570 | n/a | long *p_xdivy, long *p_xmody) |
|---|
| 571 | 1340651 | { |
|---|
| 572 | n/a | long xdivy, xmody; |
|---|
| 573 | n/a | |
|---|
| 574 | 1340651 | if (y == 0) { |
|---|
| 575 | 1327 | PyErr_SetString(PyExc_ZeroDivisionError, |
|---|
| 576 | n/a | "integer division or modulo by zero"); |
|---|
| 577 | 1327 | return DIVMOD_ERROR; |
|---|
| 578 | n/a | } |
|---|
| 579 | n/a | /* (-sys.maxint-1)/-1 is the only overflow case. */ |
|---|
| 580 | 1339324 | if (y == -1 && UNARY_NEG_WOULD_OVERFLOW(x)) |
|---|
| 581 | 1 | return DIVMOD_OVERFLOW; |
|---|
| 582 | 1339323 | xdivy = x / y; |
|---|
| 583 | n/a | /* xdiv*y can overflow on platforms where x/y gives floor(x/y) |
|---|
| 584 | n/a | * for x and y with differing signs. (This is unusual |
|---|
| 585 | n/a | * behaviour, and C99 prohibits it, but it's allowed by C89; |
|---|
| 586 | n/a | * for an example of overflow, take x = LONG_MIN, y = 5 or x = |
|---|
| 587 | n/a | * LONG_MAX, y = -5.) However, x - xdivy*y is always |
|---|
| 588 | n/a | * representable as a long, since it lies strictly between |
|---|
| 589 | n/a | * -abs(y) and abs(y). We add casts to avoid intermediate |
|---|
| 590 | n/a | * overflow. |
|---|
| 591 | n/a | */ |
|---|
| 592 | 1339323 | xmody = (long)(x - (unsigned long)xdivy * y); |
|---|
| 593 | n/a | /* If the signs of x and y differ, and the remainder is non-0, |
|---|
| 594 | n/a | * C89 doesn't define whether xdivy is now the floor or the |
|---|
| 595 | n/a | * ceiling of the infinitely precise quotient. We want the floor, |
|---|
| 596 | n/a | * and we have it iff the remainder's sign matches y's. |
|---|
| 597 | n/a | */ |
|---|
| 598 | 1339323 | if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) { |
|---|
| 599 | 29785 | xmody += y; |
|---|
| 600 | 29785 | --xdivy; |
|---|
| 601 | 29785 | assert(xmody && ((y ^ xmody) >= 0)); |
|---|
| 602 | n/a | } |
|---|
| 603 | 1339323 | *p_xdivy = xdivy; |
|---|
| 604 | 1339323 | *p_xmody = xmody; |
|---|
| 605 | 1339323 | return DIVMOD_OK; |
|---|
| 606 | n/a | } |
|---|
| 607 | n/a | |
|---|
| 608 | n/a | static PyObject * |
|---|
| 609 | n/a | int_div(PyIntObject *x, PyIntObject *y) |
|---|
| 610 | 195590 | { |
|---|
| 611 | n/a | long xi, yi; |
|---|
| 612 | n/a | long d, m; |
|---|
| 613 | 195610 | CONVERT_TO_LONG(x, xi); |
|---|
| 614 | 391140 | CONVERT_TO_LONG(y, yi); |
|---|
| 615 | 194545 | switch (i_divmod(xi, yi, &d, &m)) { |
|---|
| 616 | n/a | case DIVMOD_OK: |
|---|
| 617 | 193223 | return PyInt_FromLong(d); |
|---|
| 618 | n/a | case DIVMOD_OVERFLOW: |
|---|
| 619 | 0 | return PyLong_Type.tp_as_number->nb_divide((PyObject *)x, |
|---|
| 620 | n/a | (PyObject *)y); |
|---|
| 621 | n/a | default: |
|---|
| 622 | 1322 | return NULL; |
|---|
| 623 | n/a | } |
|---|
| 624 | n/a | } |
|---|
| 625 | n/a | |
|---|
| 626 | n/a | static PyObject * |
|---|
| 627 | n/a | int_classic_div(PyIntObject *x, PyIntObject *y) |
|---|
| 628 | 9238 | { |
|---|
| 629 | n/a | long xi, yi; |
|---|
| 630 | n/a | long d, m; |
|---|
| 631 | 9262 | CONVERT_TO_LONG(x, xi); |
|---|
| 632 | 18428 | CONVERT_TO_LONG(y, yi); |
|---|
| 633 | 1438 | if (Py_DivisionWarningFlag && |
|---|
| 634 | n/a | PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0) |
|---|
| 635 | 0 | return NULL; |
|---|
| 636 | 1438 | switch (i_divmod(xi, yi, &d, &m)) { |
|---|
| 637 | n/a | case DIVMOD_OK: |
|---|
| 638 | 1433 | return PyInt_FromLong(d); |
|---|
| 639 | n/a | case DIVMOD_OVERFLOW: |
|---|
| 640 | 0 | return PyLong_Type.tp_as_number->nb_divide((PyObject *)x, |
|---|
| 641 | n/a | (PyObject *)y); |
|---|
| 642 | n/a | default: |
|---|
| 643 | 5 | return NULL; |
|---|
| 644 | n/a | } |
|---|
| 645 | n/a | } |
|---|
| 646 | n/a | |
|---|
| 647 | n/a | static PyObject * |
|---|
| 648 | n/a | int_true_divide(PyIntObject *x, PyIntObject *y) |
|---|
| 649 | 79006 | { |
|---|
| 650 | n/a | long xi, yi; |
|---|
| 651 | n/a | /* If they aren't both ints, give someone else a chance. In |
|---|
| 652 | n/a | particular, this lets int/long get handled by longs, which |
|---|
| 653 | n/a | underflows to 0 gracefully if the long is too big to convert |
|---|
| 654 | n/a | to float. */ |
|---|
| 655 | 79008 | CONVERT_TO_LONG(x, xi); |
|---|
| 656 | 158008 | CONVERT_TO_LONG(y, yi); |
|---|
| 657 | 46 | if (yi == 0) { |
|---|
| 658 | 0 | PyErr_SetString(PyExc_ZeroDivisionError, |
|---|
| 659 | n/a | "division by zero"); |
|---|
| 660 | 0 | return NULL; |
|---|
| 661 | n/a | } |
|---|
| 662 | 46 | if (xi == 0) |
|---|
| 663 | 0 | return PyFloat_FromDouble(yi < 0 ? -0.0 : 0.0); |
|---|
| 664 | n/a | |
|---|
| 665 | n/a | #define WIDTH_OF_ULONG (CHAR_BIT*SIZEOF_LONG) |
|---|
| 666 | n/a | #if DBL_MANT_DIG < WIDTH_OF_ULONG |
|---|
| 667 | 46 | if ((xi >= 0 ? 0UL + xi : 0UL - xi) >> DBL_MANT_DIG || |
|---|
| 668 | n/a | (yi >= 0 ? 0UL + yi : 0UL - yi) >> DBL_MANT_DIG) |
|---|
| 669 | n/a | /* Large x or y. Use long integer arithmetic. */ |
|---|
| 670 | 0 | return PyLong_Type.tp_as_number->nb_true_divide( |
|---|
| 671 | n/a | (PyObject *)x, (PyObject *)y); |
|---|
| 672 | n/a | else |
|---|
| 673 | n/a | #endif |
|---|
| 674 | n/a | /* Both ints can be exactly represented as doubles. Do a |
|---|
| 675 | n/a | floating-point division. */ |
|---|
| 676 | 46 | return PyFloat_FromDouble((double)xi / (double)yi); |
|---|
| 677 | n/a | } |
|---|
| 678 | n/a | |
|---|
| 679 | n/a | static PyObject * |
|---|
| 680 | n/a | int_mod(PyIntObject *x, PyIntObject *y) |
|---|
| 681 | 498840 | { |
|---|
| 682 | n/a | long xi, yi; |
|---|
| 683 | n/a | long d, m; |
|---|
| 684 | 498865 | CONVERT_TO_LONG(x, xi); |
|---|
| 685 | 997630 | CONVERT_TO_LONG(y, yi); |
|---|
| 686 | 497402 | switch (i_divmod(xi, yi, &d, &m)) { |
|---|
| 687 | n/a | case DIVMOD_OK: |
|---|
| 688 | 497402 | return PyInt_FromLong(m); |
|---|
| 689 | n/a | case DIVMOD_OVERFLOW: |
|---|
| 690 | 0 | return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x, |
|---|
| 691 | n/a | (PyObject *)y); |
|---|
| 692 | n/a | default: |
|---|
| 693 | 0 | return NULL; |
|---|
| 694 | n/a | } |
|---|
| 695 | n/a | } |
|---|
| 696 | n/a | |
|---|
| 697 | n/a | static PyObject * |
|---|
| 698 | n/a | int_divmod(PyIntObject *x, PyIntObject *y) |
|---|
| 699 | 612183 | { |
|---|
| 700 | n/a | long xi, yi; |
|---|
| 701 | n/a | long d, m; |
|---|
| 702 | 612193 | CONVERT_TO_LONG(x, xi); |
|---|
| 703 | 1224346 | CONVERT_TO_LONG(y, yi); |
|---|
| 704 | 608572 | switch (i_divmod(xi, yi, &d, &m)) { |
|---|
| 705 | n/a | case DIVMOD_OK: |
|---|
| 706 | 608571 | return Py_BuildValue("(ll)", d, m); |
|---|
| 707 | n/a | case DIVMOD_OVERFLOW: |
|---|
| 708 | 1 | return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x, |
|---|
| 709 | n/a | (PyObject *)y); |
|---|
| 710 | n/a | default: |
|---|
| 711 | 0 | return NULL; |
|---|
| 712 | n/a | } |
|---|
| 713 | n/a | } |
|---|
| 714 | n/a | |
|---|
| 715 | n/a | static PyObject * |
|---|
| 716 | n/a | int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z) |
|---|
| 717 | 973051 | { |
|---|
| 718 | 973051 | register long iv, iw, iz=0, ix, temp, prev; |
|---|
| 719 | 973073 | CONVERT_TO_LONG(v, iv); |
|---|
| 720 | 1946058 | CONVERT_TO_LONG(w, iw); |
|---|
| 721 | 971022 | if (iw < 0) { |
|---|
| 722 | 5293 | if ((PyObject *)z != Py_None) { |
|---|
| 723 | 1 | PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " |
|---|
| 724 | n/a | "cannot be negative when 3rd argument specified"); |
|---|
| 725 | 1 | return NULL; |
|---|
| 726 | n/a | } |
|---|
| 727 | n/a | /* Return a float. This works because we know that |
|---|
| 728 | n/a | this calls float_pow() which converts its |
|---|
| 729 | n/a | arguments to double. */ |
|---|
| 730 | 5292 | return PyFloat_Type.tp_as_number->nb_power( |
|---|
| 731 | n/a | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
|---|
| 732 | n/a | } |
|---|
| 733 | 965729 | if ((PyObject *)z != Py_None) { |
|---|
| 734 | 39665 | CONVERT_TO_LONG(z, iz); |
|---|
| 735 | 38903 | if (iz == 0) { |
|---|
| 736 | 1 | PyErr_SetString(PyExc_ValueError, |
|---|
| 737 | n/a | "pow() 3rd argument cannot be 0"); |
|---|
| 738 | 1 | return NULL; |
|---|
| 739 | n/a | } |
|---|
| 740 | n/a | } |
|---|
| 741 | n/a | /* |
|---|
| 742 | n/a | * XXX: The original exponentiation code stopped looping |
|---|
| 743 | n/a | * when temp hit zero; this code will continue onwards |
|---|
| 744 | n/a | * unnecessarily, but at least it won't cause any errors. |
|---|
| 745 | n/a | * Hopefully the speed improvement from the fast exponentiation |
|---|
| 746 | n/a | * will compensate for the slight inefficiency. |
|---|
| 747 | n/a | * XXX: Better handling of overflows is desperately needed. |
|---|
| 748 | n/a | */ |
|---|
| 749 | 965347 | temp = iv; |
|---|
| 750 | 965347 | ix = 1; |
|---|
| 751 | 4970033 | while (iw > 0) { |
|---|
| 752 | 3942572 | prev = ix; /* Save value for overflow check */ |
|---|
| 753 | 3942572 | if (iw & 1) { |
|---|
| 754 | 2474497 | ix = ix*temp; |
|---|
| 755 | 2474497 | if (temp == 0) |
|---|
| 756 | 4329 | break; /* Avoid ix / 0 */ |
|---|
| 757 | 2470168 | if (ix / temp != prev) { |
|---|
| 758 | 13073 | return PyLong_Type.tp_as_number->nb_power( |
|---|
| 759 | n/a | (PyObject *)v, |
|---|
| 760 | n/a | (PyObject *)w, |
|---|
| 761 | n/a | (PyObject *)z); |
|---|
| 762 | n/a | } |
|---|
| 763 | n/a | } |
|---|
| 764 | 3925170 | iw >>= 1; /* Shift exponent down by 1 bit */ |
|---|
| 765 | 3925170 | if (iw==0) break; |
|---|
| 766 | 3120389 | prev = temp; |
|---|
| 767 | 3120389 | temp *= temp; /* Square the value of temp */ |
|---|
| 768 | 3120389 | if (prev != 0 && temp / prev != prev) { |
|---|
| 769 | 81050 | return PyLong_Type.tp_as_number->nb_power( |
|---|
| 770 | n/a | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
|---|
| 771 | n/a | } |
|---|
| 772 | 3039339 | if (iz) { |
|---|
| 773 | n/a | /* If we did a multiplication, perform a modulo */ |
|---|
| 774 | 309048 | ix = ix % iz; |
|---|
| 775 | 309048 | temp = temp % iz; |
|---|
| 776 | n/a | } |
|---|
| 777 | n/a | } |
|---|
| 778 | 871224 | if (iz) { |
|---|
| 779 | n/a | long div, mod; |
|---|
| 780 | 38694 | switch (i_divmod(ix, iz, &div, &mod)) { |
|---|
| 781 | n/a | case DIVMOD_OK: |
|---|
| 782 | 38694 | ix = mod; |
|---|
| 783 | 38694 | break; |
|---|
| 784 | n/a | case DIVMOD_OVERFLOW: |
|---|
| 785 | 0 | return PyLong_Type.tp_as_number->nb_power( |
|---|
| 786 | n/a | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
|---|
| 787 | n/a | default: |
|---|
| 788 | 0 | return NULL; |
|---|
| 789 | n/a | } |
|---|
| 790 | n/a | } |
|---|
| 791 | 871224 | return PyInt_FromLong(ix); |
|---|
| 792 | n/a | } |
|---|
| 793 | n/a | |
|---|
| 794 | n/a | static PyObject * |
|---|
| 795 | n/a | int_neg(PyIntObject *v) |
|---|
| 796 | 404960 | { |
|---|
| 797 | n/a | register long a; |
|---|
| 798 | 404960 | a = v->ob_ival; |
|---|
| 799 | n/a | /* check for overflow */ |
|---|
| 800 | 404960 | if (UNARY_NEG_WOULD_OVERFLOW(a)) { |
|---|
| 801 | 2 | PyObject *o = PyLong_FromLong(a); |
|---|
| 802 | 2 | if (o != NULL) { |
|---|
| 803 | 2 | PyObject *result = PyNumber_Negative(o); |
|---|
| 804 | 2 | Py_DECREF(o); |
|---|
| 805 | 2 | return result; |
|---|
| 806 | n/a | } |
|---|
| 807 | 0 | return NULL; |
|---|
| 808 | n/a | } |
|---|
| 809 | 404958 | return PyInt_FromLong(-a); |
|---|
| 810 | n/a | } |
|---|
| 811 | n/a | |
|---|
| 812 | n/a | static PyObject * |
|---|
| 813 | n/a | int_abs(PyIntObject *v) |
|---|
| 814 | 287995 | { |
|---|
| 815 | 287995 | if (v->ob_ival >= 0) |
|---|
| 816 | 145804 | return int_int(v); |
|---|
| 817 | n/a | else |
|---|
| 818 | 142191 | return int_neg(v); |
|---|
| 819 | n/a | } |
|---|
| 820 | n/a | |
|---|
| 821 | n/a | static int |
|---|
| 822 | n/a | int_nonzero(PyIntObject *v) |
|---|
| 823 | 67512913 | { |
|---|
| 824 | 67512913 | return v->ob_ival != 0; |
|---|
| 825 | n/a | } |
|---|
| 826 | n/a | |
|---|
| 827 | n/a | static PyObject * |
|---|
| 828 | n/a | int_invert(PyIntObject *v) |
|---|
| 829 | 19073 | { |
|---|
| 830 | 19073 | return PyInt_FromLong(~v->ob_ival); |
|---|
| 831 | n/a | } |
|---|
| 832 | n/a | |
|---|
| 833 | n/a | static PyObject * |
|---|
| 834 | n/a | int_lshift(PyIntObject *v, PyIntObject *w) |
|---|
| 835 | 750395 | { |
|---|
| 836 | n/a | long a, b, c; |
|---|
| 837 | n/a | PyObject *vv, *ww, *result; |
|---|
| 838 | n/a | |
|---|
| 839 | 750402 | CONVERT_TO_LONG(v, a); |
|---|
| 840 | 1500776 | CONVERT_TO_LONG(w, b); |
|---|
| 841 | 749987 | if (b < 0) { |
|---|
| 842 | 4 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
|---|
| 843 | 4 | return NULL; |
|---|
| 844 | n/a | } |
|---|
| 845 | 749983 | if (a == 0 || b == 0) |
|---|
| 846 | 154940 | return int_int(v); |
|---|
| 847 | 595043 | if (b >= LONG_BIT) { |
|---|
| 848 | 159208 | vv = PyLong_FromLong(PyInt_AS_LONG(v)); |
|---|
| 849 | 159208 | if (vv == NULL) |
|---|
| 850 | 0 | return NULL; |
|---|
| 851 | 159208 | ww = PyLong_FromLong(PyInt_AS_LONG(w)); |
|---|
| 852 | 159208 | if (ww == NULL) { |
|---|
| 853 | 0 | Py_DECREF(vv); |
|---|
| 854 | 0 | return NULL; |
|---|
| 855 | n/a | } |
|---|
| 856 | 159208 | result = PyNumber_Lshift(vv, ww); |
|---|
| 857 | 159208 | Py_DECREF(vv); |
|---|
| 858 | 159208 | Py_DECREF(ww); |
|---|
| 859 | 159208 | return result; |
|---|
| 860 | n/a | } |
|---|
| 861 | 435835 | c = a << b; |
|---|
| 862 | 435835 | if (a != Py_ARITHMETIC_RIGHT_SHIFT(long, c, b)) { |
|---|
| 863 | 323696 | vv = PyLong_FromLong(PyInt_AS_LONG(v)); |
|---|
| 864 | 323696 | if (vv == NULL) |
|---|
| 865 | 0 | return NULL; |
|---|
| 866 | 323696 | ww = PyLong_FromLong(PyInt_AS_LONG(w)); |
|---|
| 867 | 323696 | if (ww == NULL) { |
|---|
| 868 | 0 | Py_DECREF(vv); |
|---|
| 869 | 0 | return NULL; |
|---|
| 870 | n/a | } |
|---|
| 871 | 323696 | result = PyNumber_Lshift(vv, ww); |
|---|
| 872 | 323696 | Py_DECREF(vv); |
|---|
| 873 | 323696 | Py_DECREF(ww); |
|---|
| 874 | 323696 | return result; |
|---|
| 875 | n/a | } |
|---|
| 876 | 112139 | return PyInt_FromLong(c); |
|---|
| 877 | n/a | } |
|---|
| 878 | n/a | |
|---|
| 879 | n/a | static PyObject * |
|---|
| 880 | n/a | int_rshift(PyIntObject *v, PyIntObject *w) |
|---|
| 881 | 557653 | { |
|---|
| 882 | n/a | register long a, b; |
|---|
| 883 | 557660 | CONVERT_TO_LONG(v, a); |
|---|
| 884 | 1115292 | CONVERT_TO_LONG(w, b); |
|---|
| 885 | 557621 | if (b < 0) { |
|---|
| 886 | 4 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
|---|
| 887 | 4 | return NULL; |
|---|
| 888 | n/a | } |
|---|
| 889 | 557617 | if (a == 0 || b == 0) |
|---|
| 890 | 22127 | return int_int(v); |
|---|
| 891 | 535490 | if (b >= LONG_BIT) { |
|---|
| 892 | 0 | if (a < 0) |
|---|
| 893 | 0 | a = -1; |
|---|
| 894 | n/a | else |
|---|
| 895 | 0 | a = 0; |
|---|
| 896 | n/a | } |
|---|
| 897 | n/a | else { |
|---|
| 898 | 535490 | a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b); |
|---|
| 899 | n/a | } |
|---|
| 900 | 535490 | return PyInt_FromLong(a); |
|---|
| 901 | n/a | } |
|---|
| 902 | n/a | |
|---|
| 903 | n/a | static PyObject * |
|---|
| 904 | n/a | int_and(PyIntObject *v, PyIntObject *w) |
|---|
| 905 | 1066075 | { |
|---|
| 906 | n/a | register long a, b; |
|---|
| 907 | 1066084 | CONVERT_TO_LONG(v, a); |
|---|
| 908 | 2132132 | CONVERT_TO_LONG(w, b); |
|---|
| 909 | 1059397 | return PyInt_FromLong(a & b); |
|---|
| 910 | n/a | } |
|---|
| 911 | n/a | |
|---|
| 912 | n/a | static PyObject * |
|---|
| 913 | n/a | int_xor(PyIntObject *v, PyIntObject *w) |
|---|
| 914 | 504013 | { |
|---|
| 915 | n/a | register long a, b; |
|---|
| 916 | 504022 | CONVERT_TO_LONG(v, a); |
|---|
| 917 | 1008008 | CONVERT_TO_LONG(w, b); |
|---|
| 918 | 503981 | return PyInt_FromLong(a ^ b); |
|---|
| 919 | n/a | } |
|---|
| 920 | n/a | |
|---|
| 921 | n/a | static PyObject * |
|---|
| 922 | n/a | int_or(PyIntObject *v, PyIntObject *w) |
|---|
| 923 | 476079 | { |
|---|
| 924 | n/a | register long a, b; |
|---|
| 925 | 476088 | CONVERT_TO_LONG(v, a); |
|---|
| 926 | 952140 | CONVERT_TO_LONG(w, b); |
|---|
| 927 | 476032 | return PyInt_FromLong(a | b); |
|---|
| 928 | n/a | } |
|---|
| 929 | n/a | |
|---|
| 930 | n/a | static int |
|---|
| 931 | n/a | int_coerce(PyObject **pv, PyObject **pw) |
|---|
| 932 | 930223 | { |
|---|
| 933 | 930223 | if (PyInt_Check(*pw)) { |
|---|
| 934 | 2 | Py_INCREF(*pv); |
|---|
| 935 | 2 | Py_INCREF(*pw); |
|---|
| 936 | 2 | return 0; |
|---|
| 937 | n/a | } |
|---|
| 938 | 930221 | return 1; /* Can't do it */ |
|---|
| 939 | n/a | } |
|---|
| 940 | n/a | |
|---|
| 941 | n/a | static PyObject * |
|---|
| 942 | n/a | int_int(PyIntObject *v) |
|---|
| 943 | 344427 | { |
|---|
| 944 | 344427 | if (PyInt_CheckExact(v)) |
|---|
| 945 | 333276 | Py_INCREF(v); |
|---|
| 946 | n/a | else |
|---|
| 947 | 11151 | v = (PyIntObject *)PyInt_FromLong(v->ob_ival); |
|---|
| 948 | 344427 | return (PyObject *)v; |
|---|
| 949 | n/a | } |
|---|
| 950 | n/a | |
|---|
| 951 | n/a | static PyObject * |
|---|
| 952 | n/a | int_long(PyIntObject *v) |
|---|
| 953 | 281923 | { |
|---|
| 954 | 281923 | return PyLong_FromLong((v -> ob_ival)); |
|---|
| 955 | n/a | } |
|---|
| 956 | n/a | |
|---|
| 957 | n/a | static const unsigned char BitLengthTable[32] = { |
|---|
| 958 | n/a | 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, |
|---|
| 959 | n/a | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 |
|---|
| 960 | n/a | }; |
|---|
| 961 | n/a | |
|---|
| 962 | n/a | static int |
|---|
| 963 | n/a | bits_in_ulong(unsigned long d) |
|---|
| 964 | 174938 | { |
|---|
| 965 | 174938 | int d_bits = 0; |
|---|
| 966 | 840025 | while (d >= 32) { |
|---|
| 967 | 490149 | d_bits += 6; |
|---|
| 968 | 490149 | d >>= 6; |
|---|
| 969 | n/a | } |
|---|
| 970 | 174938 | d_bits += (int)BitLengthTable[d]; |
|---|
| 971 | 174938 | return d_bits; |
|---|
| 972 | n/a | } |
|---|
| 973 | n/a | |
|---|
| 974 | n/a | #if 8*SIZEOF_LONG-1 <= DBL_MANT_DIG |
|---|
| 975 | n/a | /* Every Python int can be exactly represented as a float. */ |
|---|
| 976 | n/a | |
|---|
| 977 | n/a | static PyObject * |
|---|
| 978 | n/a | int_float(PyIntObject *v) |
|---|
| 979 | n/a | { |
|---|
| 980 | n/a | return PyFloat_FromDouble((double)(v -> ob_ival)); |
|---|
| 981 | n/a | } |
|---|
| 982 | n/a | |
|---|
| 983 | n/a | #else |
|---|
| 984 | n/a | /* Here not all Python ints are exactly representable as floats, so we may |
|---|
| 985 | n/a | have to round. We do this manually, since the C standards don't specify |
|---|
| 986 | n/a | whether converting an integer to a float rounds up or down */ |
|---|
| 987 | n/a | |
|---|
| 988 | n/a | static PyObject * |
|---|
| 989 | n/a | int_float(PyIntObject *v) |
|---|
| 990 | 557033 | { |
|---|
| 991 | n/a | unsigned long abs_ival, lsb; |
|---|
| 992 | n/a | int round_up; |
|---|
| 993 | n/a | |
|---|
| 994 | 557033 | if (v->ob_ival < 0) |
|---|
| 995 | 97361 | abs_ival = 0U-(unsigned long)v->ob_ival; |
|---|
| 996 | n/a | else |
|---|
| 997 | 459672 | abs_ival = (unsigned long)v->ob_ival; |
|---|
| 998 | 557033 | if (abs_ival < (1L << DBL_MANT_DIG)) |
|---|
| 999 | n/a | /* small integer; no need to round */ |
|---|
| 1000 | 550678 | return PyFloat_FromDouble((double)v->ob_ival); |
|---|
| 1001 | n/a | |
|---|
| 1002 | n/a | /* Round abs_ival to MANT_DIG significant bits, using the |
|---|
| 1003 | n/a | round-half-to-even rule. abs_ival & lsb picks out the 'rounding' |
|---|
| 1004 | n/a | bit: the first bit after the most significant MANT_DIG bits of |
|---|
| 1005 | n/a | abs_ival. We round up if this bit is set, provided that either: |
|---|
| 1006 | n/a | |
|---|
| 1007 | n/a | (1) abs_ival isn't exactly halfway between two floats, in which |
|---|
| 1008 | n/a | case at least one of the bits following the rounding bit must be |
|---|
| 1009 | n/a | set; i.e., abs_ival & lsb-1 != 0, or: |
|---|
| 1010 | n/a | |
|---|
| 1011 | n/a | (2) the resulting rounded value has least significant bit 0; or |
|---|
| 1012 | n/a | in other words the bit above the rounding bit is set (this is the |
|---|
| 1013 | n/a | 'to-even' bit of round-half-to-even); i.e., abs_ival & 2*lsb != 0 |
|---|
| 1014 | n/a | |
|---|
| 1015 | n/a | The condition "(1) or (2)" equates to abs_ival & 3*lsb-1 != 0. */ |
|---|
| 1016 | n/a | |
|---|
| 1017 | 6355 | lsb = 1L << (bits_in_ulong(abs_ival)-DBL_MANT_DIG-1); |
|---|
| 1018 | 6355 | round_up = (abs_ival & lsb) && (abs_ival & (3*lsb-1)); |
|---|
| 1019 | 6355 | abs_ival &= -2*lsb; |
|---|
| 1020 | 6355 | if (round_up) |
|---|
| 1021 | 2124 | abs_ival += 2*lsb; |
|---|
| 1022 | 6355 | return PyFloat_FromDouble(v->ob_ival < 0 ? |
|---|
| 1023 | n/a | -(double)abs_ival : |
|---|
| 1024 | n/a | (double)abs_ival); |
|---|
| 1025 | n/a | } |
|---|
| 1026 | n/a | |
|---|
| 1027 | n/a | #endif |
|---|
| 1028 | n/a | |
|---|
| 1029 | n/a | static PyObject * |
|---|
| 1030 | n/a | int_oct(PyIntObject *v) |
|---|
| 1031 | 6 | { |
|---|
| 1032 | 6 | return _PyInt_Format(v, 8, 0); |
|---|
| 1033 | n/a | } |
|---|
| 1034 | n/a | |
|---|
| 1035 | n/a | static PyObject * |
|---|
| 1036 | n/a | int_hex(PyIntObject *v) |
|---|
| 1037 | 328 | { |
|---|
| 1038 | 328 | return _PyInt_Format(v, 16, 0); |
|---|
| 1039 | n/a | } |
|---|
| 1040 | n/a | |
|---|
| 1041 | n/a | static PyObject * |
|---|
| 1042 | n/a | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|---|
| 1043 | n/a | |
|---|
| 1044 | n/a | static PyObject * |
|---|
| 1045 | n/a | int_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 1046 | 4807139 | { |
|---|
| 1047 | 4807139 | PyObject *x = NULL; |
|---|
| 1048 | 4807139 | int base = -909; |
|---|
| 1049 | n/a | static char *kwlist[] = {"x", "base", 0}; |
|---|
| 1050 | n/a | |
|---|
| 1051 | 4807139 | if (type != &PyInt_Type) |
|---|
| 1052 | 40246 | return int_subtype_new(type, args, kwds); /* Wimp out */ |
|---|
| 1053 | 4766893 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, |
|---|
| 1054 | n/a | &x, &base)) |
|---|
| 1055 | 1 | return NULL; |
|---|
| 1056 | 4766892 | if (x == NULL) |
|---|
| 1057 | 32 | return PyInt_FromLong(0L); |
|---|
| 1058 | 4766860 | if (base == -909) |
|---|
| 1059 | 4296258 | return PyNumber_Int(x); |
|---|
| 1060 | 470602 | if (PyString_Check(x)) { |
|---|
| 1061 | n/a | /* Since PyInt_FromString doesn't have a length parameter, |
|---|
| 1062 | n/a | * check here for possible NULs in the string. */ |
|---|
| 1063 | 470585 | char *string = PyString_AS_STRING(x); |
|---|
| 1064 | 470585 | if (strlen(string) != PyString_Size(x)) { |
|---|
| 1065 | n/a | /* create a repr() of the input string, |
|---|
| 1066 | n/a | * just like PyInt_FromString does */ |
|---|
| 1067 | n/a | PyObject *srepr; |
|---|
| 1068 | 2 | srepr = PyObject_Repr(x); |
|---|
| 1069 | 2 | if (srepr == NULL) |
|---|
| 1070 | 0 | return NULL; |
|---|
| 1071 | 2 | PyErr_Format(PyExc_ValueError, |
|---|
| 1072 | n/a | "invalid literal for int() with base %d: %s", |
|---|
| 1073 | n/a | base, PyString_AS_STRING(srepr)); |
|---|
| 1074 | 2 | Py_DECREF(srepr); |
|---|
| 1075 | 2 | return NULL; |
|---|
| 1076 | n/a | } |
|---|
| 1077 | 470583 | return PyInt_FromString(string, NULL, base); |
|---|
| 1078 | n/a | } |
|---|
| 1079 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1080 | 17 | if (PyUnicode_Check(x)) |
|---|
| 1081 | 16 | return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x), |
|---|
| 1082 | n/a | PyUnicode_GET_SIZE(x), |
|---|
| 1083 | n/a | base); |
|---|
| 1084 | n/a | #endif |
|---|
| 1085 | 1 | PyErr_SetString(PyExc_TypeError, |
|---|
| 1086 | n/a | "int() can't convert non-string with explicit base"); |
|---|
| 1087 | 1 | return NULL; |
|---|
| 1088 | n/a | } |
|---|
| 1089 | n/a | |
|---|
| 1090 | n/a | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
|---|
| 1091 | n/a | first create a regular int from whatever arguments we got, |
|---|
| 1092 | n/a | then allocate a subtype instance and initialize its ob_ival |
|---|
| 1093 | n/a | from the regular int. The regular int is then thrown away. |
|---|
| 1094 | n/a | */ |
|---|
| 1095 | n/a | static PyObject * |
|---|
| 1096 | n/a | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 1097 | 40246 | { |
|---|
| 1098 | n/a | PyObject *tmp, *newobj; |
|---|
| 1099 | n/a | long ival; |
|---|
| 1100 | n/a | |
|---|
| 1101 | 40246 | assert(PyType_IsSubtype(type, &PyInt_Type)); |
|---|
| 1102 | 40246 | tmp = int_new(&PyInt_Type, args, kwds); |
|---|
| 1103 | 40246 | if (tmp == NULL) |
|---|
| 1104 | 0 | return NULL; |
|---|
| 1105 | 40246 | if (!PyInt_Check(tmp)) { |
|---|
| 1106 | 1 | ival = PyLong_AsLong(tmp); |
|---|
| 1107 | 1 | if (ival == -1 && PyErr_Occurred()) { |
|---|
| 1108 | 1 | Py_DECREF(tmp); |
|---|
| 1109 | 1 | return NULL; |
|---|
| 1110 | n/a | } |
|---|
| 1111 | n/a | } else { |
|---|
| 1112 | 40245 | ival = ((PyIntObject *)tmp)->ob_ival; |
|---|
| 1113 | n/a | } |
|---|
| 1114 | n/a | |
|---|
| 1115 | 40245 | newobj = type->tp_alloc(type, 0); |
|---|
| 1116 | 40245 | if (newobj == NULL) { |
|---|
| 1117 | 0 | Py_DECREF(tmp); |
|---|
| 1118 | 0 | return NULL; |
|---|
| 1119 | n/a | } |
|---|
| 1120 | 40245 | ((PyIntObject *)newobj)->ob_ival = ival; |
|---|
| 1121 | 40245 | Py_DECREF(tmp); |
|---|
| 1122 | 40245 | return newobj; |
|---|
| 1123 | n/a | } |
|---|
| 1124 | n/a | |
|---|
| 1125 | n/a | static PyObject * |
|---|
| 1126 | n/a | int_getnewargs(PyIntObject *v) |
|---|
| 1127 | 18 | { |
|---|
| 1128 | 18 | return Py_BuildValue("(l)", v->ob_ival); |
|---|
| 1129 | n/a | } |
|---|
| 1130 | n/a | |
|---|
| 1131 | n/a | static PyObject * |
|---|
| 1132 | 1 | int_get0(PyIntObject *v, void *context) { |
|---|
| 1133 | 1 | return PyInt_FromLong(0L); |
|---|
| 1134 | n/a | } |
|---|
| 1135 | n/a | |
|---|
| 1136 | n/a | static PyObject * |
|---|
| 1137 | 1000 | int_get1(PyIntObject *v, void *context) { |
|---|
| 1138 | 1000 | return PyInt_FromLong(1L); |
|---|
| 1139 | n/a | } |
|---|
| 1140 | n/a | |
|---|
| 1141 | n/a | /* Convert an integer to a decimal string. On many platforms, this |
|---|
| 1142 | n/a | will be significantly faster than the general arbitrary-base |
|---|
| 1143 | n/a | conversion machinery in _PyInt_Format, thanks to optimization |
|---|
| 1144 | n/a | opportunities offered by division by a compile-time constant. */ |
|---|
| 1145 | n/a | static PyObject * |
|---|
| 1146 | 975398 | int_to_decimal_string(PyIntObject *v) { |
|---|
| 1147 | n/a | char buf[sizeof(long)*CHAR_BIT/3+6], *p, *bufend; |
|---|
| 1148 | 975398 | long n = v->ob_ival; |
|---|
| 1149 | n/a | unsigned long absn; |
|---|
| 1150 | 975398 | p = bufend = buf + sizeof(buf); |
|---|
| 1151 | 975398 | absn = n < 0 ? 0UL - n : n; |
|---|
| 1152 | n/a | do { |
|---|
| 1153 | 2301967 | *--p = '0' + (char)(absn % 10); |
|---|
| 1154 | 2301967 | absn /= 10; |
|---|
| 1155 | 2301967 | } while (absn); |
|---|
| 1156 | 975398 | if (n < 0) |
|---|
| 1157 | 228223 | *--p = '-'; |
|---|
| 1158 | 975398 | return PyString_FromStringAndSize(p, bufend - p); |
|---|
| 1159 | n/a | } |
|---|
| 1160 | n/a | |
|---|
| 1161 | n/a | /* Convert an integer to the given base. Returns a string. |
|---|
| 1162 | n/a | If base is 2, 8 or 16, add the proper prefix '0b', '0o' or '0x'. |
|---|
| 1163 | n/a | If newstyle is zero, then use the pre-2.6 behavior of octal having |
|---|
| 1164 | n/a | a leading "0" */ |
|---|
| 1165 | n/a | PyAPI_FUNC(PyObject*) |
|---|
| 1166 | n/a | _PyInt_Format(PyIntObject *v, int base, int newstyle) |
|---|
| 1167 | 216868 | { |
|---|
| 1168 | n/a | /* There are no doubt many, many ways to optimize this, using code |
|---|
| 1169 | n/a | similar to _PyLong_Format */ |
|---|
| 1170 | 216868 | long n = v->ob_ival; |
|---|
| 1171 | 216868 | int negative = n < 0; |
|---|
| 1172 | 216868 | int is_zero = n == 0; |
|---|
| 1173 | n/a | |
|---|
| 1174 | n/a | /* For the reasoning behind this size, see |
|---|
| 1175 | n/a | http://c-faq.com/misc/hexio.html. Then, add a few bytes for |
|---|
| 1176 | n/a | the possible sign and prefix "0[box]" */ |
|---|
| 1177 | n/a | char buf[sizeof(n)*CHAR_BIT+6]; |
|---|
| 1178 | n/a | |
|---|
| 1179 | n/a | /* Start by pointing to the end of the buffer. We fill in from |
|---|
| 1180 | n/a | the back forward. */ |
|---|
| 1181 | 216868 | char* p = &buf[sizeof(buf)]; |
|---|
| 1182 | n/a | |
|---|
| 1183 | 216868 | assert(base >= 2 && base <= 36); |
|---|
| 1184 | n/a | |
|---|
| 1185 | n/a | /* Special case base 10, for speed */ |
|---|
| 1186 | 216868 | if (base == 10) |
|---|
| 1187 | 19218 | return int_to_decimal_string(v); |
|---|
| 1188 | n/a | |
|---|
| 1189 | n/a | do { |
|---|
| 1190 | n/a | /* I'd use i_divmod, except it doesn't produce the results |
|---|
| 1191 | n/a | I want when n is negative. So just duplicate the salient |
|---|
| 1192 | n/a | part here. */ |
|---|
| 1193 | 2242906 | long div = n / base; |
|---|
| 1194 | 2242906 | long mod = n - div * base; |
|---|
| 1195 | n/a | |
|---|
| 1196 | n/a | /* convert abs(mod) to the right character in [0-9, a-z] */ |
|---|
| 1197 | 2242906 | char cdigit = (char)(mod < 0 ? -mod : mod); |
|---|
| 1198 | 2242906 | cdigit += (cdigit < 10) ? '0' : 'a'-10; |
|---|
| 1199 | 2242906 | *--p = cdigit; |
|---|
| 1200 | n/a | |
|---|
| 1201 | 2242906 | n = div; |
|---|
| 1202 | 2242906 | } while(n); |
|---|
| 1203 | n/a | |
|---|
| 1204 | 197650 | if (base == 2) { |
|---|
| 1205 | 130041 | *--p = 'b'; |
|---|
| 1206 | 130041 | *--p = '0'; |
|---|
| 1207 | n/a | } |
|---|
| 1208 | 67609 | else if (base == 8) { |
|---|
| 1209 | 51 | if (newstyle) { |
|---|
| 1210 | 45 | *--p = 'o'; |
|---|
| 1211 | 45 | *--p = '0'; |
|---|
| 1212 | n/a | } |
|---|
| 1213 | n/a | else |
|---|
| 1214 | 6 | if (!is_zero) |
|---|
| 1215 | 6 | *--p = '0'; |
|---|
| 1216 | n/a | } |
|---|
| 1217 | 67558 | else if (base == 16) { |
|---|
| 1218 | 67558 | *--p = 'x'; |
|---|
| 1219 | 67558 | *--p = '0'; |
|---|
| 1220 | n/a | } |
|---|
| 1221 | n/a | else { |
|---|
| 1222 | 0 | *--p = '#'; |
|---|
| 1223 | 0 | *--p = '0' + base%10; |
|---|
| 1224 | 0 | if (base > 10) |
|---|
| 1225 | 0 | *--p = '0' + base/10; |
|---|
| 1226 | n/a | } |
|---|
| 1227 | 197650 | if (negative) |
|---|
| 1228 | 65069 | *--p = '-'; |
|---|
| 1229 | n/a | |
|---|
| 1230 | 197650 | return PyString_FromStringAndSize(p, &buf[sizeof(buf)] - p); |
|---|
| 1231 | n/a | } |
|---|
| 1232 | n/a | |
|---|
| 1233 | n/a | static PyObject * |
|---|
| 1234 | n/a | int__format__(PyObject *self, PyObject *args) |
|---|
| 1235 | 476 | { |
|---|
| 1236 | n/a | PyObject *format_spec; |
|---|
| 1237 | n/a | |
|---|
| 1238 | 476 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
|---|
| 1239 | 0 | return NULL; |
|---|
| 1240 | 476 | if (PyBytes_Check(format_spec)) |
|---|
| 1241 | 344 | return _PyInt_FormatAdvanced(self, |
|---|
| 1242 | n/a | PyBytes_AS_STRING(format_spec), |
|---|
| 1243 | n/a | PyBytes_GET_SIZE(format_spec)); |
|---|
| 1244 | 132 | if (PyUnicode_Check(format_spec)) { |
|---|
| 1245 | n/a | /* Convert format_spec to a str */ |
|---|
| 1246 | n/a | PyObject *result; |
|---|
| 1247 | 130 | PyObject *str_spec = PyObject_Str(format_spec); |
|---|
| 1248 | n/a | |
|---|
| 1249 | 130 | if (str_spec == NULL) |
|---|
| 1250 | 0 | return NULL; |
|---|
| 1251 | n/a | |
|---|
| 1252 | 130 | result = _PyInt_FormatAdvanced(self, |
|---|
| 1253 | n/a | PyBytes_AS_STRING(str_spec), |
|---|
| 1254 | n/a | PyBytes_GET_SIZE(str_spec)); |
|---|
| 1255 | n/a | |
|---|
| 1256 | 130 | Py_DECREF(str_spec); |
|---|
| 1257 | 130 | return result; |
|---|
| 1258 | n/a | } |
|---|
| 1259 | 2 | PyErr_SetString(PyExc_TypeError, "__format__ requires str or unicode"); |
|---|
| 1260 | 2 | return NULL; |
|---|
| 1261 | n/a | } |
|---|
| 1262 | n/a | |
|---|
| 1263 | n/a | static PyObject * |
|---|
| 1264 | n/a | int_bit_length(PyIntObject *v) |
|---|
| 1265 | 168583 | { |
|---|
| 1266 | n/a | unsigned long n; |
|---|
| 1267 | n/a | |
|---|
| 1268 | 168583 | if (v->ob_ival < 0) |
|---|
| 1269 | n/a | /* avoid undefined behaviour when v->ob_ival == -LONG_MAX-1 */ |
|---|
| 1270 | 65026 | n = 0U-(unsigned long)v->ob_ival; |
|---|
| 1271 | n/a | else |
|---|
| 1272 | 103557 | n = (unsigned long)v->ob_ival; |
|---|
| 1273 | n/a | |
|---|
| 1274 | 168583 | return PyInt_FromLong(bits_in_ulong(n)); |
|---|
| 1275 | n/a | } |
|---|
| 1276 | n/a | |
|---|
| 1277 | n/a | PyDoc_STRVAR(int_bit_length_doc, |
|---|
| 1278 | n/a | "int.bit_length() -> int\n\ |
|---|
| 1279 | n/a | \n\ |
|---|
| 1280 | n/a | Number of bits necessary to represent self in binary.\n\ |
|---|
| 1281 | n/a | >>> bin(37)\n\ |
|---|
| 1282 | n/a | '0b100101'\n\ |
|---|
| 1283 | n/a | >>> (37).bit_length()\n\ |
|---|
| 1284 | n/a | 6"); |
|---|
| 1285 | n/a | |
|---|
| 1286 | n/a | #if 0 |
|---|
| 1287 | n/a | static PyObject * |
|---|
| 1288 | n/a | int_is_finite(PyObject *v) |
|---|
| 1289 | n/a | { |
|---|
| 1290 | n/a | Py_RETURN_TRUE; |
|---|
| 1291 | n/a | } |
|---|
| 1292 | n/a | #endif |
|---|
| 1293 | n/a | |
|---|
| 1294 | n/a | static PyMethodDef int_methods[] = { |
|---|
| 1295 | n/a | {"conjugate", (PyCFunction)int_int, METH_NOARGS, |
|---|
| 1296 | n/a | "Returns self, the complex conjugate of any int."}, |
|---|
| 1297 | n/a | {"bit_length", (PyCFunction)int_bit_length, METH_NOARGS, |
|---|
| 1298 | n/a | int_bit_length_doc}, |
|---|
| 1299 | n/a | #if 0 |
|---|
| 1300 | n/a | {"is_finite", (PyCFunction)int_is_finite, METH_NOARGS, |
|---|
| 1301 | n/a | "Returns always True."}, |
|---|
| 1302 | n/a | #endif |
|---|
| 1303 | n/a | {"__trunc__", (PyCFunction)int_int, METH_NOARGS, |
|---|
| 1304 | n/a | "Truncating an Integral returns itself."}, |
|---|
| 1305 | n/a | {"__getnewargs__", (PyCFunction)int_getnewargs, METH_NOARGS}, |
|---|
| 1306 | n/a | {"__format__", (PyCFunction)int__format__, METH_VARARGS}, |
|---|
| 1307 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 1308 | n/a | }; |
|---|
| 1309 | n/a | |
|---|
| 1310 | n/a | static PyGetSetDef int_getset[] = { |
|---|
| 1311 | n/a | {"real", |
|---|
| 1312 | n/a | (getter)int_int, (setter)NULL, |
|---|
| 1313 | n/a | "the real part of a complex number", |
|---|
| 1314 | n/a | NULL}, |
|---|
| 1315 | n/a | {"imag", |
|---|
| 1316 | n/a | (getter)int_get0, (setter)NULL, |
|---|
| 1317 | n/a | "the imaginary part of a complex number", |
|---|
| 1318 | n/a | NULL}, |
|---|
| 1319 | n/a | {"numerator", |
|---|
| 1320 | n/a | (getter)int_int, (setter)NULL, |
|---|
| 1321 | n/a | "the numerator of a rational number in lowest terms", |
|---|
| 1322 | n/a | NULL}, |
|---|
| 1323 | n/a | {"denominator", |
|---|
| 1324 | n/a | (getter)int_get1, (setter)NULL, |
|---|
| 1325 | n/a | "the denominator of a rational number in lowest terms", |
|---|
| 1326 | n/a | NULL}, |
|---|
| 1327 | n/a | {NULL} /* Sentinel */ |
|---|
| 1328 | n/a | }; |
|---|
| 1329 | n/a | |
|---|
| 1330 | n/a | PyDoc_STRVAR(int_doc, |
|---|
| 1331 | n/a | "int(x[, base]) -> integer\n\ |
|---|
| 1332 | n/a | \n\ |
|---|
| 1333 | n/a | Convert a string or number to an integer, if possible. A floating point\n\ |
|---|
| 1334 | n/a | argument will be truncated towards zero (this does not include a string\n\ |
|---|
| 1335 | n/a | representation of a floating point number!) When converting a string, use\n\ |
|---|
| 1336 | n/a | the optional base. It is an error to supply a base when converting a\n\ |
|---|
| 1337 | n/a | non-string. If base is zero, the proper base is guessed based on the\n\ |
|---|
| 1338 | n/a | string content. If the argument is outside the integer range a\n\ |
|---|
| 1339 | n/a | long object will be returned instead."); |
|---|
| 1340 | n/a | |
|---|
| 1341 | n/a | static PyNumberMethods int_as_number = { |
|---|
| 1342 | n/a | (binaryfunc)int_add, /*nb_add*/ |
|---|
| 1343 | n/a | (binaryfunc)int_sub, /*nb_subtract*/ |
|---|
| 1344 | n/a | (binaryfunc)int_mul, /*nb_multiply*/ |
|---|
| 1345 | n/a | (binaryfunc)int_classic_div, /*nb_divide*/ |
|---|
| 1346 | n/a | (binaryfunc)int_mod, /*nb_remainder*/ |
|---|
| 1347 | n/a | (binaryfunc)int_divmod, /*nb_divmod*/ |
|---|
| 1348 | n/a | (ternaryfunc)int_pow, /*nb_power*/ |
|---|
| 1349 | n/a | (unaryfunc)int_neg, /*nb_negative*/ |
|---|
| 1350 | n/a | (unaryfunc)int_int, /*nb_positive*/ |
|---|
| 1351 | n/a | (unaryfunc)int_abs, /*nb_absolute*/ |
|---|
| 1352 | n/a | (inquiry)int_nonzero, /*nb_nonzero*/ |
|---|
| 1353 | n/a | (unaryfunc)int_invert, /*nb_invert*/ |
|---|
| 1354 | n/a | (binaryfunc)int_lshift, /*nb_lshift*/ |
|---|
| 1355 | n/a | (binaryfunc)int_rshift, /*nb_rshift*/ |
|---|
| 1356 | n/a | (binaryfunc)int_and, /*nb_and*/ |
|---|
| 1357 | n/a | (binaryfunc)int_xor, /*nb_xor*/ |
|---|
| 1358 | n/a | (binaryfunc)int_or, /*nb_or*/ |
|---|
| 1359 | n/a | int_coerce, /*nb_coerce*/ |
|---|
| 1360 | n/a | (unaryfunc)int_int, /*nb_int*/ |
|---|
| 1361 | n/a | (unaryfunc)int_long, /*nb_long*/ |
|---|
| 1362 | n/a | (unaryfunc)int_float, /*nb_float*/ |
|---|
| 1363 | n/a | (unaryfunc)int_oct, /*nb_oct*/ |
|---|
| 1364 | n/a | (unaryfunc)int_hex, /*nb_hex*/ |
|---|
| 1365 | n/a | 0, /*nb_inplace_add*/ |
|---|
| 1366 | n/a | 0, /*nb_inplace_subtract*/ |
|---|
| 1367 | n/a | 0, /*nb_inplace_multiply*/ |
|---|
| 1368 | n/a | 0, /*nb_inplace_divide*/ |
|---|
| 1369 | n/a | 0, /*nb_inplace_remainder*/ |
|---|
| 1370 | n/a | 0, /*nb_inplace_power*/ |
|---|
| 1371 | n/a | 0, /*nb_inplace_lshift*/ |
|---|
| 1372 | n/a | 0, /*nb_inplace_rshift*/ |
|---|
| 1373 | n/a | 0, /*nb_inplace_and*/ |
|---|
| 1374 | n/a | 0, /*nb_inplace_xor*/ |
|---|
| 1375 | n/a | 0, /*nb_inplace_or*/ |
|---|
| 1376 | n/a | (binaryfunc)int_div, /* nb_floor_divide */ |
|---|
| 1377 | n/a | (binaryfunc)int_true_divide, /* nb_true_divide */ |
|---|
| 1378 | n/a | 0, /* nb_inplace_floor_divide */ |
|---|
| 1379 | n/a | 0, /* nb_inplace_true_divide */ |
|---|
| 1380 | n/a | (unaryfunc)int_int, /* nb_index */ |
|---|
| 1381 | n/a | }; |
|---|
| 1382 | n/a | |
|---|
| 1383 | n/a | PyTypeObject PyInt_Type = { |
|---|
| 1384 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 1385 | n/a | "int", |
|---|
| 1386 | n/a | sizeof(PyIntObject), |
|---|
| 1387 | n/a | 0, |
|---|
| 1388 | n/a | (destructor)int_dealloc, /* tp_dealloc */ |
|---|
| 1389 | n/a | (printfunc)int_print, /* tp_print */ |
|---|
| 1390 | n/a | 0, /* tp_getattr */ |
|---|
| 1391 | n/a | 0, /* tp_setattr */ |
|---|
| 1392 | n/a | (cmpfunc)int_compare, /* tp_compare */ |
|---|
| 1393 | n/a | (reprfunc)int_to_decimal_string, /* tp_repr */ |
|---|
| 1394 | n/a | &int_as_number, /* tp_as_number */ |
|---|
| 1395 | n/a | 0, /* tp_as_sequence */ |
|---|
| 1396 | n/a | 0, /* tp_as_mapping */ |
|---|
| 1397 | n/a | (hashfunc)int_hash, /* tp_hash */ |
|---|
| 1398 | n/a | 0, /* tp_call */ |
|---|
| 1399 | n/a | (reprfunc)int_to_decimal_string, /* tp_str */ |
|---|
| 1400 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 1401 | n/a | 0, /* tp_setattro */ |
|---|
| 1402 | n/a | 0, /* tp_as_buffer */ |
|---|
| 1403 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|---|
| 1404 | n/a | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */ |
|---|
| 1405 | n/a | int_doc, /* tp_doc */ |
|---|
| 1406 | n/a | 0, /* tp_traverse */ |
|---|
| 1407 | n/a | 0, /* tp_clear */ |
|---|
| 1408 | n/a | 0, /* tp_richcompare */ |
|---|
| 1409 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 1410 | n/a | 0, /* tp_iter */ |
|---|
| 1411 | n/a | 0, /* tp_iternext */ |
|---|
| 1412 | n/a | int_methods, /* tp_methods */ |
|---|
| 1413 | n/a | 0, /* tp_members */ |
|---|
| 1414 | n/a | int_getset, /* tp_getset */ |
|---|
| 1415 | n/a | 0, /* tp_base */ |
|---|
| 1416 | n/a | 0, /* tp_dict */ |
|---|
| 1417 | n/a | 0, /* tp_descr_get */ |
|---|
| 1418 | n/a | 0, /* tp_descr_set */ |
|---|
| 1419 | n/a | 0, /* tp_dictoffset */ |
|---|
| 1420 | n/a | 0, /* tp_init */ |
|---|
| 1421 | n/a | 0, /* tp_alloc */ |
|---|
| 1422 | n/a | int_new, /* tp_new */ |
|---|
| 1423 | n/a | (freefunc)int_free, /* tp_free */ |
|---|
| 1424 | n/a | }; |
|---|
| 1425 | n/a | |
|---|
| 1426 | n/a | int |
|---|
| 1427 | n/a | _PyInt_Init(void) |
|---|
| 1428 | 293 | { |
|---|
| 1429 | n/a | PyIntObject *v; |
|---|
| 1430 | n/a | int ival; |
|---|
| 1431 | n/a | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
|---|
| 1432 | 77059 | for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) { |
|---|
| 1433 | 76766 | if (!free_list && (free_list = fill_free_list()) == NULL) |
|---|
| 1434 | 0 | return 0; |
|---|
| 1435 | n/a | /* PyObject_New is inlined */ |
|---|
| 1436 | 76766 | v = free_list; |
|---|
| 1437 | 76766 | free_list = (PyIntObject *)Py_TYPE(v); |
|---|
| 1438 | 76766 | PyObject_INIT(v, &PyInt_Type); |
|---|
| 1439 | 76766 | v->ob_ival = ival; |
|---|
| 1440 | 76766 | small_ints[ival + NSMALLNEGINTS] = v; |
|---|
| 1441 | n/a | } |
|---|
| 1442 | n/a | #endif |
|---|
| 1443 | 293 | return 1; |
|---|
| 1444 | n/a | } |
|---|
| 1445 | n/a | |
|---|
| 1446 | n/a | int |
|---|
| 1447 | n/a | PyInt_ClearFreeList(void) |
|---|
| 1448 | 1352 | { |
|---|
| 1449 | n/a | PyIntObject *p; |
|---|
| 1450 | n/a | PyIntBlock *list, *next; |
|---|
| 1451 | n/a | int i; |
|---|
| 1452 | n/a | int u; /* remaining unfreed ints per block */ |
|---|
| 1453 | 1352 | int freelist_size = 0; |
|---|
| 1454 | n/a | |
|---|
| 1455 | 1352 | list = block_list; |
|---|
| 1456 | 1352 | block_list = NULL; |
|---|
| 1457 | 1352 | free_list = NULL; |
|---|
| 1458 | 3828580 | while (list != NULL) { |
|---|
| 1459 | 3825876 | u = 0; |
|---|
| 1460 | 3825876 | for (i = 0, p = &list->objects[0]; |
|---|
| 1461 | 99472776 | i < N_INTOBJECTS; |
|---|
| 1462 | 91821024 | i++, p++) { |
|---|
| 1463 | 91821024 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
|---|
| 1464 | 85037815 | u++; |
|---|
| 1465 | n/a | } |
|---|
| 1466 | 3825876 | next = list->next; |
|---|
| 1467 | 3825876 | if (u) { |
|---|
| 1468 | 3700836 | list->next = block_list; |
|---|
| 1469 | 3700836 | block_list = list; |
|---|
| 1470 | 3700836 | for (i = 0, p = &list->objects[0]; |
|---|
| 1471 | 96221736 | i < N_INTOBJECTS; |
|---|
| 1472 | 88820064 | i++, p++) { |
|---|
| 1473 | 92602313 | if (!PyInt_CheckExact(p) || |
|---|
| 1474 | n/a | p->ob_refcnt == 0) { |
|---|
| 1475 | 3782249 | Py_TYPE(p) = (struct _typeobject *) |
|---|
| 1476 | n/a | free_list; |
|---|
| 1477 | 3782249 | free_list = p; |
|---|
| 1478 | n/a | } |
|---|
| 1479 | n/a | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
|---|
| 1480 | 85037815 | else if (-NSMALLNEGINTS <= p->ob_ival && |
|---|
| 1481 | n/a | p->ob_ival < NSMALLPOSINTS && |
|---|
| 1482 | n/a | small_ints[p->ob_ival + |
|---|
| 1483 | n/a | NSMALLNEGINTS] == NULL) { |
|---|
| 1484 | 4394 | Py_INCREF(p); |
|---|
| 1485 | 4394 | small_ints[p->ob_ival + |
|---|
| 1486 | n/a | NSMALLNEGINTS] = p; |
|---|
| 1487 | n/a | } |
|---|
| 1488 | n/a | #endif |
|---|
| 1489 | n/a | } |
|---|
| 1490 | n/a | } |
|---|
| 1491 | n/a | else { |
|---|
| 1492 | 125040 | PyMem_FREE(list); |
|---|
| 1493 | n/a | } |
|---|
| 1494 | 3825876 | freelist_size += u; |
|---|
| 1495 | 3825876 | list = next; |
|---|
| 1496 | n/a | } |
|---|
| 1497 | n/a | |
|---|
| 1498 | 1352 | return freelist_size; |
|---|
| 1499 | n/a | } |
|---|
| 1500 | n/a | |
|---|
| 1501 | n/a | void |
|---|
| 1502 | n/a | PyInt_Fini(void) |
|---|
| 1503 | 293 | { |
|---|
| 1504 | n/a | PyIntObject *p; |
|---|
| 1505 | n/a | PyIntBlock *list; |
|---|
| 1506 | n/a | int i; |
|---|
| 1507 | n/a | int u; /* total unfreed ints per block */ |
|---|
| 1508 | n/a | |
|---|
| 1509 | n/a | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
|---|
| 1510 | n/a | PyIntObject **q; |
|---|
| 1511 | n/a | |
|---|
| 1512 | 293 | i = NSMALLNEGINTS + NSMALLPOSINTS; |
|---|
| 1513 | 293 | q = small_ints; |
|---|
| 1514 | 77352 | while (--i >= 0) { |
|---|
| 1515 | 76766 | Py_XDECREF(*q); |
|---|
| 1516 | 76766 | *q++ = NULL; |
|---|
| 1517 | n/a | } |
|---|
| 1518 | n/a | #endif |
|---|
| 1519 | 293 | u = PyInt_ClearFreeList(); |
|---|
| 1520 | 293 | if (!Py_VerboseFlag) |
|---|
| 1521 | 293 | return; |
|---|
| 1522 | 0 | fprintf(stderr, "# cleanup ints"); |
|---|
| 1523 | 0 | if (!u) { |
|---|
| 1524 | 0 | fprintf(stderr, "\n"); |
|---|
| 1525 | n/a | } |
|---|
| 1526 | n/a | else { |
|---|
| 1527 | 0 | fprintf(stderr, |
|---|
| 1528 | n/a | ": %d unfreed int%s\n", |
|---|
| 1529 | n/a | u, u == 1 ? "" : "s"); |
|---|
| 1530 | n/a | } |
|---|
| 1531 | 0 | if (Py_VerboseFlag > 1) { |
|---|
| 1532 | 0 | list = block_list; |
|---|
| 1533 | 0 | while (list != NULL) { |
|---|
| 1534 | 0 | for (i = 0, p = &list->objects[0]; |
|---|
| 1535 | 0 | i < N_INTOBJECTS; |
|---|
| 1536 | 0 | i++, p++) { |
|---|
| 1537 | 0 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
|---|
| 1538 | n/a | /* XXX(twouters) cast refcount to |
|---|
| 1539 | n/a | long until %zd is universally |
|---|
| 1540 | n/a | available |
|---|
| 1541 | n/a | */ |
|---|
| 1542 | 0 | fprintf(stderr, |
|---|
| 1543 | n/a | "# <int at %p, refcnt=%ld, val=%ld>\n", |
|---|
| 1544 | n/a | p, (long)p->ob_refcnt, |
|---|
| 1545 | n/a | p->ob_ival); |
|---|
| 1546 | n/a | } |
|---|
| 1547 | 0 | list = list->next; |
|---|
| 1548 | n/a | } |
|---|
| 1549 | n/a | } |
|---|
| 1550 | n/a | } |
|---|