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; |
| 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); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 154 | |
| 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 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +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 | } |
| 169 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 170 | val = PyInt_AS_LONG(io); |
| 171 | Py_DECREF(io); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 172 | |
| 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]; |
| 222 | |
| 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 | /* |
| 315 | Integer overflow checking used to be done using a double, but on 64 |
| 316 | bit machines (where both long and double are 64 bit) this fails |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 317 | because the double doesn't have enough precision. John Tromp suggests |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 318 | the following algorithm: |
| 319 | |
| 320 | Suppose again we normalize a and b to be nonnegative. |
| 321 | Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.). |
| 322 | Now we test ah and bh against zero and get essentially 3 possible outcomes. |
| 323 | |
| 324 | 1) both ah and bh > 0 : then report overflow |
| 325 | |
| 326 | 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out |
| 327 | negative |
| 328 | |
| 329 | 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31 |
| 330 | compute al*bl and report overflow if it's negative |
| 331 | add (ah*bl)<<32 to al*bl and report overflow if |
| 332 | it's negative |
| 333 | |
| 334 | In case of no overflow the result is then negated if necessary. |
| 335 | |
| 336 | The majority of cases will be 2), in which case this method is the same as |
| 337 | what I suggested before. If multiplication is expensive enough, then the |
| 338 | other method is faster on case 3), but also more work to program, so I |
| 339 | guess the above is the preferred solution. |
| 340 | |
| 341 | */ |
| 342 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 343 | static PyObject * |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 344 | int_mul(PyObject *v, PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 346 | long a, b, ah, bh, x, y; |
| 347 | int s = 1; |
| 348 | |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 349 | if (!PyInt_Check(v) && |
| 350 | v->ob_type->tp_as_sequence && |
| 351 | v->ob_type->tp_as_sequence->sq_repeat) { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 352 | /* sequence * int */ |
| 353 | a = PyInt_AsLong(w); |
| 354 | return (*v->ob_type->tp_as_sequence->sq_repeat)(v, a); |
| 355 | } |
Guido van Rossum | 7e35d57 | 2001-09-15 03:14:32 +0000 | [diff] [blame] | 356 | if (!PyInt_Check(w) && |
| 357 | w->ob_type->tp_as_sequence && |
| 358 | w->ob_type->tp_as_sequence->sq_repeat) { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 359 | /* int * sequence */ |
| 360 | a = PyInt_AsLong(v); |
| 361 | return (*w->ob_type->tp_as_sequence->sq_repeat)(w, a); |
| 362 | } |
| 363 | |
| 364 | CONVERT_TO_LONG(v, a); |
| 365 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 366 | ah = a >> (LONG_BIT/2); |
| 367 | bh = b >> (LONG_BIT/2); |
| 368 | |
| 369 | /* Quick test for common case: two small positive ints */ |
| 370 | |
| 371 | if (ah == 0 && bh == 0) { |
| 372 | x = a*b; |
| 373 | if (x < 0) |
| 374 | goto bad; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 375 | return PyInt_FromLong(x); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /* Arrange that a >= b >= 0 */ |
| 379 | |
| 380 | if (a < 0) { |
| 381 | a = -a; |
| 382 | if (a < 0) { |
| 383 | /* Largest negative */ |
| 384 | if (b == 0 || b == 1) { |
| 385 | x = a*b; |
| 386 | goto ok; |
| 387 | } |
| 388 | else |
| 389 | goto bad; |
| 390 | } |
| 391 | s = -s; |
| 392 | ah = a >> (LONG_BIT/2); |
| 393 | } |
| 394 | if (b < 0) { |
| 395 | b = -b; |
| 396 | if (b < 0) { |
| 397 | /* Largest negative */ |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 398 | if (a == 0 || (a == 1 && s == 1)) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 399 | x = a*b; |
| 400 | goto ok; |
| 401 | } |
| 402 | else |
| 403 | goto bad; |
| 404 | } |
| 405 | s = -s; |
| 406 | bh = b >> (LONG_BIT/2); |
| 407 | } |
| 408 | |
| 409 | /* 1) both ah and bh > 0 : then report overflow */ |
| 410 | |
| 411 | if (ah != 0 && bh != 0) |
| 412 | goto bad; |
| 413 | |
| 414 | /* 2) both ah and bh = 0 : then compute a*b and report |
| 415 | overflow if it comes out negative */ |
| 416 | |
| 417 | if (ah == 0 && bh == 0) { |
| 418 | x = a*b; |
| 419 | if (x < 0) |
| 420 | goto bad; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 421 | return PyInt_FromLong(x*s); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | if (a < b) { |
| 425 | /* Swap */ |
| 426 | x = a; |
| 427 | a = b; |
| 428 | b = x; |
| 429 | ah = bh; |
| 430 | /* bh not used beyond this point */ |
| 431 | } |
| 432 | |
| 433 | /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if |
| 434 | it's >= 2^31 |
| 435 | compute al*bl and report overflow if it's negative |
| 436 | add (ah*bl)<<32 to al*bl and report overflow if |
| 437 | it's negative |
| 438 | (NB b == bl in this case, and we make a = al) */ |
| 439 | |
| 440 | y = ah*b; |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 441 | if (y >= (1L << (LONG_BIT/2 - 1))) |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 442 | goto bad; |
| 443 | a &= (1L << (LONG_BIT/2)) - 1; |
| 444 | x = a*b; |
| 445 | if (x < 0) |
| 446 | goto bad; |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 447 | x += y << (LONG_BIT/2); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 448 | if (x < 0) |
| 449 | goto bad; |
| 450 | ok: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 451 | return PyInt_FromLong(x * s); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 452 | |
| 453 | bad: |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 454 | if (err_ovf("integer multiplication")) |
| 455 | return NULL; |
| 456 | return PyLong_Type.tp_as_number->nb_multiply(v, w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 459 | /* Return type of i_divmod */ |
| 460 | enum divmod_result { |
| 461 | DIVMOD_OK, /* Correct result */ |
| 462 | DIVMOD_OVERFLOW, /* Overflow, try again using longs */ |
| 463 | DIVMOD_ERROR /* Exception raised */ |
| 464 | }; |
| 465 | |
| 466 | static enum divmod_result |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 467 | i_divmod(register long x, register long y, |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 468 | long *p_xdivy, long *p_xmody) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 469 | { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 470 | long xdivy, xmody; |
| 471 | |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 472 | if (y == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 473 | PyErr_SetString(PyExc_ZeroDivisionError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 474 | "integer division or modulo by zero"); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 475 | return DIVMOD_ERROR; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 476 | } |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 477 | /* (-sys.maxint-1)/-1 is the only overflow case. */ |
| 478 | if (y == -1 && x < 0 && x == -x) { |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 479 | if (err_ovf("integer division")) |
| 480 | return DIVMOD_ERROR; |
| 481 | return DIVMOD_OVERFLOW; |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 482 | } |
Tim Peters | 1dad6a8 | 2001-06-18 19:21:11 +0000 | [diff] [blame] | 483 | xdivy = x / y; |
| 484 | xmody = x - xdivy * y; |
| 485 | /* If the signs of x and y differ, and the remainder is non-0, |
| 486 | * C89 doesn't define whether xdivy is now the floor or the |
| 487 | * ceiling of the infinitely precise quotient. We want the floor, |
| 488 | * and we have it iff the remainder's sign matches y's. |
| 489 | */ |
| 490 | if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) { |
| 491 | xmody += y; |
| 492 | --xdivy; |
| 493 | assert(xmody && ((y ^ xmody) >= 0)); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 494 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 495 | *p_xdivy = xdivy; |
| 496 | *p_xmody = xmody; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 497 | return DIVMOD_OK; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 500 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 501 | int_div(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 502 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 503 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 504 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 505 | CONVERT_TO_LONG(x, xi); |
| 506 | CONVERT_TO_LONG(y, yi); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 507 | switch (i_divmod(xi, yi, &d, &m)) { |
| 508 | case DIVMOD_OK: |
| 509 | return PyInt_FromLong(d); |
| 510 | case DIVMOD_OVERFLOW: |
| 511 | return PyLong_Type.tp_as_number->nb_divide((PyObject *)x, |
| 512 | (PyObject *)y); |
| 513 | default: |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 514 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 515 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 518 | static PyObject * |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 519 | int_classic_div(PyIntObject *x, PyIntObject *y) |
| 520 | { |
| 521 | long xi, yi; |
| 522 | long d, m; |
| 523 | CONVERT_TO_LONG(x, xi); |
| 524 | CONVERT_TO_LONG(y, yi); |
| 525 | if (Py_DivisionWarningFlag && |
| 526 | PyErr_Warn(PyExc_DeprecationWarning, "classic int division") < 0) |
| 527 | return NULL; |
| 528 | switch (i_divmod(xi, yi, &d, &m)) { |
| 529 | case DIVMOD_OK: |
| 530 | return PyInt_FromLong(d); |
| 531 | case DIVMOD_OVERFLOW: |
| 532 | return PyLong_Type.tp_as_number->nb_divide((PyObject *)x, |
| 533 | (PyObject *)y); |
| 534 | default: |
| 535 | return NULL; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | static PyObject * |
Tim Peters | 9c1d7fd | 2001-09-04 05:52:47 +0000 | [diff] [blame] | 540 | int_true_divide(PyObject *v, PyObject *w) |
| 541 | { |
Tim Peters | e2a6000 | 2001-09-04 06:17:36 +0000 | [diff] [blame] | 542 | /* If they aren't both ints, give someone else a chance. In |
| 543 | particular, this lets int/long get handled by longs, which |
| 544 | underflows to 0 gracefully if the long is too big to convert |
| 545 | to float. */ |
| 546 | if (PyInt_Check(v) && PyInt_Check(w)) |
| 547 | return PyFloat_Type.tp_as_number->nb_true_divide(v, w); |
| 548 | Py_INCREF(Py_NotImplemented); |
| 549 | return Py_NotImplemented; |
Tim Peters | 9c1d7fd | 2001-09-04 05:52:47 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 553 | int_mod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 554 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 555 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 556 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 557 | CONVERT_TO_LONG(x, xi); |
| 558 | CONVERT_TO_LONG(y, yi); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 559 | switch (i_divmod(xi, yi, &d, &m)) { |
| 560 | case DIVMOD_OK: |
| 561 | return PyInt_FromLong(m); |
| 562 | case DIVMOD_OVERFLOW: |
| 563 | return PyLong_Type.tp_as_number->nb_remainder((PyObject *)x, |
| 564 | (PyObject *)y); |
| 565 | default: |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 566 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 567 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 570 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 571 | int_divmod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 572 | { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 573 | long xi, yi; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 574 | long d, m; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 575 | CONVERT_TO_LONG(x, xi); |
| 576 | CONVERT_TO_LONG(y, yi); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 577 | switch (i_divmod(xi, yi, &d, &m)) { |
| 578 | case DIVMOD_OK: |
| 579 | return Py_BuildValue("(ll)", d, m); |
| 580 | case DIVMOD_OVERFLOW: |
| 581 | return PyLong_Type.tp_as_number->nb_divmod((PyObject *)x, |
| 582 | (PyObject *)y); |
| 583 | default: |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 584 | return NULL; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 585 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 588 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 589 | int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 590 | { |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 591 | register long iv, iw, iz=0, ix, temp, prev; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 592 | CONVERT_TO_LONG(v, iv); |
| 593 | CONVERT_TO_LONG(w, iw); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 594 | if (iw < 0) { |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 595 | if ((PyObject *)z != Py_None) { |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 596 | PyErr_SetString(PyExc_TypeError, "pow() 2nd argument " |
| 597 | "cannot be negative when 3rd argument specified"); |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 598 | return NULL; |
| 599 | } |
Guido van Rossum | b82fedc | 2001-07-12 11:19:45 +0000 | [diff] [blame] | 600 | /* Return a float. This works because we know that |
| 601 | this calls float_pow() which converts its |
| 602 | arguments to double. */ |
| 603 | return PyFloat_Type.tp_as_number->nb_power( |
| 604 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 605 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 606 | if ((PyObject *)z != Py_None) { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 607 | CONVERT_TO_LONG(z, iz); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 608 | if (iz == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 609 | PyErr_SetString(PyExc_ValueError, |
Tim Peters | 4c483c4 | 2001-09-05 06:24:58 +0000 | [diff] [blame] | 610 | "pow() 3rd argument cannot be 0"); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 611 | return NULL; |
| 612 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 613 | } |
| 614 | /* |
| 615 | * XXX: The original exponentiation code stopped looping |
| 616 | * when temp hit zero; this code will continue onwards |
| 617 | * unnecessarily, but at least it won't cause any errors. |
| 618 | * Hopefully the speed improvement from the fast exponentiation |
| 619 | * will compensate for the slight inefficiency. |
| 620 | * XXX: Better handling of overflows is desperately needed. |
| 621 | */ |
| 622 | temp = iv; |
| 623 | ix = 1; |
| 624 | while (iw > 0) { |
| 625 | prev = ix; /* Save value for overflow check */ |
| 626 | if (iw & 1) { |
| 627 | ix = ix*temp; |
| 628 | if (temp == 0) |
| 629 | break; /* Avoid ix / 0 */ |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 630 | if (ix / temp != prev) { |
| 631 | if (err_ovf("integer exponentiation")) |
| 632 | return NULL; |
| 633 | return PyLong_Type.tp_as_number->nb_power( |
| 634 | (PyObject *)v, |
| 635 | (PyObject *)w, |
Tim Peters | 31960db | 2001-08-23 21:28:33 +0000 | [diff] [blame] | 636 | (PyObject *)z); |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 637 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 638 | } |
| 639 | iw >>= 1; /* Shift exponent down by 1 bit */ |
| 640 | if (iw==0) break; |
| 641 | prev = temp; |
| 642 | temp *= temp; /* Square the value of temp */ |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 643 | if (prev!=0 && temp/prev!=prev) { |
| 644 | if (err_ovf("integer exponentiation")) |
| 645 | return NULL; |
| 646 | return PyLong_Type.tp_as_number->nb_power( |
| 647 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
| 648 | } |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 649 | if (iz) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 650 | /* If we did a multiplication, perform a modulo */ |
| 651 | ix = ix % iz; |
| 652 | temp = temp % iz; |
| 653 | } |
| 654 | } |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 655 | if (iz) { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 656 | long div, mod; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 657 | switch (i_divmod(ix, iz, &div, &mod)) { |
| 658 | case DIVMOD_OK: |
| 659 | ix = mod; |
| 660 | break; |
| 661 | case DIVMOD_OVERFLOW: |
| 662 | return PyLong_Type.tp_as_number->nb_power( |
| 663 | (PyObject *)v, (PyObject *)w, (PyObject *)z); |
| 664 | default: |
| 665 | return NULL; |
| 666 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 667 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 668 | return PyInt_FromLong(ix); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 669 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 670 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 671 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 672 | int_neg(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 673 | { |
| 674 | register long a, x; |
| 675 | a = v->ob_ival; |
| 676 | x = -a; |
Guido van Rossum | e27f795 | 2001-08-23 02:59:04 +0000 | [diff] [blame] | 677 | if (a < 0 && x < 0) { |
| 678 | if (err_ovf("integer negation")) |
| 679 | return NULL; |
| 680 | return PyNumber_Negative(PyLong_FromLong(a)); |
| 681 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 682 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 685 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 686 | int_pos(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 687 | { |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 688 | if (PyInt_CheckExact(v)) { |
| 689 | Py_INCREF(v); |
| 690 | return (PyObject *)v; |
| 691 | } |
| 692 | else |
| 693 | return PyInt_FromLong(v->ob_ival); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +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_abs(PyIntObject *v) |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 698 | { |
| 699 | if (v->ob_ival >= 0) |
| 700 | return int_pos(v); |
| 701 | else |
| 702 | return int_neg(v); |
| 703 | } |
| 704 | |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 705 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 706 | int_nonzero(PyIntObject *v) |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 707 | { |
| 708 | return v->ob_ival != 0; |
| 709 | } |
| 710 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 711 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 712 | int_invert(PyIntObject *v) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 713 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 714 | return PyInt_FromLong(~v->ob_ival); |
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_lshift(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 | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 723 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 724 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 725 | return NULL; |
| 726 | } |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 727 | if (a == 0 || b == 0) |
| 728 | return int_pos(v); |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 729 | if (b >= LONG_BIT) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 730 | return PyInt_FromLong(0L); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 731 | } |
Fred Drake | 1bc8fab | 2001-07-19 21:49:38 +0000 | [diff] [blame] | 732 | a = (long)((unsigned long)a << b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 733 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 736 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 737 | int_rshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 738 | { |
| 739 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 740 | CONVERT_TO_LONG(v, a); |
| 741 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 742 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 743 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 744 | return NULL; |
| 745 | } |
Tim Peters | 73a1dfe | 2001-09-11 21:44:14 +0000 | [diff] [blame] | 746 | if (a == 0 || b == 0) |
| 747 | return int_pos(v); |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 748 | if (b >= LONG_BIT) { |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 749 | if (a < 0) |
| 750 | a = -1; |
| 751 | else |
| 752 | a = 0; |
| 753 | } |
| 754 | else { |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 755 | a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 756 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 757 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 760 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 761 | int_and(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 762 | { |
| 763 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 764 | CONVERT_TO_LONG(v, a); |
| 765 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 766 | return PyInt_FromLong(a & b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 769 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 770 | int_xor(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 771 | { |
| 772 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 773 | CONVERT_TO_LONG(v, a); |
| 774 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 775 | return PyInt_FromLong(a ^ b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 778 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 779 | int_or(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 780 | { |
| 781 | register long a, b; |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 782 | CONVERT_TO_LONG(v, a); |
| 783 | CONVERT_TO_LONG(w, b); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 784 | return PyInt_FromLong(a | b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 787 | static int |
| 788 | int_coerce(PyObject **pv, PyObject **pw) |
| 789 | { |
| 790 | if (PyInt_Check(*pw)) { |
| 791 | Py_INCREF(*pv); |
| 792 | Py_INCREF(*pw); |
| 793 | return 0; |
| 794 | } |
| 795 | return 1; /* Can't do it */ |
| 796 | } |
| 797 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 798 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 799 | int_int(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 800 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 801 | Py_INCREF(v); |
| 802 | return (PyObject *)v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 805 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 806 | int_long(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 807 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 808 | return PyLong_FromLong((v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 811 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 812 | int_float(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 813 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 814 | return PyFloat_FromDouble((double)(v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 817 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 818 | int_oct(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 819 | { |
Guido van Rossum | 6f72f97 | 1997-01-14 15:43:41 +0000 | [diff] [blame] | 820 | char buf[100]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 821 | long x = v -> ob_ival; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 822 | if (x == 0) |
| 823 | strcpy(buf, "0"); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 824 | else |
Barry Warsaw | 6197509 | 2001-11-28 20:55:34 +0000 | [diff] [blame] | 825 | PyOS_snprintf(buf, sizeof(buf), "0%lo", x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 826 | return PyString_FromString(buf); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 829 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 830 | int_hex(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 831 | { |
Guido van Rossum | 6f72f97 | 1997-01-14 15:43:41 +0000 | [diff] [blame] | 832 | char buf[100]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 833 | long x = v -> ob_ival; |
Barry Warsaw | 6197509 | 2001-11-28 20:55:34 +0000 | [diff] [blame] | 834 | PyOS_snprintf(buf, sizeof(buf), "0x%lx", x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 835 | return PyString_FromString(buf); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 838 | staticforward PyObject * |
| 839 | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 840 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 841 | static PyObject * |
| 842 | int_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 843 | { |
| 844 | PyObject *x = NULL; |
| 845 | int base = -909; |
| 846 | static char *kwlist[] = {"x", "base", 0}; |
| 847 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 848 | if (type != &PyInt_Type) |
| 849 | return int_subtype_new(type, args, kwds); /* Wimp out */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 850 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist, |
| 851 | &x, &base)) |
| 852 | return NULL; |
| 853 | if (x == NULL) |
| 854 | return PyInt_FromLong(0L); |
| 855 | if (base == -909) |
| 856 | return PyNumber_Int(x); |
| 857 | if (PyString_Check(x)) |
| 858 | return PyInt_FromString(PyString_AS_STRING(x), NULL, base); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 859 | #ifdef Py_USING_UNICODE |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 860 | if (PyUnicode_Check(x)) |
| 861 | return PyInt_FromUnicode(PyUnicode_AS_UNICODE(x), |
| 862 | PyUnicode_GET_SIZE(x), |
| 863 | base); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 864 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 865 | PyErr_SetString(PyExc_TypeError, |
| 866 | "int() can't convert non-string with explicit base"); |
| 867 | return NULL; |
| 868 | } |
| 869 | |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 870 | /* Wimpy, slow approach to tp_new calls for subtypes of int: |
| 871 | first create a regular int from whatever arguments we got, |
| 872 | then allocate a subtype instance and initialize its ob_ival |
| 873 | from the regular int. The regular int is then thrown away. |
| 874 | */ |
| 875 | static PyObject * |
| 876 | int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 877 | { |
| 878 | PyObject *tmp, *new; |
| 879 | |
| 880 | assert(PyType_IsSubtype(type, &PyInt_Type)); |
| 881 | tmp = int_new(&PyInt_Type, args, kwds); |
| 882 | if (tmp == NULL) |
| 883 | return NULL; |
| 884 | assert(PyInt_Check(tmp)); |
Guido van Rossum | d93dce1 | 2001-08-30 03:09:31 +0000 | [diff] [blame] | 885 | new = type->tp_alloc(type, 0); |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 886 | if (new == NULL) |
| 887 | return NULL; |
| 888 | ((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival; |
| 889 | Py_DECREF(tmp); |
| 890 | return new; |
| 891 | } |
| 892 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 893 | static char int_doc[] = |
| 894 | "int(x[, base]) -> integer\n\ |
| 895 | \n\ |
| 896 | Convert a string or number to an integer, if possible. A floating point\n\ |
| 897 | argument will be truncated towards zero (this does not include a string\n\ |
| 898 | representation of a floating point number!) When converting a string, use\n\ |
| 899 | the optional base. It is an error to supply a base when converting a\n\ |
| 900 | non-string."; |
| 901 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 902 | static PyNumberMethods int_as_number = { |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 903 | (binaryfunc)int_add, /*nb_add*/ |
| 904 | (binaryfunc)int_sub, /*nb_subtract*/ |
| 905 | (binaryfunc)int_mul, /*nb_multiply*/ |
Guido van Rossum | 393661d | 2001-08-31 17:40:15 +0000 | [diff] [blame] | 906 | (binaryfunc)int_classic_div, /*nb_divide*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 907 | (binaryfunc)int_mod, /*nb_remainder*/ |
| 908 | (binaryfunc)int_divmod, /*nb_divmod*/ |
| 909 | (ternaryfunc)int_pow, /*nb_power*/ |
| 910 | (unaryfunc)int_neg, /*nb_negative*/ |
| 911 | (unaryfunc)int_pos, /*nb_positive*/ |
| 912 | (unaryfunc)int_abs, /*nb_absolute*/ |
| 913 | (inquiry)int_nonzero, /*nb_nonzero*/ |
| 914 | (unaryfunc)int_invert, /*nb_invert*/ |
| 915 | (binaryfunc)int_lshift, /*nb_lshift*/ |
| 916 | (binaryfunc)int_rshift, /*nb_rshift*/ |
| 917 | (binaryfunc)int_and, /*nb_and*/ |
| 918 | (binaryfunc)int_xor, /*nb_xor*/ |
| 919 | (binaryfunc)int_or, /*nb_or*/ |
Guido van Rossum | 1952e38 | 2001-09-19 01:25:16 +0000 | [diff] [blame] | 920 | int_coerce, /*nb_coerce*/ |
Neil Schemenauer | 139e72a | 2001-01-04 01:45:33 +0000 | [diff] [blame] | 921 | (unaryfunc)int_int, /*nb_int*/ |
| 922 | (unaryfunc)int_long, /*nb_long*/ |
| 923 | (unaryfunc)int_float, /*nb_float*/ |
| 924 | (unaryfunc)int_oct, /*nb_oct*/ |
| 925 | (unaryfunc)int_hex, /*nb_hex*/ |
| 926 | 0, /*nb_inplace_add*/ |
| 927 | 0, /*nb_inplace_subtract*/ |
| 928 | 0, /*nb_inplace_multiply*/ |
| 929 | 0, /*nb_inplace_divide*/ |
| 930 | 0, /*nb_inplace_remainder*/ |
| 931 | 0, /*nb_inplace_power*/ |
| 932 | 0, /*nb_inplace_lshift*/ |
| 933 | 0, /*nb_inplace_rshift*/ |
| 934 | 0, /*nb_inplace_and*/ |
| 935 | 0, /*nb_inplace_xor*/ |
| 936 | 0, /*nb_inplace_or*/ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 937 | (binaryfunc)int_div, /* nb_floor_divide */ |
| 938 | int_true_divide, /* nb_true_divide */ |
| 939 | 0, /* nb_inplace_floor_divide */ |
| 940 | 0, /* nb_inplace_true_divide */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 941 | }; |
| 942 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 943 | PyTypeObject PyInt_Type = { |
| 944 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 945 | 0, |
| 946 | "int", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 947 | sizeof(PyIntObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 948 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 949 | (destructor)int_dealloc, /* tp_dealloc */ |
| 950 | (printfunc)int_print, /* tp_print */ |
| 951 | 0, /* tp_getattr */ |
| 952 | 0, /* tp_setattr */ |
| 953 | (cmpfunc)int_compare, /* tp_compare */ |
| 954 | (reprfunc)int_repr, /* tp_repr */ |
| 955 | &int_as_number, /* tp_as_number */ |
| 956 | 0, /* tp_as_sequence */ |
| 957 | 0, /* tp_as_mapping */ |
| 958 | (hashfunc)int_hash, /* tp_hash */ |
| 959 | 0, /* tp_call */ |
| 960 | 0, /* tp_str */ |
| 961 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 962 | 0, /* tp_setattro */ |
| 963 | 0, /* tp_as_buffer */ |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 964 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
| 965 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 966 | int_doc, /* tp_doc */ |
| 967 | 0, /* tp_traverse */ |
| 968 | 0, /* tp_clear */ |
| 969 | 0, /* tp_richcompare */ |
| 970 | 0, /* tp_weaklistoffset */ |
| 971 | 0, /* tp_iter */ |
| 972 | 0, /* tp_iternext */ |
| 973 | 0, /* tp_methods */ |
| 974 | 0, /* tp_members */ |
| 975 | 0, /* tp_getset */ |
| 976 | 0, /* tp_base */ |
| 977 | 0, /* tp_dict */ |
| 978 | 0, /* tp_descr_get */ |
| 979 | 0, /* tp_descr_set */ |
| 980 | 0, /* tp_dictoffset */ |
| 981 | 0, /* tp_init */ |
| 982 | 0, /* tp_alloc */ |
| 983 | int_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 984 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 985 | |
| 986 | void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 987 | PyInt_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 988 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 989 | PyIntObject *p; |
| 990 | PyIntBlock *list, *next; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 991 | int i; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 992 | int bc, bf; /* block count, number of freed blocks */ |
| 993 | int irem, isum; /* remaining unfreed ints per block, total */ |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 994 | |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 995 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 996 | PyIntObject **q; |
| 997 | |
| 998 | i = NSMALLNEGINTS + NSMALLPOSINTS; |
| 999 | q = small_ints; |
| 1000 | while (--i >= 0) { |
| 1001 | Py_XDECREF(*q); |
| 1002 | *q++ = NULL; |
| 1003 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1004 | #endif |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1005 | bc = 0; |
| 1006 | bf = 0; |
| 1007 | isum = 0; |
| 1008 | list = block_list; |
| 1009 | block_list = NULL; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1010 | free_list = NULL; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1011 | while (list != NULL) { |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1012 | bc++; |
| 1013 | irem = 0; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1014 | for (i = 0, p = &list->objects[0]; |
| 1015 | i < N_INTOBJECTS; |
| 1016 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1017 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1018 | irem++; |
| 1019 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1020 | next = list->next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1021 | if (irem) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1022 | list->next = block_list; |
| 1023 | block_list = list; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1024 | for (i = 0, p = &list->objects[0]; |
| 1025 | i < N_INTOBJECTS; |
| 1026 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1027 | if (!PyInt_CheckExact(p) || |
Guido van Rossum | bef1417 | 2001-08-29 15:47:46 +0000 | [diff] [blame] | 1028 | p->ob_refcnt == 0) { |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1029 | p->ob_type = (struct _typeobject *) |
| 1030 | free_list; |
| 1031 | free_list = p; |
| 1032 | } |
| 1033 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 1034 | else if (-NSMALLNEGINTS <= p->ob_ival && |
| 1035 | p->ob_ival < NSMALLPOSINTS && |
| 1036 | small_ints[p->ob_ival + |
| 1037 | NSMALLNEGINTS] == NULL) { |
| 1038 | Py_INCREF(p); |
| 1039 | small_ints[p->ob_ival + |
| 1040 | NSMALLNEGINTS] = p; |
| 1041 | } |
| 1042 | #endif |
| 1043 | } |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1044 | } |
| 1045 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1046 | PyMem_FREE(list); /* XXX PyObject_FREE ??? */ |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1047 | bf++; |
| 1048 | } |
| 1049 | isum += irem; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1050 | list = next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1051 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1052 | if (!Py_VerboseFlag) |
| 1053 | return; |
| 1054 | fprintf(stderr, "# cleanup ints"); |
| 1055 | if (!isum) { |
| 1056 | fprintf(stderr, "\n"); |
| 1057 | } |
| 1058 | else { |
| 1059 | fprintf(stderr, |
| 1060 | ": %d unfreed int%s in %d out of %d block%s\n", |
| 1061 | isum, isum == 1 ? "" : "s", |
| 1062 | bc - bf, bc, bc == 1 ? "" : "s"); |
| 1063 | } |
| 1064 | if (Py_VerboseFlag > 1) { |
| 1065 | list = block_list; |
| 1066 | while (list != NULL) { |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 1067 | for (i = 0, p = &list->objects[0]; |
| 1068 | i < N_INTOBJECTS; |
| 1069 | i++, p++) { |
Guido van Rossum | dea6ef9 | 2001-09-11 16:13:52 +0000 | [diff] [blame] | 1070 | if (PyInt_CheckExact(p) && p->ob_refcnt != 0) |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1071 | fprintf(stderr, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 1072 | "# <int at %p, refcnt=%d, val=%ld>\n", |
| 1073 | p, p->ob_refcnt, p->ob_ival); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 1074 | } |
| 1075 | list = list->next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 1076 | } |
| 1077 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 1078 | } |