Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3 | Amsterdam, The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Integer object implementation */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 28 | #include "modsupport.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 30 | #ifdef HAVE_LIMITS_H |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 31 | #include <limits.h> |
| 32 | #endif |
| 33 | |
| 34 | #ifndef LONG_MAX |
| 35 | #define LONG_MAX 0X7FFFFFFFL |
| 36 | #endif |
| 37 | |
| 38 | #ifndef LONG_MIN |
| 39 | #define LONG_MIN (-LONG_MAX-1) |
| 40 | #endif |
| 41 | |
| 42 | #ifndef CHAR_BIT |
| 43 | #define CHAR_BIT 8 |
| 44 | #endif |
| 45 | |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 46 | #ifndef LONG_BIT |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 47 | #define LONG_BIT (CHAR_BIT * sizeof(long)) |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 48 | #endif |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 2e1d433 | 1993-12-24 10:22:45 +0000 | [diff] [blame] | 50 | long |
| 51 | getmaxint() |
| 52 | { |
| 53 | return LONG_MAX; /* To initialize sys.maxint */ |
| 54 | } |
| 55 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | /* Standard Booleans */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | intobject FalseObject = { |
| 59 | OB_HEAD_INIT(&Inttype) |
| 60 | 0 |
| 61 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 63 | intobject TrueObject = { |
| 64 | OB_HEAD_INIT(&Inttype) |
| 65 | 1 |
| 66 | }; |
| 67 | |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 68 | static object * |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 69 | err_ovf(msg) |
| 70 | char *msg; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 71 | { |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 72 | err_setstr(OverflowError, msg); |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 73 | return NULL; |
| 74 | } |
| 75 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 76 | /* Integers are quite normal objects, to make object handling uniform. |
| 77 | (Using odd pointers to represent integers would save much space |
| 78 | but require extra checks for this special case throughout the code.) |
| 79 | Since, a typical Python program spends much of its time allocating |
| 80 | and deallocating integers, these operations should be very fast. |
| 81 | Therefore we use a dedicated allocation scheme with a much lower |
| 82 | overhead (in space and time) than straight malloc(): a simple |
| 83 | dedicated free list, filled when necessary with memory from malloc(). |
| 84 | */ |
| 85 | |
| 86 | #define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */ |
| 87 | #define N_INTOBJECTS (BLOCK_SIZE / sizeof(intobject)) |
| 88 | |
| 89 | static intobject * |
| 90 | fill_free_list() |
| 91 | { |
| 92 | intobject *p, *q; |
| 93 | p = NEW(intobject, N_INTOBJECTS); |
| 94 | if (p == NULL) |
| 95 | return (intobject *)err_nomem(); |
| 96 | q = p + N_INTOBJECTS; |
| 97 | while (--q > p) |
| 98 | *(intobject **)q = q-1; |
| 99 | *(intobject **)q = NULL; |
| 100 | return p + N_INTOBJECTS - 1; |
| 101 | } |
| 102 | |
| 103 | static intobject *free_list = NULL; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 104 | #ifndef NSMALLPOSINTS |
| 105 | #define NSMALLPOSINTS 100 |
| 106 | #endif |
| 107 | #ifndef NSMALLNEGINTS |
| 108 | #define NSMALLNEGINTS 1 |
| 109 | #endif |
| 110 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 111 | /* References to small integers are saved in this array so that they |
| 112 | can be shared. |
| 113 | The integers that are saved are those in the range |
| 114 | -NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive). |
| 115 | */ |
| 116 | static intobject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; |
| 117 | #endif |
| 118 | #ifdef COUNT_ALLOCS |
| 119 | int quick_int_allocs, quick_neg_int_allocs; |
| 120 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 122 | object * |
| 123 | newintobject(ival) |
| 124 | long ival; |
| 125 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 126 | register intobject *v; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 127 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 128 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS && |
| 129 | (v = small_ints[ival + NSMALLNEGINTS]) != NULL) { |
| 130 | INCREF(v); |
| 131 | #ifdef COUNT_ALLOCS |
| 132 | if (ival >= 0) |
| 133 | quick_int_allocs++; |
| 134 | else |
| 135 | quick_neg_int_allocs++; |
| 136 | #endif |
| 137 | return (object *) v; |
| 138 | } |
| 139 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 140 | if (free_list == NULL) { |
| 141 | if ((free_list = fill_free_list()) == NULL) |
| 142 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 144 | v = free_list; |
| 145 | free_list = *(intobject **)free_list; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 146 | v->ob_type = &Inttype; |
| 147 | v->ob_ival = ival; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 148 | NEWREF(v); |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 149 | #if NSMALLNEGINTS + NSMALLPOSINTS > 0 |
| 150 | if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { |
| 151 | /* save this one for a following allocation */ |
| 152 | INCREF(v); |
| 153 | small_ints[ival + NSMALLNEGINTS] = v; |
| 154 | } |
| 155 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 156 | return (object *) v; |
| 157 | } |
| 158 | |
| 159 | static void |
| 160 | int_dealloc(v) |
| 161 | intobject *v; |
| 162 | { |
| 163 | *(intobject **)v = free_list; |
| 164 | free_list = v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | long |
| 168 | getintvalue(op) |
| 169 | register object *op; |
| 170 | { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 171 | number_methods *nb; |
| 172 | intobject *io; |
| 173 | long val; |
| 174 | |
| 175 | if (op && is_intobject(op)) |
| 176 | return GETINTVALUE((intobject*) op); |
| 177 | |
| 178 | if (op == NULL || (nb = op->ob_type->tp_as_number) == NULL || |
| 179 | nb->nb_int == NULL) { |
| 180 | err_badarg(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | return -1; |
| 182 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 183 | |
| 184 | io = (intobject*) (*nb->nb_int) (op); |
| 185 | if (io == NULL) |
| 186 | return -1; |
| 187 | if (!is_intobject(io)) { |
| 188 | err_setstr(TypeError, "nb_int should return int object"); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | val = GETINTVALUE(io); |
| 193 | DECREF(io); |
| 194 | |
| 195 | return val; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* Methods */ |
| 199 | |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 200 | /* ARGSUSED */ |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 201 | static int |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 202 | int_print(v, fp, flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 203 | intobject *v; |
| 204 | FILE *fp; |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 205 | int flags; /* Not used but required by interface */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 206 | { |
| 207 | fprintf(fp, "%ld", v->ob_ival); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 208 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 212 | int_repr(v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 213 | intobject *v; |
| 214 | { |
| 215 | char buf[20]; |
| 216 | sprintf(buf, "%ld", v->ob_ival); |
| 217 | return newstringobject(buf); |
| 218 | } |
| 219 | |
| 220 | static int |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 221 | int_compare(v, w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | intobject *v, *w; |
| 223 | { |
| 224 | register long i = v->ob_ival; |
| 225 | register long j = w->ob_ival; |
| 226 | return (i < j) ? -1 : (i > j) ? 1 : 0; |
| 227 | } |
| 228 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 229 | static long |
| 230 | int_hash(v) |
| 231 | intobject *v; |
| 232 | { |
| 233 | long x = v -> ob_ival; |
| 234 | if (x == -1) |
| 235 | x = -2; |
| 236 | return x; |
| 237 | } |
| 238 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 239 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 240 | int_add(v, w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | intobject *v; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 242 | intobject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | { |
| 244 | register long a, b, x; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 245 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 246 | b = w->ob_ival; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 247 | x = a + b; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 248 | if ((x^a) < 0 && (x^b) < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 249 | return err_ovf("integer addition"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 250 | return newintobject(x); |
| 251 | } |
| 252 | |
| 253 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 254 | int_sub(v, w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 255 | intobject *v; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 256 | intobject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 257 | { |
| 258 | register long a, b, x; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 259 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 260 | b = w->ob_ival; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 261 | x = a - b; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 262 | if ((x^a) < 0 && (x^~b) < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 263 | return err_ovf("integer subtraction"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 264 | return newintobject(x); |
| 265 | } |
| 266 | |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 267 | /* |
| 268 | Integer overflow checking used to be done using a double, but on 64 |
| 269 | bit machines (where both long and double are 64 bit) this fails |
| 270 | because the double doesn't have enouvg precision. John Tromp suggests |
| 271 | the following algorithm: |
| 272 | |
| 273 | Suppose again we normalize a and b to be nonnegative. |
| 274 | Let ah and al (bh and bl) be the high and low 32 bits of a (b, resp.). |
| 275 | Now we test ah and bh against zero and get essentially 3 possible outcomes. |
| 276 | |
| 277 | 1) both ah and bh > 0 : then report overflow |
| 278 | |
| 279 | 2) both ah and bh = 0 : then compute a*b and report overflow if it comes out |
| 280 | negative |
| 281 | |
| 282 | 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if it's >= 2^31 |
| 283 | compute al*bl and report overflow if it's negative |
| 284 | add (ah*bl)<<32 to al*bl and report overflow if |
| 285 | it's negative |
| 286 | |
| 287 | In case of no overflow the result is then negated if necessary. |
| 288 | |
| 289 | The majority of cases will be 2), in which case this method is the same as |
| 290 | what I suggested before. If multiplication is expensive enough, then the |
| 291 | other method is faster on case 3), but also more work to program, so I |
| 292 | guess the above is the preferred solution. |
| 293 | |
| 294 | */ |
| 295 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 296 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 297 | int_mul(v, w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 298 | intobject *v; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 299 | intobject *w; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 301 | long a, b, ah, bh, x, y; |
| 302 | int s = 1; |
| 303 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 305 | b = w->ob_ival; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 306 | ah = a >> (LONG_BIT/2); |
| 307 | bh = b >> (LONG_BIT/2); |
| 308 | |
| 309 | /* Quick test for common case: two small positive ints */ |
| 310 | |
| 311 | if (ah == 0 && bh == 0) { |
| 312 | x = a*b; |
| 313 | if (x < 0) |
| 314 | goto bad; |
| 315 | return newintobject(x); |
| 316 | } |
| 317 | |
| 318 | /* Arrange that a >= b >= 0 */ |
| 319 | |
| 320 | if (a < 0) { |
| 321 | a = -a; |
| 322 | if (a < 0) { |
| 323 | /* Largest negative */ |
| 324 | if (b == 0 || b == 1) { |
| 325 | x = a*b; |
| 326 | goto ok; |
| 327 | } |
| 328 | else |
| 329 | goto bad; |
| 330 | } |
| 331 | s = -s; |
| 332 | ah = a >> (LONG_BIT/2); |
| 333 | } |
| 334 | if (b < 0) { |
| 335 | b = -b; |
| 336 | if (b < 0) { |
| 337 | /* Largest negative */ |
| 338 | if (a == 0 || a == 1 && s == 1) { |
| 339 | x = a*b; |
| 340 | goto ok; |
| 341 | } |
| 342 | else |
| 343 | goto bad; |
| 344 | } |
| 345 | s = -s; |
| 346 | bh = b >> (LONG_BIT/2); |
| 347 | } |
| 348 | |
| 349 | /* 1) both ah and bh > 0 : then report overflow */ |
| 350 | |
| 351 | if (ah != 0 && bh != 0) |
| 352 | goto bad; |
| 353 | |
| 354 | /* 2) both ah and bh = 0 : then compute a*b and report |
| 355 | overflow if it comes out negative */ |
| 356 | |
| 357 | if (ah == 0 && bh == 0) { |
| 358 | x = a*b; |
| 359 | if (x < 0) |
| 360 | goto bad; |
| 361 | return newintobject(x*s); |
| 362 | } |
| 363 | |
| 364 | if (a < b) { |
| 365 | /* Swap */ |
| 366 | x = a; |
| 367 | a = b; |
| 368 | b = x; |
| 369 | ah = bh; |
| 370 | /* bh not used beyond this point */ |
| 371 | } |
| 372 | |
| 373 | /* 3) ah > 0 and bh = 0 : compute ah*bl and report overflow if |
| 374 | it's >= 2^31 |
| 375 | compute al*bl and report overflow if it's negative |
| 376 | add (ah*bl)<<32 to al*bl and report overflow if |
| 377 | it's negative |
| 378 | (NB b == bl in this case, and we make a = al) */ |
| 379 | |
| 380 | y = ah*b; |
| 381 | if (y >= (1L << (LONG_BIT/2))) |
| 382 | goto bad; |
| 383 | a &= (1L << (LONG_BIT/2)) - 1; |
| 384 | x = a*b; |
| 385 | if (x < 0) |
| 386 | goto bad; |
| 387 | x += y << LONG_BIT/2; |
| 388 | if (x < 0) |
| 389 | goto bad; |
| 390 | ok: |
| 391 | return newintobject(x * s); |
| 392 | |
| 393 | bad: |
| 394 | return err_ovf("integer multiplication"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 397 | static int |
| 398 | i_divmod(x, y, p_xdivy, p_xmody) |
| 399 | register intobject *x, *y; |
| 400 | long *p_xdivy, *p_xmody; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 401 | { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 402 | long xi = x->ob_ival; |
| 403 | long yi = y->ob_ival; |
| 404 | long xdivy, xmody; |
| 405 | |
| 406 | if (yi == 0) { |
| 407 | err_setstr(ZeroDivisionError, "integer division or modulo"); |
| 408 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 409 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 410 | if (yi < 0) { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 411 | if (xi < 0) |
| 412 | xdivy = -xi / -yi; |
| 413 | else |
| 414 | xdivy = - (xi / -yi); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 415 | } |
| 416 | else { |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 417 | if (xi < 0) |
| 418 | xdivy = - (-xi / yi); |
| 419 | else |
| 420 | xdivy = xi / yi; |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 421 | } |
| 422 | xmody = xi - xdivy*yi; |
| 423 | if (xmody < 0 && yi > 0 || xmody > 0 && yi < 0) { |
| 424 | xmody += yi; |
| 425 | xdivy -= 1; |
| 426 | } |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 427 | *p_xdivy = xdivy; |
| 428 | *p_xmody = xmody; |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static object * |
| 433 | int_div(x, y) |
| 434 | intobject *x; |
| 435 | intobject *y; |
| 436 | { |
| 437 | long d, m; |
| 438 | if (i_divmod(x, y, &d, &m) < 0) |
| 439 | return NULL; |
| 440 | return newintobject(d); |
| 441 | } |
| 442 | |
| 443 | static object * |
| 444 | int_mod(x, y) |
| 445 | intobject *x; |
| 446 | intobject *y; |
| 447 | { |
| 448 | long d, m; |
| 449 | if (i_divmod(x, y, &d, &m) < 0) |
| 450 | return NULL; |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 451 | return newintobject(m); |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static object * |
| 455 | int_divmod(x, y) |
| 456 | intobject *x; |
| 457 | intobject *y; |
| 458 | { |
| 459 | object *v, *v0, *v1; |
| 460 | long d, m; |
| 461 | if (i_divmod(x, y, &d, &m) < 0) |
| 462 | return NULL; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 463 | return mkvalue("(ll)", d, m); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static object * |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 467 | int_pow(v, w, z) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 468 | intobject *v; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 469 | intobject *w; |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 470 | intobject *z; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 471 | { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 472 | #if 1 |
| 473 | register long iv, iw, iz, ix, temp, prev; |
| 474 | int zset = 0; |
| 475 | iv = v->ob_ival; |
| 476 | iw = w->ob_ival; |
| 477 | if (iw < 0) { |
| 478 | err_setstr(ValueError, "integer to the negative power"); |
| 479 | return NULL; |
| 480 | } |
| 481 | if ((object *)z != None) { |
| 482 | iz = z->ob_ival; |
| 483 | zset = 1; |
| 484 | } |
| 485 | /* |
| 486 | * XXX: The original exponentiation code stopped looping |
| 487 | * when temp hit zero; this code will continue onwards |
| 488 | * unnecessarily, but at least it won't cause any errors. |
| 489 | * Hopefully the speed improvement from the fast exponentiation |
| 490 | * will compensate for the slight inefficiency. |
| 491 | * XXX: Better handling of overflows is desperately needed. |
| 492 | */ |
| 493 | temp = iv; |
| 494 | ix = 1; |
| 495 | while (iw > 0) { |
| 496 | prev = ix; /* Save value for overflow check */ |
| 497 | if (iw & 1) { |
| 498 | ix = ix*temp; |
| 499 | if (temp == 0) |
| 500 | break; /* Avoid ix / 0 */ |
| 501 | if (ix / temp != prev) |
| 502 | return err_ovf("integer pow()"); |
| 503 | } |
| 504 | iw >>= 1; /* Shift exponent down by 1 bit */ |
| 505 | if (iw==0) break; |
| 506 | prev = temp; |
| 507 | temp *= temp; /* Square the value of temp */ |
| 508 | if (prev!=0 && temp/prev!=prev) |
| 509 | return err_ovf("integer pow()"); |
| 510 | if (zset) { |
| 511 | /* If we did a multiplication, perform a modulo */ |
| 512 | ix = ix % iz; |
| 513 | temp = temp % iz; |
| 514 | } |
| 515 | } |
| 516 | if (zset) { |
| 517 | object *t1, *t2; |
| 518 | long int div, mod; |
| 519 | t1=newintobject(ix); |
| 520 | t2=newintobject(iz); |
| 521 | if (t1==NULL || t2==NULL || |
| 522 | i_divmod((intobject *)t1, (intobject *)t2, &div, &mod)<0) { |
| 523 | XDECREF(t1); |
| 524 | XDECREF(t2); |
| 525 | return(NULL); |
| 526 | } |
| 527 | ix=mod; |
| 528 | } |
| 529 | return newintobject(ix); |
| 530 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 531 | register long iv, iw, ix; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 532 | iv = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 533 | iw = w->ob_ival; |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 534 | if (iw < 0) { |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 535 | err_setstr(ValueError, "integer to the negative power"); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 536 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 537 | } |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 538 | if ((object *)z != None) { |
| 539 | err_setstr(TypeError, "pow(int, int, int) not yet supported"); |
| 540 | return NULL; |
| 541 | } |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 542 | ix = 1; |
| 543 | while (--iw >= 0) { |
| 544 | long prev = ix; |
| 545 | ix = ix * iv; |
| 546 | if (iv == 0) |
| 547 | break; /* 0 to some power -- avoid ix / 0 */ |
| 548 | if (ix / iv != prev) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 549 | return err_ovf("integer pow()"); |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 550 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 551 | return newintobject(ix); |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 552 | #endif |
| 553 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 554 | |
| 555 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 556 | int_neg(v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 557 | intobject *v; |
| 558 | { |
| 559 | register long a, x; |
| 560 | a = v->ob_ival; |
| 561 | x = -a; |
Guido van Rossum | 165e67e | 1990-10-14 20:02:26 +0000 | [diff] [blame] | 562 | if (a < 0 && x < 0) |
Guido van Rossum | 3a62845 | 1991-12-10 13:57:36 +0000 | [diff] [blame] | 563 | return err_ovf("integer negation"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 564 | return newintobject(x); |
| 565 | } |
| 566 | |
| 567 | static object * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 568 | int_pos(v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 569 | intobject *v; |
| 570 | { |
| 571 | INCREF(v); |
| 572 | return (object *)v; |
| 573 | } |
| 574 | |
Guido van Rossum | 0046695 | 1991-05-05 20:08:27 +0000 | [diff] [blame] | 575 | static object * |
| 576 | int_abs(v) |
| 577 | intobject *v; |
| 578 | { |
| 579 | if (v->ob_ival >= 0) |
| 580 | return int_pos(v); |
| 581 | else |
| 582 | return int_neg(v); |
| 583 | } |
| 584 | |
Guido van Rossum | 0bff015 | 1991-05-14 12:05:32 +0000 | [diff] [blame] | 585 | static int |
| 586 | int_nonzero(v) |
| 587 | intobject *v; |
| 588 | { |
| 589 | return v->ob_ival != 0; |
| 590 | } |
| 591 | |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 592 | static object * |
| 593 | int_invert(v) |
| 594 | intobject *v; |
| 595 | { |
| 596 | return newintobject(~v->ob_ival); |
| 597 | } |
| 598 | |
| 599 | static object * |
| 600 | int_lshift(v, w) |
| 601 | intobject *v; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 602 | intobject *w; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 603 | { |
| 604 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 605 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 606 | b = w->ob_ival; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 607 | if (b < 0) { |
| 608 | err_setstr(ValueError, "negative shift count"); |
| 609 | return NULL; |
| 610 | } |
| 611 | if (a == 0 || b == 0) { |
| 612 | INCREF(v); |
| 613 | return (object *) v; |
| 614 | } |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 615 | if (b >= LONG_BIT) { |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 616 | return newintobject(0L); |
| 617 | } |
| 618 | a = (unsigned long)a << b; |
| 619 | return newintobject(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static object * |
| 623 | int_rshift(v, w) |
| 624 | intobject *v; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 625 | intobject *w; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 626 | { |
| 627 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 628 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 629 | b = w->ob_ival; |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 630 | if (b < 0) { |
| 631 | err_setstr(ValueError, "negative shift count"); |
| 632 | return NULL; |
| 633 | } |
| 634 | if (a == 0 || b == 0) { |
| 635 | INCREF(v); |
| 636 | return (object *) v; |
| 637 | } |
Guido van Rossum | 72481a3 | 1993-10-26 15:21:51 +0000 | [diff] [blame] | 638 | if (b >= LONG_BIT) { |
Guido van Rossum | f3b351f | 1992-01-14 18:33:22 +0000 | [diff] [blame] | 639 | if (a < 0) |
| 640 | a = -1; |
| 641 | else |
| 642 | a = 0; |
| 643 | } |
| 644 | else { |
| 645 | if (a < 0) |
| 646 | a = ~( ~(unsigned long)a >> b ); |
| 647 | else |
| 648 | a = (unsigned long)a >> b; |
| 649 | } |
| 650 | return newintobject(a); |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static object * |
| 654 | int_and(v, w) |
| 655 | intobject *v; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 656 | intobject *w; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 657 | { |
| 658 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 659 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 660 | b = w->ob_ival; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 661 | return newintobject(a & b); |
| 662 | } |
| 663 | |
| 664 | static object * |
| 665 | int_xor(v, w) |
| 666 | intobject *v; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 667 | intobject *w; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 668 | { |
| 669 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 670 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 671 | b = w->ob_ival; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 672 | return newintobject(a ^ b); |
| 673 | } |
| 674 | |
| 675 | static object * |
| 676 | int_or(v, w) |
| 677 | intobject *v; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 678 | intobject *w; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 679 | { |
| 680 | register long a, b; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 681 | a = v->ob_ival; |
Guido van Rossum | 2b16a6f | 1992-01-19 16:28:51 +0000 | [diff] [blame] | 682 | b = w->ob_ival; |
Guido van Rossum | 7928cd7 | 1991-10-24 14:59:31 +0000 | [diff] [blame] | 683 | return newintobject(a | b); |
| 684 | } |
| 685 | |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 686 | static object * |
| 687 | int_int(v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 688 | intobject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 689 | { |
| 690 | INCREF(v); |
Guido van Rossum | 9575a44 | 1993-04-07 14:06:14 +0000 | [diff] [blame] | 691 | return (object *)v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | static object * |
| 695 | int_long(v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 696 | intobject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 697 | { |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 698 | return newlongobject((v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static object * |
| 702 | int_float(v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 703 | intobject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 704 | { |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 705 | return newfloatobject((double)(v -> ob_ival)); |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static object * |
| 709 | int_oct(v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 710 | intobject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 711 | { |
| 712 | char buf[20]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 713 | long x = v -> ob_ival; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 714 | if (x == 0) |
| 715 | strcpy(buf, "0"); |
| 716 | else if (x > 0) |
| 717 | sprintf(buf, "0%lo", x); |
| 718 | else |
| 719 | sprintf(buf, "-0%lo", -x); |
| 720 | return newstringobject(buf); |
| 721 | } |
| 722 | |
| 723 | static object * |
| 724 | int_hex(v) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 725 | intobject *v; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 726 | { |
| 727 | char buf[20]; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 728 | long x = v -> ob_ival; |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 729 | if (x >= 0) |
| 730 | sprintf(buf, "0x%lx", x); |
| 731 | else |
| 732 | sprintf(buf, "-0x%lx", -x); |
| 733 | return newstringobject(buf); |
| 734 | } |
| 735 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 736 | static number_methods int_as_number = { |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 737 | (binaryfunc)int_add, /*nb_add*/ |
| 738 | (binaryfunc)int_sub, /*nb_subtract*/ |
| 739 | (binaryfunc)int_mul, /*nb_multiply*/ |
| 740 | (binaryfunc)int_div, /*nb_divide*/ |
| 741 | (binaryfunc)int_mod, /*nb_remainder*/ |
| 742 | (binaryfunc)int_divmod, /*nb_divmod*/ |
| 743 | (ternaryfunc)int_pow, /*nb_power*/ |
| 744 | (unaryfunc)int_neg, /*nb_negative*/ |
| 745 | (unaryfunc)int_pos, /*nb_positive*/ |
| 746 | (unaryfunc)int_abs, /*nb_absolute*/ |
| 747 | (inquiry)int_nonzero, /*nb_nonzero*/ |
| 748 | (unaryfunc)int_invert, /*nb_invert*/ |
| 749 | (binaryfunc)int_lshift, /*nb_lshift*/ |
| 750 | (binaryfunc)int_rshift, /*nb_rshift*/ |
| 751 | (binaryfunc)int_and, /*nb_and*/ |
| 752 | (binaryfunc)int_xor, /*nb_xor*/ |
| 753 | (binaryfunc)int_or, /*nb_or*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 754 | 0, /*nb_coerce*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 755 | (unaryfunc)int_int, /*nb_int*/ |
| 756 | (unaryfunc)int_long, /*nb_long*/ |
| 757 | (unaryfunc)int_float, /*nb_float*/ |
| 758 | (unaryfunc)int_oct, /*nb_oct*/ |
| 759 | (unaryfunc)int_hex, /*nb_hex*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 760 | }; |
| 761 | |
| 762 | typeobject Inttype = { |
| 763 | OB_HEAD_INIT(&Typetype) |
| 764 | 0, |
| 765 | "int", |
| 766 | sizeof(intobject), |
| 767 | 0, |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 768 | (destructor)int_dealloc, /*tp_dealloc*/ |
| 769 | (printfunc)int_print, /*tp_print*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 770 | 0, /*tp_getattr*/ |
| 771 | 0, /*tp_setattr*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 772 | (cmpfunc)int_compare, /*tp_compare*/ |
| 773 | (reprfunc)int_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 774 | &int_as_number, /*tp_as_number*/ |
| 775 | 0, /*tp_as_sequence*/ |
| 776 | 0, /*tp_as_mapping*/ |
Guido van Rossum | bf8c0e3 | 1994-08-29 12:48:32 +0000 | [diff] [blame] | 777 | (hashfunc)int_hash, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 778 | }; |