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