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 | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 7 | #ifdef HAVE_LIMITS_H |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 8 | #include <limits.h> |
| 9 | #endif |
| 10 | |
| 11 | #ifndef LONG_MAX |
| 12 | #define LONG_MAX 0X7FFFFFFFL |
| 13 | #endif |
| 14 | |
| 15 | #ifndef LONG_MIN |
| 16 | #define LONG_MIN (-LONG_MAX-1) |
| 17 | #endif |
| 18 | |
| 19 | #ifndef CHAR_BIT |
| 20 | #define CHAR_BIT 8 |
| 21 | #endif |
| 22 | |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 23 | #ifndef LONG_BIT |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 24 | #define LONG_BIT (CHAR_BIT * sizeof(long)) |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 25 | #endif |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 27 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 28 | PyInt_GetMax(void) |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 29 | { |
| 30 | return LONG_MAX; /* To initialize sys.maxint */ |
| 31 | } |
| 32 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 33 | /* Standard Booleans */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 35 | PyIntObject _Py_ZeroStruct = { |
| 36 | PyObject_HEAD_INIT(&PyInt_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | 0 |
| 38 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 40 | PyIntObject _Py_TrueStruct = { |
| 41 | PyObject_HEAD_INIT(&PyInt_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | 1 |
| 43 | }; |
| 44 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 46 | err_ovf(char *msg) |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 47 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 48 | PyErr_SetString(PyExc_OverflowError, msg); |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 49 | return NULL; |
| 50 | } |
| 51 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 52 | /* Integers are quite normal objects, to make object handling uniform. |
| 53 | (Using odd pointers to represent integers would save much space |
| 54 | but require extra checks for this special case throughout the code.) |
| 55 | Since, a typical Python program spends much of its time allocating |
| 56 | and deallocating integers, these operations should be very fast. |
| 57 | Therefore we use a dedicated allocation scheme with a much lower |
| 58 | overhead (in space and time) than straight malloc(): a simple |
| 59 | dedicated free list, filled when necessary with memory from malloc(). |
| 60 | */ |
| 61 | |
| 62 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 63 | #define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */ |
| 64 | #define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject)) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 66 | struct _intblock { |
| 67 | struct _intblock *next; |
| 68 | PyIntObject objects[N_INTOBJECTS]; |
| 69 | }; |
| 70 | |
| 71 | typedef struct _intblock PyIntBlock; |
| 72 | |
| 73 | static PyIntBlock *block_list = NULL; |
| 74 | static PyIntObject *free_list = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 76 | static PyIntObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 77 | fill_free_list(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 78 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 79 | PyIntObject *p, *q; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 80 | /* XXX Int blocks escape the object heap. Use PyObject_MALLOC ??? */ |
| 81 | p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 82 | if (p == NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 83 | return (PyIntObject *) PyErr_NoMemory(); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 84 | ((PyIntBlock *)p)->next = block_list; |
| 85 | block_list = (PyIntBlock *)p; |
| 86 | p = &((PyIntBlock *)p)->objects[0]; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 87 | q = p + N_INTOBJECTS; |
| 88 | while (--q > p) |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 89 | q->ob_type = (struct _typeobject *)(q-1); |
| 90 | q->ob_type = NULL; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 91 | return p + N_INTOBJECTS - 1; |
| 92 | } |
| 93 | |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 94 | #ifndef NSMALLPOSINTS |
| 95 | #define NSMALLPOSINTS 100 |
| 96 | #endif |
| 97 | #ifndef NSMALLNEGINTS |
| 98 | #define NSMALLNEGINTS 1 |
| 99 | #endif |
| 100 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 101 | /* References to small integers are saved in this array so that they |
| 102 | can be shared. |
| 103 | The integers that are saved are those in the range |
| 104 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
| 105 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 106 | static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 107 | #endif |
| 108 | #ifdef COUNT_ALLOCS |
| 109 | int quick_int_allocs, quick_neg_int_allocs; |
| 110 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 112 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 113 | PyInt_FromLong(long ival) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | register PyIntObject *v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 116 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 117 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS && |
| 118 | (v = small_ints[ival + NSMALLNEGINTS]) != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 119 | Py_INCREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 120 | #ifdef COUNT_ALLOCS |
| 121 | if (ival >= 0) |
| 122 | quick_int_allocs++; |
| 123 | else |
| 124 | quick_neg_int_allocs++; |
| 125 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 126 | return (PyObject *) v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 127 | } |
| 128 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 129 | if (free_list == NULL) { |
| 130 | if ((free_list = fill_free_list()) == NULL) |
| 131 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 133 | /* PyObject_New is inlined */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 134 | v = free_list; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 135 | free_list = (PyIntObject *)v->ob_type; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 136 | PyObject_INIT(v, &PyInt_Type); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 137 | v->ob_ival = ival; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 138 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 139 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 140 | /* save this one for a following allocation */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 141 | Py_INCREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 142 | small_ints[ival + NSMALLNEGINTS] = v; |
| 143 | } |
| 144 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 145 | return (PyObject *) v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 149 | int_dealloc(PyIntObject *v) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 150 | { |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 151 | v->ob_type = (struct _typeobject *)free_list; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 152 | free_list = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 156 | PyInt_AsLong(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 158 | PyNumberMethods *nb; |
| 159 | PyIntObject *io; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 160 | long val; |
| 161 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 162 | if (op && PyInt_Check(op)) |
| 163 | return PyInt_AS_LONG((PyIntObject*) op); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 164 | |
| 165 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 166 | nb->nb_int == NULL) { |
Guido van Rossum | c18a6f4 | 2000-05-09 14:27:48 +0000 | [diff] [blame] | 167 | PyErr_SetString(PyExc_TypeError, "an integer is required"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 168 | return -1; |
| 169 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 170 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 171 | io = (PyIntObject*) (*nb->nb_int) (op); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 172 | if (io == NULL) |
| 173 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 174 | if (!PyInt_Check(io)) { |
| 175 | PyErr_SetString(PyExc_TypeError, |
| 176 | "nb_int should return int object"); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 177 | return -1; |
| 178 | } |
| 179 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 180 | val = PyInt_AS_LONG(io); |
| 181 | Py_DECREF(io); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 182 | |
| 183 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 186 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 187 | PyInt_FromString(char *s, char **pend, int base) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 188 | { |
| 189 | char *end; |
| 190 | long x; |
| 191 | char buffer[256]; /* For errors */ |
| 192 | |
| 193 | if ((base != 0 && base < 2) || base > 36) { |
| 194 | PyErr_SetString(PyExc_ValueError, "invalid base for int()"); |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | while (*s && isspace(Py_CHARMASK(*s))) |
| 199 | s++; |
| 200 | errno = 0; |
| 201 | if (base == 0 && s[0] == '0') |
| 202 | x = (long) PyOS_strtoul(s, &end, base); |
| 203 | else |
| 204 | x = PyOS_strtol(s, &end, base); |
| 205 | if (end == s || !isalnum(end[-1])) |
| 206 | goto bad; |
| 207 | while (*end && isspace(Py_CHARMASK(*end))) |
| 208 | end++; |
| 209 | if (*end != '\0') { |
| 210 | bad: |
| 211 | sprintf(buffer, "invalid literal for int(): %.200s", s); |
| 212 | PyErr_SetString(PyExc_ValueError, buffer); |
| 213 | return NULL; |
| 214 | } |
| 215 | else if (errno != 0) { |
| 216 | sprintf(buffer, "int() literal too large: %.200s", s); |
| 217 | PyErr_SetString(PyExc_ValueError, buffer); |
| 218 | return NULL; |
| 219 | } |
| 220 | if (pend) |
| 221 | *pend = end; |
| 222 | return PyInt_FromLong(x); |
| 223 | } |
| 224 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 225 | PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 226 | PyInt_FromUnicode(Py_UNICODE *s, int length, int base) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 227 | { |
| 228 | char buffer[256]; |
| 229 | |
| 230 | if (length >= sizeof(buffer)) { |
| 231 | PyErr_SetString(PyExc_ValueError, |
| 232 | "int() literal too large to convert"); |
| 233 | return NULL; |
| 234 | } |
| 235 | if (PyUnicode_EncodeDecimal(s, length, buffer, NULL)) |
| 236 | return NULL; |
| 237 | return PyInt_FromString(buffer, NULL, base); |
| 238 | } |
| 239 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 240 | /* Methods */ |
| 241 | |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 242 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 243 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 244 | int_print(PyIntObject *v, FILE *fp, int flags) |
| 245 | /* flags -- not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 246 | { |
| 247 | fprintf(fp, "%ld", v->ob_ival); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 248 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 251 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 252 | int_repr(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 253 | { |
| 254 | char buf[20]; |
| 255 | sprintf(buf, "%ld", v->ob_ival); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 256 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 260 | int_compare(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 261 | { |
| 262 | register long i = v->ob_ival; |
| 263 | register long j = w->ob_ival; |
| 264 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 265 | } |
| 266 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 267 | static long |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 268 | int_hash(PyIntObject *v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 269 | { |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 270 | /* XXX If this is changed, you also need to change the way |
| 271 | Python's long, float and complex types are hashed. */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 272 | long x = v -> ob_ival; |
| 273 | if (x == -1) |
| 274 | x = -2; |
| 275 | return x; |
| 276 | } |
| 277 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 278 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 279 | int_add(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 280 | { |
| 281 | register long a, b, x; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 282 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 283 | b = w->ob_ival; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 284 | x = a + b; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 285 | if ((x^a) < 0 && (x^b) < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 286 | return err_ovf("integer addition"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 287 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 290 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 291 | int_sub(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | { |
| 293 | register long a, b, x; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 294 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 295 | b = w->ob_ival; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 296 | x = a - b; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 297 | if ((x^a) < 0 && (x^~b) < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 298 | return err_ovf("integer subtraction"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 299 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 302 | /* |
| 303 | Integer overflow checking used to be done using a double, but on 64 |
| 304 | bit machines (where both long and double are 64 bit) this fails |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 305 | because the double doesn't have enough precision. John Tromp suggests |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 306 | the following algorithm: |
| 307 | |
| 308 | Suppose again we normalize a and b to be nonnegative. |
| 309 | Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.). |
| 310 | Now we test ah and bh against zero and get essentially 3 possible outcomes. |
| 311 | |
| 312 | 1) both ah and bh > 0 : then report overflow |
| 313 | |
| 314 | 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out |
| 315 | negative |
| 316 | |
| 317 | 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31 |
| 318 | compute al*bl and report overflow if it's negative |
| 319 | add (ah*bl)<<32 to al*bl and report overflow if |
| 320 | it's negative |
| 321 | |
| 322 | In case of no overflow the result is then negated if necessary. |
| 323 | |
| 324 | The majority of cases will be 2), in which case this method is the same as |
| 325 | what I suggested before. If multiplication is expensive enough, then the |
| 326 | other method is faster on case 3), but also more work to program, so I |
| 327 | guess the above is the preferred solution. |
| 328 | |
| 329 | */ |
| 330 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 331 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 332 | int_mul(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 333 | { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 334 | long a, b, ah, bh, x, y; |
| 335 | int s = 1; |
| 336 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 337 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 338 | b = w->ob_ival; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 339 | ah = a >> (LONG_BIT/2); |
| 340 | bh = b >> (LONG_BIT/2); |
| 341 | |
| 342 | /* Quick test for common case: two small positive ints */ |
| 343 | |
| 344 | if (ah == 0 && bh == 0) { |
| 345 | x = a*b; |
| 346 | if (x < 0) |
| 347 | goto bad; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 348 | return PyInt_FromLong(x); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* Arrange that a >= b >= 0 */ |
| 352 | |
| 353 | if (a < 0) { |
| 354 | a = -a; |
| 355 | if (a < 0) { |
| 356 | /* Largest negative */ |
| 357 | if (b == 0 || b == 1) { |
| 358 | x = a*b; |
| 359 | goto ok; |
| 360 | } |
| 361 | else |
| 362 | goto bad; |
| 363 | } |
| 364 | s = -s; |
| 365 | ah = a >> (LONG_BIT/2); |
| 366 | } |
| 367 | if (b < 0) { |
| 368 | b = -b; |
| 369 | if (b < 0) { |
| 370 | /* Largest negative */ |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 371 | if (a == 0 || (a == 1 && s == 1)) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 372 | x = a*b; |
| 373 | goto ok; |
| 374 | } |
| 375 | else |
| 376 | goto bad; |
| 377 | } |
| 378 | s = -s; |
| 379 | bh = b >> (LONG_BIT/2); |
| 380 | } |
| 381 | |
| 382 | /* 1) both ah and bh > 0 : then report overflow */ |
| 383 | |
| 384 | if (ah != 0 && bh != 0) |
| 385 | goto bad; |
| 386 | |
| 387 | /* 2) both ah and bh = 0 : then compute a*b and report |
| 388 | overflow if it comes out negative */ |
| 389 | |
| 390 | if (ah == 0 && bh == 0) { |
| 391 | x = a*b; |
| 392 | if (x < 0) |
| 393 | goto bad; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 394 | return PyInt_FromLong(x*s); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (a < b) { |
| 398 | /* Swap */ |
| 399 | x = a; |
| 400 | a = b; |
| 401 | b = x; |
| 402 | ah = bh; |
| 403 | /* bh not used beyond this point */ |
| 404 | } |
| 405 | |
| 406 | /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if |
| 407 | it's >= 2^31 |
| 408 | compute al*bl and report overflow if it's negative |
| 409 | add (ah*bl)<<32 to al*bl and report overflow if |
| 410 | it's negative |
| 411 | (NB b == bl in this case, and we make a = al) */ |
| 412 | |
| 413 | y = ah*b; |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 414 | if (y >= (1L << (LONG_BIT/2 - 1))) |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 415 | goto bad; |
| 416 | a &= (1L << (LONG_BIT/2)) - 1; |
| 417 | x = a*b; |
| 418 | if (x < 0) |
| 419 | goto bad; |
Guido van Rossum | 541cdd8 | 1997-01-06 22:53:20 +0000 | [diff] [blame] | 420 | x += y << (LONG_BIT/2); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 421 | if (x < 0) |
| 422 | goto bad; |
| 423 | ok: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 424 | return PyInt_FromLong(x * s); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 425 | |
| 426 | bad: |
| 427 | return err_ovf("integer multiplication"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 430 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 431 | i_divmod(register PyIntObject *x, register PyIntObject *y, |
| 432 | long *p_xdivy, long *p_xmody) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 433 | { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 434 | long xi = x->ob_ival; |
| 435 | long yi = y->ob_ival; |
| 436 | long xdivy, xmody; |
| 437 | |
| 438 | if (yi == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 439 | PyErr_SetString(PyExc_ZeroDivisionError, |
| 440 | "integer division or modulo"); |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 441 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 442 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 443 | if (yi < 0) { |
Guido van Rossum | e13ff2e | 1999-09-27 17:12:47 +0000 | [diff] [blame] | 444 | if (xi < 0) { |
| 445 | if (yi == -1 && -xi < 0) { |
| 446 | /* most negative / -1 */ |
| 447 | err_ovf("integer division"); |
| 448 | return -1; |
| 449 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 450 | xdivy = -xi / -yi; |
Guido van Rossum | e13ff2e | 1999-09-27 17:12:47 +0000 | [diff] [blame] | 451 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 452 | else |
| 453 | xdivy = - (xi / -yi); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 454 | } |
| 455 | else { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 456 | if (xi < 0) |
| 457 | xdivy = - (-xi / yi); |
| 458 | else |
| 459 | xdivy = xi / yi; |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 460 | } |
| 461 | xmody = xi - xdivy*yi; |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 462 | if ((xmody < 0 && yi > 0) || (xmody > 0 && yi < 0)) { |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 463 | xmody += yi; |
| 464 | xdivy -= 1; |
| 465 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 466 | *p_xdivy = xdivy; |
| 467 | *p_xmody = xmody; |
| 468 | return 0; |
| 469 | } |
| 470 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 471 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 472 | int_div(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 473 | { |
| 474 | long d, m; |
| 475 | if (i_divmod(x, y, &d, &m) < 0) |
| 476 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 477 | return PyInt_FromLong(d); |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 480 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 481 | int_mod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 482 | { |
| 483 | long d, m; |
| 484 | if (i_divmod(x, y, &d, &m) < 0) |
| 485 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 486 | return PyInt_FromLong(m); |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 489 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 490 | int_divmod(PyIntObject *x, PyIntObject *y) |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 491 | { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 492 | long d, m; |
| 493 | if (i_divmod(x, y, &d, &m) < 0) |
| 494 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 495 | return Py_BuildValue("(ll)", d, m); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 498 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 499 | int_pow(PyIntObject *v, PyIntObject *w, PyIntObject *z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 500 | { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 501 | #if 1 |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 502 | register long iv, iw, iz=0, ix, temp, prev; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 503 | iv = v->ob_ival; |
| 504 | iw = w->ob_ival; |
| 505 | if (iw < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 506 | PyErr_SetString(PyExc_ValueError, |
| 507 | "integer to the negative power"); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 508 | return NULL; |
| 509 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 510 | if ((PyObject *)z != Py_None) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 511 | iz = z->ob_ival; |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 512 | if (iz == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 513 | PyErr_SetString(PyExc_ValueError, |
| 514 | "pow(x, y, z) with z==0"); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 515 | return NULL; |
| 516 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 517 | } |
| 518 | /* |
| 519 | * XXX: The original exponentiation code stopped looping |
| 520 | * when temp hit zero; this code will continue onwards |
| 521 | * unnecessarily, but at least it won't cause any errors. |
| 522 | * Hopefully the speed improvement from the fast exponentiation |
| 523 | * will compensate for the slight inefficiency. |
| 524 | * XXX: Better handling of overflows is desperately needed. |
| 525 | */ |
| 526 | temp = iv; |
| 527 | ix = 1; |
| 528 | while (iw > 0) { |
| 529 | prev = ix; /* Save value for overflow check */ |
| 530 | if (iw & 1) { |
| 531 | ix = ix*temp; |
| 532 | if (temp == 0) |
| 533 | break; /* Avoid ix / 0 */ |
| 534 | if (ix / temp != prev) |
Guido van Rossum | fb4574e | 2000-02-15 14:51:46 +0000 | [diff] [blame] | 535 | return err_ovf("integer exponentiation"); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 536 | } |
| 537 | iw >>= 1; /* Shift exponent down by 1 bit */ |
| 538 | if (iw==0) break; |
| 539 | prev = temp; |
| 540 | temp *= temp; /* Square the value of temp */ |
| 541 | if (prev!=0 && temp/prev!=prev) |
Guido van Rossum | fb4574e | 2000-02-15 14:51:46 +0000 | [diff] [blame] | 542 | return err_ovf("integer exponentiation"); |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 543 | if (iz) { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 544 | /* If we did a multiplication, perform a modulo */ |
| 545 | ix = ix % iz; |
| 546 | temp = temp % iz; |
| 547 | } |
| 548 | } |
Guido van Rossum | 9478dd4 | 1996-12-06 20:14:43 +0000 | [diff] [blame] | 549 | if (iz) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 550 | PyObject *t1, *t2; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 551 | long int div, mod; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 552 | t1=PyInt_FromLong(ix); |
| 553 | t2=PyInt_FromLong(iz); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 554 | if (t1==NULL || t2==NULL || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 555 | i_divmod((PyIntObject *)t1, |
| 556 | (PyIntObject *)t2, &div, &mod)<0) |
| 557 | { |
| 558 | Py_XDECREF(t1); |
| 559 | Py_XDECREF(t2); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 560 | return(NULL); |
| 561 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 562 | Py_DECREF(t1); |
| 563 | Py_DECREF(t2); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 564 | ix=mod; |
| 565 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 566 | return PyInt_FromLong(ix); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 567 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 568 | register long iv, iw, ix; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 569 | iv = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 570 | iw = w->ob_ival; |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 571 | if (iw < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 572 | PyErr_SetString(PyExc_ValueError, |
| 573 | "integer to the negative power"); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 574 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 575 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 576 | if ((PyObject *)z != Py_None) { |
| 577 | PyErr_SetString(PyExc_TypeError, |
| 578 | "pow(int, int, int) not yet supported"); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 579 | return NULL; |
| 580 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 581 | ix = 1; |
| 582 | while (--iw >= 0) { |
| 583 | long prev = ix; |
| 584 | ix = ix * iv; |
| 585 | if (iv == 0) |
| 586 | break; /* 0 to some power -- avoid ix / 0 */ |
| 587 | if (ix / iv != prev) |
Guido van Rossum | fb4574e | 2000-02-15 14:51:46 +0000 | [diff] [blame] | 588 | return err_ovf("integer exponentiation"); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 589 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 590 | return PyInt_FromLong(ix); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 591 | #endif |
| 592 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 593 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 594 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 595 | int_neg(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 596 | { |
| 597 | register long a, x; |
| 598 | a = v->ob_ival; |
| 599 | x = -a; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 600 | if (a < 0 && x < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 601 | return err_ovf("integer negation"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 602 | return PyInt_FromLong(x); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 605 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 606 | int_pos(PyIntObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 607 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 608 | Py_INCREF(v); |
| 609 | return (PyObject *)v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 612 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 613 | int_abs(PyIntObject *v) |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 614 | { |
| 615 | if (v->ob_ival >= 0) |
| 616 | return int_pos(v); |
| 617 | else |
| 618 | return int_neg(v); |
| 619 | } |
| 620 | |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 621 | static int |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 622 | int_nonzero(PyIntObject *v) |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 623 | { |
| 624 | return v->ob_ival != 0; |
| 625 | } |
| 626 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 627 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 628 | int_invert(PyIntObject *v) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 629 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 630 | return PyInt_FromLong(~v->ob_ival); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 633 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 634 | int_lshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 635 | { |
| 636 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 637 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 638 | b = w->ob_ival; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 639 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 640 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 641 | return NULL; |
| 642 | } |
| 643 | if (a == 0 || b == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 644 | Py_INCREF(v); |
| 645 | return (PyObject *) v; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 646 | } |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 647 | if (b >= LONG_BIT) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 648 | return PyInt_FromLong(0L); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 649 | } |
| 650 | a = (unsigned long)a << b; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 651 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 654 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 655 | int_rshift(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 656 | { |
| 657 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 658 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 659 | b = w->ob_ival; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 660 | if (b < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 661 | PyErr_SetString(PyExc_ValueError, "negative shift count"); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 662 | return NULL; |
| 663 | } |
| 664 | if (a == 0 || b == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 665 | Py_INCREF(v); |
| 666 | return (PyObject *) v; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 667 | } |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 668 | if (b >= LONG_BIT) { |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 669 | if (a < 0) |
| 670 | a = -1; |
| 671 | else |
| 672 | a = 0; |
| 673 | } |
| 674 | else { |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 675 | a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b); |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 676 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 677 | return PyInt_FromLong(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 680 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 681 | int_and(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 682 | { |
| 683 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 684 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 685 | b = w->ob_ival; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 686 | return PyInt_FromLong(a & b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 689 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 690 | int_xor(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 691 | { |
| 692 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 693 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 694 | b = w->ob_ival; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 695 | return PyInt_FromLong(a ^ b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 698 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 699 | int_or(PyIntObject *v, PyIntObject *w) |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 700 | { |
| 701 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 702 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 703 | b = w->ob_ival; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 704 | return PyInt_FromLong(a | b); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 707 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 708 | int_int(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 709 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 710 | Py_INCREF(v); |
| 711 | return (PyObject *)v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +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_long(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 716 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 717 | return PyLong_FromLong((v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 720 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 721 | int_float(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 722 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 723 | return PyFloat_FromDouble((double)(v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 726 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 727 | int_oct(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 728 | { |
Guido van Rossum | 6f72f97 | 1997-01-14 15:43:41 +0000 | [diff] [blame] | 729 | char buf[100]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 730 | long x = v -> ob_ival; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 731 | if (x == 0) |
| 732 | strcpy(buf, "0"); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 733 | else |
Guido van Rossum | ebee025 | 1997-01-12 19:48:03 +0000 | [diff] [blame] | 734 | sprintf(buf, "0%lo", x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 735 | return PyString_FromString(buf); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 738 | static PyObject * |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 739 | int_hex(PyIntObject *v) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 740 | { |
Guido van Rossum | 6f72f97 | 1997-01-14 15:43:41 +0000 | [diff] [blame] | 741 | char buf[100]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 742 | long x = v -> ob_ival; |
Guido van Rossum | ebee025 | 1997-01-12 19:48:03 +0000 | [diff] [blame] | 743 | sprintf(buf, "0x%lx", x); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 744 | return PyString_FromString(buf); |
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 PyNumberMethods int_as_number = { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 748 | (binaryfunc)int_add, /*nb_add*/ |
| 749 | (binaryfunc)int_sub, /*nb_subtract*/ |
| 750 | (binaryfunc)int_mul, /*nb_multiply*/ |
| 751 | (binaryfunc)int_div, /*nb_divide*/ |
| 752 | (binaryfunc)int_mod, /*nb_remainder*/ |
| 753 | (binaryfunc)int_divmod, /*nb_divmod*/ |
| 754 | (ternaryfunc)int_pow, /*nb_power*/ |
| 755 | (unaryfunc)int_neg, /*nb_negative*/ |
| 756 | (unaryfunc)int_pos, /*nb_positive*/ |
| 757 | (unaryfunc)int_abs, /*nb_absolute*/ |
| 758 | (inquiry)int_nonzero, /*nb_nonzero*/ |
| 759 | (unaryfunc)int_invert, /*nb_invert*/ |
| 760 | (binaryfunc)int_lshift, /*nb_lshift*/ |
| 761 | (binaryfunc)int_rshift, /*nb_rshift*/ |
| 762 | (binaryfunc)int_and, /*nb_and*/ |
| 763 | (binaryfunc)int_xor, /*nb_xor*/ |
| 764 | (binaryfunc)int_or, /*nb_or*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 765 | 0, /*nb_coerce*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 766 | (unaryfunc)int_int, /*nb_int*/ |
| 767 | (unaryfunc)int_long, /*nb_long*/ |
| 768 | (unaryfunc)int_float, /*nb_float*/ |
| 769 | (unaryfunc)int_oct, /*nb_oct*/ |
| 770 | (unaryfunc)int_hex, /*nb_hex*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 771 | }; |
| 772 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 773 | PyTypeObject PyInt_Type = { |
| 774 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 775 | 0, |
| 776 | "int", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 777 | sizeof(PyIntObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 778 | 0, |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 779 | (destructor)int_dealloc, /*tp_dealloc*/ |
| 780 | (printfunc)int_print, /*tp_print*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 781 | 0, /*tp_getattr*/ |
| 782 | 0, /*tp_setattr*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 783 | (cmpfunc)int_compare, /*tp_compare*/ |
| 784 | (reprfunc)int_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 785 | &int_as_number, /*tp_as_number*/ |
| 786 | 0, /*tp_as_sequence*/ |
| 787 | 0, /*tp_as_mapping*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 788 | (hashfunc)int_hash, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 789 | }; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 790 | |
| 791 | void |
Fred Drake | a2f5511 | 2000-07-09 15:16:51 +0000 | [diff] [blame] | 792 | PyInt_Fini(void) |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 793 | { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 794 | PyIntObject *p; |
| 795 | PyIntBlock *list, *next; |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 796 | int i; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 797 | int bc, bf; /* block count, number of freed blocks */ |
| 798 | int irem, isum; /* remaining unfreed ints per block, total */ |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 799 | |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 800 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 801 | PyIntObject **q; |
| 802 | |
| 803 | i = NSMALLNEGINTS + NSMALLPOSINTS; |
| 804 | q = small_ints; |
| 805 | while (--i >= 0) { |
| 806 | Py_XDECREF(*q); |
| 807 | *q++ = NULL; |
| 808 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 809 | #endif |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 810 | bc = 0; |
| 811 | bf = 0; |
| 812 | isum = 0; |
| 813 | list = block_list; |
| 814 | block_list = NULL; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 815 | free_list = NULL; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 816 | while (list != NULL) { |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 817 | bc++; |
| 818 | irem = 0; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 819 | for (i = 0, p = &list->objects[0]; |
| 820 | i < N_INTOBJECTS; |
| 821 | i++, p++) { |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 822 | if (PyInt_Check(p) && p->ob_refcnt != 0) |
| 823 | irem++; |
| 824 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 825 | next = list->next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 826 | if (irem) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 827 | list->next = block_list; |
| 828 | block_list = list; |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 829 | for (i = 0, p = &list->objects[0]; |
| 830 | i < N_INTOBJECTS; |
| 831 | i++, p++) { |
| 832 | if (!PyInt_Check(p) || p->ob_refcnt == 0) { |
| 833 | p->ob_type = (struct _typeobject *) |
| 834 | free_list; |
| 835 | free_list = p; |
| 836 | } |
| 837 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 838 | else if (-NSMALLNEGINTS <= p->ob_ival && |
| 839 | p->ob_ival < NSMALLPOSINTS && |
| 840 | small_ints[p->ob_ival + |
| 841 | NSMALLNEGINTS] == NULL) { |
| 842 | Py_INCREF(p); |
| 843 | small_ints[p->ob_ival + |
| 844 | NSMALLNEGINTS] = p; |
| 845 | } |
| 846 | #endif |
| 847 | } |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 848 | } |
| 849 | else { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 850 | PyMem_FREE(list); /* XXX PyObject_FREE ??? */ |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 851 | bf++; |
| 852 | } |
| 853 | isum += irem; |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 854 | list = next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 855 | } |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 856 | if (!Py_VerboseFlag) |
| 857 | return; |
| 858 | fprintf(stderr, "# cleanup ints"); |
| 859 | if (!isum) { |
| 860 | fprintf(stderr, "\n"); |
| 861 | } |
| 862 | else { |
| 863 | fprintf(stderr, |
| 864 | ": %d unfreed int%s in %d out of %d block%s\n", |
| 865 | isum, isum == 1 ? "" : "s", |
| 866 | bc - bf, bc, bc == 1 ? "" : "s"); |
| 867 | } |
| 868 | if (Py_VerboseFlag > 1) { |
| 869 | list = block_list; |
| 870 | while (list != NULL) { |
Guido van Rossum | 51288bc | 1999-03-19 20:30:39 +0000 | [diff] [blame] | 871 | for (i = 0, p = &list->objects[0]; |
| 872 | i < N_INTOBJECTS; |
| 873 | i++, p++) { |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 874 | if (PyInt_Check(p) && p->ob_refcnt != 0) |
| 875 | fprintf(stderr, |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 876 | "# <int at %p, refcnt=%d, val=%ld>\n", |
| 877 | p, p->ob_refcnt, p->ob_ival); |
Guido van Rossum | 3fce883 | 1999-03-12 19:43:17 +0000 | [diff] [blame] | 878 | } |
| 879 | list = list->next; |
Guido van Rossum | da084ed | 1999-03-10 22:55:24 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
Guido van Rossum | fbbd57e | 1997-08-05 02:16:08 +0000 | [diff] [blame] | 882 | } |