home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Objects / intobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  15.5 KB  |  787 lines  |  [TEXT/CWIE]

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