home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / Python / Source / Objects / abstract.c next >
Encoding:
C/C++ Source or Header  |  1999-04-25  |  27.9 KB  |  1,490 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. /* Abstract Object Interface (many thanks to Jim Fulton) */
  33.  
  34. #include "Python.h"
  35. #include <ctype.h>
  36. #include "protos/abstract.h"
  37.  
  38. /* Shorthands to return certain errors */
  39.  
  40. static PyObject *
  41. type_error(msg)
  42.     char *msg;
  43. {
  44.     PyErr_SetString(PyExc_TypeError, msg);
  45.     return NULL;
  46. }
  47.  
  48. static PyObject *
  49. null_error()
  50. {
  51.     if (!PyErr_Occurred())
  52.         PyErr_SetString(PyExc_SystemError,
  53.                 "null argument to internal routine");
  54.     return NULL;
  55. }
  56.  
  57. /* Copied with modifications from stropmodule.c: atoi, atof, atol */
  58.  
  59. static PyObject *
  60. int_from_string(v)
  61.     PyObject *v;
  62. {
  63.     char *s, *end;
  64.     long x;
  65.     char buffer[256]; /* For errors */
  66.  
  67.     s = PyString_AS_STRING(v);
  68.     while (*s && isspace(Py_CHARMASK(*s)))
  69.         s++;
  70.     errno = 0;
  71.     x = PyOS_strtol(s, &end, 10);
  72.     if (end == s || !isdigit(end[-1]))
  73.         goto bad;
  74.     while (*end && isspace(Py_CHARMASK(*end)))
  75.         end++;
  76.     if (*end != '\0') {
  77.   bad:
  78.         sprintf(buffer, "invalid literal for int(): %.200s", s);
  79.         PyErr_SetString(PyExc_ValueError, buffer);
  80.         return NULL;
  81.     }
  82.     else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
  83.         PyErr_SetString(PyExc_ValueError,
  84.                 "null byte in argument for int()");
  85.         return NULL;
  86.     }
  87.     else if (errno != 0) {
  88.         sprintf(buffer, "int() literal too large: %.200s", s);
  89.         PyErr_SetString(PyExc_ValueError, buffer);
  90.         return NULL;
  91.     }
  92.     return PyInt_FromLong(x);
  93. }
  94.  
  95. static PyObject *
  96. long_from_string(v)
  97.     PyObject *v;
  98. {
  99.     char *s, *end;
  100.     PyObject *x;
  101.     char buffer[256]; /* For errors */
  102.  
  103.     s = PyString_AS_STRING(v);
  104.     while (*s && isspace(Py_CHARMASK(*s)))
  105.         s++;
  106.     x = PyLong_FromString(s, &end, 10);
  107.     if (x == NULL) {
  108.         if (PyErr_ExceptionMatches(PyExc_ValueError))
  109.             goto bad;
  110.         return NULL;
  111.     }
  112.     while (*end && isspace(Py_CHARMASK(*end)))
  113.         end++;
  114.     if (*end != '\0') {
  115.   bad:
  116.         sprintf(buffer, "invalid literal for long(): %.200s", s);
  117.         PyErr_SetString(PyExc_ValueError, buffer);
  118.         Py_XDECREF(x);
  119.         return NULL;
  120.     }
  121.     else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
  122.         PyErr_SetString(PyExc_ValueError,
  123.                 "null byte in argument for long()");
  124.         return NULL;
  125.     }
  126.     return x;
  127. }
  128.  
  129. static PyObject *
  130. float_from_string(v)
  131.     PyObject *v;
  132. {
  133.     extern double strtod Py_PROTO((const char *, char **));
  134.     char *s, *last, *end;
  135.     double x;
  136.     char buffer[256]; /* For errors */
  137.  
  138.     s = PyString_AS_STRING(v);
  139.     last = s + PyString_GET_SIZE(v);
  140.     while (*s && isspace(Py_CHARMASK(*s)))
  141.         s++;
  142.     if (s[0] == '\0') {
  143.         PyErr_SetString(PyExc_ValueError, "empty string for float()");
  144.         return NULL;
  145.     }
  146.     errno = 0;
  147.     PyFPE_START_PROTECT("float_from_string", return 0)
  148.     x = strtod(s, &end);
  149.     PyFPE_END_PROTECT(x)
  150.     /* Believe it or not, Solaris 2.6 can move end *beyond* the null
  151.        byte at the end of the string, when the input is inf(inity) */
  152.     if (end > last)
  153.         end = last;
  154.     while (*end && isspace(Py_CHARMASK(*end)))
  155.         end++;
  156.     if (*end != '\0') {
  157.         sprintf(buffer, "invalid literal for float(): %.200s", s);
  158.         PyErr_SetString(PyExc_ValueError, buffer);
  159.         return NULL;
  160.     }
  161.     else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
  162.         PyErr_SetString(PyExc_ValueError,
  163.                 "null byte in argument for float()");
  164.         return NULL;
  165.     }
  166.     else if (errno != 0) {
  167.         sprintf(buffer, "float() literal too large: %.200s", s);
  168.         PyErr_SetString(PyExc_ValueError, buffer);
  169.         return NULL;
  170.     }
  171.     return PyFloat_FromDouble(x);
  172. }
  173.  
  174. /* Operations on any object */
  175.  
  176. int
  177. PyObject_Cmp(o1, o2, result)
  178.     PyObject *o1;
  179.     PyObject *o2;
  180.     int *result;
  181. {
  182.     int r;
  183.  
  184.     if (o1 == NULL || o2 == NULL) {
  185.         null_error();
  186.         return -1;
  187.     }
  188.     r = PyObject_Compare(o1, o2);
  189.     if (PyErr_Occurred())
  190.         return -1;
  191.     *result = r;
  192.     return 0;
  193. }
  194.  
  195. PyObject *
  196. PyObject_Type(o)
  197.     PyObject *o;
  198. {
  199.     PyObject *v;
  200.  
  201.     if (o == NULL)
  202.         return null_error();
  203.     v = (PyObject *)o->ob_type;
  204.     Py_INCREF(v);
  205.     return v;
  206. }
  207.  
  208. int
  209. PyObject_Length(o)
  210.     PyObject *o;
  211. {
  212.     PySequenceMethods *m;
  213.  
  214.     if (o == NULL) {
  215.         null_error();
  216.         return -1;
  217.     }
  218.  
  219.     m = o->ob_type->tp_as_sequence;
  220.     if (m && m->sq_length)
  221.         return m->sq_length(o);
  222.  
  223.     return PyMapping_Length(o);
  224. }
  225.  
  226. PyObject *
  227. PyObject_GetItem(o, key)
  228.     PyObject *o;
  229.     PyObject *key;
  230. {
  231.     PyMappingMethods *m;
  232.  
  233.     if (o == NULL || key == NULL)
  234.         return null_error();
  235.  
  236.     m = o->ob_type->tp_as_mapping;
  237.     if (m && m->mp_subscript)
  238.         return m->mp_subscript(o, key);
  239.  
  240.     if (o->ob_type->tp_as_sequence) {
  241.         if (PyInt_Check(key))
  242.             return PySequence_GetItem(o, PyInt_AsLong(key));
  243.         return type_error("sequence index must be integer");
  244.     }
  245.  
  246.     return type_error("unsubscriptable object");
  247. }
  248.  
  249. int
  250. PyObject_SetItem(o, key, value)
  251.     PyObject *o;
  252.     PyObject *key;
  253.     PyObject *value;
  254. {
  255.     PyMappingMethods *m;
  256.  
  257.     if (o == NULL || key == NULL || value == NULL) {
  258.         null_error();
  259.         return -1;
  260.     }
  261.     m = o->ob_type->tp_as_mapping;
  262.     if (m && m->mp_ass_subscript)
  263.         return m->mp_ass_subscript(o, key, value);
  264.  
  265.     if (o->ob_type->tp_as_sequence) {
  266.         if (PyInt_Check(key))
  267.             return PySequence_SetItem(o, PyInt_AsLong(key), value);
  268.         type_error("sequence index must be integer");
  269.         return -1;
  270.     }
  271.  
  272.     type_error("object does not support item assignment");
  273.     return -1;
  274. }
  275.  
  276. int
  277. PyObject_DelItem(o, key)
  278.     PyObject *o;
  279.     PyObject *key;
  280. {
  281.     PyMappingMethods *m;
  282.  
  283.     if (o == NULL || key == NULL) {
  284.         null_error();
  285.         return -1;
  286.     }
  287.     m = o->ob_type->tp_as_mapping;
  288.     if (m && m->mp_ass_subscript)
  289.         return m->mp_ass_subscript(o, key, (PyObject*)NULL);
  290.  
  291.     if (o->ob_type->tp_as_sequence) {
  292.         if (PyInt_Check(key))
  293.             return PySequence_DelItem(o, PyInt_AsLong(key));
  294.         type_error("sequence index must be integer");
  295.         return -1;
  296.     }
  297.  
  298.     type_error("object does not support item deletion");
  299.     return -1;
  300. }
  301.  
  302. /* Operations on numbers */
  303.  
  304. int
  305. PyNumber_Check(o)
  306.     PyObject *o;
  307. {
  308.     return o && o->ob_type->tp_as_number;
  309. }
  310.  
  311. /* Binary operators */
  312.  
  313. #define BINOP(v, w, opname, ropname, thisfunc) \
  314.     if (PyInstance_Check(v) || PyInstance_Check(w)) \
  315.         return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
  316.  
  317. PyObject *
  318. PyNumber_Or(v, w)
  319.     PyObject *v, *w;
  320. {
  321.         extern int PyNumber_Coerce();
  322.  
  323.     BINOP(v, w, "__or__", "__ror__", PyNumber_Or);
  324.     if (v->ob_type->tp_as_number != NULL) {
  325.         PyObject *x = NULL;
  326.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  327.         if (PyNumber_Coerce(&v, &w) != 0)
  328.             return NULL;
  329.         if ((f = v->ob_type->tp_as_number->nb_or) != NULL)
  330.             x = (*f)(v, w);
  331.         Py_DECREF(v);
  332.         Py_DECREF(w);
  333.         if (f != NULL)
  334.             return x;
  335.     }
  336.     return type_error("bad operand type(s) for |");
  337. }
  338.  
  339. PyObject *
  340. PyNumber_Xor(v, w)
  341.     PyObject *v, *w;
  342. {
  343.         extern int PyNumber_Coerce();
  344.  
  345.     BINOP(v, w, "__xor__", "__rxor__", PyNumber_Xor);
  346.     if (v->ob_type->tp_as_number != NULL) {
  347.         PyObject *x = NULL;
  348.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  349.         if (PyNumber_Coerce(&v, &w) != 0)
  350.             return NULL;
  351.         if ((f = v->ob_type->tp_as_number->nb_xor) != NULL)
  352.             x = (*f)(v, w);
  353.         Py_DECREF(v);
  354.         Py_DECREF(w);
  355.         if (f != NULL)
  356.             return x;
  357.     }
  358.     return type_error("bad operand type(s) for ^");
  359. }
  360.  
  361. PyObject *
  362. PyNumber_And(v, w)
  363.     PyObject *v, *w;
  364. {
  365.     BINOP(v, w, "__and__", "__rand__", PyNumber_And);
  366.     if (v->ob_type->tp_as_number != NULL) {
  367.         PyObject *x = NULL;
  368.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  369.         if (PyNumber_Coerce(&v, &w) != 0)
  370.             return NULL;
  371.         if ((f = v->ob_type->tp_as_number->nb_and) != NULL)
  372.             x = (*f)(v, w);
  373.         Py_DECREF(v);
  374.         Py_DECREF(w);
  375.         if (f != NULL)
  376.             return x;
  377.     }
  378.     return type_error("bad operand type(s) for &");
  379. }
  380.  
  381. PyObject *
  382. PyNumber_Lshift(v, w)
  383.     PyObject *v, *w;
  384. {
  385.     BINOP(v, w, "__lshift__", "__rlshift__", PyNumber_Lshift);
  386.     if (v->ob_type->tp_as_number != NULL) {
  387.         PyObject *x = NULL;
  388.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  389.         if (PyNumber_Coerce(&v, &w) != 0)
  390.             return NULL;
  391.         if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL)
  392.             x = (*f)(v, w);
  393.         Py_DECREF(v);
  394.         Py_DECREF(w);
  395.         if (f != NULL)
  396.             return x;
  397.     }
  398.     return type_error("bad operand type(s) for <<");
  399. }
  400.  
  401. PyObject *
  402. PyNumber_Rshift(v, w)
  403.     PyObject *v, *w;
  404. {
  405.     BINOP(v, w, "__rshift__", "__rrshift__", PyNumber_Rshift);
  406.     if (v->ob_type->tp_as_number != NULL) {
  407.         PyObject *x = NULL;
  408.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  409.         if (PyNumber_Coerce(&v, &w) != 0)
  410.             return NULL;
  411.         if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL)
  412.             x = (*f)(v, w);
  413.         Py_DECREF(v);
  414.         Py_DECREF(w);
  415.         if (f != NULL)
  416.             return x;
  417.     }
  418.     return type_error("bad operand type(s) for >>");
  419. }
  420.  
  421. PyObject *
  422. PyNumber_Add(v, w)
  423.     PyObject *v, *w;
  424. {
  425.     PySequenceMethods *m;
  426.  
  427.     BINOP(v, w, "__add__", "__radd__", PyNumber_Add);
  428.     m = v->ob_type->tp_as_sequence;
  429.     if (m && m->sq_concat)
  430.         return (*m->sq_concat)(v, w);
  431.     else if (v->ob_type->tp_as_number != NULL) {
  432.         PyObject *x = NULL;
  433.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  434.         if (PyNumber_Coerce(&v, &w) != 0)
  435.             return NULL;
  436.         if ((f = v->ob_type->tp_as_number->nb_add) != NULL)
  437.             x = (*f)(v, w);
  438.         Py_DECREF(v);
  439.         Py_DECREF(w);
  440.         if (f != NULL)
  441.             return x;
  442.     }
  443.     return type_error("bad operand type(s) for +");
  444. }
  445.  
  446. PyObject *
  447. PyNumber_Subtract(v, w)
  448.     PyObject *v, *w;
  449. {
  450.     BINOP(v, w, "__sub__", "__rsub__", PyNumber_Subtract);
  451.     if (v->ob_type->tp_as_number != NULL) {
  452.         PyObject *x = NULL;
  453.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  454.         if (PyNumber_Coerce(&v, &w) != 0)
  455.             return NULL;
  456.         if ((f = v->ob_type->tp_as_number->nb_subtract) != NULL)
  457.             x = (*f)(v, w);
  458.         Py_DECREF(v);
  459.         Py_DECREF(w);
  460.         if (f != NULL)
  461.             return x;
  462.     }
  463.     return type_error("bad operand type(s) for -");
  464. }
  465.  
  466. PyObject *
  467. PyNumber_Multiply(v, w)
  468.     PyObject *v, *w;
  469. {
  470.     PyTypeObject *tp = v->ob_type;
  471.     PySequenceMethods *m;
  472.  
  473.     BINOP(v, w, "__mul__", "__rmul__", PyNumber_Multiply);
  474.     if (tp->tp_as_number != NULL &&
  475.         w->ob_type->tp_as_sequence != NULL &&
  476.         !PyInstance_Check(v)) {
  477.         /* number*sequence -- swap v and w */
  478.         PyObject *tmp = v;
  479.         v = w;
  480.         w = tmp;
  481.         tp = v->ob_type;
  482.     }
  483.     if (tp->tp_as_number != NULL) {
  484.         PyObject *x = NULL;
  485.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  486.         if (PyInstance_Check(v)) {
  487.             /* Instances of user-defined classes get their
  488.                other argument uncoerced, so they may
  489.                implement sequence*number as well as
  490.                number*number. */
  491.             Py_INCREF(v);
  492.             Py_INCREF(w);
  493.         }
  494.         else if (PyNumber_Coerce(&v, &w) != 0)
  495.             return NULL;
  496.         if ((f = v->ob_type->tp_as_number->nb_multiply) != NULL)
  497.             x = (*f)(v, w);
  498.         Py_DECREF(v);
  499.         Py_DECREF(w);
  500.         if (f != NULL)
  501.             return x;
  502.     }
  503.     m = tp->tp_as_sequence;
  504.     if (m && m->sq_repeat) {
  505.         if (!PyInt_Check(w))
  506.             return type_error(
  507.                 "can't multiply sequence with non-int");
  508.         return (*m->sq_repeat)(v, (int)PyInt_AsLong(w));
  509.     }
  510.     return type_error("bad operand type(s) for *");
  511. }
  512.  
  513. PyObject *
  514. PyNumber_Divide(v, w)
  515.     PyObject *v, *w;
  516. {
  517.     BINOP(v, w, "__div__", "__rdiv__", PyNumber_Divide);
  518.     if (v->ob_type->tp_as_number != NULL) {
  519.         PyObject *x = NULL;
  520.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  521.         if (PyNumber_Coerce(&v, &w) != 0)
  522.             return NULL;
  523.         if ((f = v->ob_type->tp_as_number->nb_divide) != NULL)
  524.             x = (*f)(v, w);
  525.         Py_DECREF(v);
  526.         Py_DECREF(w);
  527.         if (f != NULL)
  528.             return x;
  529.     }
  530.     return type_error("bad operand type(s) for /");
  531. }
  532.  
  533. PyObject *
  534. PyNumber_Remainder(v, w)
  535.     PyObject *v, *w;
  536. {
  537.     if (PyString_Check(v))
  538.         return PyString_Format(v, w);
  539.     BINOP(v, w, "__mod__", "__rmod__", PyNumber_Remainder);
  540.     if (v->ob_type->tp_as_number != NULL) {
  541.         PyObject *x = NULL;
  542.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  543.         if (PyNumber_Coerce(&v, &w) != 0)
  544.             return NULL;
  545.         if ((f = v->ob_type->tp_as_number->nb_remainder) != NULL)
  546.             x = (*f)(v, w);
  547.         Py_DECREF(v);
  548.         Py_DECREF(w);
  549.         if (f != NULL)
  550.             return x;
  551.     }
  552.     return type_error("bad operand type(s) for %");
  553. }
  554.  
  555. PyObject *
  556. PyNumber_Divmod(v, w)
  557.     PyObject *v, *w;
  558. {
  559.     BINOP(v, w, "__divmod__", "__rdivmod__", PyNumber_Divmod);
  560.     if (v->ob_type->tp_as_number != NULL) {
  561.         PyObject *x = NULL;
  562.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  563.         if (PyNumber_Coerce(&v, &w) != 0)
  564.             return NULL;
  565.         if ((f = v->ob_type->tp_as_number->nb_divmod) != NULL)
  566.             x = (*f)(v, w);
  567.         Py_DECREF(v);
  568.         Py_DECREF(w);
  569.         if (f != NULL)
  570.             return x;
  571.     }
  572.     return type_error("bad operand type(s) for divmod()");
  573. }
  574.  
  575. /* Power (binary or ternary) */
  576.  
  577. static PyObject *
  578. do_pow(v, w)
  579.     PyObject *v, *w;
  580. {
  581.     PyObject *res;
  582.     PyObject * (*f) Py_FPROTO((PyObject *, PyObject *, PyObject *));
  583.     BINOP(v, w, "__pow__", "__rpow__", do_pow);
  584.     if (v->ob_type->tp_as_number == NULL ||
  585.         w->ob_type->tp_as_number == NULL) {
  586.         PyErr_SetString(PyExc_TypeError,
  587.                 "pow(x, y) requires numeric arguments");
  588.         return NULL;
  589.     }
  590.     if (PyNumber_Coerce(&v, &w) != 0)
  591.         return NULL;
  592.     if ((f = v->ob_type->tp_as_number->nb_power) != NULL)
  593.         res = (*f)(v, w, Py_None);
  594.     else
  595.         res = type_error("pow(x, y) not defined for these operands");
  596.     Py_DECREF(v);
  597.     Py_DECREF(w);
  598.     return res;
  599. }
  600.  
  601. PyObject *
  602. PyNumber_Power(v, w, z)
  603.     PyObject *v, *w, *z;
  604. {
  605.     PyObject *res;
  606.     PyObject *v1, *z1, *w2, *z2;
  607.     PyObject * (*f) Py_FPROTO((PyObject *, PyObject *, PyObject *));
  608.  
  609.     if (z == Py_None)
  610.         return do_pow(v, w);
  611.     /* XXX The ternary version doesn't do class instance coercions */
  612.     if (PyInstance_Check(v))
  613.         return v->ob_type->tp_as_number->nb_power(v, w, z);
  614.     if (v->ob_type->tp_as_number == NULL ||
  615.         z->ob_type->tp_as_number == NULL ||
  616.         w->ob_type->tp_as_number == NULL) {
  617.         return type_error("pow(x, y, z) requires numeric arguments");
  618.     }
  619.     if (PyNumber_Coerce(&v, &w) != 0)
  620.         return NULL;
  621.     res = NULL;
  622.     v1 = v;
  623.     z1 = z;
  624.     if (PyNumber_Coerce(&v1, &z1) != 0)
  625.         goto error2;
  626.     w2 = w;
  627.     z2 = z1;
  628.      if (PyNumber_Coerce(&w2, &z2) != 0)
  629.         goto error1;
  630.     if ((f = v1->ob_type->tp_as_number->nb_power) != NULL)
  631.         res = (*f)(v1, w2, z2);
  632.     else
  633.         res = type_error(
  634.             "pow(x, y, z) not defined for these operands");
  635.     Py_DECREF(w2);
  636.     Py_DECREF(z2);
  637.   error1:
  638.     Py_DECREF(v1);
  639.     Py_DECREF(z1);
  640.   error2:
  641.     Py_DECREF(v);
  642.     Py_DECREF(w);
  643.     return res;
  644. }
  645.  
  646. /* Unary operators and functions */
  647.  
  648. PyObject *
  649. PyNumber_Negative(o)
  650.     PyObject *o;
  651. {
  652.     PyNumberMethods *m;
  653.  
  654.     if (o == NULL)
  655.         return null_error();
  656.     m = o->ob_type->tp_as_number;
  657.     if (m && m->nb_negative)
  658.         return (*m->nb_negative)(o);
  659.  
  660.     return type_error("bad operand type for unary -");
  661. }
  662.  
  663. PyObject *
  664. PyNumber_Positive(o)
  665.     PyObject *o;
  666. {
  667.     PyNumberMethods *m;
  668.  
  669.     if (o == NULL)
  670.         return null_error();
  671.     m = o->ob_type->tp_as_number;
  672.     if (m && m->nb_positive)
  673.         return (*m->nb_positive)(o);
  674.  
  675.     return type_error("bad operand type for unary +");
  676. }
  677.  
  678. PyObject *
  679. PyNumber_Invert(o)
  680.     PyObject *o;
  681. {
  682.     PyNumberMethods *m;
  683.  
  684.     if (o == NULL)
  685.         return null_error();
  686.     m = o->ob_type->tp_as_number;
  687.     if (m && m->nb_invert)
  688.         return (*m->nb_invert)(o);
  689.  
  690.     return type_error("bad operand type for unary ~");
  691. }
  692.  
  693. PyObject *
  694. PyNumber_Absolute(o)
  695.     PyObject *o;
  696. {
  697.     PyNumberMethods *m;
  698.  
  699.     if (o == NULL)
  700.         return null_error();
  701.     m = o->ob_type->tp_as_number;
  702.     if (m && m->nb_absolute)
  703.         return m->nb_absolute(o);
  704.  
  705.     return type_error("bad operand type for abs()");
  706. }
  707.  
  708. PyObject *
  709. PyNumber_Int(o)
  710.     PyObject *o;
  711. {
  712.     PyNumberMethods *m;
  713.  
  714.     if (o == NULL)
  715.         return null_error();
  716.     if (PyString_Check(o))
  717.         return int_from_string(o);
  718.     m = o->ob_type->tp_as_number;
  719.     if (m && m->nb_int)
  720.         return m->nb_int(o);
  721.  
  722.     return type_error("object can't be converted to int");
  723. }
  724.  
  725. PyObject *
  726. PyNumber_Long(o)
  727.     PyObject *o;
  728. {
  729.     PyNumberMethods *m;
  730.  
  731.     if (o == NULL)
  732.         return null_error();
  733.     if (PyString_Check(o))
  734.         return long_from_string(o);
  735.     m = o->ob_type->tp_as_number;
  736.     if (m && m->nb_long)
  737.         return m->nb_long(o);
  738.  
  739.     return type_error("object can't be converted to long");
  740. }
  741.  
  742. PyObject *
  743. PyNumber_Float(o)
  744.     PyObject *o;
  745. {
  746.     PyNumberMethods *m;
  747.  
  748.     if (o == NULL)
  749.         return null_error();
  750.     if (PyString_Check(o))
  751.         return float_from_string(o);
  752.     m = o->ob_type->tp_as_number;
  753.     if (m && m->nb_float)
  754.         return m->nb_float(o);
  755.  
  756.     return type_error("object can't be converted to float");
  757. }
  758.  
  759. /* Operations on sequences */
  760.  
  761. int
  762. PySequence_Check(s)
  763.     PyObject *s;
  764. {
  765.     return s != NULL && s->ob_type->tp_as_sequence;
  766. }
  767.  
  768. int
  769. PySequence_Length(s)
  770.     PyObject *s;
  771. {
  772.     PySequenceMethods *m;
  773.  
  774.     if (s == NULL) {
  775.         null_error();
  776.         return -1;
  777.     }
  778.  
  779.     m = s->ob_type->tp_as_sequence;
  780.     if (m && m->sq_length)
  781.         return m->sq_length(s);
  782.  
  783.     type_error("len() of unsized object");
  784.     return -1;
  785. }
  786.  
  787. PyObject *
  788. PySequence_Concat(s, o)
  789.     PyObject *s;
  790.     PyObject *o;
  791. {
  792.     PySequenceMethods *m;
  793.  
  794.     if (s == NULL || o == NULL)
  795.         return null_error();
  796.  
  797.     m = s->ob_type->tp_as_sequence;
  798.     if (m && m->sq_concat)
  799.         return m->sq_concat(s, o);
  800.  
  801.     return type_error("object can't be concatenated");
  802. }
  803.  
  804. PyObject *
  805. PySequence_Repeat(o, count)
  806.     PyObject *o;
  807.     int count;
  808. {
  809.     PySequenceMethods *m;
  810.  
  811.     if (o == NULL)
  812.         return null_error();
  813.  
  814.     m = o->ob_type->tp_as_sequence;
  815.     if (m && m->sq_repeat)
  816.         return m->sq_repeat(o, count);
  817.  
  818.     return type_error("object can't be repeated");
  819. }
  820.  
  821. PyObject *
  822. PySequence_GetItem(s, i)
  823.     PyObject *s;
  824.     int i;
  825. {
  826.     PySequenceMethods *m;
  827.  
  828.     if (s == NULL)
  829.         return null_error();
  830.  
  831.     m = s->ob_type->tp_as_sequence;
  832.     if (m && m->sq_item) {
  833.         if (i < 0) {
  834.             if (m->sq_length) {
  835.                 int l = (*m->sq_length)(s);
  836.                 if (l < 0)
  837.                     return NULL;
  838.                 i += l;
  839.             }
  840.         }
  841.         return m->sq_item(s, i);
  842.     }
  843.  
  844.     return type_error("unindexable object");
  845. }
  846.  
  847. PyObject *
  848. PySequence_GetSlice(s, i1, i2)
  849.     PyObject *s;
  850.     int i1;
  851.     int i2;
  852. {
  853.     PySequenceMethods *m;
  854.  
  855.     if (!s) return null_error();
  856.  
  857.     m = s->ob_type->tp_as_sequence;
  858.     if (m && m->sq_slice) {
  859.         if (i1 < 0 || i2 < 0) {
  860.             if (m->sq_length) {
  861.                 int l = (*m->sq_length)(s);
  862.                 if (l < 0)
  863.                     return NULL;
  864.                 if (i1 < 0)
  865.                     i1 += l;
  866.                 if (i2 < 0)
  867.                     i2 += l;
  868.             }
  869.         }
  870.         return m->sq_slice(s, i1, i2);
  871.     }
  872.  
  873.     return type_error("unsliceable object");
  874. }
  875.  
  876. int
  877. PySequence_SetItem(s, i, o)
  878.     PyObject *s;
  879.     int i;
  880.     PyObject *o;
  881. {
  882.     PySequenceMethods *m;
  883.  
  884.     if (s == NULL) {
  885.         null_error();
  886.         return -1;
  887.     }
  888.  
  889.     m = s->ob_type->tp_as_sequence;
  890.     if (m && m->sq_ass_item) {
  891.         if (i < 0) {
  892.             if (m->sq_length) {
  893.                 int l = (*m->sq_length)(s);
  894.                 if (l < 0)
  895.                     return -1;
  896.                 i += l;
  897.             }
  898.         }
  899.         return m->sq_ass_item(s, i, o);
  900.     }
  901.  
  902.     type_error("object doesn't support item assignment");
  903.     return -1;
  904. }
  905.  
  906. int
  907. PySequence_DelItem(s, i)
  908.     PyObject *s;
  909.     int i;
  910. {
  911.     PySequenceMethods *m;
  912.  
  913.     if (s == NULL) {
  914.         null_error();
  915.         return -1;
  916.     }
  917.  
  918.     m = s->ob_type->tp_as_sequence;
  919.     if (m && m->sq_ass_item) {
  920.         if (i < 0) {
  921.             if (m->sq_length) {
  922.                 int l = (*m->sq_length)(s);
  923.                 if (l < 0)
  924.                     return -1;
  925.                 i += l;
  926.             }
  927.         }
  928.         return m->sq_ass_item(s, i, (PyObject *)NULL);
  929.     }
  930.  
  931.     type_error("object doesn't support item deletion");
  932.     return -1;
  933. }
  934.  
  935. int
  936. PySequence_SetSlice(s, i1, i2, o)
  937.     PyObject *s;
  938.     int i1;
  939.     int i2;
  940.     PyObject *o;
  941. {
  942.     PySequenceMethods *m;
  943.  
  944.     if (s == NULL) {
  945.         null_error();
  946.         return -1;
  947.     }
  948.  
  949.     m = s->ob_type->tp_as_sequence;
  950.     if (m && m->sq_ass_slice) {
  951.         if (i1 < 0 || i2 < 0) {
  952.             if (m->sq_length) {
  953.                 int l = (*m->sq_length)(s);
  954.                 if (l < 0)
  955.                     return -1;
  956.                 if (i1 < 0)
  957.                     i1 += l;
  958.                 if (i2 < 0)
  959.                     i2 += l;
  960.             }
  961.         }
  962.         return m->sq_ass_slice(s, i1, i2, o);
  963.     }
  964.     type_error("object doesn't support slice assignment");
  965.     return -1;
  966. }
  967.  
  968. int
  969. PySequence_DelSlice(s, i1, i2)
  970.     PyObject *s;
  971.     int i1;
  972.     int i2;
  973. {
  974.     PySequenceMethods *m;
  975.  
  976.     if (s == NULL) {
  977.         null_error();
  978.         return -1;
  979.     }
  980.  
  981.     m = s->ob_type->tp_as_sequence;
  982.     if (m && m->sq_ass_slice) {
  983.         if (i1 < 0 || i2 < 0) {
  984.             if (m->sq_length) {
  985.                 int l = (*m->sq_length)(s);
  986.                 if (l < 0)
  987.                     return -1;
  988.                 if (i1 < 0)
  989.                     i1 += l;
  990.                 if (i2 < 0)
  991.                     i2 += l;
  992.             }
  993.         }
  994.         return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
  995.     }
  996.     type_error("object doesn't support slice deletion");
  997.     return -1;
  998. }
  999.  
  1000. PyObject *
  1001. PySequence_Tuple(v)
  1002.     PyObject *v;
  1003. {
  1004.     PySequenceMethods *m;
  1005.  
  1006.     if (v == NULL)
  1007.         return null_error();
  1008.  
  1009.     if (PyTuple_Check(v)) {
  1010.         Py_INCREF(v);
  1011.         return v;
  1012.     }
  1013.  
  1014.     if (PyList_Check(v))
  1015.         return PyList_AsTuple(v);
  1016.  
  1017.     /* There used to be code for strings here, but tuplifying strings is
  1018.        not a common activity, so I nuked it.  Down with code bloat! */
  1019.  
  1020.     /* Generic sequence object */
  1021.     m = v->ob_type->tp_as_sequence;
  1022.     if (m && m->sq_item) {
  1023.         int i;
  1024.         PyObject *t;
  1025.         int n = PySequence_Length(v);
  1026.         if (n < 0)
  1027.             return NULL;
  1028.         t = PyTuple_New(n);
  1029.         if (t == NULL)
  1030.             return NULL;
  1031.         for (i = 0; ; i++) {
  1032.             PyObject *item = (*m->sq_item)(v, i);
  1033.             if (item == NULL) {
  1034.                 if (PyErr_ExceptionMatches(PyExc_IndexError))
  1035.                     PyErr_Clear();
  1036.                 else {
  1037.                     Py_DECREF(t);
  1038.                     t = NULL;
  1039.                 }
  1040.                 break;
  1041.             }
  1042.             if (i >= n) {
  1043.                 if (n < 500)
  1044.                     n += 10;
  1045.                 else
  1046.                     n += 100;
  1047.                 if (_PyTuple_Resize(&t, n, 0) != 0)
  1048.                     break;
  1049.             }
  1050.             PyTuple_SET_ITEM(t, i, item);
  1051.         }
  1052.         if (i < n && t != NULL)
  1053.             _PyTuple_Resize(&t, i, 0);
  1054.         return t;
  1055.     }
  1056.  
  1057.     /* None of the above */
  1058.     return type_error("tuple() argument must be a sequence");
  1059. }
  1060.  
  1061. PyObject *
  1062. PySequence_List(v)
  1063.     PyObject *v;
  1064. {
  1065.     PySequenceMethods *m;
  1066.  
  1067.     if (v == NULL)
  1068.         return null_error();
  1069.  
  1070.     if (PyList_Check(v))
  1071.         return PyList_GetSlice(v, 0, PyList_GET_SIZE(v));
  1072.  
  1073.     m = v->ob_type->tp_as_sequence;
  1074.     if (m && m->sq_item) {
  1075.         int i;
  1076.         PyObject *l;
  1077.         int n = PySequence_Length(v);
  1078.         if (n < 0)
  1079.             return NULL;
  1080.         l = PyList_New(n);
  1081.         if (l == NULL)
  1082.             return NULL;
  1083.         for (i = 0; ; i++) {
  1084.             PyObject *item = (*m->sq_item)(v, i);
  1085.             if (item == NULL) {
  1086.                 if (PyErr_ExceptionMatches(PyExc_IndexError))
  1087.                     PyErr_Clear();
  1088.                 else {
  1089.                     Py_DECREF(l);
  1090.                     l = NULL;
  1091.                 }
  1092.                 break;
  1093.             }
  1094.             if (i < n)
  1095.                 PyList_SET_ITEM(l, i, item);
  1096.             else if (PyList_Append(l, item) < 0) {
  1097.                 Py_DECREF(l);
  1098.                 l = NULL;
  1099.                 break;
  1100.             }
  1101.         }
  1102.         if (i < n && l != NULL) {
  1103.             if (PyList_SetSlice(l, i, n, (PyObject *)NULL) != 0) {
  1104.                 Py_DECREF(l);
  1105.                 l = NULL;
  1106.             }
  1107.         }
  1108.         return l;
  1109.     }
  1110.     return type_error("list() argument must be a sequence");
  1111. }
  1112.  
  1113. int
  1114. PySequence_Count(s, o)
  1115.     PyObject *s;
  1116.     PyObject *o;
  1117. {
  1118.     int l, i, n, cmp, err;
  1119.     PyObject *item;
  1120.  
  1121.     if (s == NULL || o == NULL) {
  1122.         null_error();
  1123.         return -1;
  1124.     }
  1125.     
  1126.     l = PySequence_Length(s);
  1127.     if (l < 0)
  1128.         return -1;
  1129.  
  1130.     n = 0;
  1131.     for (i = 0; i < l; i++) {
  1132.         item = PySequence_GetItem(s, i);
  1133.         if (item == NULL)
  1134.             return -1;
  1135.         err = PyObject_Cmp(item, o, &cmp);
  1136.         Py_DECREF(item);
  1137.         if (err < 0)
  1138.             return err;
  1139.         if (cmp == 0)
  1140.             n++;
  1141.     }
  1142.     return n;
  1143. }
  1144.  
  1145. int
  1146. PySequence_Contains(w, v) /* v in w */
  1147.     PyObject *w;
  1148.     PyObject *v;
  1149. {
  1150.     int i, cmp;
  1151.     PyObject *x;
  1152.     PySequenceMethods *sq;
  1153.  
  1154.     /* Special case for char in string */
  1155.     if (PyString_Check(w)) {
  1156.         register char *s, *end;
  1157.         register char c;
  1158.         if (!PyString_Check(v) || PyString_Size(v) != 1) {
  1159.             PyErr_SetString(PyExc_TypeError,
  1160.                 "string member test needs char left operand");
  1161.             return -1;
  1162.         }
  1163.         c = PyString_AsString(v)[0];
  1164.         s = PyString_AsString(w);
  1165.         end = s + PyString_Size(w);
  1166.         while (s < end) {
  1167.             if (c == *s++)
  1168.                 return 1;
  1169.         }
  1170.         return 0;
  1171.     }
  1172.  
  1173.     sq = w->ob_type->tp_as_sequence;
  1174.     if (sq == NULL || sq->sq_item == NULL) {
  1175.         PyErr_SetString(PyExc_TypeError,
  1176.             "'in' or 'not in' needs sequence right argument");
  1177.         return -1;
  1178.     }
  1179.  
  1180.     for (i = 0; ; i++) {
  1181.         x = (*sq->sq_item)(w, i);
  1182.         if (x == NULL) {
  1183.             if (PyErr_ExceptionMatches(PyExc_IndexError)) {
  1184.                 PyErr_Clear();
  1185.                 break;
  1186.             }
  1187.             return -1;
  1188.         }
  1189.         cmp = PyObject_Compare(v, x);
  1190.         Py_XDECREF(x);
  1191.         if (cmp == 0)
  1192.             return 1;
  1193.         if (PyErr_Occurred())
  1194.             return -1;
  1195.     }
  1196.  
  1197.     return 0;
  1198. }
  1199.  
  1200. /* Backwards compatibility */
  1201. #undef PySequence_In
  1202. int
  1203. PySequence_In(w, v)
  1204.     PyObject *w;
  1205.     PyObject *v;
  1206. {
  1207.     return PySequence_Contains(w, v);
  1208. }
  1209.  
  1210. int
  1211. PySequence_Index(s, o)
  1212.     PyObject *s;
  1213.     PyObject *o;
  1214. {
  1215.     int l, i, cmp, err;
  1216.     PyObject *item;
  1217.  
  1218.     if (s == NULL || o == NULL) {
  1219.         null_error();
  1220.         return -1;
  1221.     }
  1222.     
  1223.     l = PySequence_Length(s);
  1224.     if (l < 0)
  1225.         return -1;
  1226.  
  1227.     for (i = 0; i < l; i++) {
  1228.         item = PySequence_GetItem(s, i);
  1229.         if (item == NULL)
  1230.             return -1;
  1231.         err = PyObject_Cmp(item, o, &cmp);
  1232.         Py_DECREF(item);
  1233.         if (err < 0)
  1234.             return err;
  1235.         if (cmp == 0)
  1236.             return i;
  1237.     }
  1238.  
  1239.     PyErr_SetString(PyExc_ValueError, "sequence.index(x): x not in list");
  1240.     return -1;
  1241. }
  1242.  
  1243. /* Operations on mappings */
  1244.  
  1245. int
  1246. PyMapping_Check(o)
  1247.     PyObject *o;
  1248. {
  1249.     return o && o->ob_type->tp_as_mapping;
  1250. }
  1251.  
  1252. int
  1253. PyMapping_Length(o)
  1254.     PyObject *o;
  1255. {
  1256.     PyMappingMethods *m;
  1257.  
  1258.     if (o == NULL) {
  1259.         null_error();
  1260.         return -1;
  1261.     }
  1262.  
  1263.     m = o->ob_type->tp_as_mapping;
  1264.     if (m && m->mp_length)
  1265.         return m->mp_length(o);
  1266.  
  1267.     type_error("len() of unsized object");
  1268.     return -1;
  1269. }
  1270.  
  1271. PyObject *
  1272. PyMapping_GetItemString(o, key)
  1273.     PyObject *o;
  1274.     char *key;
  1275. {
  1276.     PyObject *okey, *r;
  1277.  
  1278.     if (key == NULL)
  1279.         return null_error();
  1280.  
  1281.     okey = PyString_FromString(key);
  1282.     if (okey == NULL)
  1283.         return NULL;
  1284.     r = PyObject_GetItem(o, okey);
  1285.     Py_DECREF(okey);
  1286.     return r;
  1287. }
  1288.  
  1289. int
  1290. PyMapping_SetItemString(o, key, value)
  1291.     PyObject *o;
  1292.     char *key;
  1293.     PyObject *value;
  1294. {
  1295.     PyObject *okey;
  1296.     int r;
  1297.  
  1298.     if (key == NULL) {
  1299.         null_error();
  1300.         return -1;
  1301.     }
  1302.  
  1303.     okey = PyString_FromString(key);
  1304.     if (okey == NULL)
  1305.         return -1;
  1306.     r = PyObject_SetItem(o, okey, value);
  1307.     Py_DECREF(okey);
  1308.     return r;
  1309. }
  1310.  
  1311. int
  1312. PyMapping_HasKeyString(o, key)
  1313.     PyObject *o;
  1314.     char *key;
  1315. {
  1316.     PyObject *v;
  1317.  
  1318.     v = PyMapping_GetItemString(o, key);
  1319.     if (v) {
  1320.         Py_DECREF(v);
  1321.         return 1;
  1322.     }
  1323.     PyErr_Clear();
  1324.     return 0;
  1325. }
  1326.  
  1327. int
  1328. PyMapping_HasKey(o, key)
  1329.     PyObject *o;
  1330.     PyObject *key;
  1331. {
  1332.     PyObject *v;
  1333.  
  1334.     v = PyObject_GetItem(o, key);
  1335.     if (v) {
  1336.         Py_DECREF(v);
  1337.         return 1;
  1338.     }
  1339.     PyErr_Clear();
  1340.     return 0;
  1341. }
  1342.  
  1343. /* Operations on callable objects */
  1344.  
  1345. /* XXX PyCallable_Check() is in object.c */
  1346.  
  1347. PyObject *
  1348. PyObject_CallObject(o, a)
  1349.     PyObject *o, *a;
  1350. {
  1351.     PyObject *r;
  1352.     PyObject *args = a;
  1353.  
  1354.     if (args == NULL) {
  1355.         args = PyTuple_New(0);
  1356.         if (args == NULL)
  1357.             return NULL;
  1358.     }
  1359.  
  1360.     r = PyEval_CallObject(o, args);
  1361.  
  1362.     if (args != a) {
  1363.         Py_DECREF(args);
  1364.     }
  1365.  
  1366.     return r;
  1367. }
  1368.  
  1369. PyObject *
  1370. #ifdef HAVE_STDARG_PROTOTYPES
  1371. /* VARARGS 2 */
  1372. PyObject_CallFunction(PyObject *callable, char *format, ...)
  1373. #else
  1374. /* VARARGS */
  1375.     PyObject_CallFunction(va_alist) va_dcl
  1376. #endif
  1377. {
  1378.     va_list va;
  1379.     PyObject *args, *retval;
  1380. #ifdef HAVE_STDARG_PROTOTYPES
  1381.     va_start(va, format);
  1382. #else
  1383.     PyObject *callable;
  1384.     char *format;
  1385.     va_start(va);
  1386.     callable = va_arg(va, PyObject *);
  1387.     format   = va_arg(va, char *);
  1388. #endif
  1389.  
  1390.     if (callable == NULL) {
  1391.         va_end(va);
  1392.         return null_error();
  1393.     }
  1394.  
  1395.     if (format)
  1396.         args = Py_VaBuildValue(format, va);
  1397.     else
  1398.         args = PyTuple_New(0);
  1399.  
  1400.     va_end(va);
  1401.     
  1402.     if (args == NULL)
  1403.         return NULL;
  1404.  
  1405.     if (!PyTuple_Check(args)) {
  1406.         PyObject *a;
  1407.  
  1408.         a = PyTuple_New(1);
  1409.         if (a == NULL)
  1410.             return NULL;
  1411.         if (PyTuple_SetItem(a, 0, args) < 0)
  1412.             return NULL;
  1413.         args = a;
  1414.     }
  1415.     retval = PyObject_CallObject(callable, args);
  1416.  
  1417.     Py_DECREF(args);
  1418.  
  1419.     return retval;
  1420. }
  1421.  
  1422. PyObject *
  1423. #ifdef HAVE_STDARG_PROTOTYPES
  1424. /* VARARGS 2 */
  1425. PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
  1426. #else
  1427. /* VARARGS */
  1428.     PyObject_CallMethod(va_alist) va_dcl
  1429. #endif
  1430. {
  1431.     va_list va;
  1432.     PyObject *args, *func = 0, *retval;
  1433. #ifdef HAVE_STDARG_PROTOTYPES
  1434.     va_start(va, format);
  1435. #else
  1436.     PyObject *o;
  1437.     char *name;
  1438.     char *format;
  1439.     va_start(va);
  1440.     o      = va_arg(va, PyObject *);
  1441.     name   = va_arg(va, char *);
  1442.     format = va_arg(va, char *);
  1443. #endif
  1444.  
  1445.     if (o == NULL || name == NULL) {
  1446.         va_end(va);
  1447.         return null_error();
  1448.     }
  1449.  
  1450.     func = PyObject_GetAttrString(o, name);
  1451.     if (func == NULL) {
  1452.         va_end(va);
  1453.         PyErr_SetString(PyExc_AttributeError, name);
  1454.         return 0;
  1455.     }
  1456.  
  1457.     if (!PyCallable_Check(func)) {
  1458.         va_end(va);
  1459.         return type_error("call of non-callable attribute");
  1460.     }
  1461.  
  1462.     if (format && *format)
  1463.         args = Py_VaBuildValue(format, va);
  1464.     else
  1465.         args = PyTuple_New(0);
  1466.  
  1467.     va_end(va);
  1468.  
  1469.     if (!args)
  1470.         return NULL;
  1471.  
  1472.     if (!PyTuple_Check(args)) {
  1473.         PyObject *a;
  1474.  
  1475.         a = PyTuple_New(1);
  1476.         if (a == NULL)
  1477.             return NULL;
  1478.         if (PyTuple_SetItem(a, 0, args) < 0)
  1479.             return NULL;
  1480.         args = a;
  1481.     }
  1482.  
  1483.     retval = PyObject_CallObject(func, args);
  1484.  
  1485.     Py_DECREF(args);
  1486.     Py_DECREF(func);
  1487.  
  1488.     return retval;
  1489. }
  1490.