home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Objects / intobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  18.0 KB  |  867 lines

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