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