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 / object.c < prev    next >
C/C++ Source or Header  |  2000-12-21  |  19KB  |  891 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. /* Generic object operations; and implementation of None (NoObject) */
  33.  
  34. #include "Python.h"
  35. #include "other/object_c.h"
  36.  
  37. #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
  38. DL_IMPORT(long) _Py_RefTotal;
  39. #endif
  40.  
  41. /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
  42.    These are used by the individual routines for object creation.
  43.    Do not call them otherwise, they do not initialize the object! */
  44.  
  45. #ifdef COUNT_ALLOCS
  46. static PyTypeObject *type_list;
  47. extern int tuple_zero_allocs, fast_tuple_allocs;
  48. extern int quick_int_allocs, quick_neg_int_allocs;
  49. extern int null_strings, one_strings;
  50. void
  51. dump_counts()
  52. {
  53.     PyTypeObject *tp;
  54.  
  55.     for (tp = type_list; tp; tp = tp->tp_next)
  56.         fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
  57.             tp->tp_name, tp->tp_alloc, tp->tp_free,
  58.             tp->tp_maxalloc);
  59.     fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
  60.         fast_tuple_allocs, tuple_zero_allocs);
  61.     fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
  62.         quick_int_allocs, quick_neg_int_allocs);
  63.     fprintf(stderr, "null strings: %d, 1-strings: %d\n",
  64.         null_strings, one_strings);
  65. }
  66.  
  67. PyObject *
  68. get_counts()
  69. {
  70.     PyTypeObject *tp;
  71.     PyObject *result;
  72.     PyObject *v;
  73.  
  74.     result = PyList_New(0);
  75.     if (result == NULL)
  76.         return NULL;
  77.     for (tp = type_list; tp; tp = tp->tp_next) {
  78.         v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
  79.                   tp->tp_free, tp->tp_maxalloc);
  80.         if (v == NULL) {
  81.             Py_DECREF(result);
  82.             return NULL;
  83.         }
  84.         if (PyList_Append(result, v) < 0) {
  85.             Py_DECREF(v);
  86.             Py_DECREF(result);
  87.             return NULL;
  88.         }
  89.         Py_DECREF(v);
  90.     }
  91.     return result;
  92. }
  93.  
  94. void
  95. inc_count(tp)
  96.     PyTypeObject *tp;
  97. {
  98.     if (tp->tp_alloc == 0) {
  99.         /* first time; insert in linked list */
  100.         if (tp->tp_next != NULL) /* sanity check */
  101.             Py_FatalError("XXX inc_count sanity check");
  102.         tp->tp_next = type_list;
  103.         type_list = tp;
  104.     }
  105.     tp->tp_alloc++;
  106.     if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
  107.         tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
  108. }
  109. #endif
  110.  
  111. #ifndef MS_COREDLL
  112. PyObject *
  113. _PyObject_New(tp)
  114.     PyTypeObject *tp;
  115. #else
  116. PyObject *
  117. _PyObject_New(tp,op)
  118.     PyTypeObject *tp;
  119.     PyObject *op;
  120. #endif
  121. {
  122. #ifndef MS_COREDLL
  123.     PyObject *op = (PyObject *) malloc(tp->tp_basicsize);
  124. #endif
  125.     if (op == NULL)
  126.         return PyErr_NoMemory();
  127.     op->ob_type = tp;
  128.     _Py_NewReference(op);
  129.     return op;
  130. }
  131.  
  132. #ifndef MS_COREDLL
  133. PyVarObject *
  134. _PyObject_NewVar(tp, size)
  135.     PyTypeObject *tp;
  136.     int size;
  137. #else
  138. PyVarObject *
  139. _PyObject_NewVar(tp, size, op)
  140.     PyTypeObject *tp;
  141.     int size;
  142.     PyVarObject *op;
  143. #endif
  144. {
  145. #ifndef MS_COREDLL
  146.     PyVarObject *op = (PyVarObject *)
  147.         malloc(tp->tp_basicsize + size * tp->tp_itemsize);
  148. #endif
  149.     if (op == NULL)
  150.         return (PyVarObject *)PyErr_NoMemory();
  151.     op->ob_type = tp;
  152.     op->ob_size = size;
  153.     _Py_NewReference((PyObject *)op);
  154.     return op;
  155. }
  156.  
  157. int
  158. PyObject_Print(op, fp, flags)
  159.     PyObject *op;
  160.     FILE *fp;
  161.     int flags;
  162. {
  163.     int ret = 0;
  164.     if (PyErr_CheckSignals())
  165.         return -1;
  166. #ifdef USE_STACKCHECK
  167.     if (PyOS_CheckStack()) {
  168.         PyErr_SetString(PyExc_MemoryError, "Stack overflow");
  169.         return -1;
  170.     }
  171. #endif
  172.     clearerr(fp); /* Clear any previous error condition */
  173.     if (op == NULL) {
  174.         fprintf(fp, "<nil>");
  175.     }
  176.     else {
  177.         if (op->ob_refcnt <= 0)
  178.             fprintf(fp, "<refcnt %u at %lx>",
  179.                 op->ob_refcnt, (long)op);
  180.         else if (op->ob_type->tp_print == NULL) {
  181.             if (op->ob_type->tp_repr == NULL) {
  182.                 fprintf(fp, "<%s object at %lx>",
  183.                     op->ob_type->tp_name, (long)op);
  184.             }
  185.             else {
  186.                 PyObject *s;
  187.                 if (flags & Py_PRINT_RAW)
  188.                     s = PyObject_Str(op);
  189.                 else
  190.                     s = PyObject_Repr(op);
  191.                 if (s == NULL)
  192.                     ret = -1;
  193.                 else if (!PyString_Check(s)) {
  194.                     PyErr_SetString(PyExc_TypeError,
  195.                            "repr not string");
  196.                     ret = -1;
  197.                 }
  198.                 else {
  199.                     ret = PyObject_Print(s, fp,
  200.                                  Py_PRINT_RAW);
  201.                 }
  202.                 Py_XDECREF(s);
  203.             }
  204.         }
  205.         else
  206.             ret = (*op->ob_type->tp_print)(op, fp, flags);
  207.     }
  208.     if (ret == 0) {
  209.         if (ferror(fp)) {
  210.             PyErr_SetFromErrno(PyExc_IOError);
  211.             clearerr(fp);
  212.             ret = -1;
  213.         }
  214.     }
  215.     return ret;
  216. }
  217.  
  218. PyObject *
  219. PyObject_Repr(v)
  220.     PyObject *v;
  221. {
  222.     if (PyErr_CheckSignals())
  223.         return NULL;
  224. #ifdef USE_STACKCHECK
  225.     if (PyOS_CheckStack()) {
  226.         PyErr_SetString(PyExc_MemoryError, "Stack overflow");
  227.         return NULL;
  228.     }
  229. #endif
  230.     if (v == NULL)
  231.         return PyString_FromString("<NULL>");
  232.     else if (v->ob_type->tp_repr == NULL) {
  233.         char buf[120];
  234.         sprintf(buf, "<%.80s object at %lx>",
  235.             v->ob_type->tp_name, (long)v);
  236.         return PyString_FromString(buf);
  237.     }
  238.     else
  239.         return (*v->ob_type->tp_repr)(v);
  240. }
  241.  
  242. PyObject *
  243. PyObject_Str(v)
  244.     PyObject *v;
  245. {
  246.     if (v == NULL)
  247.         return PyString_FromString("<NULL>");
  248.     else if (PyString_Check(v)) {
  249.         Py_INCREF(v);
  250.         return v;
  251.     }
  252.     else if (v->ob_type->tp_str != NULL)
  253.         return (*v->ob_type->tp_str)(v);
  254.     else {
  255.         PyObject *func;
  256.         PyObject *res;
  257.         if (!PyInstance_Check(v) ||
  258.             (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
  259.             PyErr_Clear();
  260.             return PyObject_Repr(v);
  261.         }
  262.         res = PyEval_CallObject(func, (PyObject *)NULL);
  263.         Py_DECREF(func);
  264.         return res;
  265.     }
  266. }
  267.  
  268. static PyObject *
  269. do_cmp(v, w)
  270.     PyObject *v, *w;
  271. {
  272.     long c;
  273.     /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
  274.        because the check in cmpobject() reverses the objects first.
  275.        This is intentional -- it makes no sense to define cmp(x,y)
  276.        different than -cmp(y,x). */
  277.     if (PyInstance_Check(v) || PyInstance_Check(w))
  278.         return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
  279.     c = PyObject_Compare(v, w);
  280.     if (c && PyErr_Occurred())
  281.         return NULL;
  282.     return PyInt_FromLong(c);
  283. }
  284.  
  285. int
  286. PyObject_Compare(v, w)
  287.     PyObject *v, *w;
  288. {
  289.     PyTypeObject *vtp, *wtp;
  290.     if (v == NULL || w == NULL) {
  291.         PyErr_BadInternalCall();
  292.         return -1;
  293.     }
  294.     if (v == w)
  295.         return 0;
  296.     if (PyInstance_Check(v) || PyInstance_Check(w)) {
  297.         PyObject *res;
  298.         int c;
  299.         if (!PyInstance_Check(v))
  300.             return -PyObject_Compare(w, v);
  301.         res = do_cmp(v, w);
  302.         if (res == NULL)
  303.             return -1;
  304.         if (!PyInt_Check(res)) {
  305.             Py_DECREF(res);
  306.             PyErr_SetString(PyExc_TypeError,
  307.                     "comparison did not return an int");
  308.             return -1;
  309.         }
  310.         c = PyInt_AsLong(res);
  311.         Py_DECREF(res);
  312.         return (c < 0) ? -1 : (c > 0) ? 1 : 0;    
  313.     }
  314.     if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
  315.         char *vname = vtp->tp_name;
  316.         char *wname = wtp->tp_name;
  317.         if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
  318.             int err;
  319.             err = PyNumber_CoerceEx(&v, &w);
  320.             if (err < 0)
  321.                 return -1;
  322.             else if (err == 0) {
  323.                 int cmp;
  324.                 vtp = v->ob_type;
  325.                 if (vtp->tp_compare == NULL)
  326.                     cmp = (v < w) ? -1 : 1;
  327.                 else
  328.                     cmp = (*vtp->tp_compare)(v, w);
  329.                 Py_DECREF(v);
  330.                 Py_DECREF(w);
  331.                 return cmp;
  332.             }
  333.         }
  334.         else if (vtp->tp_as_number != NULL)
  335.             vname = "";
  336.         else if (wtp->tp_as_number != NULL)
  337.             wname = "";
  338.         /* Numerical types compare smaller than all other types */
  339.         return strcmp(vname, wname);
  340.     }
  341.     if (vtp->tp_compare == NULL)
  342.         return (v < w) ? -1 : 1;
  343.     return (*vtp->tp_compare)(v, w);
  344. }
  345.  
  346. long
  347. PyObject_Hash(v)
  348.     PyObject *v;
  349. {
  350.     PyTypeObject *tp = v->ob_type;
  351.     if (tp->tp_hash != NULL)
  352.         return (*tp->tp_hash)(v);
  353.     if (tp->tp_compare == NULL)
  354.         return (long) v; /* Use address as hash value */
  355.     /* If there's a cmp but no hash defined, the object can't be hashed */
  356.     PyErr_SetString(PyExc_TypeError, "unhashable type");
  357.     return -1;
  358. }
  359.  
  360. PyObject *
  361. PyObject_GetAttrString(v, name)
  362.     PyObject *v;
  363.     char *name;
  364. {
  365.     if (v->ob_type->tp_getattro != NULL) {
  366.         PyObject *w, *res;
  367.         w = PyString_InternFromString(name);
  368.         if (w == NULL)
  369.             return NULL;
  370.         res = (*v->ob_type->tp_getattro)(v, w);
  371.         Py_XDECREF(w);
  372.         return res;
  373.     }
  374.  
  375.     if (v->ob_type->tp_getattr == NULL) {
  376.         PyErr_Format(PyExc_AttributeError,
  377.                  "'%.50s' object has no attribute '%.400s'",
  378.                  v->ob_type->tp_name,
  379.                  name);
  380.         return NULL;
  381.     }
  382.     else {
  383.         return (*v->ob_type->tp_getattr)(v, name);
  384.     }
  385. }
  386.  
  387. int
  388. PyObject_HasAttrString(v, name)
  389.     PyObject *v;
  390.     char *name;
  391. {
  392.     PyObject *res = PyObject_GetAttrString(v, name);
  393.     if (res != NULL) {
  394.         Py_DECREF(res);
  395.         return 1;
  396.     }
  397.     PyErr_Clear();
  398.     return 0;
  399. }
  400.  
  401. int
  402. PyObject_SetAttrString(v, name, w)
  403.     PyObject *v;
  404.     char *name;
  405.     PyObject *w;
  406. {
  407.     if (v->ob_type->tp_setattro != NULL) {
  408.         PyObject *s;
  409.         int res;
  410.         s = PyString_InternFromString(name);
  411.         if (s == NULL)
  412.             return -1;
  413.         res = (*v->ob_type->tp_setattro)(v, s, w);
  414.         Py_XDECREF(s);
  415.         return res;
  416.     }
  417.  
  418.     if (v->ob_type->tp_setattr == NULL) {
  419.         if (v->ob_type->tp_getattr == NULL)
  420.             PyErr_SetString(PyExc_TypeError,
  421.                    "attribute-less object (assign or del)");
  422.         else
  423.             PyErr_SetString(PyExc_TypeError,
  424.                    "object has read-only attributes");
  425.         return -1;
  426.     }
  427.     else {
  428.         return (*v->ob_type->tp_setattr)(v, name, w);
  429.     }
  430. }
  431.  
  432. PyObject *
  433. PyObject_GetAttr(v, name)
  434.     PyObject *v;
  435.     PyObject *name;
  436. {
  437.     if (v->ob_type->tp_getattro != NULL)
  438.         return (*v->ob_type->tp_getattro)(v, name);
  439.     else
  440.         return PyObject_GetAttrString(v, PyString_AsString(name));
  441. }
  442.  
  443. int
  444. PyObject_HasAttr(v, name)
  445.     PyObject *v;
  446.     PyObject *name;
  447. {
  448.     PyObject *res = PyObject_GetAttr(v, name);
  449.     if (res != NULL) {
  450.         Py_DECREF(res);
  451.         return 1;
  452.     }
  453.     PyErr_Clear();
  454.     return 0;
  455. }
  456.  
  457. int
  458. PyObject_SetAttr(v, name, value)
  459.     PyObject *v;
  460.     PyObject *name;
  461.     PyObject *value;
  462. {
  463.     int err;
  464.     Py_INCREF(name);
  465.     PyString_InternInPlace(&name);
  466.     if (v->ob_type->tp_setattro != NULL)
  467.         err = (*v->ob_type->tp_setattro)(v, name, value);
  468.     else
  469.         err = PyObject_SetAttrString(
  470.             v, PyString_AsString(name), value);
  471.     Py_DECREF(name);
  472.     return err;
  473. }
  474.  
  475. /* Test a value used as condition, e.g., in a for or if statement.
  476.    Return -1 if an error occurred */
  477.  
  478. int
  479. PyObject_IsTrue(v)
  480.     PyObject *v;
  481. {
  482.     int res;
  483.     if (v == Py_None)
  484.         res = 0;
  485.     else if (v->ob_type->tp_as_number != NULL &&
  486.          v->ob_type->tp_as_number->nb_nonzero != NULL)
  487.         res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
  488.     else if (v->ob_type->tp_as_mapping != NULL &&
  489.          v->ob_type->tp_as_mapping->mp_length != NULL)
  490.         res = (*v->ob_type->tp_as_mapping->mp_length)(v);
  491.     else if (v->ob_type->tp_as_sequence != NULL &&
  492.          v->ob_type->tp_as_sequence->sq_length != NULL)
  493.         res = (*v->ob_type->tp_as_sequence->sq_length)(v);
  494.     else
  495.         res = 1;
  496.     if (res > 0)
  497.         res = 1;
  498.     return res;
  499. }
  500.  
  501. /* equivalent of 'not v' 
  502.    Return -1 if an error occurred */
  503.  
  504. int
  505. PyObject_Not(v)
  506.     PyObject *v;
  507. {
  508.     int res;
  509.     res = PyObject_IsTrue(v);
  510.     if (res < 0)
  511.         return res;
  512.     return res == 0;
  513. }
  514.  
  515. /* Coerce two numeric types to the "larger" one.
  516.    Increment the reference count on each argument.
  517.    Return -1 and raise an exception if no coercion is possible
  518.    (and then no reference count is incremented).
  519. */
  520.  
  521. int
  522. PyNumber_CoerceEx(pv, pw)
  523.     PyObject **pv, **pw;
  524. {
  525.     register PyObject *v = *pv;
  526.     register PyObject *w = *pw;
  527.     int res;
  528.  
  529.     if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
  530.         Py_INCREF(v);
  531.         Py_INCREF(w);
  532.         return 0;
  533.     }
  534.     if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
  535.         res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
  536.         if (res <= 0)
  537.             return res;
  538.     }
  539.     if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
  540.         res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
  541.         if (res <= 0)
  542.             return res;
  543.     }
  544.     return 1;
  545. }
  546.  
  547. int
  548. PyNumber_Coerce(pv, pw)
  549.     PyObject **pv, **pw;
  550. {
  551.     int err = PyNumber_CoerceEx(pv, pw);
  552.     if (err <= 0)
  553.         return err;
  554.     PyErr_SetString(PyExc_TypeError, "number coercion failed");
  555.     return -1;
  556. }
  557.  
  558.  
  559. /* Test whether an object can be called */
  560.  
  561. int
  562. PyCallable_Check(x)
  563.     PyObject *x;
  564. {
  565.     if (x == NULL)
  566.         return 0;
  567.     if (x->ob_type->tp_call != NULL ||
  568.         PyFunction_Check(x) ||
  569.         PyMethod_Check(x) ||
  570.         PyCFunction_Check(x) ||
  571.         PyClass_Check(x))
  572.         return 1;
  573.     if (PyInstance_Check(x)) {
  574.         PyObject *call = PyObject_GetAttrString(x, "__call__");
  575.         if (call == NULL) {
  576.             PyErr_Clear();
  577.             return 0;
  578.         }
  579.         /* Could test recursively but don't, for fear of endless
  580.            recursion if some joker sets self.__call__ = self */
  581.         Py_DECREF(call);
  582.         return 1;
  583.     }
  584.     return 0;
  585. }
  586.  
  587.  
  588. /*
  589. NoObject is usable as a non-NULL undefined value, used by the macro None.
  590. There is (and should be!) no way to create other objects of this type,
  591. so there is exactly one (which is indestructible, by the way).
  592. */
  593.  
  594. /* ARGSUSED */
  595. static PyObject *
  596. none_repr(op)
  597.     PyObject *op;
  598. {
  599.     return PyString_FromString("None");
  600. }
  601.  
  602. static PyTypeObject PyNothing_Type = {
  603.     PyObject_HEAD_INIT(&PyType_Type)
  604.     0,
  605.     "None",
  606.     0,
  607.     0,
  608.     0,        /*tp_dealloc*/ /*never called*/
  609.     0,        /*tp_print*/
  610.     0,        /*tp_getattr*/
  611.     0,        /*tp_setattr*/
  612.     0,        /*tp_compare*/
  613.     (reprfunc)none_repr, /*tp_repr*/
  614.     0,        /*tp_as_number*/
  615.     0,        /*tp_as_sequence*/
  616.     0,        /*tp_as_mapping*/
  617.     0,        /*tp_hash */
  618. };
  619.  
  620. PyObject _Py_NoneStruct = {
  621.     PyObject_HEAD_INIT(&PyNothing_Type)
  622. };
  623.  
  624.  
  625. #ifdef Py_TRACE_REFS
  626.  
  627. static PyObject refchain = {&refchain, &refchain};
  628.  
  629. void
  630. _Py_ResetReferences()
  631. {
  632.     refchain._ob_prev = refchain._ob_next = &refchain;
  633.     _Py_RefTotal = 0;
  634. }
  635.  
  636. void
  637. _Py_NewReference(op)
  638.     PyObject *op;
  639. {
  640.     _Py_RefTotal++;
  641.     op->ob_refcnt = 1;
  642.     op->_ob_next = refchain._ob_next;
  643.     op->_ob_prev = &refchain;
  644.     refchain._ob_next->_ob_prev = op;
  645.     refchain._ob_next = op;
  646. #ifdef COUNT_ALLOCS
  647.     inc_count(op->ob_type);
  648. #endif
  649. }
  650.  
  651. void
  652. _Py_ForgetReference(op)
  653.     register PyObject *op;
  654. {
  655. #ifdef SLOW_UNREF_CHECK
  656.     register PyObject *p;
  657. #endif
  658.     if (op->ob_refcnt < 0)
  659.         Py_FatalError("UNREF negative refcnt");
  660.     if (op == &refchain ||
  661.         op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
  662.         Py_FatalError("UNREF invalid object");
  663. #ifdef SLOW_UNREF_CHECK
  664.     for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
  665.         if (p == op)
  666.             break;
  667.     }
  668.     if (p == &refchain) /* Not found */
  669.         Py_FatalError("UNREF unknown object");
  670. #endif
  671.     op->_ob_next->_ob_prev = op->_ob_prev;
  672.     op->_ob_prev->_ob_next = op->_ob_next;
  673.     op->_ob_next = op->_ob_prev = NULL;
  674. #ifdef COUNT_ALLOCS
  675.     op->ob_type->tp_free++;
  676. #endif
  677. }
  678.  
  679. void
  680. _Py_Dealloc(op)
  681.     PyObject *op;
  682. {
  683.     destructor dealloc = op->ob_type->tp_dealloc;
  684.     _Py_ForgetReference(op);
  685.     op->ob_type = NULL;
  686.     (*dealloc)(op);
  687. }
  688.  
  689. void
  690. _Py_PrintReferences(fp)
  691.     FILE *fp;
  692. {
  693.     PyObject *op;
  694.     fprintf(fp, "Remaining objects:\n");
  695.     for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
  696.         fprintf(fp, "[%d] ", op->ob_refcnt);
  697.         if (PyObject_Print(op, fp, 0) != 0)
  698.             PyErr_Clear();
  699.         putc('\n', fp);
  700.     }
  701. }
  702.  
  703. PyObject *
  704. _Py_GetObjects(self, args)
  705.     PyObject *self;
  706.     PyObject *args;
  707. {
  708.     int i, n;
  709.     PyObject *t = NULL;
  710.     PyObject *res, *op;
  711.  
  712.     if (!PyArg_ParseTuple(args, "i|O", &n, &t))
  713.         return NULL;
  714.     op = refchain._ob_next;
  715.     res = PyList_New(0);
  716.     if (res == NULL)
  717.         return NULL;
  718.     for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
  719.         while (op == self || op == args || op == res || op == t ||
  720.                t != NULL && op->ob_type != (PyTypeObject *) t) {
  721.             op = op->_ob_next;
  722.             if (op == &refchain)
  723.                 return res;
  724.         }
  725.         if (PyList_Append(res, op) < 0) {
  726.             Py_DECREF(res);
  727.             return NULL;
  728.         }
  729.         op = op->_ob_next;
  730.     }
  731.     return res;
  732. }
  733.  
  734. #endif
  735.  
  736.  
  737. /* Hack to force loading of cobject.o */
  738. PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
  739.  
  740.  
  741. /* Hack to force loading of abstract.o */
  742. int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
  743.  
  744.  
  745. /* Malloc wrappers (see mymalloc.h) */
  746.  
  747. /* The Py_{Malloc,Realloc} wrappers call PyErr_NoMemory() on failure */
  748.  
  749. ANY *
  750. Py_Malloc(nbytes)
  751.     size_t nbytes;
  752. {
  753.     ANY *p;
  754. #if _PyMem_EXTRA > 0
  755.     if (nbytes == 0)
  756.         nbytes = _PyMem_EXTRA;
  757. #endif
  758.     p = malloc(nbytes);
  759.     if (p != NULL)
  760.         return p;
  761.     else {
  762.         PyErr_NoMemory();
  763.         return NULL;
  764.     }
  765. }
  766.  
  767. ANY *
  768. Py_Realloc(p, nbytes)
  769.     ANY *p;
  770.     size_t nbytes;
  771. {
  772. #if _PyMem_EXTRA > 0
  773.     if (nbytes == 0)
  774.         nbytes = _PyMem_EXTRA;
  775. #endif
  776.     p = realloc(p, nbytes);
  777.     if (p != NULL)
  778.         return p;
  779.     else {
  780.         PyErr_NoMemory();
  781.         return NULL;
  782.     }
  783. }
  784.  
  785. void
  786. Py_Free(p)
  787.     ANY *p;
  788. {
  789.     free(p);
  790. }
  791.  
  792. /* The PyMem_{Malloc,Realloc} wrappers don't call anything on failure */
  793.  
  794. ANY *
  795. PyMem_Malloc(nbytes)
  796.     size_t nbytes;
  797. {
  798. #if _PyMem_EXTRA > 0
  799.     if (nbytes == 0)
  800.         nbytes = _PyMem_EXTRA;
  801. #endif
  802.     return malloc(nbytes);
  803. }
  804.  
  805. ANY *
  806. PyMem_Realloc(p, nbytes)
  807.     ANY *p;
  808.     size_t nbytes;
  809. {
  810. #if _PyMem_EXTRA > 0
  811.     if (nbytes == 0)
  812.         nbytes = _PyMem_EXTRA;
  813. #endif
  814.     return realloc(p, nbytes);
  815. }
  816.  
  817. void
  818. PyMem_Free(p)
  819.     ANY *p;
  820. {
  821.     free(p);
  822. }
  823.  
  824.  
  825. /* These methods are used to control infinite recursion in repr, str, print,
  826.    etc.  Container objects that may recursively contain themselves,
  827.    e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
  828.    Py_ReprLeave() to avoid infinite recursion.
  829.  
  830.    Py_ReprEnter() returns 0 the first time it is called for a particular
  831.    object and 1 every time thereafter.  It returns -1 if an exception
  832.    occurred.  Py_ReprLeave() has no return value.
  833.  
  834.    See dictobject.c and listobject.c for examples of use.
  835. */
  836.  
  837. #define KEY "Py_Repr"
  838.  
  839. int
  840. Py_ReprEnter(obj)
  841.     PyObject *obj;
  842. {
  843.     PyObject *dict;
  844.     PyObject *list;
  845.     int i;
  846.  
  847.     dict = PyThreadState_GetDict();
  848.     if (dict == NULL)
  849.         return -1;
  850.     list = PyDict_GetItemString(dict, KEY);
  851.     if (list == NULL) {
  852.         list = PyList_New(0);
  853.         if (list == NULL)
  854.             return -1;
  855.         if (PyDict_SetItemString(dict, KEY, list) < 0)
  856.             return -1;
  857.         Py_DECREF(list);
  858.     }
  859.     i = PyList_GET_SIZE(list);
  860.     while (--i >= 0) {
  861.         if (PyList_GET_ITEM(list, i) == obj)
  862.             return 1;
  863.     }
  864.     PyList_Append(list, obj);
  865.     return 0;
  866. }
  867.  
  868. void
  869. Py_ReprLeave(obj)
  870.     PyObject *obj;
  871. {
  872.     PyObject *dict;
  873.     PyObject *list;
  874.     int i;
  875.  
  876.     dict = PyThreadState_GetDict();
  877.     if (dict == NULL)
  878.         return;
  879.     list = PyDict_GetItemString(dict, KEY);
  880.     if (list == NULL || !PyList_Check(list))
  881.         return;
  882.     i = PyList_GET_SIZE(list);
  883.     /* Count backwards because we always expect obj to be list[-1] */
  884.     while (--i >= 0) {
  885.         if (PyList_GET_ITEM(list, i) == obj) {
  886.             PyList_SetSlice(list, i, i + 1, NULL);
  887.             break;
  888.         }
  889.     }
  890. }
  891.