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

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Abstract Object Interface (many thanks to Jim Fulton) */
  33.  
  34. #include "Python.h"
  35.  
  36. #define Py_TRY(E) if(!(E)) return NULL
  37. #define Py_ASSERT(EXP,E,V) if(!(EXP)) return PyErr_SetString(E,V), (void*)NULL
  38. #define SPAM printf("line %d\n",__LINE__)
  39.  
  40. static PyObject *
  41. Py_ReturnMethodError(name)
  42.   char *name;
  43. {
  44.   if(! name) name = "Unknown Error";
  45.   PyErr_SetString(PyExc_AttributeError,name);
  46.   return 0;
  47. }
  48.  
  49. PyObject *
  50. Py_ReturnNullError()
  51. {
  52.   if(! PyErr_Occurred())
  53.     PyErr_SetString(PyExc_SystemError,
  54.             "null argument to internal routine");
  55.   return 0;
  56. }
  57.  
  58. int 
  59. PyObject_Cmp(o1, o2, result)
  60.   PyObject *o1;
  61.   PyObject *o2;
  62.   int *result;
  63. {
  64.   int r;
  65.  
  66.   if(! o1 || ! o2) return Py_ReturnNullError(),-1;
  67.   r=PyObject_Compare(o1,o2);
  68.   if(PyErr_Occurred()) return -1;
  69.   *result=r;
  70.   return 0;
  71. }
  72.  
  73. #if 0 /* Already in object.c */
  74. int
  75. PyCallable_Check(x)
  76.   PyObject *x;
  77. {
  78.     if (x == NULL)
  79.         return 0;
  80.     if (x->ob_type->tp_call != NULL ||
  81.         PyFunction_Check(x) ||
  82.         PyMethod_Check(x) ||
  83.         PyCFunction_Check(x) ||
  84.         PyClass_Check(x))
  85.         return 1;
  86.     if (PyInstance_Check(x)) {
  87.         PyObject *call = PyObject_GetAttrString(x, "__call__");
  88.         if (call == NULL) {
  89.             PyErr_Clear();
  90.             return 0;
  91.         }
  92.         /* Could test recursively but don't, for fear of endless
  93.            recursion if some joker sets self.__call__ = self */
  94.         Py_DECREF(call);
  95.         return 1;
  96.     }
  97.     return 0;
  98. }
  99. #endif
  100.  
  101. PyObject *
  102. PyObject_Type(o)
  103.     PyObject *o;
  104. {
  105.     PyObject *v;
  106.  
  107.     if(! o) return Py_ReturnNullError();
  108.     v = (PyObject *)o->ob_type;
  109.     Py_INCREF(v);
  110.     return v;
  111. }
  112.  
  113. int
  114. PyObject_Length(o)
  115.   PyObject *o;
  116. {
  117.   PySequenceMethods *m;
  118.  
  119.   if(! o) return Py_ReturnNullError(),-1;
  120.  
  121.   if((m=o->ob_type->tp_as_sequence) && m->sq_length)
  122.     return m->sq_length(o);
  123.  
  124.   return PyMapping_Length(o);
  125. }
  126.  
  127. PyObject *
  128. PyObject_GetItem(o, key)
  129.   PyObject *o;
  130.   PyObject *key;
  131. {
  132.   PyMappingMethods *m;
  133.  
  134.   if(! o || ! key) return Py_ReturnNullError();
  135.  
  136.   if((m=o->ob_type->tp_as_mapping) && m->mp_subscript)
  137.     return m->mp_subscript(o,key);
  138.   
  139.   if(PyInt_Check(key))
  140.     return PySequence_GetItem(o,PyInt_AsLong(key));
  141.  
  142.   PyErr_SetString(PyExc_TypeError,"expected integer index");
  143.   return NULL;
  144. }
  145.  
  146. int
  147. PyObject_SetItem(o, key, value)
  148.   PyObject *o;
  149.   PyObject *key;
  150.   PyObject *value;
  151. {
  152.   PyMappingMethods *m;
  153.  
  154.   if(! o || ! key || ! value) return Py_ReturnNullError(),-1;
  155.   if((m=o->ob_type->tp_as_mapping) && m->mp_ass_subscript)
  156.     return m->mp_ass_subscript(o,key,value);
  157.   
  158.   if(PyInt_Check(key))
  159.     return PySequence_SetItem(o,PyInt_AsLong(key),value);
  160.  
  161.   PyErr_SetString(PyExc_TypeError,"expeced integer index");
  162.   return -1;
  163. }
  164.  
  165. int
  166. PyObject_DelItem(o, key)
  167.   PyObject *o;
  168.   PyObject *key;
  169. {
  170.   PyMappingMethods *m;
  171.  
  172.   if(! o || ! key) return Py_ReturnNullError(),-1;
  173.   if((m=o->ob_type->tp_as_mapping) && m->mp_ass_subscript)
  174.     return m->mp_ass_subscript(o,key,(PyObject*)NULL);
  175.   
  176.   if(PyInt_Check(key))
  177.     return PySequence_SetItem(o,PyInt_AsLong(key),(PyObject*)NULL);
  178.  
  179.   PyErr_SetString(PyExc_TypeError,"expeced integer index");
  180.   return -1;
  181. }
  182.  
  183. int 
  184. PyNumber_Check(o)
  185.   PyObject *o;
  186. {
  187.   return o && o->ob_type->tp_as_number;
  188. }
  189.  
  190.  
  191. #define BINOP(opname, ropname, thisfunc) \
  192.     if (!PyInstance_Check(v) && !PyInstance_Check(w)) \
  193.         ; \
  194.     else \
  195.         return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
  196.  
  197. PyObject *
  198. PyNumber_Or(v, w)
  199.     PyObject *v, *w;
  200. {
  201.         extern int PyNumber_Coerce();
  202.  
  203.     BINOP("__or__", "__ror__", PyNumber_Or);
  204.     if (v->ob_type->tp_as_number != NULL) {
  205.         PyObject *x;
  206.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  207.         if (PyNumber_Coerce(&v, &w) != 0)
  208.             return NULL;
  209.         if ((f = v->ob_type->tp_as_number->nb_or) != NULL)
  210.             x = (*f)(v, w);
  211.         Py_DECREF(v);
  212.         Py_DECREF(w);
  213.         if (f != NULL)
  214.             return x;
  215.     }
  216.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for |");
  217.     return NULL;
  218. }
  219.  
  220. PyObject *
  221. PyNumber_Xor(v, w)
  222.     PyObject *v, *w;
  223. {
  224.         extern int PyNumber_Coerce();
  225.  
  226.     BINOP("__xor__", "__rxor__", PyNumber_Xor);
  227.     if (v->ob_type->tp_as_number != NULL) {
  228.         PyObject *x;
  229.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  230.         if (PyNumber_Coerce(&v, &w) != 0)
  231.             return NULL;
  232.         if ((f = v->ob_type->tp_as_number->nb_xor) != NULL)
  233.             x = (*f)(v, w);
  234.         Py_DECREF(v);
  235.         Py_DECREF(w);
  236.         if (f != NULL)
  237.             return x;
  238.     }
  239.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for ^");
  240.     return NULL;
  241. }
  242.  
  243. PyObject *
  244. PyNumber_And(v, w)
  245.     PyObject *v, *w;
  246. {
  247.     BINOP("__and__", "__rand__", PyNumber_And);
  248.     if (v->ob_type->tp_as_number != NULL) {
  249.         PyObject *x;
  250.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  251.         if (PyNumber_Coerce(&v, &w) != 0)
  252.             return NULL;
  253.         if ((f = v->ob_type->tp_as_number->nb_and) != NULL)
  254.             x = (*f)(v, w);
  255.         Py_DECREF(v);
  256.         Py_DECREF(w);
  257.         if (f != NULL)
  258.             return x;
  259.     }
  260.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for &");
  261.     return NULL;
  262. }
  263.  
  264. PyObject *
  265. PyNumber_Lshift(v, w)
  266.     PyObject *v, *w;
  267. {
  268.     BINOP("__lshift__", "__rlshift__", PyNumber_Lshift);
  269.     if (v->ob_type->tp_as_number != NULL) {
  270.         PyObject *x;
  271.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  272.         if (PyNumber_Coerce(&v, &w) != 0)
  273.             return NULL;
  274.         if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL)
  275.             x = (*f)(v, w);
  276.         Py_DECREF(v);
  277.         Py_DECREF(w);
  278.         if (f != NULL)
  279.             return x;
  280.     }
  281.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for <<");
  282.     return NULL;
  283. }
  284.  
  285. PyObject *
  286. PyNumber_Rshift(v, w)
  287.     PyObject *v, *w;
  288. {
  289.     BINOP("__rshift__", "__rrshift__", PyNumber_Rshift);
  290.     if (v->ob_type->tp_as_number != NULL) {
  291.         PyObject *x;
  292.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  293.         if (PyNumber_Coerce(&v, &w) != 0)
  294.             return NULL;
  295.         if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL)
  296.             x = (*f)(v, w);
  297.         Py_DECREF(v);
  298.         Py_DECREF(w);
  299.         if (f != NULL)
  300.             return x;
  301.     }
  302.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for >>");
  303.     return NULL;
  304. }
  305.  
  306. PyObject *
  307. PyNumber_Add(v, w)
  308.     PyObject *v, *w;
  309. {
  310.     BINOP("__add__", "__radd__", PyNumber_Add);
  311.     if (v->ob_type->tp_as_sequence != NULL)
  312.         return (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
  313.     else if (v->ob_type->tp_as_number != NULL) {
  314.         PyObject *x;
  315.         if (PyNumber_Coerce(&v, &w) != 0)
  316.             return NULL;
  317.         x = (*v->ob_type->tp_as_number->nb_add)(v, w);
  318.         Py_DECREF(v);
  319.         Py_DECREF(w);
  320.         return x;
  321.     }
  322.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for +");
  323.     return NULL;
  324. }
  325.  
  326. PyObject *
  327. PyNumber_Subtract(v, w)
  328.     PyObject *v, *w;
  329. {
  330.     BINOP("__sub__", "__rsub__", PyNumber_Subtract);
  331.     if (v->ob_type->tp_as_number != NULL) {
  332.         PyObject *x;
  333.         if (PyNumber_Coerce(&v, &w) != 0)
  334.             return NULL;
  335.         x = (*v->ob_type->tp_as_number->nb_subtract)(v, w);
  336.         Py_DECREF(v);
  337.         Py_DECREF(w);
  338.         return x;
  339.     }
  340.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for -");
  341.     return NULL;
  342. }
  343.  
  344. PyObject *
  345. PyNumber_Multiply(v, w)
  346.     PyObject *v, *w;
  347. {
  348.     PyTypeObject *tp;
  349.     tp = v->ob_type;
  350.     BINOP("__mul__", "__rmul__", PyNumber_Multiply);
  351.     if (tp->tp_as_number != NULL &&
  352.         w->ob_type->tp_as_sequence != NULL &&
  353.         !PyInstance_Check(v)) {
  354.         /* number*sequence -- swap v and w */
  355.         PyObject *tmp = v;
  356.         v = w;
  357.         w = tmp;
  358.         tp = v->ob_type;
  359.     }
  360.     if (tp->tp_as_number != NULL) {
  361.         PyObject *x;
  362.         if (PyInstance_Check(v)) {
  363.             /* Instances of user-defined classes get their
  364.                other argument uncoerced, so they may
  365.                implement sequence*number as well as
  366.                number*number. */
  367.             Py_INCREF(v);
  368.             Py_INCREF(w);
  369.         }
  370.         else if (PyNumber_Coerce(&v, &w) != 0)
  371.             return NULL;
  372.         x = (*v->ob_type->tp_as_number->nb_multiply)(v, w);
  373.         Py_DECREF(v);
  374.         Py_DECREF(w);
  375.         return x;
  376.     }
  377.     if (tp->tp_as_sequence != NULL) {
  378.         if (!PyInt_Check(w)) {
  379.             PyErr_SetString(PyExc_TypeError,
  380.                 "can't multiply sequence with non-int");
  381.             return NULL;
  382.         }
  383.         return (*tp->tp_as_sequence->sq_repeat)
  384.                         (v, (int)PyInt_AsLong(w));
  385.     }
  386.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for *");
  387.     return NULL;
  388. }
  389.  
  390. PyObject *
  391. PyNumber_Divide(v, w)
  392.     PyObject *v, *w;
  393. {
  394.     BINOP("__div__", "__rdiv__", PyNumber_Divide);
  395.     if (v->ob_type->tp_as_number != NULL) {
  396.         PyObject *x;
  397.         if (PyNumber_Coerce(&v, &w) != 0)
  398.             return NULL;
  399.         x = (*v->ob_type->tp_as_number->nb_divide)(v, w);
  400.         Py_DECREF(v);
  401.         Py_DECREF(w);
  402.         return x;
  403.     }
  404.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for /");
  405.     return NULL;
  406. }
  407.  
  408. PyObject *
  409. PyNumber_Remainder(v, w)
  410.     PyObject *v, *w;
  411. {
  412.     if (PyString_Check(v)) {
  413.         return PyString_Format(v, w);
  414.     }
  415.     BINOP("__mod__", "__rmod__", PyNumber_Remainder);
  416.     if (v->ob_type->tp_as_number != NULL) {
  417.         PyObject *x;
  418.         if (PyNumber_Coerce(&v, &w) != 0)
  419.             return NULL;
  420.         x = (*v->ob_type->tp_as_number->nb_remainder)(v, w);
  421.         Py_DECREF(v);
  422.         Py_DECREF(w);
  423.         return x;
  424.     }
  425.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for %");
  426.     return NULL;
  427. }
  428.  
  429. PyObject *
  430. PyNumber_Divmod(v, w)
  431.     PyObject *v, *w;
  432. {
  433.     PyObject *res;
  434.  
  435.     if (PyInstance_Check(v) || PyInstance_Check(w))
  436.         return PyInstance_DoBinOp(v, w, "__divmod__", "__rdivmod__",
  437.                      PyNumber_Divmod);
  438.     if (v->ob_type->tp_as_number == NULL ||
  439.                 w->ob_type->tp_as_number == NULL) {
  440.         PyErr_SetString(PyExc_TypeError,
  441.             "divmod() requires numeric or class instance arguments");
  442.         return NULL;
  443.     }
  444.     if (PyNumber_Coerce(&v, &w) != 0)
  445.         return NULL;
  446.     res = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
  447.     Py_DECREF(v);
  448.     Py_DECREF(w);
  449.     return res;
  450. }
  451.  
  452.  
  453. static PyObject *
  454. do_pow(v, w)
  455.     PyObject *v, *w;
  456. {
  457.     PyObject *res;
  458.     if (PyInstance_Check(v) || PyInstance_Check(w))
  459.         return PyInstance_DoBinOp(v, w, "__pow__", "__rpow__", do_pow);
  460.     if (v->ob_type->tp_as_number == NULL ||
  461.         w->ob_type->tp_as_number == NULL) {
  462.         PyErr_SetString(PyExc_TypeError,
  463.                 "pow() requires numeric arguments");
  464.         return NULL;
  465.     }
  466.     if (PyFloat_Check(w) && PyFloat_AsDouble(v) < 0.0) {
  467.         if (!PyErr_Occurred())
  468.             PyErr_SetString(PyExc_ValueError,
  469.                     "negative number to float power");
  470.         return NULL;
  471.     }
  472.     if (PyNumber_Coerce(&v, &w) != 0)
  473.         return NULL;
  474.     res = (*v->ob_type->tp_as_number->nb_power)(v, w, Py_None);
  475.     Py_DECREF(v);
  476.     Py_DECREF(w);
  477.     return res;
  478. }
  479.  
  480. PyObject *
  481. PyNumber_Power(v,w,z)
  482.     PyObject *v, *w, *z;
  483. {
  484.     PyObject *res;
  485.     PyObject *v1, *z1, *w2, *z2;
  486.  
  487.     if (z == Py_None)
  488.         return do_pow(v, w);
  489.     /* XXX The ternary version doesn't do class instance coercions */
  490.     if (PyInstance_Check(v))
  491.         return v->ob_type->tp_as_number->nb_power(v, w, z);
  492.     if (v->ob_type->tp_as_number == NULL ||
  493.         z->ob_type->tp_as_number == NULL ||
  494.         w->ob_type->tp_as_number == NULL) {
  495.         PyErr_SetString(PyExc_TypeError, "pow() requires numeric arguments");
  496.         return NULL;
  497.     }
  498.     if (PyNumber_Coerce(&v, &w) != 0)
  499.         return NULL;
  500.     res = NULL;
  501.     v1 = v;
  502.     z1 = z;
  503.     if (PyNumber_Coerce(&v1, &z1) != 0)
  504.         goto error2;
  505.     w2 = w;
  506.     z2 = z1;
  507.      if (PyNumber_Coerce(&w2, &z2) != 0)
  508.         goto error1;
  509.     res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2);
  510.     Py_DECREF(w2);
  511.     Py_DECREF(z2);
  512.  error1:
  513.     Py_DECREF(v1);
  514.     Py_DECREF(z1);
  515.  error2:
  516.     Py_DECREF(v);
  517.     Py_DECREF(w);
  518.     return res;
  519. }
  520.  
  521.  
  522. PyObject *
  523. PyNumber_Negative(v)
  524.     PyObject *v;
  525. {
  526.     if (v->ob_type->tp_as_number != NULL)
  527.         return (*v->ob_type->tp_as_number->nb_negative)(v);
  528.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary -");
  529.     return NULL;
  530. }
  531.  
  532. PyObject *
  533. PyNumber_Positive(v)
  534.     PyObject *v;
  535. {
  536.     if (v->ob_type->tp_as_number != NULL)
  537.         return (*v->ob_type->tp_as_number->nb_positive)(v);
  538.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary +");
  539.     return NULL;
  540. }
  541.  
  542. PyObject *
  543. PyNumber_Invert(v)
  544.     PyObject *v;
  545. {
  546.     PyObject * (*f) Py_FPROTO((PyObject *));
  547.     if (v->ob_type->tp_as_number != NULL &&
  548.         (f = v->ob_type->tp_as_number->nb_invert) != NULL)
  549.         return (*f)(v);
  550.     PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary ~");
  551.     return NULL;
  552. }
  553.  
  554. PyObject *
  555. PyNumber_Absolute(o)
  556.   PyObject *o;
  557. {
  558.   PyNumberMethods *m;
  559.  
  560.   if(! o) return Py_ReturnNullError();
  561.   if((m=o->ob_type->tp_as_number) && m->nb_absolute)
  562.     return m->nb_absolute(o);
  563.  
  564.   return Py_ReturnMethodError("__abs__");  
  565. }
  566.  
  567. PyObject *
  568. PyNumber_Int(o)
  569.   PyObject *o;
  570. {
  571.   PyNumberMethods *m;
  572.  
  573.   if(! o) return Py_ReturnNullError();
  574.   if((m=o->ob_type->tp_as_number) && m->nb_int)
  575.     return m->nb_int(o);
  576.  
  577.   return Py_ReturnMethodError("__int__");  
  578. }
  579.  
  580. PyObject *
  581. PyNumber_Long(o)
  582.   PyObject *o;
  583. {
  584.   PyNumberMethods *m;
  585.  
  586.   if(! o) return Py_ReturnNullError();
  587.   if((m=o->ob_type->tp_as_number) && m->nb_long)
  588.     return m->nb_long(o);
  589.  
  590.   return Py_ReturnMethodError("__long__");  
  591. }
  592.  
  593. PyObject *
  594. PyNumber_Float(o)
  595.   PyObject *o;
  596. {
  597.   PyNumberMethods *m;
  598.  
  599.   if(! o) return Py_ReturnNullError();
  600.   if((m=o->ob_type->tp_as_number) && m->nb_float)
  601.     return m->nb_float(o);
  602.  
  603.   return Py_ReturnMethodError("__float__");  
  604. }
  605.  
  606.  
  607. int 
  608. PySequence_Check(o)
  609.   PyObject *o;
  610. {
  611.   return o && o->ob_type->tp_as_sequence;
  612. }
  613.  
  614. int 
  615. PySequence_Length(s)
  616.   PyObject *s;
  617. {
  618.   PySequenceMethods *m;
  619.  
  620.   if(! s) return Py_ReturnNullError(),-1;
  621.  
  622.   if((m=s->ob_type->tp_as_sequence) && m->sq_length)
  623.     return m->sq_length(s);
  624.  
  625.   Py_ReturnMethodError("__len__");
  626.   return -1;
  627. }
  628.  
  629. PyObject *
  630. PySequence_Concat(s, o)
  631.   PyObject *s;
  632.   PyObject *o;
  633. {
  634.   PySequenceMethods *m;
  635.  
  636.   if(! s || ! o) return Py_ReturnNullError();
  637.       
  638.   if((m=s->ob_type->tp_as_sequence) && m->sq_concat)
  639.     return m->sq_concat(s,o);
  640.  
  641.   return Py_ReturnMethodError("__concat__");
  642. }
  643.  
  644. PyObject *
  645. PySequence_Repeat(o, count)
  646.   PyObject *o;
  647.   int count;
  648. {
  649.   PySequenceMethods *m;
  650.  
  651.   if(! o) return Py_ReturnNullError();
  652.       
  653.   if((m=o->ob_type->tp_as_sequence) && m->sq_repeat)
  654.     return m->sq_repeat(o,count);
  655.  
  656.   return Py_ReturnMethodError("__repeat__");
  657. }
  658.  
  659. PyObject *
  660. PySequence_GetItem(s, i)
  661.   PyObject *s;
  662.   int i;
  663. {
  664.   PySequenceMethods *m;
  665.   int l;
  666.  
  667.   if(! s) return Py_ReturnNullError();
  668.  
  669.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_item))
  670.     return Py_ReturnMethodError("__getitem__");  
  671.  
  672.   if(0 > (l=m->sq_length(s))) return NULL;
  673.  
  674.   if(i < 0) i += l;
  675.       
  676.   return m->sq_item(s,i);
  677. }
  678.  
  679. PyObject *
  680. PySequence_GetSlice(s, i1, i2)
  681.   PyObject *s;
  682.   int i1;
  683.   int i2;
  684. {
  685.   PySequenceMethods *m;
  686.   int l;
  687.  
  688.   if(! s) return Py_ReturnNullError();
  689.  
  690.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_slice))
  691.     return Py_ReturnMethodError("__getslice__");  
  692.  
  693.   if(0 > (l=m->sq_length(s))) return NULL;
  694.  
  695.   if(i1 < 0) i1 += l;
  696.   if(i2 < 0) i2 += l;
  697.       
  698.   return m->sq_slice(s,i1,i2);
  699. }
  700.  
  701. int
  702. PySequence_SetItem(s, i, o)
  703.   PyObject *s;
  704.   int i;
  705.   PyObject *o;
  706. {
  707.   PySequenceMethods *m;
  708.   int l;
  709.   if(! s) return Py_ReturnNullError(),-1;
  710.  
  711.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_item))
  712.     return Py_ReturnMethodError("__setitem__"),-1;  
  713.  
  714.   if(i < 0)
  715.     {
  716.       if(0 > (l=m->sq_length(s))) return -1;
  717.       i += l;
  718.     }
  719.       
  720.   return m->sq_ass_item(s,i,o);
  721. }
  722.  
  723. int
  724. PySequence_DelItem(s, i)
  725.   PyObject *s;
  726.   int i;
  727. {
  728.   PySequenceMethods *m;
  729.   int l;
  730.   if(! s) return Py_ReturnNullError(),-1;
  731.  
  732.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_item))
  733.     return Py_ReturnMethodError("__delitem__"),-1;  
  734.  
  735.   if(i < 0)
  736.     {
  737.       if(0 > (l=m->sq_length(s))) return -1;
  738.       i += l;
  739.     }
  740.       
  741.   return m->sq_ass_item(s,i,(PyObject*)NULL);
  742. }
  743.  
  744. int 
  745. PySequence_SetSlice(s, i1, i2, o)
  746.   PyObject *s;
  747.   int i1;
  748.   int i2;
  749.   PyObject *o;
  750. {
  751.   PySequenceMethods *m;
  752.   int l;
  753.  
  754.   if(! s) return Py_ReturnNullError(),-1;
  755.  
  756.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_slice))
  757.     return Py_ReturnMethodError("__setslice__"),-1;  
  758.  
  759.   if(0 > (l=m->sq_length(s))) return -1;
  760.  
  761.   if(i1 < 0) i1 += l;
  762.   if(i2 < 0) i2 += l;
  763.       
  764.   return m->sq_ass_slice(s,i1,i2,o);
  765. }
  766.  
  767. int 
  768. PySequence_DelSlice(s, i1, i2)
  769.   PyObject *s;
  770.   int i1;
  771.   int i2;
  772. {
  773.   PySequenceMethods *m;
  774.   int l;
  775.  
  776.   if(! s) return Py_ReturnNullError(),-1;
  777.  
  778.   if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_slice))
  779.     return Py_ReturnMethodError("__delslice__"),-1;  
  780.  
  781.   if(0 > (l=m->sq_length(s))) return -1;
  782.  
  783.   if(i1 < 0) i1 += l;
  784.   if(i2 < 0) i2 += l;
  785.       
  786.   return m->sq_ass_slice(s,i1,i2,(PyObject*)NULL);
  787. }
  788.  
  789. PyObject *
  790. PySequence_Tuple(s)
  791.   PyObject *s;
  792. {
  793.   int l, i;
  794.   PyObject *t, *item;
  795.  
  796.   if(! s) return Py_ReturnNullError();
  797.  
  798.   Py_TRY((l=PySequence_Length(s)) != -1);
  799.   Py_TRY(t=PyTuple_New(l));
  800.  
  801.   for(i=0; i < l; i++)
  802.     {
  803.       if(item=PySequence_GetItem(s,i))
  804.     {
  805.       if(PyTuple_SetItem(t,i,item) == -1)
  806.         {
  807.           Py_DECREF(item);
  808.           Py_DECREF(t);
  809.           return NULL;
  810.         }
  811.     }
  812.       else
  813.     {
  814.       Py_DECREF(t);
  815.       return NULL;
  816.     }
  817.     }
  818.   return t;
  819. }
  820.  
  821. int 
  822. PySequence_Count(s, o)
  823.   PyObject *s;
  824.   PyObject *o;
  825. {
  826.   int l, i, n=0, not_equal, err;
  827.   PyObject *item;
  828.  
  829.   if(! s || ! o) return Py_ReturnNullError(), -1;
  830.   Py_TRY((l=PySequence_Length(s)) != -1),-1;
  831.  
  832.   for(i=0; i < l; i++)
  833.     {
  834.       Py_TRY(item=PySequence_GetItem(s,i)),-1;
  835.       err=PyObject_Cmp(item,o,¬_equal) == -1;
  836.       Py_DECREF(item);
  837.       if(err) return -1;
  838.       n += ! not_equal;
  839.     }
  840.   return n;
  841. }
  842.  
  843. int 
  844. PySequence_In(s, o)
  845.   PyObject *s;
  846.   PyObject *o;
  847. {
  848.   int l, i, not_equal, err;
  849.   PyObject *item;
  850.  
  851.   if(! o || ! s) return Py_ReturnNullError(), -1;
  852.   Py_TRY((l=PySequence_Length(s)) != -1),-1;
  853.  
  854.   for(i=0; i < l; i++)
  855.     {
  856.       Py_TRY(item=PySequence_GetItem(s,i)),-1;
  857.       err=PyObject_Cmp(item,o,¬_equal) == -1;
  858.       Py_DECREF(item);
  859.       if(err) return -1;
  860.       if(! not_equal) return 1;
  861.     }
  862.   return 0;
  863. }
  864.  
  865. int 
  866. PySequence_Index(s, o)
  867.   PyObject *s;
  868.   PyObject *o;
  869. {
  870.   int l, i, n=0, not_equal, err;
  871.   PyObject *item;
  872.  
  873.   if(! s || ! o) return Py_ReturnNullError(), -1;
  874.   Py_TRY((l=PySequence_Length(s)) != -1),-1;
  875.  
  876.   for(i=0; i < l; i++)
  877.     {
  878.       Py_TRY(item=PySequence_GetItem(s,i)),-1;
  879.       err=PyObject_Cmp(item,o,¬_equal) == -1;
  880.       Py_DECREF(item);
  881.       if(err) return -1;
  882.       if(! not_equal) return n;
  883.     }
  884.   return -1;
  885. }
  886.  
  887. int 
  888. PyMapping_Check(o)
  889.   PyObject *o;
  890. {
  891.   return o && o->ob_type->tp_as_mapping;
  892. }
  893.  
  894. int 
  895. PyMapping_Length(s)
  896.   PyObject *s;
  897. {
  898.   PyMappingMethods *m;
  899.  
  900.   if(! s) return Py_ReturnNullError(),-1;
  901.  
  902.   if((m=s->ob_type->tp_as_mapping) && m->mp_length)
  903.     return m->mp_length(s);
  904.  
  905.   Py_ReturnMethodError("__len__");
  906.   return -1;
  907. }
  908.  
  909. int 
  910. PyMapping_HasKeyString(o, key)
  911.   PyObject *o;
  912.   char *key;
  913. {
  914.   PyObject *v;
  915.  
  916.   v=PyMapping_GetItemString(o,key);
  917.   if(v) return 1;
  918.   PyErr_Clear();
  919.   return 0;
  920. }
  921.  
  922. int 
  923. PyMapping_HasKey(o, key)
  924.   PyObject *o;
  925.   PyObject *key;
  926. {
  927.   PyObject *v;
  928.  
  929.   v=PyObject_GetItem(o,key);
  930.   if(v) return 1;
  931.   PyErr_Clear();
  932.   return 0;
  933. }
  934.  
  935. PyObject *
  936. PyObject_CallObject(o, a)
  937.   PyObject *o, *a;
  938. {
  939.   PyObject *r;
  940.  
  941.   if(a) return PyEval_CallObject(o,a);
  942.  
  943.   if(! (a=PyTuple_New(0)))
  944.     return NULL;
  945.   r=PyEval_CallObject(o,a);
  946.   Py_DECREF(a);
  947.   return r;
  948.  
  949. PyObject *
  950. #ifdef HAVE_STDARG_PROTOTYPES
  951. /* VARARGS 2 */
  952. PyObject_CallFunction(PyObject *callable, char *format, ...)
  953. #else
  954. /* VARARGS */
  955. PyObject_CallFunction(va_alist) va_dcl
  956. #endif
  957. {
  958.   va_list va;
  959.   PyObject *args, *retval;
  960. #ifdef HAVE_STDARG_PROTOTYPES
  961.   va_start(va, format);
  962. #else
  963.   PyObject *callable;
  964.   char *format;
  965.   va_start(va);
  966.   callable = va_arg(va, PyObject *);
  967.   format   = va_arg(va, char *);
  968. #endif
  969.  
  970.   if( ! callable)
  971.     {
  972.       va_end(va);
  973.       return Py_ReturnNullError();
  974.     }
  975.  
  976.   if(format)
  977.     args = Py_VaBuildValue(format, va);
  978.   else
  979.     args = PyTuple_New(0);
  980.   
  981.   va_end(va);
  982.   if(! args) return NULL;
  983.  
  984.   if(! PyTuple_Check(args))
  985.     {
  986.       PyObject *a;
  987.       
  988.       Py_TRY(a=PyTuple_New(1));
  989.       Py_TRY(PyTuple_SetItem(a,0,args) != -1);
  990.       args=a;
  991.     }
  992.   retval = PyObject_CallObject(callable,args);
  993.   Py_DECREF(args);
  994.   return retval;
  995. }
  996.  
  997. PyObject *
  998. #ifdef HAVE_STDARG_PROTOTYPES
  999. /* VARARGS 2 */
  1000. PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
  1001. #else
  1002. /* VARARGS */
  1003. PyObject_CallMethod(va_alist) va_dcl
  1004. #endif
  1005. {
  1006.   va_list va;
  1007.   PyObject *args, *method=0, *retval;
  1008. #ifdef HAVE_STDARG_PROTOTYPES
  1009.   va_start(va, format);
  1010. #else
  1011.   PyObject *o;
  1012.   char *name;
  1013.   char *format;
  1014.   va_start(va);
  1015.   o      = va_arg(va, PyObject *);
  1016.   name   = va_arg(va, char *);
  1017.   format = va_arg(va, char *);
  1018. #endif
  1019.  
  1020.   if( ! o || ! name)
  1021.     {
  1022.       va_end(va);
  1023.       return Py_ReturnNullError();
  1024.     }
  1025.  
  1026.   method=PyObject_GetAttrString(o,name);
  1027.   if(! method)
  1028.     {
  1029.       va_end(va);
  1030.       PyErr_SetString(PyExc_AttributeError,name);
  1031.       return 0;
  1032.     }
  1033.    
  1034.   if(! (PyCallable_Check(method)))
  1035.     {
  1036.       va_end(va);
  1037.       PyErr_SetString(PyExc_TypeError,"call of non-callable attribute");
  1038.       return 0;
  1039.     }
  1040.  
  1041.   if(format && *format)
  1042.     args = Py_VaBuildValue(format, va);
  1043.   else
  1044.     args = PyTuple_New(0);
  1045.   
  1046.   va_end(va);
  1047.  
  1048.   if(! args) return NULL;
  1049.  
  1050.   if(! PyTuple_Check(args))
  1051.     {
  1052.       PyObject *a;
  1053.       
  1054.       Py_TRY(a=PyTuple_New(1));
  1055.       Py_TRY(PyTuple_SetItem(a,0,args) != -1);
  1056.       args=a;
  1057.     }
  1058.  
  1059.   retval = PyObject_CallObject(method,args);
  1060.   Py_DECREF(args);
  1061.   Py_DECREF(method);
  1062.   return retval;
  1063. }
  1064.  
  1065. PyObject *
  1066. PyMapping_GetItemString(o, key)
  1067.   PyObject *o;
  1068.   char *key;
  1069. {
  1070.   PyObject *okey, *r;
  1071.  
  1072.   if( ! key) return Py_ReturnNullError();
  1073.   Py_TRY(okey=PyString_FromString(key));
  1074.   r = PyObject_GetItem(o,okey);
  1075.   Py_DECREF(okey);
  1076.   return r;
  1077. }
  1078.  
  1079. int
  1080. PyMapping_SetItemString(o, key, value)
  1081.  PyObject *o;
  1082.  char *key;
  1083.  PyObject *value;
  1084. {
  1085.   PyObject *okey;
  1086.   int r;
  1087.  
  1088.   if( ! key) return Py_ReturnNullError(),-1;
  1089.   if (!(okey=PyString_FromString(key))) return -1;
  1090.   r = PyObject_SetItem(o,okey,value);
  1091.   Py_DECREF(okey);
  1092.   return r;
  1093. }
  1094.