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