home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Objects / intobject.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  20KB  |  902 lines

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