longobject.c revision 0ec9abaa2b5f85837feae5c6cbc1491f2f34f16f
1a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko 2a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko/* Long (arbitrary precision) integer object implementation */ 3a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko 4a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko/* XXX The functional organization of this file is terrible */ 5fed60b0c640828b320f56293c8bebc43fd2b1da8Alex Vakulenko 6fed60b0c640828b320f56293c8bebc43fd2b1da8Alex Vakulenko#include "Python.h" 7a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko#include "longintrepr.h" 8a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko 9a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko#include <assert.h> 10a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko#include <ctype.h> 117fb5e5490f50cde360b30a7fb4ad9c29f80b3f12Alex Vakulenko 12f2418e562d358917b02b53290d5f4b3690d6f5d3Alex Vakulenko#define ABS(x) ((x) < 0 ? -(x) : (x)) 139ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko 149ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko/* Forward */ 15a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenkostatic PyLongObject *long_normalize(PyLongObject *); 169ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenkostatic PyLongObject *mul1(PyLongObject *, wdigit); 17a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenkostatic PyLongObject *muladd1(PyLongObject *, wdigit, wdigit); 18a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenkostatic PyLongObject *divrem1(PyLongObject *, wdigit, digit *); 19a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenkostatic PyObject *long_format(PyObject *aa, int base, int addL); 20a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko 21a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenkostatic int ticker; /* XXX Could be shared with ceval? */ 229ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko 23847b871c166235af441bd59572dd9aabcd8ae976Alex Vakulenko#define SIGCHECK(PyTryBlock) \ 24847b871c166235af441bd59572dd9aabcd8ae976Alex Vakulenko if (--ticker < 0) { \ 259ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko ticker = 100; \ 26a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko if (PyErr_CheckSignals()) { PyTryBlock; } \ 27a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko } 28a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko 29a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko/* Normalize (remove leading zeros from) a long int object. 30a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko Doesn't attempt to free the storage--in most cases, due to the nature 31a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko of the algorithms used, this could save at most be one word anyway. */ 32a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko 339ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenkostatic PyLongObject * 34a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenkolong_normalize(register PyLongObject *v) 35a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko{ 36a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko int j = ABS(v->ob_size); 37a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko register int i = j; 38a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko 39a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko while (i > 0 && v->ob_digit[i-1] == 0) 40a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko --i; 41a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko if (i != j) 42a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko v->ob_size = (v->ob_size < 0) ? -(i) : i; 43a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko return v; 449ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko} 459ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko 46a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko/* Allocate a new long int object with size digits. 47a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko Return NULL and set exception if we run out of memory. */ 489ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko 49847b871c166235af441bd59572dd9aabcd8ae976Alex VakulenkoPyLongObject * 50a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko_PyLong_New(int size) 51a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko{ 529ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size); 53847b871c166235af441bd59572dd9aabcd8ae976Alex Vakulenko} 54847b871c166235af441bd59572dd9aabcd8ae976Alex Vakulenko 559ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko/* Create a new long int object from a C long int */ 56847b871c166235af441bd59572dd9aabcd8ae976Alex Vakulenko 57847b871c166235af441bd59572dd9aabcd8ae976Alex VakulenkoPyObject * 58a629bed6477de7b667b8253eca1932e704f332faAlex VakulenkoPyLong_FromLong(long ival) 59a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko{ 60a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko PyLongObject *v; 61a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko unsigned long t; /* unsigned so >> doesn't propagate sign bit */ 62a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko int ndigits = 0; 63a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko int negative = 0; 64a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko 659ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko if (ival < 0) { 66847b871c166235af441bd59572dd9aabcd8ae976Alex Vakulenko ival = -ival; 67847b871c166235af441bd59572dd9aabcd8ae976Alex Vakulenko negative = 1; 68a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko } 69a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko 709ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko /* Count the number of Python digits. 71847b871c166235af441bd59572dd9aabcd8ae976Alex Vakulenko We used to pick 5 ("big enough for anything"), but that's a 72847b871c166235af441bd59572dd9aabcd8ae976Alex Vakulenko waste of time and space given that 5*15 = 75 bits are rarely 73847b871c166235af441bd59572dd9aabcd8ae976Alex Vakulenko needed. */ 74a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko t = (unsigned long)ival; 759ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko while (t) { 76a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko ++ndigits; 77a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko t >>= SHIFT; 78a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko } 79a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko v = _PyLong_New(ndigits); 809ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko if (v != NULL) { 81a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko digit *p = v->ob_digit; 82a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko v->ob_size = negative ? -ndigits : ndigits; 839ed0cab99f18acb3570a35e9408f24355f6b8324Alex Vakulenko t = (unsigned long)ival; 84a629bed6477de7b667b8253eca1932e704f332faAlex Vakulenko while (t) { 85fed60b0c640828b320f56293c8bebc43fd2b1da8Alex Vakulenko *p++ = (digit)(t & MASK); 86 t >>= SHIFT; 87 } 88 } 89 return (PyObject *)v; 90} 91 92/* Create a new long int object from a C unsigned long int */ 93 94PyObject * 95PyLong_FromUnsignedLong(unsigned long ival) 96{ 97 PyLongObject *v; 98 unsigned long t; 99 int ndigits = 0; 100 101 /* Count the number of Python digits. */ 102 t = (unsigned long)ival; 103 while (t) { 104 ++ndigits; 105 t >>= SHIFT; 106 } 107 v = _PyLong_New(ndigits); 108 if (v != NULL) { 109 digit *p = v->ob_digit; 110 v->ob_size = ndigits; 111 while (ival) { 112 *p++ = (digit)(ival & MASK); 113 ival >>= SHIFT; 114 } 115 } 116 return (PyObject *)v; 117} 118 119/* Create a new long int object from a C double */ 120 121PyObject * 122PyLong_FromDouble(double dval) 123{ 124 PyLongObject *v; 125 double frac; 126 int i, ndig, expo, neg; 127 neg = 0; 128 if (Py_IS_INFINITY(dval)) { 129 PyErr_SetString(PyExc_OverflowError, 130 "cannot convert float infinity to long"); 131 return NULL; 132 } 133 if (dval < 0.0) { 134 neg = 1; 135 dval = -dval; 136 } 137 frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */ 138 if (expo <= 0) 139 return PyLong_FromLong(0L); 140 ndig = (expo-1) / SHIFT + 1; /* Number of 'digits' in result */ 141 v = _PyLong_New(ndig); 142 if (v == NULL) 143 return NULL; 144 frac = ldexp(frac, (expo-1) % SHIFT + 1); 145 for (i = ndig; --i >= 0; ) { 146 long bits = (long)frac; 147 v->ob_digit[i] = (digit) bits; 148 frac = frac - (double)bits; 149 frac = ldexp(frac, SHIFT); 150 } 151 if (neg) 152 v->ob_size = -(v->ob_size); 153 return (PyObject *)v; 154} 155 156/* Get a C long int from a long int object. 157 Returns -1 and sets an error condition if overflow occurs. */ 158 159long 160PyLong_AsLong(PyObject *vv) 161{ 162 /* This version by Tim Peters */ 163 register PyLongObject *v; 164 unsigned long x, prev; 165 int i, sign; 166 167 if (vv == NULL || !PyLong_Check(vv)) { 168 PyErr_BadInternalCall(); 169 return -1; 170 } 171 v = (PyLongObject *)vv; 172 i = v->ob_size; 173 sign = 1; 174 x = 0; 175 if (i < 0) { 176 sign = -1; 177 i = -(i); 178 } 179 while (--i >= 0) { 180 prev = x; 181 x = (x << SHIFT) + v->ob_digit[i]; 182 if ((x >> SHIFT) != prev) 183 goto overflow; 184 } 185 /* Haven't lost any bits, but if the sign bit is set we're in 186 * trouble *unless* this is the min negative number. So, 187 * trouble iff sign bit set && (positive || some bit set other 188 * than the sign bit). 189 */ 190 if ((long)x < 0 && (sign > 0 || (x << 1) != 0)) 191 goto overflow; 192 return (long)x * sign; 193 194 overflow: 195 PyErr_SetString(PyExc_OverflowError, 196 "long int too large to convert"); 197 return -1; 198} 199 200/* Get a C long int from a long int object. 201 Returns -1 and sets an error condition if overflow occurs. */ 202 203unsigned long 204PyLong_AsUnsignedLong(PyObject *vv) 205{ 206 register PyLongObject *v; 207 unsigned long x, prev; 208 int i; 209 210 if (vv == NULL || !PyLong_Check(vv)) { 211 PyErr_BadInternalCall(); 212 return (unsigned long) -1; 213 } 214 v = (PyLongObject *)vv; 215 i = v->ob_size; 216 x = 0; 217 if (i < 0) { 218 PyErr_SetString(PyExc_OverflowError, 219 "can't convert negative value to unsigned long"); 220 return (unsigned long) -1; 221 } 222 while (--i >= 0) { 223 prev = x; 224 x = (x << SHIFT) + v->ob_digit[i]; 225 if ((x >> SHIFT) != prev) { 226 PyErr_SetString(PyExc_OverflowError, 227 "long int too large to convert"); 228 return (unsigned long) -1; 229 } 230 } 231 return x; 232} 233 234PyObject * 235_PyLong_FromByteArray(const unsigned char* bytes, size_t n, 236 int little_endian, int is_signed) 237{ 238 const unsigned char* pstartbyte;/* LSB of bytes */ 239 int incr; /* direction to move pstartbyte */ 240 const unsigned char* pendbyte; /* MSB of bytes */ 241 size_t numsignificantbytes; /* number of bytes that matter */ 242 size_t ndigits; /* number of Python long digits */ 243 PyLongObject* v; /* result */ 244 int idigit = 0; /* next free index in v->ob_digit */ 245 246 if (n == 0) 247 return PyLong_FromLong(0L); 248 249 if (little_endian) { 250 pstartbyte = bytes; 251 pendbyte = bytes + n - 1; 252 incr = 1; 253 } 254 else { 255 pstartbyte = bytes + n - 1; 256 pendbyte = bytes; 257 incr = -1; 258 } 259 260 if (is_signed) 261 is_signed = *pendbyte >= 0x80; 262 263 /* Compute numsignificantbytes. This consists of finding the most 264 significant byte. Leading 0 bytes are insignficant if the number 265 is positive, and leading 0xff bytes if negative. */ 266 { 267 size_t i; 268 const unsigned char* p = pendbyte; 269 const int pincr = -incr; /* search MSB to LSB */ 270 const unsigned char insignficant = is_signed ? 0xff : 0x00; 271 272 for (i = 0; i < n; ++i, p += pincr) { 273 if (*p != insignficant) 274 break; 275 } 276 numsignificantbytes = n - i; 277 /* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so 278 actually has 2 significant bytes. OTOH, 0xff0001 == 279 -0x00ffff, so we wouldn't *need* to bump it there; but we 280 do for 0xffff = -0x0001. To be safe without bothering to 281 check every case, bump it regardless. */ 282 if (is_signed && numsignificantbytes < n) 283 ++numsignificantbytes; 284 } 285 286 /* How many Python long digits do we need? We have 287 8*numsignificantbytes bits, and each Python long digit has SHIFT 288 bits, so it's the ceiling of the quotient. */ 289 ndigits = (numsignificantbytes * 8 + SHIFT - 1) / SHIFT; 290 if (ndigits > (size_t)INT_MAX) 291 return PyErr_NoMemory(); 292 v = _PyLong_New((int)ndigits); 293 if (v == NULL) 294 return NULL; 295 296 /* Copy the bits over. The tricky parts are computing 2's-comp on 297 the fly for signed numbers, and dealing with the mismatch between 298 8-bit bytes and (probably) 15-bit Python digits.*/ 299 { 300 size_t i; 301 twodigits carry = 1; /* for 2's-comp calculation */ 302 twodigits accum = 0; /* sliding register */ 303 unsigned int accumbits = 0; /* number of bits in accum */ 304 const unsigned char* p = pstartbyte; 305 306 for (i = 0; i < numsignificantbytes; ++i, p += incr) { 307 twodigits thisbyte = *p; 308 /* Compute correction for 2's comp, if needed. */ 309 if (is_signed) { 310 thisbyte = (0xff ^ thisbyte) + carry; 311 carry = thisbyte >> 8; 312 thisbyte &= 0xff; 313 } 314 /* Because we're going LSB to MSB, thisbyte is 315 more significant than what's already in accum, 316 so needs to be prepended to accum. */ 317 accum |= thisbyte << accumbits; 318 accumbits += 8; 319 if (accumbits >= SHIFT) { 320 /* There's enough to fill a Python digit. */ 321 assert(idigit < (int)ndigits); 322 v->ob_digit[idigit] = (digit)(accum & MASK); 323 ++idigit; 324 accum >>= SHIFT; 325 accumbits -= SHIFT; 326 assert(accumbits < SHIFT); 327 } 328 } 329 assert(accumbits < SHIFT); 330 if (accumbits) { 331 assert(idigit < (int)ndigits); 332 v->ob_digit[idigit] = (digit)accum; 333 ++idigit; 334 } 335 } 336 337 v->ob_size = is_signed ? -idigit : idigit; 338 return (PyObject *)long_normalize(v); 339} 340 341int 342_PyLong_AsByteArray(PyLongObject* v, 343 unsigned char* bytes, size_t n, 344 int little_endian, int is_signed) 345{ 346 int i; /* index into v->ob_digit */ 347 int ndigits; /* |v->ob_size| */ 348 twodigits accum; /* sliding register */ 349 unsigned int accumbits; /* # bits in accum */ 350 int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */ 351 twodigits carry; /* for computing 2's-comp */ 352 size_t j; /* # bytes filled */ 353 unsigned char* p; /* pointer to next byte in bytes */ 354 int pincr; /* direction to move p */ 355 356 assert(v != NULL && PyLong_Check(v)); 357 358 if (v->ob_size < 0) { 359 ndigits = -(v->ob_size); 360 if (!is_signed) { 361 PyErr_SetString(PyExc_TypeError, 362 "can't convert negative long to unsigned"); 363 return -1; 364 } 365 do_twos_comp = 1; 366 } 367 else { 368 ndigits = v->ob_size; 369 do_twos_comp = 0; 370 } 371 372 if (little_endian) { 373 p = bytes; 374 pincr = 1; 375 } 376 else { 377 p = bytes + n - 1; 378 pincr = -1; 379 } 380 381 /* Copy over all the Python digits. 382 It's crucial that every Python digit except for the MSD contribute 383 exactly SHIFT bits to the total, so first assert that the long is 384 normalized. */ 385 assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0); 386 j = 0; 387 accum = 0; 388 accumbits = 0; 389 carry = do_twos_comp ? 1 : 0; 390 for (i = 0; i < ndigits; ++i) { 391 twodigits thisdigit = v->ob_digit[i]; 392 if (do_twos_comp) { 393 thisdigit = (thisdigit ^ MASK) + carry; 394 carry = thisdigit >> SHIFT; 395 thisdigit &= MASK; 396 } 397 /* Because we're going LSB to MSB, thisdigit is more 398 significant than what's already in accum, so needs to be 399 prepended to accum. */ 400 accum |= thisdigit << accumbits; 401 accumbits += SHIFT; 402 403 /* The most-significant digit may be (probably is) at least 404 partly empty. */ 405 if (i == ndigits - 1) { 406 /* Count # of sign bits -- they needn't be stored, 407 * although for signed conversion we need later to 408 * make sure at least one sign bit gets stored. 409 * First shift conceptual sign bit to real sign bit. 410 */ 411 stwodigits s = (stwodigits)(thisdigit << 412 (8*sizeof(stwodigits) - SHIFT)); 413 unsigned int nsignbits = 0; 414 while ((s < 0) == do_twos_comp && nsignbits < SHIFT) { 415 ++nsignbits; 416 s <<= 1; 417 } 418 accumbits -= nsignbits; 419 } 420 421 /* Store as many bytes as possible. */ 422 while (accumbits >= 8) { 423 if (j >= n) 424 goto Overflow; 425 ++j; 426 *p = (unsigned char)(accum & 0xff); 427 p += pincr; 428 accumbits -= 8; 429 accum >>= 8; 430 } 431 } 432 433 /* Store the straggler (if any). */ 434 assert(accumbits < 8); 435 assert(carry == 0); /* else do_twos_comp and *every* digit was 0 */ 436 if (accumbits > 0) { 437 if (j >= n) 438 goto Overflow; 439 ++j; 440 if (do_twos_comp) { 441 /* Fill leading bits of the byte with sign bits 442 (appropriately pretending that the long had an 443 infinite supply of sign bits). */ 444 accum |= (~(twodigits)0) << accumbits; 445 } 446 *p = (unsigned char)(accum & 0xff); 447 p += pincr; 448 } 449 else if (j == n && n > 0 && is_signed) { 450 /* The main loop filled the byte array exactly, so the code 451 just above didn't get to ensure there's a sign bit, and the 452 loop below wouldn't add one either. Make sure a sign bit 453 exists. */ 454 unsigned char msb = *(p - pincr); 455 int sign_bit_set = msb >= 0x80; 456 assert(accumbits == 0); 457 if (sign_bit_set == do_twos_comp) 458 return 0; 459 else 460 goto Overflow; 461 } 462 463 /* Fill remaining bytes with copies of the sign bit. */ 464 { 465 unsigned char signbyte = do_twos_comp ? 0xffU : 0U; 466 for ( ; j < n; ++j, p += pincr) 467 *p = signbyte; 468 } 469 470 return 0; 471 472Overflow: 473 PyErr_SetString(PyExc_OverflowError, "long too big to convert"); 474 return -1; 475 476} 477 478/* Get a C double from a long int object. */ 479 480double 481PyLong_AsDouble(PyObject *vv) 482{ 483 register PyLongObject *v; 484 double x; 485 double multiplier = (double) (1L << SHIFT); 486 int i, sign; 487 488 if (vv == NULL || !PyLong_Check(vv)) { 489 PyErr_BadInternalCall(); 490 return -1; 491 } 492 v = (PyLongObject *)vv; 493 i = v->ob_size; 494 sign = 1; 495 x = 0.0; 496 if (i < 0) { 497 sign = -1; 498 i = -(i); 499 } 500 while (--i >= 0) { 501 x = x*multiplier + (double)v->ob_digit[i]; 502 } 503 return x * sign; 504} 505 506/* Create a new long (or int) object from a C pointer */ 507 508PyObject * 509PyLong_FromVoidPtr(void *p) 510{ 511#if SIZEOF_VOID_P <= SIZEOF_LONG 512 return PyInt_FromLong((long)p); 513#else 514 515#ifndef HAVE_LONG_LONG 516# error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long" 517#endif 518#if SIZEOF_LONG_LONG < SIZEOF_VOID_P 519# error "PyLong_FromVoidPtr: sizeof(LONG_LONG) < sizeof(void*)" 520#endif 521 /* optimize null pointers */ 522 if (p == NULL) 523 return PyInt_FromLong(0); 524 return PyLong_FromLongLong((LONG_LONG)p); 525 526#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ 527} 528 529/* Get a C pointer from a long object (or an int object in some cases) */ 530 531void * 532PyLong_AsVoidPtr(PyObject *vv) 533{ 534 /* This function will allow int or long objects. If vv is neither, 535 then the PyLong_AsLong*() functions will raise the exception: 536 PyExc_SystemError, "bad argument to internal function" 537 */ 538#if SIZEOF_VOID_P <= SIZEOF_LONG 539 long x; 540 541 if (PyInt_Check(vv)) 542 x = PyInt_AS_LONG(vv); 543 else 544 x = PyLong_AsLong(vv); 545#else 546 547#ifndef HAVE_LONG_LONG 548# error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long" 549#endif 550#if SIZEOF_LONG_LONG < SIZEOF_VOID_P 551# error "PyLong_AsVoidPtr: sizeof(LONG_LONG) < sizeof(void*)" 552#endif 553 LONG_LONG x; 554 555 if (PyInt_Check(vv)) 556 x = PyInt_AS_LONG(vv); 557 else 558 x = PyLong_AsLongLong(vv); 559 560#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ 561 562 if (x == -1 && PyErr_Occurred()) 563 return NULL; 564 return (void *)x; 565} 566 567#ifdef HAVE_LONG_LONG 568 569/* Initial LONG_LONG support by Chris Herborth (chrish@qnx.com), later 570 * rewritten to use the newer PyLong_{As,From}ByteArray API. 571 */ 572 573#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one 574 575/* Create a new long int object from a C LONG_LONG int. */ 576 577PyObject * 578PyLong_FromLongLong(LONG_LONG ival) 579{ 580 LONG_LONG bytes = ival; 581 int one = 1; 582 return _PyLong_FromByteArray( 583 (unsigned char *)&bytes, 584 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1); 585} 586 587/* Create a new long int object from a C unsigned LONG_LONG int. */ 588 589PyObject * 590PyLong_FromUnsignedLongLong(unsigned LONG_LONG ival) 591{ 592 unsigned LONG_LONG bytes = ival; 593 int one = 1; 594 return _PyLong_FromByteArray( 595 (unsigned char *)&bytes, 596 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); 597} 598 599/* Get a C LONG_LONG int from a long int object. 600 Return -1 and set an error if overflow occurs. */ 601 602LONG_LONG 603PyLong_AsLongLong(PyObject *vv) 604{ 605 LONG_LONG bytes; 606 int one = 1; 607 int res; 608 609 if (vv == NULL || !PyLong_Check(vv)) { 610 PyErr_BadInternalCall(); 611 return -1; 612 } 613 614 res = _PyLong_AsByteArray( 615 (PyLongObject *)vv, (unsigned char *)&bytes, 616 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1); 617 618 return res < 0 ? (LONG_LONG)res : bytes; 619} 620 621/* Get a C unsigned LONG_LONG int from a long int object. 622 Return -1 and set an error if overflow occurs. */ 623 624unsigned LONG_LONG 625PyLong_AsUnsignedLongLong(PyObject *vv) 626{ 627 unsigned LONG_LONG bytes; 628 int one = 1; 629 int res; 630 631 if (vv == NULL || !PyLong_Check(vv)) { 632 PyErr_BadInternalCall(); 633 return -1; 634 } 635 636 res = _PyLong_AsByteArray( 637 (PyLongObject *)vv, (unsigned char *)&bytes, 638 SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); 639 640 return res < 0 ? (unsigned LONG_LONG)res : bytes; 641} 642 643#undef IS_LITTLE_ENDIAN 644 645#endif /* HAVE_LONG_LONG */ 646 647 648static int 649convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) { 650 if (PyLong_Check(v)) { 651 *a = (PyLongObject *) v; 652 Py_INCREF(v); 653 } 654 else if (PyInt_Check(v)) { 655 *a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v)); 656 } 657 else { 658 return 0; 659 } 660 if (PyLong_Check(w)) { 661 *b = (PyLongObject *) w; 662 Py_INCREF(w); 663 } 664 else if (PyInt_Check(w)) { 665 *b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w)); 666 } 667 else { 668 Py_DECREF(*a); 669 return 0; 670 } 671 return 1; 672} 673 674#define CONVERT_BINOP(v, w, a, b) \ 675 if (!convert_binop(v, w, a, b)) { \ 676 Py_INCREF(Py_NotImplemented); \ 677 return Py_NotImplemented; \ 678 } 679 680 681/* Multiply by a single digit, ignoring the sign. */ 682 683static PyLongObject * 684mul1(PyLongObject *a, wdigit n) 685{ 686 return muladd1(a, n, (digit)0); 687} 688 689/* Multiply by a single digit and add a single digit, ignoring the sign. */ 690 691static PyLongObject * 692muladd1(PyLongObject *a, wdigit n, wdigit extra) 693{ 694 int size_a = ABS(a->ob_size); 695 PyLongObject *z = _PyLong_New(size_a+1); 696 twodigits carry = extra; 697 int i; 698 699 if (z == NULL) 700 return NULL; 701 for (i = 0; i < size_a; ++i) { 702 carry += (twodigits)a->ob_digit[i] * n; 703 z->ob_digit[i] = (digit) (carry & MASK); 704 carry >>= SHIFT; 705 } 706 z->ob_digit[i] = (digit) carry; 707 return long_normalize(z); 708} 709 710/* Divide a long integer by a digit, returning both the quotient 711 (as function result) and the remainder (through *prem). 712 The sign of a is ignored; n should not be zero. */ 713 714static PyLongObject * 715divrem1(PyLongObject *a, wdigit n, digit *prem) 716{ 717 int size = ABS(a->ob_size); 718 PyLongObject *z; 719 int i; 720 twodigits rem = 0; 721 722 assert(n > 0 && n <= MASK); 723 z = _PyLong_New(size); 724 if (z == NULL) 725 return NULL; 726 for (i = size; --i >= 0; ) { 727 rem = (rem << SHIFT) + a->ob_digit[i]; 728 z->ob_digit[i] = (digit) (rem/n); 729 rem %= n; 730 } 731 *prem = (digit) rem; 732 return long_normalize(z); 733} 734 735/* Convert a long int object to a string, using a given conversion base. 736 Return a string object. 737 If base is 8 or 16, add the proper prefix '0' or '0x'. */ 738 739static PyObject * 740long_format(PyObject *aa, int base, int addL) 741{ 742 register PyLongObject *a = (PyLongObject *)aa; 743 PyStringObject *str; 744 int i; 745 int size_a = ABS(a->ob_size); 746 char *p; 747 int bits; 748 char sign = '\0'; 749 750 if (a == NULL || !PyLong_Check(a)) { 751 PyErr_BadInternalCall(); 752 return NULL; 753 } 754 assert(base >= 2 && base <= 36); 755 756 /* Compute a rough upper bound for the length of the string */ 757 i = base; 758 bits = 0; 759 while (i > 1) { 760 ++bits; 761 i >>= 1; 762 } 763 i = 5 + (addL ? 1 : 0) + (size_a*SHIFT + bits-1) / bits; 764 str = (PyStringObject *) PyString_FromStringAndSize((char *)0, i); 765 if (str == NULL) 766 return NULL; 767 p = PyString_AS_STRING(str) + i; 768 *p = '\0'; 769 if (addL) 770 *--p = 'L'; 771 if (a->ob_size < 0) 772 sign = '-'; 773 774 if (a->ob_size == 0) { 775 *--p = '0'; 776 } 777 else if ((base & (base - 1)) == 0) { 778 /* JRH: special case for power-of-2 bases */ 779 twodigits temp = a->ob_digit[0]; 780 int bitsleft = SHIFT; 781 int rem; 782 int last = abs(a->ob_size); 783 int basebits = 1; 784 i = base; 785 while ((i >>= 1) > 1) 786 ++basebits; 787 788 i = 0; 789 for (;;) { 790 while (bitsleft >= basebits) { 791 if ((temp == 0) && (i >= last - 1)) break; 792 rem = temp & (base - 1); 793 if (rem < 10) 794 rem += '0'; 795 else 796 rem += 'A' - 10; 797 assert(p > PyString_AS_STRING(str)); 798 *--p = (char) rem; 799 bitsleft -= basebits; 800 temp >>= basebits; 801 } 802 if (++i >= last) { 803 if (temp == 0) break; 804 bitsleft = 99; 805 /* loop again to pick up final digits */ 806 } 807 else { 808 temp = (a->ob_digit[i] << bitsleft) | temp; 809 bitsleft += SHIFT; 810 } 811 } 812 } 813 else { 814 Py_INCREF(a); 815 do { 816 digit rem; 817 PyLongObject *temp = divrem1(a, (digit)base, &rem); 818 if (temp == NULL) { 819 Py_DECREF(a); 820 Py_DECREF(str); 821 return NULL; 822 } 823 if (rem < 10) 824 rem += '0'; 825 else 826 rem += 'A'-10; 827 assert(p > PyString_AS_STRING(str)); 828 *--p = (char) rem; 829 Py_DECREF(a); 830 a = temp; 831 SIGCHECK({ 832 Py_DECREF(a); 833 Py_DECREF(str); 834 return NULL; 835 }) 836 } while (ABS(a->ob_size) != 0); 837 Py_DECREF(a); 838 } 839 840 if (base == 8) { 841 if (size_a != 0) 842 *--p = '0'; 843 } 844 else if (base == 16) { 845 *--p = 'x'; 846 *--p = '0'; 847 } 848 else if (base != 10) { 849 *--p = '#'; 850 *--p = '0' + base%10; 851 if (base > 10) 852 *--p = '0' + base/10; 853 } 854 if (sign) 855 *--p = sign; 856 if (p != PyString_AS_STRING(str)) { 857 char *q = PyString_AS_STRING(str); 858 assert(p > q); 859 do { 860 } while ((*q++ = *p++) != '\0'); 861 q--; 862 _PyString_Resize((PyObject **)&str, 863 (int) (q - PyString_AS_STRING(str))); 864 } 865 return (PyObject *)str; 866} 867 868PyObject * 869PyLong_FromString(char *str, char **pend, int base) 870{ 871 int sign = 1; 872 char *start, *orig_str = str; 873 PyLongObject *z; 874 875 if ((base != 0 && base < 2) || base > 36) { 876 PyErr_SetString(PyExc_ValueError, 877 "long() arg 2 must be >= 2 and <= 36"); 878 return NULL; 879 } 880 while (*str != '\0' && isspace(Py_CHARMASK(*str))) 881 str++; 882 if (*str == '+') 883 ++str; 884 else if (*str == '-') { 885 ++str; 886 sign = -1; 887 } 888 while (*str != '\0' && isspace(Py_CHARMASK(*str))) 889 str++; 890 if (base == 0) { 891 if (str[0] != '0') 892 base = 10; 893 else if (str[1] == 'x' || str[1] == 'X') 894 base = 16; 895 else 896 base = 8; 897 } 898 if (base == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 899 str += 2; 900 z = _PyLong_New(0); 901 start = str; 902 for ( ; z != NULL; ++str) { 903 int k = -1; 904 PyLongObject *temp; 905 906 if (*str <= '9') 907 k = *str - '0'; 908 else if (*str >= 'a') 909 k = *str - 'a' + 10; 910 else if (*str >= 'A') 911 k = *str - 'A' + 10; 912 if (k < 0 || k >= base) 913 break; 914 temp = muladd1(z, (digit)base, (digit)k); 915 Py_DECREF(z); 916 z = temp; 917 } 918 if (z == NULL) 919 return NULL; 920 if (str == start) 921 goto onError; 922 if (sign < 0 && z != NULL && z->ob_size != 0) 923 z->ob_size = -(z->ob_size); 924 if (*str == 'L' || *str == 'l') 925 str++; 926 while (*str && isspace(Py_CHARMASK(*str))) 927 str++; 928 if (*str != '\0') 929 goto onError; 930 if (pend) 931 *pend = str; 932 return (PyObject *) z; 933 934 onError: 935 PyErr_Format(PyExc_ValueError, 936 "invalid literal for long(): %.200s", orig_str); 937 Py_XDECREF(z); 938 return NULL; 939} 940 941PyObject * 942PyLong_FromUnicode(Py_UNICODE *u, int length, int base) 943{ 944 char buffer[256]; 945 946 if (length >= sizeof(buffer)) { 947 PyErr_SetString(PyExc_ValueError, 948 "long() literal too large to convert"); 949 return NULL; 950 } 951 if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) 952 return NULL; 953 954 return PyLong_FromString(buffer, NULL, base); 955} 956 957/* forward */ 958static PyLongObject *x_divrem 959 (PyLongObject *, PyLongObject *, PyLongObject **); 960static PyObject *long_pos(PyLongObject *); 961static int long_divrem(PyLongObject *, PyLongObject *, 962 PyLongObject **, PyLongObject **); 963 964/* Long division with remainder, top-level routine */ 965 966static int 967long_divrem(PyLongObject *a, PyLongObject *b, 968 PyLongObject **pdiv, PyLongObject **prem) 969{ 970 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); 971 PyLongObject *z; 972 973 if (size_b == 0) { 974 PyErr_SetString(PyExc_ZeroDivisionError, 975 "long division or modulo by zero"); 976 return -1; 977 } 978 if (size_a < size_b || 979 (size_a == size_b && 980 a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) { 981 /* |a| < |b|. */ 982 *pdiv = _PyLong_New(0); 983 Py_INCREF(a); 984 *prem = (PyLongObject *) a; 985 return 0; 986 } 987 if (size_b == 1) { 988 digit rem = 0; 989 z = divrem1(a, b->ob_digit[0], &rem); 990 if (z == NULL) 991 return -1; 992 *prem = (PyLongObject *) PyLong_FromLong((long)rem); 993 } 994 else { 995 z = x_divrem(a, b, prem); 996 if (z == NULL) 997 return -1; 998 } 999 /* Set the signs. 1000 The quotient z has the sign of a*b; 1001 the remainder r has the sign of a, 1002 so a = b*z + r. */ 1003 if ((a->ob_size < 0) != (b->ob_size < 0)) 1004 z->ob_size = -(z->ob_size); 1005 if (a->ob_size < 0 && (*prem)->ob_size != 0) 1006 (*prem)->ob_size = -((*prem)->ob_size); 1007 *pdiv = z; 1008 return 0; 1009} 1010 1011/* Unsigned long division with remainder -- the algorithm */ 1012 1013static PyLongObject * 1014x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem) 1015{ 1016 int size_v = ABS(v1->ob_size), size_w = ABS(w1->ob_size); 1017 digit d = (digit) ((twodigits)BASE / (w1->ob_digit[size_w-1] + 1)); 1018 PyLongObject *v = mul1(v1, d); 1019 PyLongObject *w = mul1(w1, d); 1020 PyLongObject *a; 1021 int j, k; 1022 1023 if (v == NULL || w == NULL) { 1024 Py_XDECREF(v); 1025 Py_XDECREF(w); 1026 return NULL; 1027 } 1028 1029 assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */ 1030 assert(v->ob_refcnt == 1); /* Since v will be used as accumulator! */ 1031 assert(size_w == ABS(w->ob_size)); /* That's how d was calculated */ 1032 1033 size_v = ABS(v->ob_size); 1034 a = _PyLong_New(size_v - size_w + 1); 1035 1036 for (j = size_v, k = a->ob_size-1; a != NULL && k >= 0; --j, --k) { 1037 digit vj = (j >= size_v) ? 0 : v->ob_digit[j]; 1038 twodigits q; 1039 stwodigits carry = 0; 1040 int i; 1041 1042 SIGCHECK({ 1043 Py_DECREF(a); 1044 a = NULL; 1045 break; 1046 }) 1047 if (vj == w->ob_digit[size_w-1]) 1048 q = MASK; 1049 else 1050 q = (((twodigits)vj << SHIFT) + v->ob_digit[j-1]) / 1051 w->ob_digit[size_w-1]; 1052 1053 while (w->ob_digit[size_w-2]*q > 1054 (( 1055 ((twodigits)vj << SHIFT) 1056 + v->ob_digit[j-1] 1057 - q*w->ob_digit[size_w-1] 1058 ) << SHIFT) 1059 + v->ob_digit[j-2]) 1060 --q; 1061 1062 for (i = 0; i < size_w && i+k < size_v; ++i) { 1063 twodigits z = w->ob_digit[i] * q; 1064 digit zz = (digit) (z >> SHIFT); 1065 carry += v->ob_digit[i+k] - z 1066 + ((twodigits)zz << SHIFT); 1067 v->ob_digit[i+k] = carry & MASK; 1068 carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE, 1069 carry, SHIFT); 1070 carry -= zz; 1071 } 1072 1073 if (i+k < size_v) { 1074 carry += v->ob_digit[i+k]; 1075 v->ob_digit[i+k] = 0; 1076 } 1077 1078 if (carry == 0) 1079 a->ob_digit[k] = (digit) q; 1080 else { 1081 assert(carry == -1); 1082 a->ob_digit[k] = (digit) q-1; 1083 carry = 0; 1084 for (i = 0; i < size_w && i+k < size_v; ++i) { 1085 carry += v->ob_digit[i+k] + w->ob_digit[i]; 1086 v->ob_digit[i+k] = carry & MASK; 1087 carry = Py_ARITHMETIC_RIGHT_SHIFT( 1088 BASE_TWODIGITS_TYPE, 1089 carry, SHIFT); 1090 } 1091 } 1092 } /* for j, k */ 1093 1094 if (a == NULL) 1095 *prem = NULL; 1096 else { 1097 a = long_normalize(a); 1098 *prem = divrem1(v, d, &d); 1099 /* d receives the (unused) remainder */ 1100 if (*prem == NULL) { 1101 Py_DECREF(a); 1102 a = NULL; 1103 } 1104 } 1105 Py_DECREF(v); 1106 Py_DECREF(w); 1107 return a; 1108} 1109 1110/* Methods */ 1111 1112static void 1113long_dealloc(PyObject *v) 1114{ 1115 PyObject_DEL(v); 1116} 1117 1118static PyObject * 1119long_repr(PyObject *v) 1120{ 1121 return long_format(v, 10, 1); 1122} 1123 1124static PyObject * 1125long_str(PyObject *v) 1126{ 1127 return long_format(v, 10, 0); 1128} 1129 1130static int 1131long_compare(PyLongObject *a, PyLongObject *b) 1132{ 1133 int sign; 1134 1135 if (a->ob_size != b->ob_size) { 1136 if (ABS(a->ob_size) == 0 && ABS(b->ob_size) == 0) 1137 sign = 0; 1138 else 1139 sign = a->ob_size - b->ob_size; 1140 } 1141 else { 1142 int i = ABS(a->ob_size); 1143 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) 1144 ; 1145 if (i < 0) 1146 sign = 0; 1147 else { 1148 sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; 1149 if (a->ob_size < 0) 1150 sign = -sign; 1151 } 1152 } 1153 return sign < 0 ? -1 : sign > 0 ? 1 : 0; 1154} 1155 1156static long 1157long_hash(PyLongObject *v) 1158{ 1159 long x; 1160 int i, sign; 1161 1162 /* This is designed so that Python ints and longs with the 1163 same value hash to the same value, otherwise comparisons 1164 of mapping keys will turn out weird */ 1165 i = v->ob_size; 1166 sign = 1; 1167 x = 0; 1168 if (i < 0) { 1169 sign = -1; 1170 i = -(i); 1171 } 1172 while (--i >= 0) { 1173 /* Force a 32-bit circular shift */ 1174 x = ((x << SHIFT) & ~MASK) | ((x >> (32-SHIFT)) & MASK); 1175 x += v->ob_digit[i]; 1176 } 1177 x = x * sign; 1178 if (x == -1) 1179 x = -2; 1180 return x; 1181} 1182 1183 1184/* Add the absolute values of two long integers. */ 1185 1186static PyLongObject * 1187x_add(PyLongObject *a, PyLongObject *b) 1188{ 1189 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); 1190 PyLongObject *z; 1191 int i; 1192 digit carry = 0; 1193 1194 /* Ensure a is the larger of the two: */ 1195 if (size_a < size_b) { 1196 { PyLongObject *temp = a; a = b; b = temp; } 1197 { int size_temp = size_a; 1198 size_a = size_b; 1199 size_b = size_temp; } 1200 } 1201 z = _PyLong_New(size_a+1); 1202 if (z == NULL) 1203 return NULL; 1204 for (i = 0; i < size_b; ++i) { 1205 carry += a->ob_digit[i] + b->ob_digit[i]; 1206 z->ob_digit[i] = carry & MASK; 1207 carry >>= SHIFT; 1208 } 1209 for (; i < size_a; ++i) { 1210 carry += a->ob_digit[i]; 1211 z->ob_digit[i] = carry & MASK; 1212 carry >>= SHIFT; 1213 } 1214 z->ob_digit[i] = carry; 1215 return long_normalize(z); 1216} 1217 1218/* Subtract the absolute values of two integers. */ 1219 1220static PyLongObject * 1221x_sub(PyLongObject *a, PyLongObject *b) 1222{ 1223 int size_a = ABS(a->ob_size), size_b = ABS(b->ob_size); 1224 PyLongObject *z; 1225 int i; 1226 int sign = 1; 1227 digit borrow = 0; 1228 1229 /* Ensure a is the larger of the two: */ 1230 if (size_a < size_b) { 1231 sign = -1; 1232 { PyLongObject *temp = a; a = b; b = temp; } 1233 { int size_temp = size_a; 1234 size_a = size_b; 1235 size_b = size_temp; } 1236 } 1237 else if (size_a == size_b) { 1238 /* Find highest digit where a and b differ: */ 1239 i = size_a; 1240 while (--i >= 0 && a->ob_digit[i] == b->ob_digit[i]) 1241 ; 1242 if (i < 0) 1243 return _PyLong_New(0); 1244 if (a->ob_digit[i] < b->ob_digit[i]) { 1245 sign = -1; 1246 { PyLongObject *temp = a; a = b; b = temp; } 1247 } 1248 size_a = size_b = i+1; 1249 } 1250 z = _PyLong_New(size_a); 1251 if (z == NULL) 1252 return NULL; 1253 for (i = 0; i < size_b; ++i) { 1254 /* The following assumes unsigned arithmetic 1255 works module 2**N for some N>SHIFT. */ 1256 borrow = a->ob_digit[i] - b->ob_digit[i] - borrow; 1257 z->ob_digit[i] = borrow & MASK; 1258 borrow >>= SHIFT; 1259 borrow &= 1; /* Keep only one sign bit */ 1260 } 1261 for (; i < size_a; ++i) { 1262 borrow = a->ob_digit[i] - borrow; 1263 z->ob_digit[i] = borrow & MASK; 1264 borrow >>= SHIFT; 1265 borrow &= 1; /* Keep only one sign bit */ 1266 } 1267 assert(borrow == 0); 1268 if (sign < 0) 1269 z->ob_size = -(z->ob_size); 1270 return long_normalize(z); 1271} 1272 1273static PyObject * 1274long_add(PyLongObject *v, PyLongObject *w) 1275{ 1276 PyLongObject *a, *b, *z; 1277 1278 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); 1279 1280 if (a->ob_size < 0) { 1281 if (b->ob_size < 0) { 1282 z = x_add(a, b); 1283 if (z != NULL && z->ob_size != 0) 1284 z->ob_size = -(z->ob_size); 1285 } 1286 else 1287 z = x_sub(b, a); 1288 } 1289 else { 1290 if (b->ob_size < 0) 1291 z = x_sub(a, b); 1292 else 1293 z = x_add(a, b); 1294 } 1295 Py_DECREF(a); 1296 Py_DECREF(b); 1297 return (PyObject *)z; 1298} 1299 1300static PyObject * 1301long_sub(PyLongObject *v, PyLongObject *w) 1302{ 1303 PyLongObject *a, *b, *z; 1304 1305 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); 1306 1307 if (a->ob_size < 0) { 1308 if (b->ob_size < 0) 1309 z = x_sub(a, b); 1310 else 1311 z = x_add(a, b); 1312 if (z != NULL && z->ob_size != 0) 1313 z->ob_size = -(z->ob_size); 1314 } 1315 else { 1316 if (b->ob_size < 0) 1317 z = x_add(a, b); 1318 else 1319 z = x_sub(a, b); 1320 } 1321 Py_DECREF(a); 1322 Py_DECREF(b); 1323 return (PyObject *)z; 1324} 1325 1326static PyObject * 1327long_repeat(PyObject *v, PyLongObject *w) 1328{ 1329 /* sequence * long */ 1330 long n = PyLong_AsLong((PyObject *) w); 1331 if (n == -1 && PyErr_Occurred()) 1332 return NULL; 1333 else 1334 return (*v->ob_type->tp_as_sequence->sq_repeat)(v, n); 1335} 1336 1337static PyObject * 1338long_mul(PyLongObject *v, PyLongObject *w) 1339{ 1340 PyLongObject *a, *b, *z; 1341 int size_a; 1342 int size_b; 1343 int i; 1344 1345 if (v->ob_type->tp_as_sequence && 1346 v->ob_type->tp_as_sequence->sq_repeat) { 1347 return long_repeat((PyObject *)v, w); 1348 } 1349 else if (w->ob_type->tp_as_sequence && 1350 w->ob_type->tp_as_sequence->sq_repeat) { 1351 return long_repeat((PyObject *)w, v); 1352 } 1353 1354 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); 1355 1356 size_a = ABS(a->ob_size); 1357 size_b = ABS(b->ob_size); 1358 if (size_a > size_b) { 1359 /* we are faster with the small object on the left */ 1360 int hold_sa = size_a; 1361 PyLongObject *hold_a = a; 1362 size_a = size_b; 1363 size_b = hold_sa; 1364 a = b; 1365 b = hold_a; 1366 } 1367 z = _PyLong_New(size_a + size_b); 1368 if (z == NULL) { 1369 Py_DECREF(a); 1370 Py_DECREF(b); 1371 return NULL; 1372 } 1373 for (i = 0; i < z->ob_size; ++i) 1374 z->ob_digit[i] = 0; 1375 for (i = 0; i < size_a; ++i) { 1376 twodigits carry = 0; 1377 twodigits f = a->ob_digit[i]; 1378 int j; 1379 1380 SIGCHECK({ 1381 Py_DECREF(a); 1382 Py_DECREF(b); 1383 Py_DECREF(z); 1384 return NULL; 1385 }) 1386 for (j = 0; j < size_b; ++j) { 1387 carry += z->ob_digit[i+j] + b->ob_digit[j] * f; 1388 z->ob_digit[i+j] = (digit) (carry & MASK); 1389 carry >>= SHIFT; 1390 } 1391 for (; carry != 0; ++j) { 1392 assert(i+j < z->ob_size); 1393 carry += z->ob_digit[i+j]; 1394 z->ob_digit[i+j] = (digit) (carry & MASK); 1395 carry >>= SHIFT; 1396 } 1397 } 1398 if (a->ob_size < 0) 1399 z->ob_size = -(z->ob_size); 1400 if (b->ob_size < 0) 1401 z->ob_size = -(z->ob_size); 1402 Py_DECREF(a); 1403 Py_DECREF(b); 1404 return (PyObject *) long_normalize(z); 1405} 1406 1407/* The / and % operators are now defined in terms of divmod(). 1408 The expression a mod b has the value a - b*floor(a/b). 1409 The long_divrem function gives the remainder after division of 1410 |a| by |b|, with the sign of a. This is also expressed 1411 as a - b*trunc(a/b), if trunc truncates towards zero. 1412 Some examples: 1413 a b a rem b a mod b 1414 13 10 3 3 1415 -13 10 -3 7 1416 13 -10 3 -7 1417 -13 -10 -3 -3 1418 So, to get from rem to mod, we have to add b if a and b 1419 have different signs. We then subtract one from the 'div' 1420 part of the outcome to keep the invariant intact. */ 1421 1422static int 1423l_divmod(PyLongObject *v, PyLongObject *w, 1424 PyLongObject **pdiv, PyLongObject **pmod) 1425{ 1426 PyLongObject *div, *mod; 1427 1428 if (long_divrem(v, w, &div, &mod) < 0) 1429 return -1; 1430 if ((mod->ob_size < 0 && w->ob_size > 0) || 1431 (mod->ob_size > 0 && w->ob_size < 0)) { 1432 PyLongObject *temp; 1433 PyLongObject *one; 1434 temp = (PyLongObject *) long_add(mod, w); 1435 Py_DECREF(mod); 1436 mod = temp; 1437 if (mod == NULL) { 1438 Py_DECREF(div); 1439 return -1; 1440 } 1441 one = (PyLongObject *) PyLong_FromLong(1L); 1442 if (one == NULL || 1443 (temp = (PyLongObject *) long_sub(div, one)) == NULL) { 1444 Py_DECREF(mod); 1445 Py_DECREF(div); 1446 Py_XDECREF(one); 1447 return -1; 1448 } 1449 Py_DECREF(one); 1450 Py_DECREF(div); 1451 div = temp; 1452 } 1453 *pdiv = div; 1454 *pmod = mod; 1455 return 0; 1456} 1457 1458static PyObject * 1459long_div(PyObject *v, PyObject *w) 1460{ 1461 PyLongObject *a, *b, *div, *mod; 1462 1463 CONVERT_BINOP(v, w, &a, &b); 1464 1465 if (l_divmod(a, b, &div, &mod) < 0) { 1466 Py_DECREF(a); 1467 Py_DECREF(b); 1468 return NULL; 1469 } 1470 Py_DECREF(a); 1471 Py_DECREF(b); 1472 Py_DECREF(mod); 1473 return (PyObject *)div; 1474} 1475 1476static PyObject * 1477long_mod(PyObject *v, PyObject *w) 1478{ 1479 PyLongObject *a, *b, *div, *mod; 1480 1481 CONVERT_BINOP(v, w, &a, &b); 1482 1483 if (l_divmod(a, b, &div, &mod) < 0) { 1484 Py_DECREF(a); 1485 Py_DECREF(b); 1486 return NULL; 1487 } 1488 Py_DECREF(a); 1489 Py_DECREF(b); 1490 Py_DECREF(div); 1491 return (PyObject *)mod; 1492} 1493 1494static PyObject * 1495long_divmod(PyObject *v, PyObject *w) 1496{ 1497 PyLongObject *a, *b, *div, *mod; 1498 PyObject *z; 1499 1500 CONVERT_BINOP(v, w, &a, &b); 1501 1502 if (l_divmod(a, b, &div, &mod) < 0) { 1503 Py_DECREF(a); 1504 Py_DECREF(b); 1505 return NULL; 1506 } 1507 z = PyTuple_New(2); 1508 if (z != NULL) { 1509 PyTuple_SetItem(z, 0, (PyObject *) div); 1510 PyTuple_SetItem(z, 1, (PyObject *) mod); 1511 } 1512 else { 1513 Py_DECREF(div); 1514 Py_DECREF(mod); 1515 } 1516 Py_DECREF(a); 1517 Py_DECREF(b); 1518 return z; 1519} 1520 1521static PyObject * 1522long_pow(PyObject *v, PyObject *w, PyObject *x) 1523{ 1524 PyLongObject *a, *b; 1525 PyObject *c; 1526 PyLongObject *z, *div, *mod; 1527 int size_b, i; 1528 1529 CONVERT_BINOP(v, w, &a, &b); 1530 if (PyLong_Check(x) || Py_None == x) { 1531 c = x; 1532 Py_INCREF(x); 1533 } 1534 else if (PyInt_Check(x)) { 1535 c = PyLong_FromLong(PyInt_AS_LONG(x)); 1536 } 1537 else { 1538 Py_DECREF(a); 1539 Py_DECREF(b); 1540 Py_INCREF(Py_NotImplemented); 1541 return Py_NotImplemented; 1542 } 1543 1544 size_b = b->ob_size; 1545 if (size_b < 0) { 1546 /* Return a float. This works because we know that 1547 this calls float_pow() which converts its 1548 arguments to double. */ 1549 Py_DECREF(a); 1550 Py_DECREF(b); 1551 Py_DECREF(c); 1552 return PyFloat_Type.tp_as_number->nb_power(v, w, x); 1553 } 1554 z = (PyLongObject *)PyLong_FromLong(1L); 1555 for (i = 0; i < size_b; ++i) { 1556 digit bi = b->ob_digit[i]; 1557 int j; 1558 1559 for (j = 0; j < SHIFT; ++j) { 1560 PyLongObject *temp; 1561 1562 if (bi & 1) { 1563 temp = (PyLongObject *)long_mul(z, a); 1564 Py_DECREF(z); 1565 if (c!=Py_None && temp!=NULL) { 1566 if (l_divmod(temp,(PyLongObject *)c, 1567 &div,&mod) < 0) { 1568 Py_DECREF(temp); 1569 z = NULL; 1570 goto error; 1571 } 1572 Py_XDECREF(div); 1573 Py_DECREF(temp); 1574 temp = mod; 1575 } 1576 z = temp; 1577 if (z == NULL) 1578 break; 1579 } 1580 bi >>= 1; 1581 if (bi == 0 && i+1 == size_b) 1582 break; 1583 temp = (PyLongObject *)long_mul(a, a); 1584 Py_DECREF(a); 1585 if (c!=Py_None && temp!=NULL) { 1586 if (l_divmod(temp, (PyLongObject *)c, &div, 1587 &mod) < 0) { 1588 Py_DECREF(temp); 1589 z = NULL; 1590 goto error; 1591 } 1592 Py_XDECREF(div); 1593 Py_DECREF(temp); 1594 temp = mod; 1595 } 1596 a = temp; 1597 if (a == NULL) { 1598 Py_DECREF(z); 1599 z = NULL; 1600 break; 1601 } 1602 } 1603 if (a == NULL || z == NULL) 1604 break; 1605 } 1606 if (c!=Py_None && z!=NULL) { 1607 if (l_divmod(z, (PyLongObject *)c, &div, &mod) < 0) { 1608 Py_DECREF(z); 1609 z = NULL; 1610 } 1611 else { 1612 Py_XDECREF(div); 1613 Py_DECREF(z); 1614 z = mod; 1615 } 1616 } 1617 error: 1618 Py_XDECREF(a); 1619 Py_DECREF(b); 1620 Py_DECREF(c); 1621 return (PyObject *)z; 1622} 1623 1624static PyObject * 1625long_invert(PyLongObject *v) 1626{ 1627 /* Implement ~x as -(x+1) */ 1628 PyLongObject *x; 1629 PyLongObject *w; 1630 w = (PyLongObject *)PyLong_FromLong(1L); 1631 if (w == NULL) 1632 return NULL; 1633 x = (PyLongObject *) long_add(v, w); 1634 Py_DECREF(w); 1635 if (x == NULL) 1636 return NULL; 1637 if (x->ob_size != 0) 1638 x->ob_size = -(x->ob_size); 1639 return (PyObject *)x; 1640} 1641 1642static PyObject * 1643long_pos(PyLongObject *v) 1644{ 1645 Py_INCREF(v); 1646 return (PyObject *)v; 1647} 1648 1649static PyObject * 1650long_neg(PyLongObject *v) 1651{ 1652 PyLongObject *z; 1653 int i, n; 1654 n = ABS(v->ob_size); 1655 if (n == 0) { 1656 /* -0 == 0 */ 1657 Py_INCREF(v); 1658 return (PyObject *) v; 1659 } 1660 z = _PyLong_New(ABS(n)); 1661 if (z == NULL) 1662 return NULL; 1663 for (i = 0; i < n; i++) 1664 z->ob_digit[i] = v->ob_digit[i]; 1665 z->ob_size = -(v->ob_size); 1666 return (PyObject *)z; 1667} 1668 1669static PyObject * 1670long_abs(PyLongObject *v) 1671{ 1672 if (v->ob_size < 0) 1673 return long_neg(v); 1674 else { 1675 Py_INCREF(v); 1676 return (PyObject *)v; 1677 } 1678} 1679 1680static int 1681long_nonzero(PyLongObject *v) 1682{ 1683 return ABS(v->ob_size) != 0; 1684} 1685 1686static PyObject * 1687long_rshift(PyLongObject *v, PyLongObject *w) 1688{ 1689 PyLongObject *a, *b; 1690 PyLongObject *z = NULL; 1691 long shiftby; 1692 int newsize, wordshift, loshift, hishift, i, j; 1693 digit lomask, himask; 1694 1695 CONVERT_BINOP((PyObject *)v, (PyObject *)w, &a, &b); 1696 1697 if (a->ob_size < 0) { 1698 /* Right shifting negative numbers is harder */ 1699 PyLongObject *a1, *a2; 1700 a1 = (PyLongObject *) long_invert(a); 1701 if (a1 == NULL) 1702 goto rshift_error; 1703 a2 = (PyLongObject *) long_rshift(a1, b); 1704 Py_DECREF(a1); 1705 if (a2 == NULL) 1706 goto rshift_error; 1707 z = (PyLongObject *) long_invert(a2); 1708 Py_DECREF(a2); 1709 } 1710 else { 1711 1712 shiftby = PyLong_AsLong((PyObject *)b); 1713 if (shiftby == -1L && PyErr_Occurred()) 1714 goto rshift_error; 1715 if (shiftby < 0) { 1716 PyErr_SetString(PyExc_ValueError, 1717 "negative shift count"); 1718 goto rshift_error; 1719 } 1720 wordshift = shiftby / SHIFT; 1721 newsize = ABS(a->ob_size) - wordshift; 1722 if (newsize <= 0) { 1723 z = _PyLong_New(0); 1724 Py_DECREF(a); 1725 Py_DECREF(b); 1726 return (PyObject *)z; 1727 } 1728 loshift = shiftby % SHIFT; 1729 hishift = SHIFT - loshift; 1730 lomask = ((digit)1 << hishift) - 1; 1731 himask = MASK ^ lomask; 1732 z = _PyLong_New(newsize); 1733 if (z == NULL) 1734 goto rshift_error; 1735 if (a->ob_size < 0) 1736 z->ob_size = -(z->ob_size); 1737 for (i = 0, j = wordshift; i < newsize; i++, j++) { 1738 z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; 1739 if (i+1 < newsize) 1740 z->ob_digit[i] |= 1741 (a->ob_digit[j+1] << hishift) & himask; 1742 } 1743 z = long_normalize(z); 1744 } 1745rshift_error: 1746 Py_DECREF(a); 1747 Py_DECREF(b); 1748 return (PyObject *) z; 1749 1750} 1751 1752static PyObject * 1753long_lshift(PyObject *v, PyObject *w) 1754{ 1755 /* This version due to Tim Peters */ 1756 PyLongObject *a, *b; 1757 PyLongObject *z = NULL; 1758 long shiftby; 1759 int oldsize, newsize, wordshift, remshift, i, j; 1760 twodigits accum; 1761 1762 CONVERT_BINOP(v, w, &a, &b); 1763 1764 shiftby = PyLong_AsLong((PyObject *)b); 1765 if (shiftby == -1L && PyErr_Occurred()) 1766 goto lshift_error; 1767 if (shiftby < 0) { 1768 PyErr_SetString(PyExc_ValueError, "negative shift count"); 1769 goto lshift_error; 1770 } 1771 if ((long)(int)shiftby != shiftby) { 1772 PyErr_SetString(PyExc_ValueError, 1773 "outrageous left shift count"); 1774 goto lshift_error; 1775 } 1776 /* wordshift, remshift = divmod(shiftby, SHIFT) */ 1777 wordshift = (int)shiftby / SHIFT; 1778 remshift = (int)shiftby - wordshift * SHIFT; 1779 1780 oldsize = ABS(a->ob_size); 1781 newsize = oldsize + wordshift; 1782 if (remshift) 1783 ++newsize; 1784 z = _PyLong_New(newsize); 1785 if (z == NULL) 1786 goto lshift_error; 1787 if (a->ob_size < 0) 1788 z->ob_size = -(z->ob_size); 1789 for (i = 0; i < wordshift; i++) 1790 z->ob_digit[i] = 0; 1791 accum = 0; 1792 for (i = wordshift, j = 0; j < oldsize; i++, j++) { 1793 accum |= a->ob_digit[j] << remshift; 1794 z->ob_digit[i] = (digit)(accum & MASK); 1795 accum >>= SHIFT; 1796 } 1797 if (remshift) 1798 z->ob_digit[newsize-1] = (digit)accum; 1799 else 1800 assert(!accum); 1801 z = long_normalize(z); 1802lshift_error: 1803 Py_DECREF(a); 1804 Py_DECREF(b); 1805 return (PyObject *) z; 1806} 1807 1808 1809/* Bitwise and/xor/or operations */ 1810 1811#define MAX(x, y) ((x) < (y) ? (y) : (x)) 1812#define MIN(x, y) ((x) > (y) ? (y) : (x)) 1813 1814static PyObject * 1815long_bitwise(PyLongObject *a, 1816 int op, /* '&', '|', '^' */ 1817 PyLongObject *b) 1818{ 1819 digit maska, maskb; /* 0 or MASK */ 1820 int negz; 1821 int size_a, size_b, size_z; 1822 PyLongObject *z; 1823 int i; 1824 digit diga, digb; 1825 PyObject *v; 1826 1827 if (a->ob_size < 0) { 1828 a = (PyLongObject *) long_invert(a); 1829 maska = MASK; 1830 } 1831 else { 1832 Py_INCREF(a); 1833 maska = 0; 1834 } 1835 if (b->ob_size < 0) { 1836 b = (PyLongObject *) long_invert(b); 1837 maskb = MASK; 1838 } 1839 else { 1840 Py_INCREF(b); 1841 maskb = 0; 1842 } 1843 1844 negz = 0; 1845 switch (op) { 1846 case '^': 1847 if (maska != maskb) { 1848 maska ^= MASK; 1849 negz = -1; 1850 } 1851 break; 1852 case '&': 1853 if (maska && maskb) { 1854 op = '|'; 1855 maska ^= MASK; 1856 maskb ^= MASK; 1857 negz = -1; 1858 } 1859 break; 1860 case '|': 1861 if (maska || maskb) { 1862 op = '&'; 1863 maska ^= MASK; 1864 maskb ^= MASK; 1865 negz = -1; 1866 } 1867 break; 1868 } 1869 1870 /* JRH: The original logic here was to allocate the result value (z) 1871 as the longer of the two operands. However, there are some cases 1872 where the result is guaranteed to be shorter than that: AND of two 1873 positives, OR of two negatives: use the shorter number. AND with 1874 mixed signs: use the positive number. OR with mixed signs: use the 1875 negative number. After the transformations above, op will be '&' 1876 iff one of these cases applies, and mask will be non-0 for operands 1877 whose length should be ignored. 1878 */ 1879 1880 size_a = a->ob_size; 1881 size_b = b->ob_size; 1882 size_z = op == '&' 1883 ? (maska 1884 ? size_b 1885 : (maskb ? size_a : MIN(size_a, size_b))) 1886 : MAX(size_a, size_b); 1887 z = _PyLong_New(size_z); 1888 if (a == NULL || b == NULL || z == NULL) { 1889 Py_XDECREF(a); 1890 Py_XDECREF(b); 1891 Py_XDECREF(z); 1892 return NULL; 1893 } 1894 1895 for (i = 0; i < size_z; ++i) { 1896 diga = (i < size_a ? a->ob_digit[i] : 0) ^ maska; 1897 digb = (i < size_b ? b->ob_digit[i] : 0) ^ maskb; 1898 switch (op) { 1899 case '&': z->ob_digit[i] = diga & digb; break; 1900 case '|': z->ob_digit[i] = diga | digb; break; 1901 case '^': z->ob_digit[i] = diga ^ digb; break; 1902 } 1903 } 1904 1905 Py_DECREF(a); 1906 Py_DECREF(b); 1907 z = long_normalize(z); 1908 if (negz == 0) 1909 return (PyObject *) z; 1910 v = long_invert(z); 1911 Py_DECREF(z); 1912 return v; 1913} 1914 1915static PyObject * 1916long_and(PyObject *v, PyObject *w) 1917{ 1918 PyLongObject *a, *b; 1919 PyObject *c; 1920 CONVERT_BINOP(v, w, &a, &b); 1921 c = long_bitwise(a, '&', b); 1922 Py_DECREF(a); 1923 Py_DECREF(b); 1924 return c; 1925} 1926 1927static PyObject * 1928long_xor(PyObject *v, PyObject *w) 1929{ 1930 PyLongObject *a, *b; 1931 PyObject *c; 1932 CONVERT_BINOP(v, w, &a, &b); 1933 c = long_bitwise(a, '^', b); 1934 Py_DECREF(a); 1935 Py_DECREF(b); 1936 return c; 1937} 1938 1939static PyObject * 1940long_or(PyObject *v, PyObject *w) 1941{ 1942 PyLongObject *a, *b; 1943 PyObject *c; 1944 CONVERT_BINOP(v, w, &a, &b); 1945 c = long_bitwise(a, '|', b); 1946 Py_DECREF(a); 1947 Py_DECREF(b); 1948 return c; 1949} 1950 1951static int 1952long_coerce(PyObject **pv, PyObject **pw) 1953{ 1954 if (PyInt_Check(*pw)) { 1955 *pw = PyLong_FromLong(PyInt_AS_LONG(*pw)); 1956 Py_INCREF(*pv); 1957 return 0; 1958 } 1959 return 1; /* Can't do it */ 1960} 1961 1962static PyObject * 1963long_int(PyObject *v) 1964{ 1965 long x; 1966 x = PyLong_AsLong(v); 1967 if (PyErr_Occurred()) 1968 return NULL; 1969 return PyInt_FromLong(x); 1970} 1971 1972static PyObject * 1973long_long(PyObject *v) 1974{ 1975 Py_INCREF(v); 1976 return v; 1977} 1978 1979static PyObject * 1980long_float(PyObject *v) 1981{ 1982 double result; 1983 PyFPE_START_PROTECT("long_float", return 0) 1984 result = PyLong_AsDouble(v); 1985 PyFPE_END_PROTECT(result) 1986 return PyFloat_FromDouble(result); 1987} 1988 1989static PyObject * 1990long_oct(PyObject *v) 1991{ 1992 return long_format(v, 8, 1); 1993} 1994 1995static PyObject * 1996long_hex(PyObject *v) 1997{ 1998 return long_format(v, 16, 1); 1999} 2000 2001static PyNumberMethods long_as_number = { 2002 (binaryfunc) long_add, /*nb_add*/ 2003 (binaryfunc) long_sub, /*nb_subtract*/ 2004 (binaryfunc) long_mul, /*nb_multiply*/ 2005 (binaryfunc) long_div, /*nb_divide*/ 2006 (binaryfunc) long_mod, /*nb_remainder*/ 2007 (binaryfunc) long_divmod, /*nb_divmod*/ 2008 (ternaryfunc) long_pow, /*nb_power*/ 2009 (unaryfunc) long_neg, /*nb_negative*/ 2010 (unaryfunc) long_pos, /*tp_positive*/ 2011 (unaryfunc) long_abs, /*tp_absolute*/ 2012 (inquiry) long_nonzero, /*tp_nonzero*/ 2013 (unaryfunc) long_invert, /*nb_invert*/ 2014 (binaryfunc) long_lshift, /*nb_lshift*/ 2015 (binaryfunc) long_rshift, /*nb_rshift*/ 2016 (binaryfunc) long_and, /*nb_and*/ 2017 (binaryfunc) long_xor, /*nb_xor*/ 2018 (binaryfunc) long_or, /*nb_or*/ 2019 (coercion) long_coerce, /*nb_coerce*/ 2020 (unaryfunc) long_int, /*nb_int*/ 2021 (unaryfunc) long_long, /*nb_long*/ 2022 (unaryfunc) long_float, /*nb_float*/ 2023 (unaryfunc) long_oct, /*nb_oct*/ 2024 (unaryfunc) long_hex, /*nb_hex*/ 2025 0, /*nb_inplace_add*/ 2026 0, /*nb_inplace_subtract*/ 2027 0, /*nb_inplace_multiply*/ 2028 0, /*nb_inplace_divide*/ 2029 0, /*nb_inplace_remainder*/ 2030 0, /*nb_inplace_power*/ 2031 0, /*nb_inplace_lshift*/ 2032 0, /*nb_inplace_rshift*/ 2033 0, /*nb_inplace_and*/ 2034 0, /*nb_inplace_xor*/ 2035 0, /*nb_inplace_or*/ 2036}; 2037 2038PyTypeObject PyLong_Type = { 2039 PyObject_HEAD_INIT(&PyType_Type) 2040 0, 2041 "long int", 2042 sizeof(PyLongObject) - sizeof(digit), 2043 sizeof(digit), 2044 (destructor)long_dealloc, /*tp_dealloc*/ 2045 0, /*tp_print*/ 2046 0, /*tp_getattr*/ 2047 0, /*tp_setattr*/ 2048 (cmpfunc)long_compare, /*tp_compare*/ 2049 (reprfunc)long_repr, /*tp_repr*/ 2050 &long_as_number, /*tp_as_number*/ 2051 0, /*tp_as_sequence*/ 2052 0, /*tp_as_mapping*/ 2053 (hashfunc)long_hash, /*tp_hash*/ 2054 0, /*tp_call*/ 2055 (reprfunc)long_str, /*tp_str*/ 2056 0, /*tp_getattro*/ 2057 0, /*tp_setattro*/ 2058 0, /*tp_as_buffer*/ 2059 Py_TPFLAGS_CHECKTYPES /*tp_flags*/ 2060}; 2061