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