home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Objects / abstract.c next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  42.3 KB  |  2,039 lines

  1. /* Abstract Object Interface (many thanks to Jim Fulton) */
  2.  
  3. #include "Python.h"
  4. #include <ctype.h>
  5.  
  6. /* Shorthands to return certain errors */
  7.  
  8. static PyObject *
  9. type_error(const char *msg)
  10. {
  11.     PyErr_SetString(PyExc_TypeError, msg);
  12.     return NULL;
  13. }
  14.  
  15. static PyObject *
  16. null_error(void)
  17. {
  18.     if (!PyErr_Occurred())
  19.         PyErr_SetString(PyExc_SystemError,
  20.                 "null argument to internal routine");
  21.     return NULL;
  22. }
  23.  
  24. /* Operations on any object */
  25.  
  26. int
  27. PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)
  28. {
  29.     int r;
  30.  
  31.     if (o1 == NULL || o2 == NULL) {
  32.         null_error();
  33.         return -1;
  34.     }
  35.     r = PyObject_Compare(o1, o2);
  36.     if (PyErr_Occurred())
  37.         return -1;
  38.     *result = r;
  39.     return 0;
  40. }
  41.  
  42. PyObject *
  43. PyObject_Type(PyObject *o)
  44. {
  45.     PyObject *v;
  46.  
  47.     if (o == NULL)
  48.         return null_error();
  49.     v = (PyObject *)o->ob_type;
  50.     Py_INCREF(v);
  51.     return v;
  52. }
  53.  
  54. int
  55. PyObject_Size(PyObject *o)
  56. {
  57.     PySequenceMethods *m;
  58.  
  59.     if (o == NULL) {
  60.         null_error();
  61.         return -1;
  62.     }
  63.  
  64.     m = o->ob_type->tp_as_sequence;
  65.     if (m && m->sq_length)
  66.         return m->sq_length(o);
  67.  
  68.     return PyMapping_Size(o);
  69. }
  70.  
  71. #undef PyObject_Length
  72. int
  73. PyObject_Length(PyObject *o)
  74. {
  75.     return PyObject_Size(o);
  76. }
  77. #define PyObject_Length PyObject_Size
  78.  
  79. PyObject *
  80. PyObject_GetItem(PyObject *o, PyObject *key)
  81. {
  82.     PyMappingMethods *m;
  83.  
  84.     if (o == NULL || key == NULL)
  85.         return null_error();
  86.  
  87.     m = o->ob_type->tp_as_mapping;
  88.     if (m && m->mp_subscript)
  89.         return m->mp_subscript(o, key);
  90.  
  91.     if (o->ob_type->tp_as_sequence) {
  92.         if (PyInt_Check(key))
  93.             return PySequence_GetItem(o, PyInt_AsLong(key));
  94.         else if (PyLong_Check(key)) {
  95.             long key_value = PyLong_AsLong(key);
  96.             if (key_value == -1 && PyErr_Occurred())
  97.                 return NULL;
  98.             return PySequence_GetItem(o, key_value);
  99.         }
  100.         return type_error("sequence index must be integer");
  101.     }
  102.  
  103.     return type_error("unsubscriptable object");
  104. }
  105.  
  106. int
  107. PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
  108. {
  109.     PyMappingMethods *m;
  110.  
  111.     if (o == NULL || key == NULL || value == NULL) {
  112.         null_error();
  113.         return -1;
  114.     }
  115.     m = o->ob_type->tp_as_mapping;
  116.     if (m && m->mp_ass_subscript)
  117.         return m->mp_ass_subscript(o, key, value);
  118.  
  119.     if (o->ob_type->tp_as_sequence) {
  120.         if (PyInt_Check(key))
  121.             return PySequence_SetItem(o, PyInt_AsLong(key), value);
  122.         else if (PyLong_Check(key)) {
  123.             long key_value = PyLong_AsLong(key);
  124.             if (key_value == -1 && PyErr_Occurred())
  125.                 return -1;
  126.             return PySequence_SetItem(o, key_value, value);
  127.         }
  128.         type_error("sequence index must be integer");
  129.         return -1;
  130.     }
  131.  
  132.     type_error("object does not support item assignment");
  133.     return -1;
  134. }
  135.  
  136. int
  137. PyObject_DelItem(PyObject *o, PyObject *key)
  138. {
  139.     PyMappingMethods *m;
  140.  
  141.     if (o == NULL || key == NULL) {
  142.         null_error();
  143.         return -1;
  144.     }
  145.     m = o->ob_type->tp_as_mapping;
  146.     if (m && m->mp_ass_subscript)
  147.         return m->mp_ass_subscript(o, key, (PyObject*)NULL);
  148.  
  149.     if (o->ob_type->tp_as_sequence) {
  150.         if (PyInt_Check(key))
  151.             return PySequence_DelItem(o, PyInt_AsLong(key));
  152.         else if (PyLong_Check(key)) {
  153.             long key_value = PyLong_AsLong(key);
  154.             if (key_value == -1 && PyErr_Occurred())
  155.                 return -1;
  156.             return PySequence_DelItem(o, key_value);
  157.         }
  158.         type_error("sequence index must be integer");
  159.         return -1;
  160.     }
  161.  
  162.     type_error("object does not support item deletion");
  163.     return -1;
  164. }
  165.  
  166. int PyObject_AsCharBuffer(PyObject *obj,
  167.               const char **buffer,
  168.               int *buffer_len)
  169. {
  170.     PyBufferProcs *pb;
  171.     const char *pp;
  172.     int len;
  173.  
  174.     if (obj == NULL || buffer == NULL || buffer_len == NULL) {
  175.         null_error();
  176.         return -1;
  177.     }
  178.     pb = obj->ob_type->tp_as_buffer;
  179.     if ( pb == NULL ||
  180.          pb->bf_getcharbuffer == NULL ||
  181.          pb->bf_getsegcount == NULL ) {
  182.         PyErr_SetString(PyExc_TypeError,
  183.                 "expected a character buffer object");
  184.         goto onError;
  185.     }
  186.     if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
  187.         PyErr_SetString(PyExc_TypeError,
  188.                 "expected a single-segment buffer object");
  189.         goto onError;
  190.     }
  191.     len = (*pb->bf_getcharbuffer)(obj,0,&pp);
  192.     if (len < 0)
  193.         goto onError;
  194.     *buffer = pp;
  195.     *buffer_len = len;
  196.     return 0;
  197.  
  198.  onError:
  199.     return -1;
  200. }
  201.  
  202. int PyObject_AsReadBuffer(PyObject *obj,
  203.               const void **buffer,
  204.               int *buffer_len)
  205. {
  206.     PyBufferProcs *pb;
  207.     void *pp;
  208.     int len;
  209.  
  210.     if (obj == NULL || buffer == NULL || buffer_len == NULL) {
  211.         null_error();
  212.         return -1;
  213.     }
  214.     pb = obj->ob_type->tp_as_buffer;
  215.     if ( pb == NULL ||
  216.          pb->bf_getreadbuffer == NULL ||
  217.          pb->bf_getsegcount == NULL ) {
  218.         PyErr_SetString(PyExc_TypeError,
  219.                 "expected a readable buffer object");
  220.         goto onError;
  221.     }
  222.     if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
  223.         PyErr_SetString(PyExc_TypeError,
  224.                 "expected a single-segment buffer object");
  225.         goto onError;
  226.     }
  227.     len = (*pb->bf_getreadbuffer)(obj,0,&pp);
  228.     if (len < 0)
  229.         goto onError;
  230.     *buffer = pp;
  231.     *buffer_len = len;
  232.     return 0;
  233.  
  234.  onError:
  235.     return -1;
  236. }
  237.  
  238. int PyObject_AsWriteBuffer(PyObject *obj,
  239.                void **buffer,
  240.                int *buffer_len)
  241. {
  242.     PyBufferProcs *pb;
  243.     void*pp;
  244.     int len;
  245.  
  246.     if (obj == NULL || buffer == NULL || buffer_len == NULL) {
  247.         null_error();
  248.         return -1;
  249.     }
  250.     pb = obj->ob_type->tp_as_buffer;
  251.     if ( pb == NULL ||
  252.          pb->bf_getwritebuffer == NULL ||
  253.          pb->bf_getsegcount == NULL ) {
  254.         PyErr_SetString(PyExc_TypeError,
  255.                 "expected a writeable buffer object");
  256.         goto onError;
  257.     }
  258.     if ( (*pb->bf_getsegcount)(obj,NULL) != 1 ) {
  259.         PyErr_SetString(PyExc_TypeError,
  260.                 "expected a single-segment buffer object");
  261.         goto onError;
  262.     }
  263.     len = (*pb->bf_getwritebuffer)(obj,0,&pp);
  264.     if (len < 0)
  265.         goto onError;
  266.     *buffer = pp;
  267.     *buffer_len = len;
  268.     return 0;
  269.  
  270.  onError:
  271.     return -1;
  272. }
  273.  
  274. /* Operations on numbers */
  275.  
  276. int
  277. PyNumber_Check(PyObject *o)
  278. {
  279.     return o && o->ob_type->tp_as_number;
  280. }
  281.  
  282. /* Binary operators */
  283.  
  284. #define BINOP(v, w, opname, ropname, thisfunc) \
  285.     if (PyInstance_Check(v) || PyInstance_Check(w)) \
  286.         return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
  287.  
  288. PyObject *
  289. PyNumber_Or(PyObject *v, PyObject *w)
  290. {
  291.     BINOP(v, w, "__or__", "__ror__", PyNumber_Or);
  292.     if (v->ob_type->tp_as_number != NULL) {
  293.         PyObject *x = NULL;
  294.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  295.         if (PyNumber_Coerce(&v, &w) != 0)
  296.             return NULL;
  297.         if (v->ob_type->tp_as_number != NULL &&
  298.             (f = v->ob_type->tp_as_number->nb_or) != NULL)
  299.             x = (*f)(v, w);
  300.         Py_DECREF(v);
  301.         Py_DECREF(w);
  302.         if (f != NULL)
  303.             return x;
  304.     }
  305.     return type_error("bad operand type(s) for |");
  306. }
  307.  
  308. PyObject *
  309. PyNumber_Xor(PyObject *v, PyObject *w)
  310. {
  311.     BINOP(v, w, "__xor__", "__rxor__", PyNumber_Xor);
  312.     if (v->ob_type->tp_as_number != NULL) {
  313.         PyObject *x = NULL;
  314.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  315.         if (PyNumber_Coerce(&v, &w) != 0)
  316.             return NULL;
  317.         if (v->ob_type->tp_as_number != NULL &&
  318.             (f = v->ob_type->tp_as_number->nb_xor) != NULL)
  319.             x = (*f)(v, w);
  320.         Py_DECREF(v);
  321.         Py_DECREF(w);
  322.         if (f != NULL)
  323.             return x;
  324.     }
  325.     return type_error("bad operand type(s) for ^");
  326. }
  327.  
  328. PyObject *
  329. PyNumber_And(PyObject *v, PyObject *w)
  330. {
  331.     BINOP(v, w, "__and__", "__rand__", PyNumber_And);
  332.     if (v->ob_type->tp_as_number != NULL) {
  333.         PyObject *x = NULL;
  334.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  335.         if (PyNumber_Coerce(&v, &w) != 0)
  336.             return NULL;
  337.         if (v->ob_type->tp_as_number != NULL &&
  338.             (f = v->ob_type->tp_as_number->nb_and) != NULL)
  339.             x = (*f)(v, w);
  340.         Py_DECREF(v);
  341.         Py_DECREF(w);
  342.         if (f != NULL)
  343.             return x;
  344.     }
  345.     return type_error("bad operand type(s) for &");
  346. }
  347.  
  348. PyObject *
  349. PyNumber_Lshift(PyObject *v, PyObject *w)
  350. {
  351.     BINOP(v, w, "__lshift__", "__rlshift__", PyNumber_Lshift);
  352.     if (v->ob_type->tp_as_number != NULL) {
  353.         PyObject *x = NULL;
  354.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  355.         if (PyNumber_Coerce(&v, &w) != 0)
  356.             return NULL;
  357.         if (v->ob_type->tp_as_number != NULL &&
  358.             (f = v->ob_type->tp_as_number->nb_lshift) != NULL)
  359.             x = (*f)(v, w);
  360.         Py_DECREF(v);
  361.         Py_DECREF(w);
  362.         if (f != NULL)
  363.             return x;
  364.     }
  365.     return type_error("bad operand type(s) for <<");
  366. }
  367.  
  368. PyObject *
  369. PyNumber_Rshift(PyObject *v, PyObject *w)
  370. {
  371.     BINOP(v, w, "__rshift__", "__rrshift__", PyNumber_Rshift);
  372.     if (v->ob_type->tp_as_number != NULL) {
  373.         PyObject *x = NULL;
  374.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  375.         if (PyNumber_Coerce(&v, &w) != 0)
  376.             return NULL;
  377.         if (v->ob_type->tp_as_number != NULL &&
  378.             (f = v->ob_type->tp_as_number->nb_rshift) != NULL)
  379.             x = (*f)(v, w);
  380.         Py_DECREF(v);
  381.         Py_DECREF(w);
  382.         if (f != NULL)
  383.             return x;
  384.     }
  385.     return type_error("bad operand type(s) for >>");
  386. }
  387.  
  388. PyObject *
  389. PyNumber_Add(PyObject *v, PyObject *w)
  390. {
  391.     PySequenceMethods *m;
  392.  
  393.     BINOP(v, w, "__add__", "__radd__", PyNumber_Add);
  394.     m = v->ob_type->tp_as_sequence;
  395.     if (m && m->sq_concat)
  396.         return (*m->sq_concat)(v, w);
  397.     else if (v->ob_type->tp_as_number != NULL) {
  398.         PyObject *x = NULL;
  399.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  400.         if (PyNumber_Coerce(&v, &w) != 0)
  401.             return NULL;
  402.         if (v->ob_type->tp_as_number != NULL &&
  403.             (f = v->ob_type->tp_as_number->nb_add) != NULL)
  404.             x = (*f)(v, w);
  405.         Py_DECREF(v);
  406.         Py_DECREF(w);
  407.         if (f != NULL)
  408.             return x;
  409.     }
  410.     return type_error("bad operand type(s) for +");
  411. }
  412.  
  413. PyObject *
  414. PyNumber_Subtract(PyObject *v, PyObject *w)
  415. {
  416.     BINOP(v, w, "__sub__", "__rsub__", PyNumber_Subtract);
  417.     if (v->ob_type->tp_as_number != NULL) {
  418.         PyObject *x = NULL;
  419.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  420.         if (PyNumber_Coerce(&v, &w) != 0)
  421.             return NULL;
  422.         if (v->ob_type->tp_as_number != NULL &&
  423.             (f = v->ob_type->tp_as_number->nb_subtract) != NULL)
  424.             x = (*f)(v, w);
  425.         Py_DECREF(v);
  426.         Py_DECREF(w);
  427.         if (f != NULL)
  428.             return x;
  429.     }
  430.     return type_error("bad operand type(s) for -");
  431. }
  432.  
  433. PyObject *
  434. PyNumber_Multiply(PyObject *v, PyObject *w)
  435. {
  436.     PyTypeObject *tp = v->ob_type;
  437.     PySequenceMethods *m;
  438.  
  439.     BINOP(v, w, "__mul__", "__rmul__", PyNumber_Multiply);
  440.     if (tp->tp_as_number != NULL &&
  441.         w->ob_type->tp_as_sequence != NULL) {
  442.         /* number*sequence -- swap v and w */
  443.         PyObject *tmp = v;
  444.         v = w;
  445.         w = tmp;
  446.         tp = v->ob_type;
  447.     }
  448.     if (tp->tp_as_number != NULL) {
  449.         PyObject *x = NULL;
  450.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  451.         if (PyNumber_Coerce(&v, &w) != 0)
  452.             return NULL;
  453.         if (v->ob_type->tp_as_number != NULL &&
  454.             (f = v->ob_type->tp_as_number->nb_multiply) != NULL)
  455.             x = (*f)(v, w);
  456.         Py_DECREF(v);
  457.         Py_DECREF(w);
  458.         if (f != NULL)
  459.             return x;
  460.     }
  461.     m = tp->tp_as_sequence;
  462.     if (m && m->sq_repeat) {
  463.         long mul_value;
  464.  
  465.         if (PyInt_Check(w)) {
  466.             mul_value = PyInt_AsLong(w);
  467.         }
  468.         else if (PyLong_Check(w)) {
  469.             mul_value = PyLong_AsLong(w);
  470.             if (mul_value == -1 && PyErr_Occurred())
  471.                                 return NULL; 
  472.         }
  473.         else {
  474.             return type_error(
  475.                 "can't multiply sequence with non-int");
  476.         }
  477.         return (*m->sq_repeat)(v, (int)mul_value);
  478.     }
  479.     return type_error("bad operand type(s) for *");
  480. }
  481.  
  482. PyObject *
  483. PyNumber_Divide(PyObject *v, PyObject *w)
  484. {
  485.     BINOP(v, w, "__div__", "__rdiv__", PyNumber_Divide);
  486.     if (v->ob_type->tp_as_number != NULL) {
  487.         PyObject *x = NULL;
  488.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  489.         if (PyNumber_Coerce(&v, &w) != 0)
  490.             return NULL;
  491.         if (v->ob_type->tp_as_number != NULL &&
  492.             (f = v->ob_type->tp_as_number->nb_divide) != NULL)
  493.             x = (*f)(v, w);
  494.         Py_DECREF(v);
  495.         Py_DECREF(w);
  496.         if (f != NULL)
  497.             return x;
  498.     }
  499.     return type_error("bad operand type(s) for /");
  500. }
  501.  
  502. PyObject *
  503. PyNumber_Remainder(PyObject *v, PyObject *w)
  504. {
  505.     if (PyString_Check(v))
  506.         return PyString_Format(v, w);
  507.     else if (PyUnicode_Check(v))
  508.         return PyUnicode_Format(v, w);
  509.     BINOP(v, w, "__mod__", "__rmod__", PyNumber_Remainder);
  510.     if (v->ob_type->tp_as_number != NULL) {
  511.         PyObject *x = NULL;
  512.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  513.         if (PyNumber_Coerce(&v, &w) != 0)
  514.             return NULL;
  515.         if (v->ob_type->tp_as_number != NULL &&
  516.             (f = v->ob_type->tp_as_number->nb_remainder) != NULL)
  517.             x = (*f)(v, w);
  518.         Py_DECREF(v);
  519.         Py_DECREF(w);
  520.         if (f != NULL)
  521.             return x;
  522.     }
  523.     return type_error("bad operand type(s) for %");
  524. }
  525.  
  526. PyObject *
  527. PyNumber_Divmod(PyObject *v, PyObject *w)
  528. {
  529.     BINOP(v, w, "__divmod__", "__rdivmod__", PyNumber_Divmod);
  530.     if (v->ob_type->tp_as_number != NULL) {
  531.         PyObject *x = NULL;
  532.         PyObject * (*f)(PyObject *, PyObject *) = NULL;
  533.         if (PyNumber_Coerce(&v, &w) != 0)
  534.             return NULL;
  535.         if (v->ob_type->tp_as_number != NULL &&
  536.             (f = v->ob_type->tp_as_number->nb_divmod) != NULL)
  537.             x = (*f)(v, w);
  538.         Py_DECREF(v);
  539.         Py_DECREF(w);
  540.         if (f != NULL)
  541.             return x;
  542.     }
  543.     return type_error("bad operand type(s) for divmod()");
  544. }
  545.  
  546. /* Power (binary or ternary) */
  547.  
  548. static PyObject *
  549. do_pow(PyObject *v, PyObject *w)
  550. {
  551.     PyObject *res;
  552.     PyObject * (*f)(PyObject *, PyObject *, PyObject *);
  553.     BINOP(v, w, "__pow__", "__rpow__", do_pow);
  554.     if (v->ob_type->tp_as_number == NULL ||
  555.         w->ob_type->tp_as_number == NULL) {
  556.         PyErr_SetString(PyExc_TypeError,
  557.                 "pow(x, y) requires numeric arguments");
  558.         return NULL;
  559.     }
  560.     if (PyNumber_Coerce(&v, &w) != 0)
  561.         return NULL;
  562.     if (v->ob_type->tp_as_number != NULL &&
  563.         (f = v->ob_type->tp_as_number->nb_power) != NULL)
  564.         res = (*f)(v, w, Py_None);
  565.     else
  566.         res = type_error("pow(x, y) not defined for these operands");
  567.     Py_DECREF(v);
  568.     Py_DECREF(w);
  569.     return res;
  570. }
  571.  
  572. PyObject *
  573. PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
  574. {
  575.     PyObject *res;
  576.     PyObject *v1, *z1, *w2, *z2;
  577.     PyObject * (*f)(PyObject *, PyObject *, PyObject *);
  578.  
  579.     if (z == Py_None)
  580.         return do_pow(v, w);
  581.     /* XXX The ternary version doesn't do class instance coercions */
  582.     if (PyInstance_Check(v))
  583.         return v->ob_type->tp_as_number->nb_power(v, w, z);
  584.     if (v->ob_type->tp_as_number == NULL ||
  585.         z->ob_type->tp_as_number == NULL ||
  586.         w->ob_type->tp_as_number == NULL) {
  587.         return type_error("pow(x, y, z) requires numeric arguments");
  588.     }
  589.     if (PyNumber_Coerce(&v, &w) != 0)
  590.         return NULL;
  591.     res = NULL;
  592.     v1 = v;
  593.     z1 = z;
  594.     if (PyNumber_Coerce(&v1, &z1) != 0)
  595.         goto error2;
  596.     w2 = w;
  597.     z2 = z1;
  598.      if (PyNumber_Coerce(&w2, &z2) != 0)
  599.         goto error1;
  600.     if (v->ob_type->tp_as_number != NULL &&
  601.         (f = v1->ob_type->tp_as_number->nb_power) != NULL)
  602.         res = (*f)(v1, w2, z2);
  603.     else
  604.         res = type_error(
  605.             "pow(x, y, z) not defined for these operands");
  606.     Py_DECREF(w2);
  607.     Py_DECREF(z2);
  608.   error1:
  609.     Py_DECREF(v1);
  610.     Py_DECREF(z1);
  611.   error2:
  612.     Py_DECREF(v);
  613.     Py_DECREF(w);
  614.     return res;
  615. }
  616.  
  617. /* Binary in-place operators */
  618.  
  619. /* The in-place operators are defined to fall back to the 'normal',
  620.    non in-place operations, if the in-place methods are not in place, and to
  621.    take class instances into account. This is how it is supposed to work:
  622.  
  623.    - If the left-hand-side object (the first argument) is an
  624.      instance object, try to let PyInstance_HalfBinOp() handle it.  Pass the
  625.      non in-place variant of the function as callback, because it will only
  626.      be used if the left-hand object is changed by coercion.
  627.  
  628.    - Otherwise, if the left hand object is not an instance object, it has
  629.      the appropriate struct members, and they are filled, call the
  630.      appropriate function and return the result. No coercion is done on the
  631.      arguments; the left-hand object is the one the operation is performed
  632.      on, and it's up to the function to deal with the right-hand object.
  633.      
  634.    - Otherwise, in-place modification is not supported. Handle it exactly as
  635.      a non in-place operation of the same kind:
  636.  
  637.      - If either object is an instance, let PyInstance_DoBinOp() handle it.
  638.      
  639.      - Otherwise, both arguments are C types. If the left-hand object has
  640.        the appropriate struct members filled, coerce, call the
  641.        appropriate function, and return the result.
  642.   
  643.      - Otherwise, we are out of options: raise a type error specific to
  644.        augmented assignment.
  645.  
  646.    */
  647.  
  648. #define HASINPLACE(t) PyType_HasFeature((t)->ob_type, Py_TPFLAGS_HAVE_INPLACEOPS)
  649.  
  650. PyObject *
  651. PyNumber_InPlaceOr(PyObject *v, PyObject *w)
  652. {
  653.     PyObject * (*f)(PyObject *, PyObject *) = NULL;
  654.     PyObject *x = NULL;
  655.  
  656.     if (PyInstance_Check(v)) {
  657.         if (PyInstance_HalfBinOp(v, w, "__ior__", &x,
  658.                      PyNumber_Or, 0) <= 0)
  659.             return x;
  660.     }
  661.     else if (v->ob_type->tp_as_number != NULL && HASINPLACE(v) &&
  662.          (f = v->ob_type->tp_as_number->nb_inplace_or) != NULL)
  663.         return (*f)(v, w);
  664.  
  665.     BINOP(v, w, "__or__", "__ror__", PyNumber_Or);
  666.  
  667.     if (v->ob_type->tp_as_number != NULL) {
  668.         if (PyNumber_Coerce(&v, &w) != 0)
  669.             return NULL;
  670.         if (v->ob_type->tp_as_number != NULL &&
  671.             (f = v->ob_type->tp_as_number->nb_or) != NULL)
  672.             x = (*f)(v, w);
  673.         Py_DECREF(v);
  674.         Py_DECREF(w);
  675.         if (f != NULL)
  676.             return x;
  677.     }
  678.  
  679.     return type_error("bad operand type(s) for |=");
  680. }
  681.  
  682. PyObject *
  683. PyNumber_InPlaceXor(PyObject *v, PyObject *w)
  684. {
  685.     PyObject * (*f)(PyObject *, PyObject *) = NULL;
  686.     PyObject *x = NULL;
  687.  
  688.     if (PyInstance_Check(v)) {
  689.         if (PyInstance_HalfBinOp(v, w, "__ixor__", &x,
  690.                      PyNumber_Xor, 0) <= 0)
  691.             return x;
  692.     }
  693.     else if (v->ob_type->tp_as_number != NULL && HASINPLACE(v) &&
  694.          (f = v->ob_type->tp_as_number->nb_inplace_xor) != NULL)
  695.         return (*f)(v, w);
  696.  
  697.     BINOP(v, w, "__xor__", "__rxor__", PyNumber_Xor);
  698.  
  699.     if (v->ob_type->tp_as_number != NULL) {
  700.         if (PyNumber_Coerce(&v, &w) != 0)
  701.             return NULL;
  702.         if (v->ob_type->tp_as_number != NULL &&
  703.             (f = v->ob_type->tp_as_number->nb_xor) != NULL)
  704.             x = (*f)(v, w);
  705.         Py_DECREF(v);
  706.         Py_DECREF(w);
  707.         if (f != NULL)
  708.             return x;
  709.     }
  710.  
  711.     return type_error("bad operand type(s) for ^=");
  712. }
  713.  
  714. PyObject *
  715. PyNumber_InPlaceAnd(PyObject *v, PyObject *w)
  716. {
  717.     PyObject * (*f)(PyObject *, PyObject *) = NULL;
  718.     PyObject *x = NULL;
  719.  
  720.     if (PyInstance_Check(v)) {
  721.         if (PyInstance_HalfBinOp(v, w, "__iand__", &x,
  722.                      PyNumber_And, 0) <= 0)
  723.             return x;
  724.     }
  725.     else if (v->ob_type->tp_as_number != NULL && HASINPLACE(v) &&
  726.          (f = v->ob_type->tp_as_number->nb_inplace_and) != NULL)
  727.         return (*f)(v, w);
  728.  
  729.     BINOP(v, w, "__and__", "__rand__", PyNumber_And);
  730.  
  731.     if (v->ob_type->tp_as_number != NULL) {
  732.         if (PyNumber_Coerce(&v, &w) != 0)
  733.             return NULL;
  734.         if (v->ob_type->tp_as_number != NULL &&
  735.             (f = v->ob_type->tp_as_number->nb_and) != NULL)
  736.             x = (*f)(v, w);
  737.         Py_DECREF(v);
  738.         Py_DECREF(w);
  739.         if (f != NULL)
  740.             return x;
  741.     }
  742.  
  743.     return type_error("bad operand type(s) for &=");
  744. }
  745.  
  746. PyObject *
  747. PyNumber_InPlaceLshift(PyObject *v, PyObject *w)
  748. {
  749.     PyObject * (*f)(PyObject *, PyObject *) = NULL;
  750.     PyObject *x = NULL;
  751.  
  752.     if (PyInstance_Check(v)) {
  753.         if (PyInstance_HalfBinOp(v, w, "__ilshift__", &x,
  754.                     PyNumber_Lshift, 0) <= 0)
  755.             return x;
  756.     }
  757.     else if (v->ob_type->tp_as_number != NULL && HASINPLACE(v) &&
  758.          (f = v->ob_type->tp_as_number->nb_inplace_lshift) != NULL)
  759.         return (*f)(v, w);
  760.  
  761.     BINOP(v, w, "__lshift__", "__rlshift__", PyNumber_Lshift);
  762.  
  763.     if (v->ob_type->tp_as_number != NULL) {
  764.         if (PyNumber_Coerce(&v, &w) != 0)
  765.             return NULL;
  766.         if (v->ob_type->tp_as_number != NULL &&
  767.             (f = v->ob_type->tp_as_number->nb_lshift) != NULL)
  768.             x = (*f)(v, w);
  769.         Py_DECREF(v);
  770.         Py_DECREF(w);
  771.         if (f != NULL)
  772.             return x;
  773.     }
  774.  
  775.     return type_error("bad operand type(s) for <<=");
  776. }
  777.  
  778. PyObject *
  779. PyNumber_InPlaceRshift(PyObject *v, PyObject *w)
  780. {
  781.     PyObject * (*f)(PyObject *, PyObject *) = NULL;
  782.     PyObject *x = NULL;
  783.  
  784.     if (PyInstance_Check(v)) {
  785.         if (PyInstance_HalfBinOp(v, w, "__irshift__", &x,
  786.                     PyNumber_Rshift, 0) <= 0)
  787.             return x;
  788.     }
  789.     else if (v->ob_type->tp_as_number != NULL && HASINPLACE(v) &&
  790.          (f = v->ob_type->tp_as_number->nb_inplace_rshift) != NULL)
  791.         return (*f)(v, w);
  792.  
  793.     BINOP(v, w, "__rshift__", "__rrshift__", PyNumber_Rshift);
  794.  
  795.     if (v->ob_type->tp_as_number != NULL) {
  796.         if (PyNumber_Coerce(&v, &w) != 0)
  797.             return NULL;
  798.         if (v->ob_type->tp_as_number != NULL &&
  799.             (f = v->ob_type->tp_as_number->nb_rshift) != NULL)
  800.             x = (*f)(v, w);
  801.         Py_DECREF(v);
  802.         Py_DECREF(w);
  803.         if (f != NULL)
  804.             return x;
  805.     }
  806.  
  807.     return type_error("bad operand type(s) for >>=");
  808. }
  809.  
  810. PyObject *
  811. PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
  812. {
  813.     PyObject * (*f)(PyObject *, PyObject *) = NULL;
  814.     PyObject *x = NULL;
  815.  
  816.     if (PyInstance_Check(v)) {
  817.         if (PyInstance_HalfBinOp(v, w, "__iadd__", &x,
  818.                      PyNumber_Add, 0) <= 0)
  819.             return x;
  820.     }
  821.     else if (HASINPLACE(v)) {
  822.         if (v->ob_type->tp_as_sequence != NULL)
  823.             f = v->ob_type->tp_as_sequence->sq_inplace_concat;
  824.         if (f == NULL && v->ob_type->tp_as_number != NULL)
  825.             f = v->ob_type->tp_as_number->nb_inplace_add;
  826.         if (f != NULL)
  827.             return (*f)(v, w);
  828.     }
  829.  
  830.     BINOP(v, w, "__add__", "__radd__", PyNumber_Add);
  831.  
  832.     if (v->ob_type->tp_as_sequence != NULL) {
  833.         f = v->ob_type->tp_as_sequence->sq_concat;
  834.         if (f != NULL)
  835.             return (*f)(v, w);
  836.     }
  837.     if (v->ob_type->tp_as_number != NULL) {
  838.         if (PyNumber_Coerce(&v, &w) != 0)
  839.             return NULL;
  840.         if (v->ob_type->tp_as_number != NULL) {
  841.             f = v->ob_type->tp_as_number->nb_add;
  842.             if (f != NULL)
  843.                 x = (*f)(v, w);
  844.         }
  845.         Py_DECREF(v);
  846.         Py_DECREF(w);
  847.         if (f != NULL)
  848.             return x;
  849.     }
  850.  
  851.     return type_error("bad operand type(s) for +=");
  852. }
  853.  
  854. PyObject *
  855. PyNumber_InPlaceSubtract(PyObject *v, PyObject *w)
  856. {
  857.     PyObject * (*f)(PyObject *, PyObject *) = NULL;
  858.     PyObject *x = NULL;
  859.  
  860.     if (PyInstance_Check(v)) {
  861.         if (PyInstance_HalfBinOp(v, w, "__isub__", &x,
  862.                     PyNumber_Subtract, 0) <= 0)
  863.             return x;
  864.     }
  865.     else if (v->ob_type->tp_as_number != NULL && HASINPLACE(v) &&
  866.          (f = v->ob_type->tp_as_number->nb_inplace_subtract) != NULL)
  867.         return (*f)(v, w);
  868.  
  869.     BINOP(v, w, "__sub__", "__rsub__", PyNumber_Subtract);
  870.  
  871.     if (v->ob_type->tp_as_number != NULL) {
  872.         if (PyNumber_Coerce(&v, &w) != 0)
  873.             return NULL;
  874.         if (v->ob_type->tp_as_number != NULL &&
  875.             (f = v->ob_type->tp_as_number->nb_subtract) != NULL)
  876.             x = (*f)(v, w);
  877.         Py_DECREF(v);
  878.         Py_DECREF(w);
  879.         if (f != NULL)
  880.             return x;
  881.     }
  882.  
  883.     return type_error("bad operand type(s) for -=");
  884. }
  885.  
  886. PyObject *
  887. PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
  888. {
  889.     PyObject * (*f)(PyObject *, PyObject *) = NULL;
  890.     PyObject * (*g)(PyObject *, int) = NULL;
  891.     PyObject *x = NULL;
  892.  
  893.     if (PyInstance_Check(v)) {
  894.         if (PyInstance_HalfBinOp(v, w, "__imul__", &x,
  895.                     PyNumber_Multiply, 0) <= 0)
  896.             return x;
  897.     }
  898.     else if (v->ob_type->tp_as_number != NULL && HASINPLACE(v) &&
  899.          (f = v->ob_type->tp_as_number->nb_inplace_multiply) != NULL)
  900.         return (*f)(v, w);
  901.     else if (v->ob_type->tp_as_sequence != NULL && HASINPLACE(v) &&
  902.          (g = v->ob_type->tp_as_sequence->sq_inplace_repeat) != NULL) {
  903.         long mul_value;
  904.  
  905.         if (PyInt_Check(w)) {
  906.             mul_value = PyInt_AsLong(w);
  907.         }
  908.         else if (PyLong_Check(w)) {
  909.             mul_value = PyLong_AsLong(w);
  910.             if (mul_value == -1 && PyErr_Occurred())
  911.                                 return NULL; 
  912.         }
  913.         else {
  914.             return type_error(
  915.                 "can't multiply sequence with non-int");
  916.         }
  917.         return (*g)(v, (int)mul_value);
  918.     }
  919.  
  920.     BINOP(v, w, "__mul__", "__rmul__", PyNumber_Multiply);
  921.  
  922.     if (v->ob_type->tp_as_number != NULL) {
  923.         if (PyNumber_Coerce(&v, &w) != 0)
  924.             return NULL;
  925.         if (v->ob_type->tp_as_number != NULL &&
  926.             (f = v->ob_type->tp_as_number->nb_multiply) != NULL)
  927.             x = (*f)(v, w);
  928.         Py_DECREF(v);
  929.         Py_DECREF(w);
  930.         if (f != NULL)
  931.             return x;
  932.     }
  933.     else if (v->ob_type->tp_as_sequence != NULL &&
  934.          (g = v->ob_type->tp_as_sequence->sq_repeat) != NULL) {
  935.         long mul_value;
  936.  
  937.         if (PyInt_Check(w)) {
  938.             mul_value = PyInt_AsLong(w);
  939.         }
  940.         else if (PyLong_Check(w)) {
  941.             mul_value = PyLong_AsLong(w);
  942.             if (mul_value == -1 && PyErr_Occurred())
  943.                                 return NULL; 
  944.         }
  945.         else {
  946.             return type_error(
  947.                 "can't multiply sequence with non-int");
  948.         }
  949.         return (*g)(v, (int)mul_value);
  950.     }
  951.     return type_error("bad operand type(s) for *=");
  952. }
  953.  
  954. PyObject *
  955. PyNumber_InPlaceDivide(PyObject *v, PyObject *w)
  956. {
  957.     PyObject * (*f)(PyObject *, PyObject *) = NULL;
  958.     PyObject *x = NULL;
  959.  
  960.     if (PyInstance_Check(v)) {
  961.         if (PyInstance_HalfBinOp(v, w, "__idiv__", &x,
  962.                     PyNumber_Divide, 0) <= 0)
  963.             return x;
  964.     }
  965.     else if (v->ob_type->tp_as_number != NULL && HASINPLACE(v) &&
  966.          (f = v->ob_type->tp_as_number->nb_inplace_divide) != NULL)
  967.         return (*f)(v, w);
  968.  
  969.     BINOP(v, w, "__div__", "__rdiv__", PyNumber_Divide);
  970.  
  971.     if (v->ob_type->tp_as_number != NULL) {
  972.         if (PyNumber_Coerce(&v, &w) != 0)
  973.             return NULL;
  974.         if (v->ob_type->tp_as_number != NULL &&
  975.             (f = v->ob_type->tp_as_number->nb_divide) != NULL)
  976.             x = (*f)(v, w);
  977.         Py_DECREF(v);
  978.         Py_DECREF(w);
  979.         if (f != NULL)
  980.             return x;
  981.     }
  982.  
  983.     return type_error("bad operand type(s) for /=");
  984. }
  985.  
  986. PyObject *
  987. PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
  988. {
  989.     PyObject * (*f)(PyObject *, PyObject *) = NULL;
  990.     PyObject *x = NULL;
  991.  
  992.     if (PyInstance_Check(v)) {
  993.         if (PyInstance_HalfBinOp(v, w, "__imod__", &x,
  994.                     PyNumber_Remainder, 0) <= 0)
  995.             return x;
  996.     }
  997.     else if (v->ob_type->tp_as_number != NULL && HASINPLACE(v) &&
  998.          (f = v->ob_type->tp_as_number->nb_inplace_remainder) != NULL)
  999.         return (*f)(v, w);
  1000.  
  1001.     if (PyString_Check(v))
  1002.         return PyString_Format(v, w);
  1003.     else if (PyUnicode_Check(v))
  1004.         return PyUnicode_Format(v, w);
  1005.  
  1006.     BINOP(v, w, "__mod__", "__rmod__", PyNumber_Remainder);
  1007.  
  1008.     if (v->ob_type->tp_as_number != NULL) {
  1009.         if (PyNumber_Coerce(&v, &w) != 0)
  1010.             return NULL;
  1011.         if ((f = v->ob_type->tp_as_number->nb_remainder) != NULL)
  1012.             x = (*f)(v, w);
  1013.         Py_DECREF(v);
  1014.         Py_DECREF(w);
  1015.         if (f != NULL)
  1016.             return x;
  1017.     }
  1018.  
  1019.     return type_error("bad operand type(s) for %=");
  1020. }
  1021.  
  1022.  
  1023. /* In-place Power (binary or ternary, for API consistency) */
  1024.  
  1025. static PyObject *
  1026. do_inplace_pow(PyObject *v, PyObject *w)
  1027. {
  1028.     PyObject * (*f)(PyObject *, PyObject *, PyObject *) = NULL;
  1029.     PyObject *x = NULL;
  1030.  
  1031.     if (PyInstance_Check(v)) {
  1032.         if (PyInstance_HalfBinOp(v, w, "__ipow__", &x, do_pow, 0) <= 0)
  1033.             return x;
  1034.     }
  1035.     else if (v->ob_type->tp_as_number != NULL && HASINPLACE(v) &&
  1036.          (f = v->ob_type->tp_as_number->nb_inplace_power) != NULL)
  1037.         return (*f)(v, w, Py_None);
  1038.  
  1039.     BINOP(v, w, "__pow__", "__rpow__", do_pow);
  1040.  
  1041.     if (v->ob_type->tp_as_number == NULL ||
  1042.         w->ob_type->tp_as_number == NULL) {
  1043.         return type_error("bad operand type(s) for **=");
  1044.     }
  1045.     if (PyNumber_Coerce(&v, &w) != 0)
  1046.         return NULL;
  1047.     if ((f = v->ob_type->tp_as_number->nb_power) != NULL)
  1048.         x = (*f)(v, w, Py_None);
  1049.     else
  1050.         x = type_error("bad operand type(s) for **=");
  1051.     Py_DECREF(v);
  1052.     Py_DECREF(w);
  1053.     return x;
  1054. }
  1055.  
  1056. PyObject *
  1057. PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
  1058. {
  1059.     PyObject *res;
  1060.     PyObject *v1, *z1, *w2, *z2, *oldv;
  1061.     PyObject * (*f)(PyObject *, PyObject *, PyObject *);
  1062.  
  1063.     if (z == Py_None)
  1064.         return do_inplace_pow(v, w);
  1065.     /* XXX The ternary version doesn't do class instance coercions */
  1066.     if (PyInstance_Check(v))
  1067.         return v->ob_type->tp_as_number->nb_inplace_power(v, w, z);
  1068.     if (v->ob_type->tp_as_number == NULL ||
  1069.         z->ob_type->tp_as_number == NULL ||
  1070.         w->ob_type->tp_as_number == NULL) {
  1071.         return type_error(
  1072.             "(inplace) pow(x, y, z) requires numeric arguments");
  1073.     }
  1074.     oldv = v;
  1075.     Py_INCREF(oldv);
  1076.     res = NULL;
  1077.     if (PyNumber_Coerce(&v, &w) != 0)
  1078.         goto error3;
  1079.     v1 = v;
  1080.     z1 = z;
  1081.     if (PyNumber_Coerce(&v1, &z1) != 0)
  1082.         goto error2;
  1083.     w2 = w;
  1084.     z2 = z1;
  1085.      if (PyNumber_Coerce(&w2, &z2) != 0)
  1086.         goto error1;
  1087.     if (oldv == v1 && HASINPLACE(v1) &&
  1088.         v->ob_type->tp_as_number != NULL &&
  1089.         (f = v1->ob_type->tp_as_number->nb_inplace_power) != NULL)
  1090.         res = (*f)(v1, w2, z2);
  1091.     else if (v1->ob_type->tp_as_number != NULL &&
  1092.          (f = v1->ob_type->tp_as_number->nb_power) != NULL)
  1093.         res = (*f)(v1, w2, z2);
  1094.     else
  1095.         res = type_error(
  1096.              "(inplace) pow(x, y, z) not defined for these operands");
  1097.     Py_DECREF(w2);
  1098.     Py_DECREF(z2);
  1099.   error1:
  1100.     Py_DECREF(v1);
  1101.     Py_DECREF(z1);
  1102.   error2:
  1103.     Py_DECREF(v);
  1104.     Py_DECREF(w);
  1105.   error3:
  1106.     Py_DECREF(oldv);
  1107.     return res;
  1108. }
  1109.  
  1110.  
  1111. /* Unary operators and functions */
  1112.  
  1113. PyObject *
  1114. PyNumber_Negative(PyObject *o)
  1115. {
  1116.     PyNumberMethods *m;
  1117.  
  1118.     if (o == NULL)
  1119.         return null_error();
  1120.     m = o->ob_type->tp_as_number;
  1121.     if (m && m->nb_negative)
  1122.         return (*m->nb_negative)(o);
  1123.  
  1124.     return type_error("bad operand type for unary -");
  1125. }
  1126.  
  1127. PyObject *
  1128. PyNumber_Positive(PyObject *o)
  1129. {
  1130.     PyNumberMethods *m;
  1131.  
  1132.     if (o == NULL)
  1133.         return null_error();
  1134.     m = o->ob_type->tp_as_number;
  1135.     if (m && m->nb_positive)
  1136.         return (*m->nb_positive)(o);
  1137.  
  1138.     return type_error("bad operand type for unary +");
  1139. }
  1140.  
  1141. PyObject *
  1142. PyNumber_Invert(PyObject *o)
  1143. {
  1144.     PyNumberMethods *m;
  1145.  
  1146.     if (o == NULL)
  1147.         return null_error();
  1148.     m = o->ob_type->tp_as_number;
  1149.     if (m && m->nb_invert)
  1150.         return (*m->nb_invert)(o);
  1151.  
  1152.     return type_error("bad operand type for unary ~");
  1153. }
  1154.  
  1155. PyObject *
  1156. PyNumber_Absolute(PyObject *o)
  1157. {
  1158.     PyNumberMethods *m;
  1159.  
  1160.     if (o == NULL)
  1161.         return null_error();
  1162.     m = o->ob_type->tp_as_number;
  1163.     if (m && m->nb_absolute)
  1164.         return m->nb_absolute(o);
  1165.  
  1166.     return type_error("bad operand type for abs()");
  1167. }
  1168.  
  1169. /* Add a check for embedded NULL-bytes in the argument. */
  1170. static PyObject *
  1171. int_from_string(const char *s, int len)
  1172. {
  1173.     char *end;
  1174.     PyObject *x;
  1175.  
  1176.     x = PyInt_FromString((char*)s, &end, 10);
  1177.     if (x == NULL)
  1178.         return NULL;
  1179.     if (end != s + len) {
  1180.         PyErr_SetString(PyExc_ValueError,
  1181.                 "null byte in argument for int()");
  1182.         Py_DECREF(x);
  1183.         return NULL;
  1184.     }
  1185.     return x;
  1186. }
  1187.  
  1188. PyObject *
  1189. PyNumber_Int(PyObject *o)
  1190. {
  1191.     PyNumberMethods *m;
  1192.     const char *buffer;
  1193.     int buffer_len;
  1194.  
  1195.     if (o == NULL)
  1196.         return null_error();
  1197.     if (PyInt_Check(o)) {
  1198.         Py_INCREF(o);
  1199.         return o;
  1200.     }
  1201.     if (PyString_Check(o))
  1202.         return int_from_string(PyString_AS_STRING(o), 
  1203.                        PyString_GET_SIZE(o));
  1204.     if (PyUnicode_Check(o))
  1205.         return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o),
  1206.                      PyUnicode_GET_SIZE(o),
  1207.                      10);
  1208.     m = o->ob_type->tp_as_number;
  1209.     if (m && m->nb_int)
  1210.         return m->nb_int(o);
  1211.     if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
  1212.         return int_from_string((char*)buffer, buffer_len);
  1213.  
  1214.     return type_error("object can't be converted to int");
  1215. }
  1216.  
  1217. /* Add a check for embedded NULL-bytes in the argument. */
  1218. static PyObject *
  1219. long_from_string(const char *s, int len)
  1220. {
  1221.     char *end;
  1222.     PyObject *x;
  1223.  
  1224.     x = PyLong_FromString((char*)s, &end, 10);
  1225.     if (x == NULL)
  1226.         return NULL;
  1227.     if (end != s + len) {
  1228.         PyErr_SetString(PyExc_ValueError,
  1229.                 "null byte in argument for long()");
  1230.         Py_DECREF(x);
  1231.         return NULL;
  1232.     }
  1233.     return x;
  1234. }
  1235.  
  1236. PyObject *
  1237. PyNumber_Long(PyObject *o)
  1238. {
  1239.     PyNumberMethods *m;
  1240.     const char *buffer;
  1241.     int buffer_len;
  1242.  
  1243.     if (o == NULL)
  1244.         return null_error();
  1245.     if (PyLong_Check(o)) {
  1246.         Py_INCREF(o);
  1247.         return o;
  1248.     }
  1249.     if (PyString_Check(o))
  1250.         /* need to do extra error checking that PyLong_FromString() 
  1251.          * doesn't do.  In particular long('9.5') must raise an
  1252.          * exception, not truncate the float.
  1253.          */
  1254.         return long_from_string(PyString_AS_STRING(o),
  1255.                     PyString_GET_SIZE(o));
  1256.     if (PyUnicode_Check(o))
  1257.         /* The above check is done in PyLong_FromUnicode(). */
  1258.         return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
  1259.                       PyUnicode_GET_SIZE(o),
  1260.                       10);
  1261.     m = o->ob_type->tp_as_number;
  1262.     if (m && m->nb_long)
  1263.         return m->nb_long(o);
  1264.     if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
  1265.         return long_from_string(buffer, buffer_len);
  1266.  
  1267.     return type_error("object can't be converted to long");
  1268. }
  1269.  
  1270. PyObject *
  1271. PyNumber_Float(PyObject *o)
  1272. {
  1273.     PyNumberMethods *m;
  1274.  
  1275.     if (o == NULL)
  1276.         return null_error();
  1277.     if (PyFloat_Check(o)) {
  1278.         Py_INCREF(o);
  1279.         return o;
  1280.     }
  1281.     if (!PyString_Check(o)) {
  1282.         m = o->ob_type->tp_as_number;
  1283.         if (m && m->nb_float)
  1284.             return m->nb_float(o);
  1285.     }
  1286.     return PyFloat_FromString(o, NULL);
  1287. }
  1288.  
  1289. /* Operations on sequences */
  1290.  
  1291. int
  1292. PySequence_Check(PyObject *s)
  1293. {
  1294.     return s != NULL && s->ob_type->tp_as_sequence;
  1295. }
  1296.  
  1297. int
  1298. PySequence_Size(PyObject *s)
  1299. {
  1300.     PySequenceMethods *m;
  1301.  
  1302.     if (s == NULL) {
  1303.         null_error();
  1304.         return -1;
  1305.     }
  1306.  
  1307.     m = s->ob_type->tp_as_sequence;
  1308.     if (m && m->sq_length)
  1309.         return m->sq_length(s);
  1310.  
  1311.     type_error("len() of unsized object");
  1312.     return -1;
  1313. }
  1314.  
  1315. #undef PySequence_Length
  1316. int
  1317. PySequence_Length(PyObject *s)
  1318. {
  1319.     return PySequence_Size(s);
  1320. }
  1321. #define PySequence_Length PySequence_Size
  1322.  
  1323. PyObject *
  1324. PySequence_Concat(PyObject *s, PyObject *o)
  1325. {
  1326.     PySequenceMethods *m;
  1327.  
  1328.     if (s == NULL || o == NULL)
  1329.         return null_error();
  1330.  
  1331.     m = s->ob_type->tp_as_sequence;
  1332.     if (m && m->sq_concat)
  1333.         return m->sq_concat(s, o);
  1334.  
  1335.     return type_error("object can't be concatenated");
  1336. }
  1337.  
  1338. PyObject *
  1339. PySequence_Repeat(PyObject *o, int count)
  1340. {
  1341.     PySequenceMethods *m;
  1342.  
  1343.     if (o == NULL)
  1344.         return null_error();
  1345.  
  1346.     m = o->ob_type->tp_as_sequence;
  1347.     if (m && m->sq_repeat)
  1348.         return m->sq_repeat(o, count);
  1349.  
  1350.     return type_error("object can't be repeated");
  1351. }
  1352.  
  1353. PyObject *
  1354. PySequence_InPlaceConcat(PyObject *s, PyObject *o)
  1355. {
  1356.     PySequenceMethods *m;
  1357.  
  1358.     if (s == NULL || o == NULL)
  1359.         return null_error();
  1360.  
  1361.     m = s->ob_type->tp_as_sequence;
  1362.     if (m && HASINPLACE(s) && m->sq_inplace_concat)
  1363.         return m->sq_inplace_concat(s, o);
  1364.     if (m && m->sq_concat)
  1365.         return m->sq_concat(s, o);
  1366.  
  1367.     return type_error("object can't be concatenated");
  1368. }
  1369.  
  1370. PyObject *
  1371. PySequence_InPlaceRepeat(PyObject *o, int count)
  1372. {
  1373.     PySequenceMethods *m;
  1374.  
  1375.     if (o == NULL)
  1376.         return null_error();
  1377.  
  1378.     m = o->ob_type->tp_as_sequence;
  1379.     if (m && HASINPLACE(o) && m->sq_inplace_repeat)
  1380.         return m->sq_inplace_repeat(o, count);
  1381.     if (m && m->sq_repeat)
  1382.         return m->sq_repeat(o, count);
  1383.  
  1384.     return type_error("object can't be repeated");
  1385. }
  1386.  
  1387. PyObject *
  1388. PySequence_GetItem(PyObject *s, int i)
  1389. {
  1390.     PySequenceMethods *m;
  1391.  
  1392.     if (s == NULL)
  1393.         return null_error();
  1394.  
  1395.     m = s->ob_type->tp_as_sequence;
  1396.     if (m && m->sq_item) {
  1397.         if (i < 0) {
  1398.             if (m->sq_length) {
  1399.                 int l = (*m->sq_length)(s);
  1400.                 if (l < 0)
  1401.                     return NULL;
  1402.                 i += l;
  1403.             }
  1404.         }
  1405.         return m->sq_item(s, i);
  1406.     }
  1407.  
  1408.     return type_error("unindexable object");
  1409. }
  1410.  
  1411. static PyObject *
  1412. sliceobj_from_intint(int i, int j)
  1413. {
  1414.     PyObject *start, *end, *slice;
  1415.     start = PyInt_FromLong((long)i);
  1416.     if (!start)
  1417.         return NULL;
  1418.     end = PyInt_FromLong((long)j);
  1419.     if (!end) {
  1420.         Py_DECREF(start);
  1421.         return NULL;
  1422.     }
  1423.     slice = PySlice_New(start, end, NULL);
  1424.     Py_DECREF(start);
  1425.     Py_DECREF(end);
  1426.     return slice;
  1427. }
  1428.  
  1429. PyObject *
  1430. PySequence_GetSlice(PyObject *s, int i1, int i2)
  1431. {
  1432.     PySequenceMethods *m;
  1433.     PyMappingMethods *mp;
  1434.  
  1435.     if (!s) return null_error();
  1436.  
  1437.     m = s->ob_type->tp_as_sequence;
  1438.     if (m && m->sq_slice) {
  1439.         if (i1 < 0 || i2 < 0) {
  1440.             if (m->sq_length) {
  1441.                 int l = (*m->sq_length)(s);
  1442.                 if (l < 0)
  1443.                     return NULL;
  1444.                 if (i1 < 0)
  1445.                     i1 += l;
  1446.                 if (i2 < 0)
  1447.                     i2 += l;
  1448.             }
  1449.         }
  1450.         return m->sq_slice(s, i1, i2);
  1451.     } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) {
  1452.         PyObject *res;
  1453.         PyObject *slice = sliceobj_from_intint(i1, i2);
  1454.         if (!slice)
  1455.             return NULL;
  1456.         res = mp->mp_subscript(s, slice);
  1457.         Py_DECREF(slice);
  1458.         return res;
  1459.     }
  1460.  
  1461.     return type_error("unsliceable object");
  1462. }
  1463.  
  1464. int
  1465. PySequence_SetItem(PyObject *s, int i, PyObject *o)
  1466. {
  1467.     PySequenceMethods *m;
  1468.  
  1469.     if (s == NULL) {
  1470.         null_error();
  1471.         return -1;
  1472.     }
  1473.  
  1474.     m = s->ob_type->tp_as_sequence;
  1475.     if (m && m->sq_ass_item) {
  1476.         if (i < 0) {
  1477.             if (m->sq_length) {
  1478.                 int l = (*m->sq_length)(s);
  1479.                 if (l < 0)
  1480.                     return -1;
  1481.                 i += l;
  1482.             }
  1483.         }
  1484.         return m->sq_ass_item(s, i, o);
  1485.     }
  1486.  
  1487.     type_error("object doesn't support item assignment");
  1488.     return -1;
  1489. }
  1490.  
  1491. int
  1492. PySequence_DelItem(PyObject *s, int i)
  1493. {
  1494.     PySequenceMethods *m;
  1495.  
  1496.     if (s == NULL) {
  1497.         null_error();
  1498.         return -1;
  1499.     }
  1500.  
  1501.     m = s->ob_type->tp_as_sequence;
  1502.     if (m && m->sq_ass_item) {
  1503.         if (i < 0) {
  1504.             if (m->sq_length) {
  1505.                 int l = (*m->sq_length)(s);
  1506.                 if (l < 0)
  1507.                     return -1;
  1508.                 i += l;
  1509.             }
  1510.         }
  1511.         return m->sq_ass_item(s, i, (PyObject *)NULL);
  1512.     }
  1513.  
  1514.     type_error("object doesn't support item deletion");
  1515.     return -1;
  1516. }
  1517.  
  1518. int
  1519. PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o)
  1520. {
  1521.     PySequenceMethods *m;
  1522.     PyMappingMethods *mp;
  1523.  
  1524.     if (s == NULL) {
  1525.         null_error();
  1526.         return -1;
  1527.     }
  1528.  
  1529.     m = s->ob_type->tp_as_sequence;
  1530.     if (m && m->sq_ass_slice) {
  1531.         if (i1 < 0 || i2 < 0) {
  1532.             if (m->sq_length) {
  1533.                 int l = (*m->sq_length)(s);
  1534.                 if (l < 0)
  1535.                     return -1;
  1536.                 if (i1 < 0)
  1537.                     i1 += l;
  1538.                 if (i2 < 0)
  1539.                     i2 += l;
  1540.             }
  1541.         }
  1542.         return m->sq_ass_slice(s, i1, i2, o);
  1543.     } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) {
  1544.         int res;
  1545.         PyObject *slice = sliceobj_from_intint(i1, i2);
  1546.         if (!slice)
  1547.             return -1;
  1548.         res = mp->mp_ass_subscript(s, slice, o);
  1549.         Py_DECREF(slice);
  1550.         return res;
  1551.     }
  1552.  
  1553.     type_error("object doesn't support slice assignment");
  1554.     return -1;
  1555. }
  1556.  
  1557. int
  1558. PySequence_DelSlice(PyObject *s, int i1, int i2)
  1559. {
  1560.     PySequenceMethods *m;
  1561.  
  1562.     if (s == NULL) {
  1563.         null_error();
  1564.         return -1;
  1565.     }
  1566.  
  1567.     m = s->ob_type->tp_as_sequence;
  1568.     if (m && m->sq_ass_slice) {
  1569.         if (i1 < 0 || i2 < 0) {
  1570.             if (m->sq_length) {
  1571.                 int l = (*m->sq_length)(s);
  1572.                 if (l < 0)
  1573.                     return -1;
  1574.                 if (i1 < 0)
  1575.                     i1 += l;
  1576.                 if (i2 < 0)
  1577.                     i2 += l;
  1578.             }
  1579.         }
  1580.         return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
  1581.     }
  1582.     type_error("object doesn't support slice deletion");
  1583.     return -1;
  1584. }
  1585.  
  1586. PyObject *
  1587. PySequence_Tuple(PyObject *v)
  1588. {
  1589.     PySequenceMethods *m;
  1590.  
  1591.     if (v == NULL)
  1592.         return null_error();
  1593.  
  1594.     if (PyTuple_Check(v)) {
  1595.         Py_INCREF(v);
  1596.         return v;
  1597.     }
  1598.  
  1599.     if (PyList_Check(v))
  1600.         return PyList_AsTuple(v);
  1601.  
  1602.     /* There used to be code for strings here, but tuplifying strings is
  1603.        not a common activity, so I nuked it.  Down with code bloat! */
  1604.  
  1605.     /* Generic sequence object */
  1606.     m = v->ob_type->tp_as_sequence;
  1607.     if (m && m->sq_item) {
  1608.         int i;
  1609.         PyObject *t;
  1610.         int n = PySequence_Size(v);
  1611.         if (n < 0)
  1612.             return NULL;
  1613.         t = PyTuple_New(n);
  1614.         if (t == NULL)
  1615.             return NULL;
  1616.         for (i = 0; ; i++) {
  1617.             PyObject *item = (*m->sq_item)(v, i);
  1618.             if (item == NULL) {
  1619.                 if (PyErr_ExceptionMatches(PyExc_IndexError))
  1620.                     PyErr_Clear();
  1621.                 else {
  1622.                     Py_DECREF(t);
  1623.                     t = NULL;
  1624.                 }
  1625.                 break;
  1626.             }
  1627.             if (i >= n) {
  1628.                 if (n < 500)
  1629.                     n += 10;
  1630.                 else
  1631.                     n += 100;
  1632.                 if (_PyTuple_Resize(&t, n, 0) != 0)
  1633.                     break;
  1634.             }
  1635.             PyTuple_SET_ITEM(t, i, item);
  1636.         }
  1637.         if (i < n && t != NULL)
  1638.             _PyTuple_Resize(&t, i, 0);
  1639.         return t;
  1640.     }
  1641.  
  1642.     /* None of the above */
  1643.     return type_error("tuple() argument must be a sequence");
  1644. }
  1645.  
  1646. PyObject *
  1647. PySequence_List(PyObject *v)
  1648. {
  1649.     PySequenceMethods *m;
  1650.  
  1651.     if (v == NULL)
  1652.         return null_error();
  1653.  
  1654.     if (PyList_Check(v))
  1655.         return PyList_GetSlice(v, 0, PyList_GET_SIZE(v));
  1656.  
  1657.     m = v->ob_type->tp_as_sequence;
  1658.     if (m && m->sq_item) {
  1659.         int i;
  1660.         PyObject *l;
  1661.         int n = PySequence_Size(v);
  1662.         if (n < 0)
  1663.             return NULL;
  1664.         l = PyList_New(n);
  1665.         if (l == NULL)
  1666.             return NULL;
  1667.         for (i = 0; ; i++) {
  1668.             PyObject *item = (*m->sq_item)(v, i);
  1669.             if (item == NULL) {
  1670.                 if (PyErr_ExceptionMatches(PyExc_IndexError))
  1671.                     PyErr_Clear();
  1672.                 else {
  1673.                     Py_DECREF(l);
  1674.                     l = NULL;
  1675.                 }
  1676.                 break;
  1677.             }
  1678.             if (i < n)
  1679.                 PyList_SET_ITEM(l, i, item);
  1680.             else if (PyList_Append(l, item) < 0) {
  1681.                 Py_DECREF(l);
  1682.                 l = NULL;
  1683.                 break;
  1684.             }
  1685.         }
  1686.         if (i < n && l != NULL) {
  1687.             if (PyList_SetSlice(l, i, n, (PyObject *)NULL) != 0) {
  1688.                 Py_DECREF(l);
  1689.                 l = NULL;
  1690.             }
  1691.         }
  1692.         return l;
  1693.     }
  1694.     return type_error("list() argument must be a sequence");
  1695. }
  1696.  
  1697. PyObject *
  1698. PySequence_Fast(PyObject *v, const char *m)
  1699. {
  1700.     if (v == NULL)
  1701.         return null_error();
  1702.  
  1703.     if (PyList_Check(v) || PyTuple_Check(v)) {
  1704.         Py_INCREF(v);
  1705.         return v;
  1706.     }
  1707.  
  1708.     v = PySequence_Tuple(v);
  1709.     if (v == NULL && PyErr_ExceptionMatches(PyExc_TypeError))
  1710.         return type_error(m);
  1711.  
  1712.     return v;
  1713. }
  1714.  
  1715. int
  1716. PySequence_Count(PyObject *s, PyObject *o)
  1717. {
  1718.     int l, i, n, cmp, err;
  1719.     PyObject *item;
  1720.  
  1721.     if (s == NULL || o == NULL) {
  1722.         null_error();
  1723.         return -1;
  1724.     }
  1725.     
  1726.     l = PySequence_Size(s);
  1727.     if (l < 0)
  1728.         return -1;
  1729.  
  1730.     n = 0;
  1731.     for (i = 0; i < l; i++) {
  1732.         item = PySequence_GetItem(s, i);
  1733.         if (item == NULL)
  1734.             return -1;
  1735.         err = PyObject_Cmp(item, o, &cmp);
  1736.         Py_DECREF(item);
  1737.         if (err < 0)
  1738.             return err;
  1739.         if (cmp == 0)
  1740.             n++;
  1741.     }
  1742.     return n;
  1743. }
  1744.  
  1745. int
  1746. PySequence_Contains(PyObject *w, PyObject *v) /* v in w */
  1747. {
  1748.     int i, cmp;
  1749.     PyObject *x;
  1750.     PySequenceMethods *sq;
  1751.  
  1752.     if(PyType_HasFeature(w->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
  1753.         sq = w->ob_type->tp_as_sequence;
  1754.             if(sq != NULL && sq->sq_contains != NULL)
  1755.             return (*sq->sq_contains)(w, v);
  1756.     }
  1757.     
  1758.     /* If there is no better way to check whether an item is is contained,
  1759.        do it the hard way */
  1760.     sq = w->ob_type->tp_as_sequence;
  1761.     if (sq == NULL || sq->sq_item == NULL) {
  1762.         PyErr_SetString(PyExc_TypeError,
  1763.             "'in' or 'not in' needs sequence right argument");
  1764.         return -1;
  1765.     }
  1766.  
  1767.     for (i = 0; ; i++) {
  1768.         x = (*sq->sq_item)(w, i);
  1769.         if (x == NULL) {
  1770.             if (PyErr_ExceptionMatches(PyExc_IndexError)) {
  1771.                 PyErr_Clear();
  1772.                 break;
  1773.             }
  1774.             return -1;
  1775.         }
  1776.         cmp = PyObject_Compare(v, x);
  1777.         Py_XDECREF(x);
  1778.         if (cmp == 0)
  1779.             return 1;
  1780.         if (PyErr_Occurred())
  1781.             return -1;
  1782.     }
  1783.  
  1784.     return 0;
  1785. }
  1786.  
  1787. /* Backwards compatibility */
  1788. #undef PySequence_In
  1789. int
  1790. PySequence_In(PyObject *w, PyObject *v)
  1791. {
  1792.     return PySequence_Contains(w, v);
  1793. }
  1794.  
  1795. int
  1796. PySequence_Index(PyObject *s, PyObject *o)
  1797. {
  1798.     int l, i, cmp, err;
  1799.     PyObject *item;
  1800.  
  1801.     if (s == NULL || o == NULL) {
  1802.         null_error();
  1803.         return -1;
  1804.     }
  1805.     
  1806.     l = PySequence_Size(s);
  1807.     if (l < 0)
  1808.         return -1;
  1809.  
  1810.     for (i = 0; i < l; i++) {
  1811.         item = PySequence_GetItem(s, i);
  1812.         if (item == NULL)
  1813.             return -1;
  1814.         err = PyObject_Cmp(item, o, &cmp);
  1815.         Py_DECREF(item);
  1816.         if (err < 0)
  1817.             return err;
  1818.         if (cmp == 0)
  1819.             return i;
  1820.     }
  1821.  
  1822.     PyErr_SetString(PyExc_ValueError, "sequence.index(x): x not in list");
  1823.     return -1;
  1824. }
  1825.  
  1826. /* Operations on mappings */
  1827.  
  1828. int
  1829. PyMapping_Check(PyObject *o)
  1830. {
  1831.     return o && o->ob_type->tp_as_mapping;
  1832. }
  1833.  
  1834. int
  1835. PyMapping_Size(PyObject *o)
  1836. {
  1837.     PyMappingMethods *m;
  1838.  
  1839.     if (o == NULL) {
  1840.         null_error();
  1841.         return -1;
  1842.     }
  1843.  
  1844.     m = o->ob_type->tp_as_mapping;
  1845.     if (m && m->mp_length)
  1846.         return m->mp_length(o);
  1847.  
  1848.     type_error("len() of unsized object");
  1849.     return -1;
  1850. }
  1851.  
  1852. #undef PyMapping_Length
  1853. int
  1854. PyMapping_Length(PyObject *o)
  1855. {
  1856.     return PyMapping_Size(o);
  1857. }
  1858. #define PyMapping_Length PyMapping_Size
  1859.  
  1860. PyObject *
  1861. PyMapping_GetItemString(PyObject *o, char *key)
  1862. {
  1863.     PyObject *okey, *r;
  1864.  
  1865.     if (key == NULL)
  1866.         return null_error();
  1867.  
  1868.     okey = PyString_FromString(key);
  1869.     if (okey == NULL)
  1870.         return NULL;
  1871.     r = PyObject_GetItem(o, okey);
  1872.     Py_DECREF(okey);
  1873.     return r;
  1874. }
  1875.  
  1876. int
  1877. PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
  1878. {
  1879.     PyObject *okey;
  1880.     int r;
  1881.  
  1882.     if (key == NULL) {
  1883.         null_error();
  1884.         return -1;
  1885.     }
  1886.  
  1887.     okey = PyString_FromString(key);
  1888.     if (okey == NULL)
  1889.         return -1;
  1890.     r = PyObject_SetItem(o, okey, value);
  1891.     Py_DECREF(okey);
  1892.     return r;
  1893. }
  1894.  
  1895. int
  1896. PyMapping_HasKeyString(PyObject *o, char *key)
  1897. {
  1898.     PyObject *v;
  1899.  
  1900.     v = PyMapping_GetItemString(o, key);
  1901.     if (v) {
  1902.         Py_DECREF(v);
  1903.         return 1;
  1904.     }
  1905.     PyErr_Clear();
  1906.     return 0;
  1907. }
  1908.  
  1909. int
  1910. PyMapping_HasKey(PyObject *o, PyObject *key)
  1911. {
  1912.     PyObject *v;
  1913.  
  1914.     v = PyObject_GetItem(o, key);
  1915.     if (v) {
  1916.         Py_DECREF(v);
  1917.         return 1;
  1918.     }
  1919.     PyErr_Clear();
  1920.     return 0;
  1921. }
  1922.  
  1923. /* Operations on callable objects */
  1924.  
  1925. /* XXX PyCallable_Check() is in object.c */
  1926.  
  1927. PyObject *
  1928. PyObject_CallObject(PyObject *o, PyObject *a)
  1929. {
  1930.     PyObject *r;
  1931.     PyObject *args = a;
  1932.  
  1933.     if (args == NULL) {
  1934.         args = PyTuple_New(0);
  1935.         if (args == NULL)
  1936.             return NULL;
  1937.     }
  1938.  
  1939.     r = PyEval_CallObject(o, args);
  1940.  
  1941.     if (args != a) {
  1942.         Py_DECREF(args);
  1943.     }
  1944.  
  1945.     return r;
  1946. }
  1947.  
  1948. PyObject *
  1949. PyObject_CallFunction(PyObject *callable, char *format, ...)
  1950. {
  1951.     va_list va;
  1952.     PyObject *args, *retval;
  1953.     va_start(va, format);
  1954.  
  1955.     if (callable == NULL) {
  1956.         va_end(va);
  1957.         return null_error();
  1958.     }
  1959.  
  1960.     if (format)
  1961.         args = Py_VaBuildValue(format, va);
  1962.     else
  1963.         args = PyTuple_New(0);
  1964.  
  1965.     va_end(va);
  1966.     
  1967.     if (args == NULL)
  1968.         return NULL;
  1969.  
  1970.     if (!PyTuple_Check(args)) {
  1971.         PyObject *a;
  1972.  
  1973.         a = PyTuple_New(1);
  1974.         if (a == NULL)
  1975.             return NULL;
  1976.         if (PyTuple_SetItem(a, 0, args) < 0)
  1977.             return NULL;
  1978.         args = a;
  1979.     }
  1980.     retval = PyObject_CallObject(callable, args);
  1981.  
  1982.     Py_DECREF(args);
  1983.  
  1984.     return retval;
  1985. }
  1986.  
  1987. PyObject *
  1988. PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
  1989. {
  1990.     va_list va;
  1991.     PyObject *args, *func = 0, *retval;
  1992.     va_start(va, format);
  1993.  
  1994.     if (o == NULL || name == NULL) {
  1995.         va_end(va);
  1996.         return null_error();
  1997.     }
  1998.  
  1999.     func = PyObject_GetAttrString(o, name);
  2000.     if (func == NULL) {
  2001.         va_end(va);
  2002.         PyErr_SetString(PyExc_AttributeError, name);
  2003.         return 0;
  2004.     }
  2005.  
  2006.     if (!PyCallable_Check(func)) {
  2007.         va_end(va);
  2008.         return type_error("call of non-callable attribute");
  2009.     }
  2010.  
  2011.     if (format && *format)
  2012.         args = Py_VaBuildValue(format, va);
  2013.     else
  2014.         args = PyTuple_New(0);
  2015.  
  2016.     va_end(va);
  2017.  
  2018.     if (!args)
  2019.         return NULL;
  2020.  
  2021.     if (!PyTuple_Check(args)) {
  2022.         PyObject *a;
  2023.  
  2024.         a = PyTuple_New(1);
  2025.         if (a == NULL)
  2026.             return NULL;
  2027.         if (PyTuple_SetItem(a, 0, args) < 0)
  2028.             return NULL;
  2029.         args = a;
  2030.     }
  2031.  
  2032.     retval = PyObject_CallObject(func, args);
  2033.  
  2034.     Py_DECREF(args);
  2035.     Py_DECREF(func);
  2036.  
  2037.     return retval;
  2038. }
  2039.