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