Home | History | Annotate | Download | only in Objects
      1 
      2 /* Generic object operations; and implementation of None */
      3 
      4 #include "Python.h"
      5 #include "frameobject.h"
      6 
      7 #ifdef __cplusplus
      8 extern "C" {
      9 #endif
     10 
     11 _Py_IDENTIFIER(Py_Repr);
     12 _Py_IDENTIFIER(__bytes__);
     13 _Py_IDENTIFIER(__dir__);
     14 _Py_IDENTIFIER(__isabstractmethod__);
     15 _Py_IDENTIFIER(builtins);
     16 
     17 #ifdef Py_REF_DEBUG
     18 Py_ssize_t _Py_RefTotal;
     19 
     20 Py_ssize_t
     21 _Py_GetRefTotal(void)
     22 {
     23     PyObject *o;
     24     Py_ssize_t total = _Py_RefTotal;
     25     o = _PySet_Dummy;
     26     if (o != NULL)
     27         total -= o->ob_refcnt;
     28     return total;
     29 }
     30 
     31 void
     32 _PyDebug_PrintTotalRefs(void) {
     33     PyObject *xoptions, *value;
     34     _Py_IDENTIFIER(showrefcount);
     35 
     36     xoptions = PySys_GetXOptions();
     37     if (xoptions == NULL)
     38         return;
     39     value = _PyDict_GetItemId(xoptions, &PyId_showrefcount);
     40     if (value == Py_True)
     41         fprintf(stderr,
     42                 "[%" PY_FORMAT_SIZE_T "d refs, "
     43                 "%" PY_FORMAT_SIZE_T "d blocks]\n",
     44                 _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
     45 }
     46 #endif /* Py_REF_DEBUG */
     47 
     48 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
     49    These are used by the individual routines for object creation.
     50    Do not call them otherwise, they do not initialize the object! */
     51 
     52 #ifdef Py_TRACE_REFS
     53 /* Head of circular doubly-linked list of all objects.  These are linked
     54  * together via the _ob_prev and _ob_next members of a PyObject, which
     55  * exist only in a Py_TRACE_REFS build.
     56  */
     57 static PyObject refchain = {&refchain, &refchain};
     58 
     59 /* Insert op at the front of the list of all objects.  If force is true,
     60  * op is added even if _ob_prev and _ob_next are non-NULL already.  If
     61  * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
     62  * force should be true if and only if op points to freshly allocated,
     63  * uninitialized memory, or you've unlinked op from the list and are
     64  * relinking it into the front.
     65  * Note that objects are normally added to the list via _Py_NewReference,
     66  * which is called by PyObject_Init.  Not all objects are initialized that
     67  * way, though; exceptions include statically allocated type objects, and
     68  * statically allocated singletons (like Py_True and Py_None).
     69  */
     70 void
     71 _Py_AddToAllObjects(PyObject *op, int force)
     72 {
     73 #ifdef  Py_DEBUG
     74     if (!force) {
     75         /* If it's initialized memory, op must be in or out of
     76          * the list unambiguously.
     77          */
     78         assert((op->_ob_prev == NULL) == (op->_ob_next == NULL));
     79     }
     80 #endif
     81     if (force || op->_ob_prev == NULL) {
     82         op->_ob_next = refchain._ob_next;
     83         op->_ob_prev = &refchain;
     84         refchain._ob_next->_ob_prev = op;
     85         refchain._ob_next = op;
     86     }
     87 }
     88 #endif  /* Py_TRACE_REFS */
     89 
     90 #ifdef COUNT_ALLOCS
     91 static PyTypeObject *type_list;
     92 /* All types are added to type_list, at least when
     93    they get one object created. That makes them
     94    immortal, which unfortunately contributes to
     95    garbage itself. If unlist_types_without_objects
     96    is set, they will be removed from the type_list
     97    once the last object is deallocated. */
     98 static int unlist_types_without_objects;
     99 extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs;
    100 extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs;
    101 extern Py_ssize_t null_strings, one_strings;
    102 void
    103 dump_counts(FILE* f)
    104 {
    105     PyTypeObject *tp;
    106     PyObject *xoptions, *value;
    107     _Py_IDENTIFIER(showalloccount);
    108 
    109     xoptions = PySys_GetXOptions();
    110     if (xoptions == NULL)
    111         return;
    112     value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
    113     if (value != Py_True)
    114         return;
    115 
    116     for (tp = type_list; tp; tp = tp->tp_next)
    117         fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
    118             "freed: %" PY_FORMAT_SIZE_T "d, "
    119             "max in use: %" PY_FORMAT_SIZE_T "d\n",
    120             tp->tp_name, tp->tp_allocs, tp->tp_frees,
    121             tp->tp_maxalloc);
    122     fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, "
    123         "empty: %" PY_FORMAT_SIZE_T "d\n",
    124         fast_tuple_allocs, tuple_zero_allocs);
    125     fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, "
    126         "neg: %" PY_FORMAT_SIZE_T "d\n",
    127         quick_int_allocs, quick_neg_int_allocs);
    128     fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, "
    129         "1-strings: %" PY_FORMAT_SIZE_T "d\n",
    130         null_strings, one_strings);
    131 }
    132 
    133 PyObject *
    134 get_counts(void)
    135 {
    136     PyTypeObject *tp;
    137     PyObject *result;
    138     PyObject *v;
    139 
    140     result = PyList_New(0);
    141     if (result == NULL)
    142         return NULL;
    143     for (tp = type_list; tp; tp = tp->tp_next) {
    144         v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs,
    145                           tp->tp_frees, tp->tp_maxalloc);
    146         if (v == NULL) {
    147             Py_DECREF(result);
    148             return NULL;
    149         }
    150         if (PyList_Append(result, v) < 0) {
    151             Py_DECREF(v);
    152             Py_DECREF(result);
    153             return NULL;
    154         }
    155         Py_DECREF(v);
    156     }
    157     return result;
    158 }
    159 
    160 void
    161 inc_count(PyTypeObject *tp)
    162 {
    163     if (tp->tp_next == NULL && tp->tp_prev == NULL) {
    164         /* first time; insert in linked list */
    165         if (tp->tp_next != NULL) /* sanity check */
    166             Py_FatalError("XXX inc_count sanity check");
    167         if (type_list)
    168             type_list->tp_prev = tp;
    169         tp->tp_next = type_list;
    170         /* Note that as of Python 2.2, heap-allocated type objects
    171          * can go away, but this code requires that they stay alive
    172          * until program exit.  That's why we're careful with
    173          * refcounts here.  type_list gets a new reference to tp,
    174          * while ownership of the reference type_list used to hold
    175          * (if any) was transferred to tp->tp_next in the line above.
    176          * tp is thus effectively immortal after this.
    177          */
    178         Py_INCREF(tp);
    179         type_list = tp;
    180 #ifdef Py_TRACE_REFS
    181         /* Also insert in the doubly-linked list of all objects,
    182          * if not already there.
    183          */
    184         _Py_AddToAllObjects((PyObject *)tp, 0);
    185 #endif
    186     }
    187     tp->tp_allocs++;
    188     if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc)
    189         tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees;
    190 }
    191 
    192 void dec_count(PyTypeObject *tp)
    193 {
    194     tp->tp_frees++;
    195     if (unlist_types_without_objects &&
    196         tp->tp_allocs == tp->tp_frees) {
    197         /* unlink the type from type_list */
    198         if (tp->tp_prev)
    199             tp->tp_prev->tp_next = tp->tp_next;
    200         else
    201             type_list = tp->tp_next;
    202         if (tp->tp_next)
    203             tp->tp_next->tp_prev = tp->tp_prev;
    204         tp->tp_next = tp->tp_prev = NULL;
    205         Py_DECREF(tp);
    206     }
    207 }
    208 
    209 #endif
    210 
    211 #ifdef Py_REF_DEBUG
    212 /* Log a fatal error; doesn't return. */
    213 void
    214 _Py_NegativeRefcount(const char *fname, int lineno, PyObject *op)
    215 {
    216     char buf[300];
    217 
    218     PyOS_snprintf(buf, sizeof(buf),
    219                   "%s:%i object at %p has negative ref count "
    220                   "%" PY_FORMAT_SIZE_T "d",
    221                   fname, lineno, op, op->ob_refcnt);
    222     Py_FatalError(buf);
    223 }
    224 
    225 #endif /* Py_REF_DEBUG */
    226 
    227 void
    228 Py_IncRef(PyObject *o)
    229 {
    230     Py_XINCREF(o);
    231 }
    232 
    233 void
    234 Py_DecRef(PyObject *o)
    235 {
    236     Py_XDECREF(o);
    237 }
    238 
    239 PyObject *
    240 PyObject_Init(PyObject *op, PyTypeObject *tp)
    241 {
    242     if (op == NULL)
    243         return PyErr_NoMemory();
    244     /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
    245     Py_TYPE(op) = tp;
    246     _Py_NewReference(op);
    247     return op;
    248 }
    249 
    250 PyVarObject *
    251 PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
    252 {
    253     if (op == NULL)
    254         return (PyVarObject *) PyErr_NoMemory();
    255     /* Any changes should be reflected in PyObject_INIT_VAR */
    256     op->ob_size = size;
    257     Py_TYPE(op) = tp;
    258     _Py_NewReference((PyObject *)op);
    259     return op;
    260 }
    261 
    262 PyObject *
    263 _PyObject_New(PyTypeObject *tp)
    264 {
    265     PyObject *op;
    266     op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp));
    267     if (op == NULL)
    268         return PyErr_NoMemory();
    269     return PyObject_INIT(op, tp);
    270 }
    271 
    272 PyVarObject *
    273 _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
    274 {
    275     PyVarObject *op;
    276     const size_t size = _PyObject_VAR_SIZE(tp, nitems);
    277     op = (PyVarObject *) PyObject_MALLOC(size);
    278     if (op == NULL)
    279         return (PyVarObject *)PyErr_NoMemory();
    280     return PyObject_INIT_VAR(op, tp, nitems);
    281 }
    282 
    283 void
    284 PyObject_CallFinalizer(PyObject *self)
    285 {
    286     PyTypeObject *tp = Py_TYPE(self);
    287 
    288     /* The former could happen on heaptypes created from the C API, e.g.
    289        PyType_FromSpec(). */
    290     if (!PyType_HasFeature(tp, Py_TPFLAGS_HAVE_FINALIZE) ||
    291         tp->tp_finalize == NULL)
    292         return;
    293     /* tp_finalize should only be called once. */
    294     if (PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
    295         return;
    296 
    297     tp->tp_finalize(self);
    298     if (PyType_IS_GC(tp))
    299         _PyGC_SET_FINALIZED(self, 1);
    300 }
    301 
    302 int
    303 PyObject_CallFinalizerFromDealloc(PyObject *self)
    304 {
    305     Py_ssize_t refcnt;
    306 
    307     /* Temporarily resurrect the object. */
    308     if (self->ob_refcnt != 0) {
    309         Py_FatalError("PyObject_CallFinalizerFromDealloc called on "
    310                       "object with a non-zero refcount");
    311     }
    312     self->ob_refcnt = 1;
    313 
    314     PyObject_CallFinalizer(self);
    315 
    316     /* Undo the temporary resurrection; can't use DECREF here, it would
    317      * cause a recursive call.
    318      */
    319     assert(self->ob_refcnt > 0);
    320     if (--self->ob_refcnt == 0)
    321         return 0;         /* this is the normal path out */
    322 
    323     /* tp_finalize resurrected it!  Make it look like the original Py_DECREF
    324      * never happened.
    325      */
    326     refcnt = self->ob_refcnt;
    327     _Py_NewReference(self);
    328     self->ob_refcnt = refcnt;
    329 
    330     if (PyType_IS_GC(Py_TYPE(self))) {
    331         assert(_PyGC_REFS(self) != _PyGC_REFS_UNTRACKED);
    332     }
    333     /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
    334      * we need to undo that. */
    335     _Py_DEC_REFTOTAL;
    336     /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
    337      * chain, so no more to do there.
    338      * If COUNT_ALLOCS, the original decref bumped tp_frees, and
    339      * _Py_NewReference bumped tp_allocs:  both of those need to be
    340      * undone.
    341      */
    342 #ifdef COUNT_ALLOCS
    343     --Py_TYPE(self)->tp_frees;
    344     --Py_TYPE(self)->tp_allocs;
    345 #endif
    346     return -1;
    347 }
    348 
    349 int
    350 PyObject_Print(PyObject *op, FILE *fp, int flags)
    351 {
    352     int ret = 0;
    353     if (PyErr_CheckSignals())
    354         return -1;
    355 #ifdef USE_STACKCHECK
    356     if (PyOS_CheckStack()) {
    357         PyErr_SetString(PyExc_MemoryError, "stack overflow");
    358         return -1;
    359     }
    360 #endif
    361     clearerr(fp); /* Clear any previous error condition */
    362     if (op == NULL) {
    363         Py_BEGIN_ALLOW_THREADS
    364         fprintf(fp, "<nil>");
    365         Py_END_ALLOW_THREADS
    366     }
    367     else {
    368         if (op->ob_refcnt <= 0)
    369             /* XXX(twouters) cast refcount to long until %zd is
    370                universally available */
    371             Py_BEGIN_ALLOW_THREADS
    372             fprintf(fp, "<refcnt %ld at %p>",
    373                 (long)op->ob_refcnt, op);
    374             Py_END_ALLOW_THREADS
    375         else {
    376             PyObject *s;
    377             if (flags & Py_PRINT_RAW)
    378                 s = PyObject_Str(op);
    379             else
    380                 s = PyObject_Repr(op);
    381             if (s == NULL)
    382                 ret = -1;
    383             else if (PyBytes_Check(s)) {
    384                 fwrite(PyBytes_AS_STRING(s), 1,
    385                        PyBytes_GET_SIZE(s), fp);
    386             }
    387             else if (PyUnicode_Check(s)) {
    388                 PyObject *t;
    389                 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
    390                 if (t == NULL)
    391                     ret = 0;
    392                 else {
    393                     fwrite(PyBytes_AS_STRING(t), 1,
    394                            PyBytes_GET_SIZE(t), fp);
    395                     Py_DECREF(t);
    396                 }
    397             }
    398             else {
    399                 PyErr_Format(PyExc_TypeError,
    400                              "str() or repr() returned '%.100s'",
    401                              s->ob_type->tp_name);
    402                 ret = -1;
    403             }
    404             Py_XDECREF(s);
    405         }
    406     }
    407     if (ret == 0) {
    408         if (ferror(fp)) {
    409             PyErr_SetFromErrno(PyExc_IOError);
    410             clearerr(fp);
    411             ret = -1;
    412         }
    413     }
    414     return ret;
    415 }
    416 
    417 /* For debugging convenience.  Set a breakpoint here and call it from your DLL */
    418 void
    419 _Py_BreakPoint(void)
    420 {
    421 }
    422 
    423 
    424 /* For debugging convenience.  See Misc/gdbinit for some useful gdb hooks */
    425 void
    426 _PyObject_Dump(PyObject* op)
    427 {
    428     if (op == NULL)
    429         fprintf(stderr, "NULL\n");
    430     else {
    431 #ifdef WITH_THREAD
    432         PyGILState_STATE gil;
    433 #endif
    434         PyObject *error_type, *error_value, *error_traceback;
    435 
    436         fprintf(stderr, "object  : ");
    437 #ifdef WITH_THREAD
    438         gil = PyGILState_Ensure();
    439 #endif
    440 
    441         PyErr_Fetch(&error_type, &error_value, &error_traceback);
    442         (void)PyObject_Print(op, stderr, 0);
    443         PyErr_Restore(error_type, error_value, error_traceback);
    444 
    445 #ifdef WITH_THREAD
    446         PyGILState_Release(gil);
    447 #endif
    448         /* XXX(twouters) cast refcount to long until %zd is
    449            universally available */
    450         fprintf(stderr, "\n"
    451             "type    : %s\n"
    452             "refcount: %ld\n"
    453             "address : %p\n",
    454             Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name,
    455             (long)op->ob_refcnt,
    456             op);
    457     }
    458 }
    459 
    460 PyObject *
    461 PyObject_Repr(PyObject *v)
    462 {
    463     PyObject *res;
    464     if (PyErr_CheckSignals())
    465         return NULL;
    466 #ifdef USE_STACKCHECK
    467     if (PyOS_CheckStack()) {
    468         PyErr_SetString(PyExc_MemoryError, "stack overflow");
    469         return NULL;
    470     }
    471 #endif
    472     if (v == NULL)
    473         return PyUnicode_FromString("<NULL>");
    474     if (Py_TYPE(v)->tp_repr == NULL)
    475         return PyUnicode_FromFormat("<%s object at %p>",
    476                                     v->ob_type->tp_name, v);
    477 
    478 #ifdef Py_DEBUG
    479     /* PyObject_Repr() must not be called with an exception set,
    480        because it may clear it (directly or indirectly) and so the
    481        caller loses its exception */
    482     assert(!PyErr_Occurred());
    483 #endif
    484 
    485     res = (*v->ob_type->tp_repr)(v);
    486     if (res == NULL)
    487         return NULL;
    488     if (!PyUnicode_Check(res)) {
    489         PyErr_Format(PyExc_TypeError,
    490                      "__repr__ returned non-string (type %.200s)",
    491                      res->ob_type->tp_name);
    492         Py_DECREF(res);
    493         return NULL;
    494     }
    495 #ifndef Py_DEBUG
    496     if (PyUnicode_READY(res) < 0)
    497         return NULL;
    498 #endif
    499     return res;
    500 }
    501 
    502 PyObject *
    503 PyObject_Str(PyObject *v)
    504 {
    505     PyObject *res;
    506     if (PyErr_CheckSignals())
    507         return NULL;
    508 #ifdef USE_STACKCHECK
    509     if (PyOS_CheckStack()) {
    510         PyErr_SetString(PyExc_MemoryError, "stack overflow");
    511         return NULL;
    512     }
    513 #endif
    514     if (v == NULL)
    515         return PyUnicode_FromString("<NULL>");
    516     if (PyUnicode_CheckExact(v)) {
    517 #ifndef Py_DEBUG
    518         if (PyUnicode_READY(v) < 0)
    519             return NULL;
    520 #endif
    521         Py_INCREF(v);
    522         return v;
    523     }
    524     if (Py_TYPE(v)->tp_str == NULL)
    525         return PyObject_Repr(v);
    526 
    527 #ifdef Py_DEBUG
    528     /* PyObject_Str() must not be called with an exception set,
    529        because it may clear it (directly or indirectly) and so the
    530        caller loses its exception */
    531     assert(!PyErr_Occurred());
    532 #endif
    533 
    534     /* It is possible for a type to have a tp_str representation that loops
    535        infinitely. */
    536     if (Py_EnterRecursiveCall(" while getting the str of an object"))
    537         return NULL;
    538     res = (*Py_TYPE(v)->tp_str)(v);
    539     Py_LeaveRecursiveCall();
    540     if (res == NULL)
    541         return NULL;
    542     if (!PyUnicode_Check(res)) {
    543         PyErr_Format(PyExc_TypeError,
    544                      "__str__ returned non-string (type %.200s)",
    545                      Py_TYPE(res)->tp_name);
    546         Py_DECREF(res);
    547         return NULL;
    548     }
    549 #ifndef Py_DEBUG
    550     if (PyUnicode_READY(res) < 0)
    551         return NULL;
    552 #endif
    553     assert(_PyUnicode_CheckConsistency(res, 1));
    554     return res;
    555 }
    556 
    557 PyObject *
    558 PyObject_ASCII(PyObject *v)
    559 {
    560     PyObject *repr, *ascii, *res;
    561 
    562     repr = PyObject_Repr(v);
    563     if (repr == NULL)
    564         return NULL;
    565 
    566     if (PyUnicode_IS_ASCII(repr))
    567         return repr;
    568 
    569     /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
    570     ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
    571     Py_DECREF(repr);
    572     if (ascii == NULL)
    573         return NULL;
    574 
    575     res = PyUnicode_DecodeASCII(
    576         PyBytes_AS_STRING(ascii),
    577         PyBytes_GET_SIZE(ascii),
    578         NULL);
    579 
    580     Py_DECREF(ascii);
    581     return res;
    582 }
    583 
    584 PyObject *
    585 PyObject_Bytes(PyObject *v)
    586 {
    587     PyObject *result, *func;
    588 
    589     if (v == NULL)
    590         return PyBytes_FromString("<NULL>");
    591 
    592     if (PyBytes_CheckExact(v)) {
    593         Py_INCREF(v);
    594         return v;
    595     }
    596 
    597     func = _PyObject_LookupSpecial(v, &PyId___bytes__);
    598     if (func != NULL) {
    599         result = PyObject_CallFunctionObjArgs(func, NULL);
    600         Py_DECREF(func);
    601         if (result == NULL)
    602             return NULL;
    603         if (!PyBytes_Check(result)) {
    604             PyErr_Format(PyExc_TypeError,
    605                          "__bytes__ returned non-bytes (type %.200s)",
    606                          Py_TYPE(result)->tp_name);
    607             Py_DECREF(result);
    608             return NULL;
    609         }
    610         return result;
    611     }
    612     else if (PyErr_Occurred())
    613         return NULL;
    614     return PyBytes_FromObject(v);
    615 }
    616 
    617 /* For Python 3.0.1 and later, the old three-way comparison has been
    618    completely removed in favour of rich comparisons.  PyObject_Compare() and
    619    PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
    620    The old tp_compare slot has been renamed to tp_reserved, and should no
    621    longer be used.  Use tp_richcompare instead.
    622 
    623    See (*) below for practical amendments.
    624 
    625    tp_richcompare gets called with a first argument of the appropriate type
    626    and a second object of an arbitrary type.  We never do any kind of
    627    coercion.
    628 
    629    The tp_richcompare slot should return an object, as follows:
    630 
    631     NULL if an exception occurred
    632     NotImplemented if the requested comparison is not implemented
    633     any other false value if the requested comparison is false
    634     any other true value if the requested comparison is true
    635 
    636   The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
    637   NotImplemented.
    638 
    639   (*) Practical amendments:
    640 
    641   - If rich comparison returns NotImplemented, == and != are decided by
    642     comparing the object pointer (i.e. falling back to the base object
    643     implementation).
    644 
    645 */
    646 
    647 /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
    648 int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
    649 
    650 static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
    651 
    652 /* Perform a rich comparison, raising TypeError when the requested comparison
    653    operator is not supported. */
    654 static PyObject *
    655 do_richcompare(PyObject *v, PyObject *w, int op)
    656 {
    657     richcmpfunc f;
    658     PyObject *res;
    659     int checked_reverse_op = 0;
    660 
    661     if (v->ob_type != w->ob_type &&
    662         PyType_IsSubtype(w->ob_type, v->ob_type) &&
    663         (f = w->ob_type->tp_richcompare) != NULL) {
    664         checked_reverse_op = 1;
    665         res = (*f)(w, v, _Py_SwappedOp[op]);
    666         if (res != Py_NotImplemented)
    667             return res;
    668         Py_DECREF(res);
    669     }
    670     if ((f = v->ob_type->tp_richcompare) != NULL) {
    671         res = (*f)(v, w, op);
    672         if (res != Py_NotImplemented)
    673             return res;
    674         Py_DECREF(res);
    675     }
    676     if (!checked_reverse_op && (f = w->ob_type->tp_richcompare) != NULL) {
    677         res = (*f)(w, v, _Py_SwappedOp[op]);
    678         if (res != Py_NotImplemented)
    679             return res;
    680         Py_DECREF(res);
    681     }
    682     /* If neither object implements it, provide a sensible default
    683        for == and !=, but raise an exception for ordering. */
    684     switch (op) {
    685     case Py_EQ:
    686         res = (v == w) ? Py_True : Py_False;
    687         break;
    688     case Py_NE:
    689         res = (v != w) ? Py_True : Py_False;
    690         break;
    691     default:
    692         PyErr_Format(PyExc_TypeError,
    693                      "'%s' not supported between instances of '%.100s' and '%.100s'",
    694                      opstrings[op],
    695                      v->ob_type->tp_name,
    696                      w->ob_type->tp_name);
    697         return NULL;
    698     }
    699     Py_INCREF(res);
    700     return res;
    701 }
    702 
    703 /* Perform a rich comparison with object result.  This wraps do_richcompare()
    704    with a check for NULL arguments and a recursion check. */
    705 
    706 PyObject *
    707 PyObject_RichCompare(PyObject *v, PyObject *w, int op)
    708 {
    709     PyObject *res;
    710 
    711     assert(Py_LT <= op && op <= Py_GE);
    712     if (v == NULL || w == NULL) {
    713         if (!PyErr_Occurred())
    714             PyErr_BadInternalCall();
    715         return NULL;
    716     }
    717     if (Py_EnterRecursiveCall(" in comparison"))
    718         return NULL;
    719     res = do_richcompare(v, w, op);
    720     Py_LeaveRecursiveCall();
    721     return res;
    722 }
    723 
    724 /* Perform a rich comparison with integer result.  This wraps
    725    PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
    726 int
    727 PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
    728 {
    729     PyObject *res;
    730     int ok;
    731 
    732     /* Quick result when objects are the same.
    733        Guarantees that identity implies equality. */
    734     if (v == w) {
    735         if (op == Py_EQ)
    736             return 1;
    737         else if (op == Py_NE)
    738             return 0;
    739     }
    740 
    741     res = PyObject_RichCompare(v, w, op);
    742     if (res == NULL)
    743         return -1;
    744     if (PyBool_Check(res))
    745         ok = (res == Py_True);
    746     else
    747         ok = PyObject_IsTrue(res);
    748     Py_DECREF(res);
    749     return ok;
    750 }
    751 
    752 Py_hash_t
    753 PyObject_HashNotImplemented(PyObject *v)
    754 {
    755     PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
    756                  Py_TYPE(v)->tp_name);
    757     return -1;
    758 }
    759 
    760 Py_hash_t
    761 PyObject_Hash(PyObject *v)
    762 {
    763     PyTypeObject *tp = Py_TYPE(v);
    764     if (tp->tp_hash != NULL)
    765         return (*tp->tp_hash)(v);
    766     /* To keep to the general practice that inheriting
    767      * solely from object in C code should work without
    768      * an explicit call to PyType_Ready, we implicitly call
    769      * PyType_Ready here and then check the tp_hash slot again
    770      */
    771     if (tp->tp_dict == NULL) {
    772         if (PyType_Ready(tp) < 0)
    773             return -1;
    774         if (tp->tp_hash != NULL)
    775             return (*tp->tp_hash)(v);
    776     }
    777     /* Otherwise, the object can't be hashed */
    778     return PyObject_HashNotImplemented(v);
    779 }
    780 
    781 PyObject *
    782 PyObject_GetAttrString(PyObject *v, const char *name)
    783 {
    784     PyObject *w, *res;
    785 
    786     if (Py_TYPE(v)->tp_getattr != NULL)
    787         return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
    788     w = PyUnicode_InternFromString(name);
    789     if (w == NULL)
    790         return NULL;
    791     res = PyObject_GetAttr(v, w);
    792     Py_DECREF(w);
    793     return res;
    794 }
    795 
    796 int
    797 PyObject_HasAttrString(PyObject *v, const char *name)
    798 {
    799     PyObject *res = PyObject_GetAttrString(v, name);
    800     if (res != NULL) {
    801         Py_DECREF(res);
    802         return 1;
    803     }
    804     PyErr_Clear();
    805     return 0;
    806 }
    807 
    808 int
    809 PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
    810 {
    811     PyObject *s;
    812     int res;
    813 
    814     if (Py_TYPE(v)->tp_setattr != NULL)
    815         return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
    816     s = PyUnicode_InternFromString(name);
    817     if (s == NULL)
    818         return -1;
    819     res = PyObject_SetAttr(v, s, w);
    820     Py_XDECREF(s);
    821     return res;
    822 }
    823 
    824 int
    825 _PyObject_IsAbstract(PyObject *obj)
    826 {
    827     int res;
    828     PyObject* isabstract;
    829 
    830     if (obj == NULL)
    831         return 0;
    832 
    833     isabstract = _PyObject_GetAttrId(obj, &PyId___isabstractmethod__);
    834     if (isabstract == NULL) {
    835         if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
    836             PyErr_Clear();
    837             return 0;
    838         }
    839         return -1;
    840     }
    841     res = PyObject_IsTrue(isabstract);
    842     Py_DECREF(isabstract);
    843     return res;
    844 }
    845 
    846 PyObject *
    847 _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
    848 {
    849     PyObject *result;
    850     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
    851     if (!oname)
    852         return NULL;
    853     result = PyObject_GetAttr(v, oname);
    854     return result;
    855 }
    856 
    857 int
    858 _PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
    859 {
    860     int result;
    861     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
    862     if (!oname)
    863         return -1;
    864     result = PyObject_HasAttr(v, oname);
    865     return result;
    866 }
    867 
    868 int
    869 _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
    870 {
    871     int result;
    872     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
    873     if (!oname)
    874         return -1;
    875     result = PyObject_SetAttr(v, oname, w);
    876     return result;
    877 }
    878 
    879 PyObject *
    880 PyObject_GetAttr(PyObject *v, PyObject *name)
    881 {
    882     PyTypeObject *tp = Py_TYPE(v);
    883 
    884     if (!PyUnicode_Check(name)) {
    885         PyErr_Format(PyExc_TypeError,
    886                      "attribute name must be string, not '%.200s'",
    887                      name->ob_type->tp_name);
    888         return NULL;
    889     }
    890     if (tp->tp_getattro != NULL)
    891         return (*tp->tp_getattro)(v, name);
    892     if (tp->tp_getattr != NULL) {
    893         char *name_str = PyUnicode_AsUTF8(name);
    894         if (name_str == NULL)
    895             return NULL;
    896         return (*tp->tp_getattr)(v, name_str);
    897     }
    898     PyErr_Format(PyExc_AttributeError,
    899                  "'%.50s' object has no attribute '%U'",
    900                  tp->tp_name, name);
    901     return NULL;
    902 }
    903 
    904 int
    905 PyObject_HasAttr(PyObject *v, PyObject *name)
    906 {
    907     PyObject *res = PyObject_GetAttr(v, name);
    908     if (res != NULL) {
    909         Py_DECREF(res);
    910         return 1;
    911     }
    912     PyErr_Clear();
    913     return 0;
    914 }
    915 
    916 int
    917 PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
    918 {
    919     PyTypeObject *tp = Py_TYPE(v);
    920     int err;
    921 
    922     if (!PyUnicode_Check(name)) {
    923         PyErr_Format(PyExc_TypeError,
    924                      "attribute name must be string, not '%.200s'",
    925                      name->ob_type->tp_name);
    926         return -1;
    927     }
    928     Py_INCREF(name);
    929 
    930     PyUnicode_InternInPlace(&name);
    931     if (tp->tp_setattro != NULL) {
    932         err = (*tp->tp_setattro)(v, name, value);
    933         Py_DECREF(name);
    934         return err;
    935     }
    936     if (tp->tp_setattr != NULL) {
    937         char *name_str = PyUnicode_AsUTF8(name);
    938         if (name_str == NULL)
    939             return -1;
    940         err = (*tp->tp_setattr)(v, name_str, value);
    941         Py_DECREF(name);
    942         return err;
    943     }
    944     Py_DECREF(name);
    945     assert(name->ob_refcnt >= 1);
    946     if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
    947         PyErr_Format(PyExc_TypeError,
    948                      "'%.100s' object has no attributes "
    949                      "(%s .%U)",
    950                      tp->tp_name,
    951                      value==NULL ? "del" : "assign to",
    952                      name);
    953     else
    954         PyErr_Format(PyExc_TypeError,
    955                      "'%.100s' object has only read-only attributes "
    956                      "(%s .%U)",
    957                      tp->tp_name,
    958                      value==NULL ? "del" : "assign to",
    959                      name);
    960     return -1;
    961 }
    962 
    963 /* Helper to get a pointer to an object's __dict__ slot, if any */
    964 
    965 PyObject **
    966 _PyObject_GetDictPtr(PyObject *obj)
    967 {
    968     Py_ssize_t dictoffset;
    969     PyTypeObject *tp = Py_TYPE(obj);
    970 
    971     dictoffset = tp->tp_dictoffset;
    972     if (dictoffset == 0)
    973         return NULL;
    974     if (dictoffset < 0) {
    975         Py_ssize_t tsize;
    976         size_t size;
    977 
    978         tsize = ((PyVarObject *)obj)->ob_size;
    979         if (tsize < 0)
    980             tsize = -tsize;
    981         size = _PyObject_VAR_SIZE(tp, tsize);
    982 
    983         dictoffset += (long)size;
    984         assert(dictoffset > 0);
    985         assert(dictoffset % SIZEOF_VOID_P == 0);
    986     }
    987     return (PyObject **) ((char *)obj + dictoffset);
    988 }
    989 
    990 PyObject *
    991 PyObject_SelfIter(PyObject *obj)
    992 {
    993     Py_INCREF(obj);
    994     return obj;
    995 }
    996 
    997 /* Convenience function to get a builtin from its name */
    998 PyObject *
    999 _PyObject_GetBuiltin(const char *name)
   1000 {
   1001     PyObject *mod_name, *mod, *attr;
   1002 
   1003     mod_name = _PyUnicode_FromId(&PyId_builtins);   /* borrowed */
   1004     if (mod_name == NULL)
   1005         return NULL;
   1006     mod = PyImport_Import(mod_name);
   1007     if (mod == NULL)
   1008         return NULL;
   1009     attr = PyObject_GetAttrString(mod, name);
   1010     Py_DECREF(mod);
   1011     return attr;
   1012 }
   1013 
   1014 /* Helper used when the __next__ method is removed from a type:
   1015    tp_iternext is never NULL and can be safely called without checking
   1016    on every iteration.
   1017  */
   1018 
   1019 PyObject *
   1020 _PyObject_NextNotImplemented(PyObject *self)
   1021 {
   1022     PyErr_Format(PyExc_TypeError,
   1023                  "'%.200s' object is not iterable",
   1024                  Py_TYPE(self)->tp_name);
   1025     return NULL;
   1026 }
   1027 
   1028 /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
   1029 
   1030 PyObject *
   1031 _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
   1032 {
   1033     PyTypeObject *tp = Py_TYPE(obj);
   1034     PyObject *descr = NULL;
   1035     PyObject *res = NULL;
   1036     descrgetfunc f;
   1037     Py_ssize_t dictoffset;
   1038     PyObject **dictptr;
   1039 
   1040     if (!PyUnicode_Check(name)){
   1041         PyErr_Format(PyExc_TypeError,
   1042                      "attribute name must be string, not '%.200s'",
   1043                      name->ob_type->tp_name);
   1044         return NULL;
   1045     }
   1046     Py_INCREF(name);
   1047 
   1048     if (tp->tp_dict == NULL) {
   1049         if (PyType_Ready(tp) < 0)
   1050             goto done;
   1051     }
   1052 
   1053     descr = _PyType_Lookup(tp, name);
   1054 
   1055     f = NULL;
   1056     if (descr != NULL) {
   1057         Py_INCREF(descr);
   1058         f = descr->ob_type->tp_descr_get;
   1059         if (f != NULL && PyDescr_IsData(descr)) {
   1060             res = f(descr, obj, (PyObject *)obj->ob_type);
   1061             goto done;
   1062         }
   1063     }
   1064 
   1065     if (dict == NULL) {
   1066         /* Inline _PyObject_GetDictPtr */
   1067         dictoffset = tp->tp_dictoffset;
   1068         if (dictoffset != 0) {
   1069             if (dictoffset < 0) {
   1070                 Py_ssize_t tsize;
   1071                 size_t size;
   1072 
   1073                 tsize = ((PyVarObject *)obj)->ob_size;
   1074                 if (tsize < 0)
   1075                     tsize = -tsize;
   1076                 size = _PyObject_VAR_SIZE(tp, tsize);
   1077                 assert(size <= PY_SSIZE_T_MAX);
   1078 
   1079                 dictoffset += (Py_ssize_t)size;
   1080                 assert(dictoffset > 0);
   1081                 assert(dictoffset % SIZEOF_VOID_P == 0);
   1082             }
   1083             dictptr = (PyObject **) ((char *)obj + dictoffset);
   1084             dict = *dictptr;
   1085         }
   1086     }
   1087     if (dict != NULL) {
   1088         Py_INCREF(dict);
   1089         res = PyDict_GetItem(dict, name);
   1090         if (res != NULL) {
   1091             Py_INCREF(res);
   1092             Py_DECREF(dict);
   1093             goto done;
   1094         }
   1095         Py_DECREF(dict);
   1096     }
   1097 
   1098     if (f != NULL) {
   1099         res = f(descr, obj, (PyObject *)Py_TYPE(obj));
   1100         goto done;
   1101     }
   1102 
   1103     if (descr != NULL) {
   1104         res = descr;
   1105         descr = NULL;
   1106         goto done;
   1107     }
   1108 
   1109     PyErr_Format(PyExc_AttributeError,
   1110                  "'%.50s' object has no attribute '%U'",
   1111                  tp->tp_name, name);
   1112   done:
   1113     Py_XDECREF(descr);
   1114     Py_DECREF(name);
   1115     return res;
   1116 }
   1117 
   1118 PyObject *
   1119 PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
   1120 {
   1121     return _PyObject_GenericGetAttrWithDict(obj, name, NULL);
   1122 }
   1123 
   1124 int
   1125 _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
   1126                                  PyObject *value, PyObject *dict)
   1127 {
   1128     PyTypeObject *tp = Py_TYPE(obj);
   1129     PyObject *descr;
   1130     descrsetfunc f;
   1131     PyObject **dictptr;
   1132     int res = -1;
   1133 
   1134     if (!PyUnicode_Check(name)){
   1135         PyErr_Format(PyExc_TypeError,
   1136                      "attribute name must be string, not '%.200s'",
   1137                      name->ob_type->tp_name);
   1138         return -1;
   1139     }
   1140 
   1141     if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
   1142         return -1;
   1143 
   1144     Py_INCREF(name);
   1145 
   1146     descr = _PyType_Lookup(tp, name);
   1147 
   1148     if (descr != NULL) {
   1149         Py_INCREF(descr);
   1150         f = descr->ob_type->tp_descr_set;
   1151         if (f != NULL) {
   1152             res = f(descr, obj, value);
   1153             goto done;
   1154         }
   1155     }
   1156 
   1157     if (dict == NULL) {
   1158         dictptr = _PyObject_GetDictPtr(obj);
   1159         if (dictptr == NULL) {
   1160             if (descr == NULL) {
   1161                 PyErr_Format(PyExc_AttributeError,
   1162                              "'%.100s' object has no attribute '%U'",
   1163                              tp->tp_name, name);
   1164             }
   1165             else {
   1166                 PyErr_Format(PyExc_AttributeError,
   1167                              "'%.50s' object attribute '%U' is read-only",
   1168                              tp->tp_name, name);
   1169             }
   1170             goto done;
   1171         }
   1172         res = _PyObjectDict_SetItem(tp, dictptr, name, value);
   1173     }
   1174     else {
   1175         Py_INCREF(dict);
   1176         if (value == NULL)
   1177             res = PyDict_DelItem(dict, name);
   1178         else
   1179             res = PyDict_SetItem(dict, name, value);
   1180         Py_DECREF(dict);
   1181     }
   1182     if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
   1183         PyErr_SetObject(PyExc_AttributeError, name);
   1184 
   1185   done:
   1186     Py_XDECREF(descr);
   1187     Py_DECREF(name);
   1188     return res;
   1189 }
   1190 
   1191 int
   1192 PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
   1193 {
   1194     return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
   1195 }
   1196 
   1197 int
   1198 PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
   1199 {
   1200     PyObject **dictptr = _PyObject_GetDictPtr(obj);
   1201     if (dictptr == NULL) {
   1202         PyErr_SetString(PyExc_AttributeError,
   1203                         "This object has no __dict__");
   1204         return -1;
   1205     }
   1206     if (value == NULL) {
   1207         PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
   1208         return -1;
   1209     }
   1210     if (!PyDict_Check(value)) {
   1211         PyErr_Format(PyExc_TypeError,
   1212                      "__dict__ must be set to a dictionary, "
   1213                      "not a '%.200s'", Py_TYPE(value)->tp_name);
   1214         return -1;
   1215     }
   1216     Py_INCREF(value);
   1217     Py_XSETREF(*dictptr, value);
   1218     return 0;
   1219 }
   1220 
   1221 
   1222 /* Test a value used as condition, e.g., in a for or if statement.
   1223    Return -1 if an error occurred */
   1224 
   1225 int
   1226 PyObject_IsTrue(PyObject *v)
   1227 {
   1228     Py_ssize_t res;
   1229     if (v == Py_True)
   1230         return 1;
   1231     if (v == Py_False)
   1232         return 0;
   1233     if (v == Py_None)
   1234         return 0;
   1235     else if (v->ob_type->tp_as_number != NULL &&
   1236              v->ob_type->tp_as_number->nb_bool != NULL)
   1237         res = (*v->ob_type->tp_as_number->nb_bool)(v);
   1238     else if (v->ob_type->tp_as_mapping != NULL &&
   1239              v->ob_type->tp_as_mapping->mp_length != NULL)
   1240         res = (*v->ob_type->tp_as_mapping->mp_length)(v);
   1241     else if (v->ob_type->tp_as_sequence != NULL &&
   1242              v->ob_type->tp_as_sequence->sq_length != NULL)
   1243         res = (*v->ob_type->tp_as_sequence->sq_length)(v);
   1244     else
   1245         return 1;
   1246     /* if it is negative, it should be either -1 or -2 */
   1247     return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
   1248 }
   1249 
   1250 /* equivalent of 'not v'
   1251    Return -1 if an error occurred */
   1252 
   1253 int
   1254 PyObject_Not(PyObject *v)
   1255 {
   1256     int res;
   1257     res = PyObject_IsTrue(v);
   1258     if (res < 0)
   1259         return res;
   1260     return res == 0;
   1261 }
   1262 
   1263 /* Test whether an object can be called */
   1264 
   1265 int
   1266 PyCallable_Check(PyObject *x)
   1267 {
   1268     if (x == NULL)
   1269         return 0;
   1270     return x->ob_type->tp_call != NULL;
   1271 }
   1272 
   1273 
   1274 /* Helper for PyObject_Dir without arguments: returns the local scope. */
   1275 static PyObject *
   1276 _dir_locals(void)
   1277 {
   1278     PyObject *names;
   1279     PyObject *locals;
   1280 
   1281     locals = PyEval_GetLocals();
   1282     if (locals == NULL)
   1283         return NULL;
   1284 
   1285     names = PyMapping_Keys(locals);
   1286     if (!names)
   1287         return NULL;
   1288     if (!PyList_Check(names)) {
   1289         PyErr_Format(PyExc_TypeError,
   1290             "dir(): expected keys() of locals to be a list, "
   1291             "not '%.200s'", Py_TYPE(names)->tp_name);
   1292         Py_DECREF(names);
   1293         return NULL;
   1294     }
   1295     if (PyList_Sort(names)) {
   1296         Py_DECREF(names);
   1297         return NULL;
   1298     }
   1299     /* the locals don't need to be DECREF'd */
   1300     return names;
   1301 }
   1302 
   1303 /* Helper for PyObject_Dir: object introspection. */
   1304 static PyObject *
   1305 _dir_object(PyObject *obj)
   1306 {
   1307     PyObject *result, *sorted;
   1308     PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
   1309 
   1310     assert(obj);
   1311     if (dirfunc == NULL) {
   1312         if (!PyErr_Occurred())
   1313             PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
   1314         return NULL;
   1315     }
   1316     /* use __dir__ */
   1317     result = PyObject_CallFunctionObjArgs(dirfunc, NULL);
   1318     Py_DECREF(dirfunc);
   1319     if (result == NULL)
   1320         return NULL;
   1321     /* return sorted(result) */
   1322     sorted = PySequence_List(result);
   1323     Py_DECREF(result);
   1324     if (sorted == NULL)
   1325         return NULL;
   1326     if (PyList_Sort(sorted)) {
   1327         Py_DECREF(sorted);
   1328         return NULL;
   1329     }
   1330     return sorted;
   1331 }
   1332 
   1333 /* Implementation of dir() -- if obj is NULL, returns the names in the current
   1334    (local) scope.  Otherwise, performs introspection of the object: returns a
   1335    sorted list of attribute names (supposedly) accessible from the object
   1336 */
   1337 PyObject *
   1338 PyObject_Dir(PyObject *obj)
   1339 {
   1340     return (obj == NULL) ? _dir_locals() : _dir_object(obj);
   1341 }
   1342 
   1343 /*
   1344 None is a non-NULL undefined value.
   1345 There is (and should be!) no way to create other objects of this type,
   1346 so there is exactly one (which is indestructible, by the way).
   1347 */
   1348 
   1349 /* ARGSUSED */
   1350 static PyObject *
   1351 none_repr(PyObject *op)
   1352 {
   1353     return PyUnicode_FromString("None");
   1354 }
   1355 
   1356 /* ARGUSED */
   1357 static void
   1358 none_dealloc(PyObject* ignore)
   1359 {
   1360     /* This should never get called, but we also don't want to SEGV if
   1361      * we accidentally decref None out of existence.
   1362      */
   1363     Py_FatalError("deallocating None");
   1364 }
   1365 
   1366 static PyObject *
   1367 none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
   1368 {
   1369     if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
   1370         PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
   1371         return NULL;
   1372     }
   1373     Py_RETURN_NONE;
   1374 }
   1375 
   1376 static int
   1377 none_bool(PyObject *v)
   1378 {
   1379     return 0;
   1380 }
   1381 
   1382 static PyNumberMethods none_as_number = {
   1383     0,                          /* nb_add */
   1384     0,                          /* nb_subtract */
   1385     0,                          /* nb_multiply */
   1386     0,                          /* nb_remainder */
   1387     0,                          /* nb_divmod */
   1388     0,                          /* nb_power */
   1389     0,                          /* nb_negative */
   1390     0,                          /* nb_positive */
   1391     0,                          /* nb_absolute */
   1392     (inquiry)none_bool,         /* nb_bool */
   1393     0,                          /* nb_invert */
   1394     0,                          /* nb_lshift */
   1395     0,                          /* nb_rshift */
   1396     0,                          /* nb_and */
   1397     0,                          /* nb_xor */
   1398     0,                          /* nb_or */
   1399     0,                          /* nb_int */
   1400     0,                          /* nb_reserved */
   1401     0,                          /* nb_float */
   1402     0,                          /* nb_inplace_add */
   1403     0,                          /* nb_inplace_subtract */
   1404     0,                          /* nb_inplace_multiply */
   1405     0,                          /* nb_inplace_remainder */
   1406     0,                          /* nb_inplace_power */
   1407     0,                          /* nb_inplace_lshift */
   1408     0,                          /* nb_inplace_rshift */
   1409     0,                          /* nb_inplace_and */
   1410     0,                          /* nb_inplace_xor */
   1411     0,                          /* nb_inplace_or */
   1412     0,                          /* nb_floor_divide */
   1413     0,                          /* nb_true_divide */
   1414     0,                          /* nb_inplace_floor_divide */
   1415     0,                          /* nb_inplace_true_divide */
   1416     0,                          /* nb_index */
   1417 };
   1418 
   1419 PyTypeObject _PyNone_Type = {
   1420     PyVarObject_HEAD_INIT(&PyType_Type, 0)
   1421     "NoneType",
   1422     0,
   1423     0,
   1424     none_dealloc,       /*tp_dealloc*/ /*never called*/
   1425     0,                  /*tp_print*/
   1426     0,                  /*tp_getattr*/
   1427     0,                  /*tp_setattr*/
   1428     0,                  /*tp_reserved*/
   1429     none_repr,          /*tp_repr*/
   1430     &none_as_number,    /*tp_as_number*/
   1431     0,                  /*tp_as_sequence*/
   1432     0,                  /*tp_as_mapping*/
   1433     0,                  /*tp_hash */
   1434     0,                  /*tp_call */
   1435     0,                  /*tp_str */
   1436     0,                  /*tp_getattro */
   1437     0,                  /*tp_setattro */
   1438     0,                  /*tp_as_buffer */
   1439     Py_TPFLAGS_DEFAULT, /*tp_flags */
   1440     0,                  /*tp_doc */
   1441     0,                  /*tp_traverse */
   1442     0,                  /*tp_clear */
   1443     0,                  /*tp_richcompare */
   1444     0,                  /*tp_weaklistoffset */
   1445     0,                  /*tp_iter */
   1446     0,                  /*tp_iternext */
   1447     0,                  /*tp_methods */
   1448     0,                  /*tp_members */
   1449     0,                  /*tp_getset */
   1450     0,                  /*tp_base */
   1451     0,                  /*tp_dict */
   1452     0,                  /*tp_descr_get */
   1453     0,                  /*tp_descr_set */
   1454     0,                  /*tp_dictoffset */
   1455     0,                  /*tp_init */
   1456     0,                  /*tp_alloc */
   1457     none_new,           /*tp_new */
   1458 };
   1459 
   1460 PyObject _Py_NoneStruct = {
   1461   _PyObject_EXTRA_INIT
   1462   1, &_PyNone_Type
   1463 };
   1464 
   1465 /* NotImplemented is an object that can be used to signal that an
   1466    operation is not implemented for the given type combination. */
   1467 
   1468 static PyObject *
   1469 NotImplemented_repr(PyObject *op)
   1470 {
   1471     return PyUnicode_FromString("NotImplemented");
   1472 }
   1473 
   1474 static PyObject *
   1475 NotImplemented_reduce(PyObject *op)
   1476 {
   1477     return PyUnicode_FromString("NotImplemented");
   1478 }
   1479 
   1480 static PyMethodDef notimplemented_methods[] = {
   1481     {"__reduce__", (PyCFunction)NotImplemented_reduce, METH_NOARGS, NULL},
   1482     {NULL, NULL}
   1483 };
   1484 
   1485 static PyObject *
   1486 notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
   1487 {
   1488     if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
   1489         PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
   1490         return NULL;
   1491     }
   1492     Py_RETURN_NOTIMPLEMENTED;
   1493 }
   1494 
   1495 static void
   1496 notimplemented_dealloc(PyObject* ignore)
   1497 {
   1498     /* This should never get called, but we also don't want to SEGV if
   1499      * we accidentally decref NotImplemented out of existence.
   1500      */
   1501     Py_FatalError("deallocating NotImplemented");
   1502 }
   1503 
   1504 PyTypeObject _PyNotImplemented_Type = {
   1505     PyVarObject_HEAD_INIT(&PyType_Type, 0)
   1506     "NotImplementedType",
   1507     0,
   1508     0,
   1509     notimplemented_dealloc,       /*tp_dealloc*/ /*never called*/
   1510     0,                  /*tp_print*/
   1511     0,                  /*tp_getattr*/
   1512     0,                  /*tp_setattr*/
   1513     0,                  /*tp_reserved*/
   1514     NotImplemented_repr, /*tp_repr*/
   1515     0,                  /*tp_as_number*/
   1516     0,                  /*tp_as_sequence*/
   1517     0,                  /*tp_as_mapping*/
   1518     0,                  /*tp_hash */
   1519     0,                  /*tp_call */
   1520     0,                  /*tp_str */
   1521     0,                  /*tp_getattro */
   1522     0,                  /*tp_setattro */
   1523     0,                  /*tp_as_buffer */
   1524     Py_TPFLAGS_DEFAULT, /*tp_flags */
   1525     0,                  /*tp_doc */
   1526     0,                  /*tp_traverse */
   1527     0,                  /*tp_clear */
   1528     0,                  /*tp_richcompare */
   1529     0,                  /*tp_weaklistoffset */
   1530     0,                  /*tp_iter */
   1531     0,                  /*tp_iternext */
   1532     notimplemented_methods, /*tp_methods */
   1533     0,                  /*tp_members */
   1534     0,                  /*tp_getset */
   1535     0,                  /*tp_base */
   1536     0,                  /*tp_dict */
   1537     0,                  /*tp_descr_get */
   1538     0,                  /*tp_descr_set */
   1539     0,                  /*tp_dictoffset */
   1540     0,                  /*tp_init */
   1541     0,                  /*tp_alloc */
   1542     notimplemented_new, /*tp_new */
   1543 };
   1544 
   1545 PyObject _Py_NotImplementedStruct = {
   1546     _PyObject_EXTRA_INIT
   1547     1, &_PyNotImplemented_Type
   1548 };
   1549 
   1550 void
   1551 _Py_ReadyTypes(void)
   1552 {
   1553     if (PyType_Ready(&PyBaseObject_Type) < 0)
   1554         Py_FatalError("Can't initialize object type");
   1555 
   1556     if (PyType_Ready(&PyType_Type) < 0)
   1557         Py_FatalError("Can't initialize type type");
   1558 
   1559     if (PyType_Ready(&_PyWeakref_RefType) < 0)
   1560         Py_FatalError("Can't initialize weakref type");
   1561 
   1562     if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0)
   1563         Py_FatalError("Can't initialize callable weakref proxy type");
   1564 
   1565     if (PyType_Ready(&_PyWeakref_ProxyType) < 0)
   1566         Py_FatalError("Can't initialize weakref proxy type");
   1567 
   1568     if (PyType_Ready(&PyLong_Type) < 0)
   1569         Py_FatalError("Can't initialize int type");
   1570 
   1571     if (PyType_Ready(&PyBool_Type) < 0)
   1572         Py_FatalError("Can't initialize bool type");
   1573 
   1574     if (PyType_Ready(&PyByteArray_Type) < 0)
   1575         Py_FatalError("Can't initialize bytearray type");
   1576 
   1577     if (PyType_Ready(&PyBytes_Type) < 0)
   1578         Py_FatalError("Can't initialize 'str'");
   1579 
   1580     if (PyType_Ready(&PyList_Type) < 0)
   1581         Py_FatalError("Can't initialize list type");
   1582 
   1583     if (PyType_Ready(&_PyNone_Type) < 0)
   1584         Py_FatalError("Can't initialize None type");
   1585 
   1586     if (PyType_Ready(&_PyNotImplemented_Type) < 0)
   1587         Py_FatalError("Can't initialize NotImplemented type");
   1588 
   1589     if (PyType_Ready(&PyTraceBack_Type) < 0)
   1590         Py_FatalError("Can't initialize traceback type");
   1591 
   1592     if (PyType_Ready(&PySuper_Type) < 0)
   1593         Py_FatalError("Can't initialize super type");
   1594 
   1595     if (PyType_Ready(&PyRange_Type) < 0)
   1596         Py_FatalError("Can't initialize range type");
   1597 
   1598     if (PyType_Ready(&PyDict_Type) < 0)
   1599         Py_FatalError("Can't initialize dict type");
   1600 
   1601     if (PyType_Ready(&PyDictKeys_Type) < 0)
   1602         Py_FatalError("Can't initialize dict keys type");
   1603 
   1604     if (PyType_Ready(&PyDictValues_Type) < 0)
   1605         Py_FatalError("Can't initialize dict values type");
   1606 
   1607     if (PyType_Ready(&PyDictItems_Type) < 0)
   1608         Py_FatalError("Can't initialize dict items type");
   1609 
   1610     if (PyType_Ready(&PyODict_Type) < 0)
   1611         Py_FatalError("Can't initialize OrderedDict type");
   1612 
   1613     if (PyType_Ready(&PyODictKeys_Type) < 0)
   1614         Py_FatalError("Can't initialize odict_keys type");
   1615 
   1616     if (PyType_Ready(&PyODictItems_Type) < 0)
   1617         Py_FatalError("Can't initialize odict_items type");
   1618 
   1619     if (PyType_Ready(&PyODictValues_Type) < 0)
   1620         Py_FatalError("Can't initialize odict_values type");
   1621 
   1622     if (PyType_Ready(&PyODictIter_Type) < 0)
   1623         Py_FatalError("Can't initialize odict_keyiterator type");
   1624 
   1625     if (PyType_Ready(&PySet_Type) < 0)
   1626         Py_FatalError("Can't initialize set type");
   1627 
   1628     if (PyType_Ready(&PyUnicode_Type) < 0)
   1629         Py_FatalError("Can't initialize str type");
   1630 
   1631     if (PyType_Ready(&PySlice_Type) < 0)
   1632         Py_FatalError("Can't initialize slice type");
   1633 
   1634     if (PyType_Ready(&PyStaticMethod_Type) < 0)
   1635         Py_FatalError("Can't initialize static method type");
   1636 
   1637     if (PyType_Ready(&PyComplex_Type) < 0)
   1638         Py_FatalError("Can't initialize complex type");
   1639 
   1640     if (PyType_Ready(&PyFloat_Type) < 0)
   1641         Py_FatalError("Can't initialize float type");
   1642 
   1643     if (PyType_Ready(&PyFrozenSet_Type) < 0)
   1644         Py_FatalError("Can't initialize frozenset type");
   1645 
   1646     if (PyType_Ready(&PyProperty_Type) < 0)
   1647         Py_FatalError("Can't initialize property type");
   1648 
   1649     if (PyType_Ready(&_PyManagedBuffer_Type) < 0)
   1650         Py_FatalError("Can't initialize managed buffer type");
   1651 
   1652     if (PyType_Ready(&PyMemoryView_Type) < 0)
   1653         Py_FatalError("Can't initialize memoryview type");
   1654 
   1655     if (PyType_Ready(&PyTuple_Type) < 0)
   1656         Py_FatalError("Can't initialize tuple type");
   1657 
   1658     if (PyType_Ready(&PyEnum_Type) < 0)
   1659         Py_FatalError("Can't initialize enumerate type");
   1660 
   1661     if (PyType_Ready(&PyReversed_Type) < 0)
   1662         Py_FatalError("Can't initialize reversed type");
   1663 
   1664     if (PyType_Ready(&PyStdPrinter_Type) < 0)
   1665         Py_FatalError("Can't initialize StdPrinter");
   1666 
   1667     if (PyType_Ready(&PyCode_Type) < 0)
   1668         Py_FatalError("Can't initialize code type");
   1669 
   1670     if (PyType_Ready(&PyFrame_Type) < 0)
   1671         Py_FatalError("Can't initialize frame type");
   1672 
   1673     if (PyType_Ready(&PyCFunction_Type) < 0)
   1674         Py_FatalError("Can't initialize builtin function type");
   1675 
   1676     if (PyType_Ready(&PyMethod_Type) < 0)
   1677         Py_FatalError("Can't initialize method type");
   1678 
   1679     if (PyType_Ready(&PyFunction_Type) < 0)
   1680         Py_FatalError("Can't initialize function type");
   1681 
   1682     if (PyType_Ready(&PyDictProxy_Type) < 0)
   1683         Py_FatalError("Can't initialize dict proxy type");
   1684 
   1685     if (PyType_Ready(&PyGen_Type) < 0)
   1686         Py_FatalError("Can't initialize generator type");
   1687 
   1688     if (PyType_Ready(&PyGetSetDescr_Type) < 0)
   1689         Py_FatalError("Can't initialize get-set descriptor type");
   1690 
   1691     if (PyType_Ready(&PyWrapperDescr_Type) < 0)
   1692         Py_FatalError("Can't initialize wrapper type");
   1693 
   1694     if (PyType_Ready(&_PyMethodWrapper_Type) < 0)
   1695         Py_FatalError("Can't initialize method wrapper type");
   1696 
   1697     if (PyType_Ready(&PyEllipsis_Type) < 0)
   1698         Py_FatalError("Can't initialize ellipsis type");
   1699 
   1700     if (PyType_Ready(&PyMemberDescr_Type) < 0)
   1701         Py_FatalError("Can't initialize member descriptor type");
   1702 
   1703     if (PyType_Ready(&_PyNamespace_Type) < 0)
   1704         Py_FatalError("Can't initialize namespace type");
   1705 
   1706     if (PyType_Ready(&PyCapsule_Type) < 0)
   1707         Py_FatalError("Can't initialize capsule type");
   1708 
   1709     if (PyType_Ready(&PyLongRangeIter_Type) < 0)
   1710         Py_FatalError("Can't initialize long range iterator type");
   1711 
   1712     if (PyType_Ready(&PyCell_Type) < 0)
   1713         Py_FatalError("Can't initialize cell type");
   1714 
   1715     if (PyType_Ready(&PyInstanceMethod_Type) < 0)
   1716         Py_FatalError("Can't initialize instance method type");
   1717 
   1718     if (PyType_Ready(&PyClassMethodDescr_Type) < 0)
   1719         Py_FatalError("Can't initialize class method descr type");
   1720 
   1721     if (PyType_Ready(&PyMethodDescr_Type) < 0)
   1722         Py_FatalError("Can't initialize method descr type");
   1723 
   1724     if (PyType_Ready(&PyCallIter_Type) < 0)
   1725         Py_FatalError("Can't initialize call iter type");
   1726 
   1727     if (PyType_Ready(&PySeqIter_Type) < 0)
   1728         Py_FatalError("Can't initialize sequence iterator type");
   1729 
   1730     if (PyType_Ready(&PyCoro_Type) < 0)
   1731         Py_FatalError("Can't initialize coroutine type");
   1732 
   1733     if (PyType_Ready(&_PyCoroWrapper_Type) < 0)
   1734         Py_FatalError("Can't initialize coroutine wrapper type");
   1735 }
   1736 
   1737 
   1738 #ifdef Py_TRACE_REFS
   1739 
   1740 void
   1741 _Py_NewReference(PyObject *op)
   1742 {
   1743     _Py_INC_REFTOTAL;
   1744     op->ob_refcnt = 1;
   1745     _Py_AddToAllObjects(op, 1);
   1746     _Py_INC_TPALLOCS(op);
   1747 }
   1748 
   1749 void
   1750 _Py_ForgetReference(PyObject *op)
   1751 {
   1752 #ifdef SLOW_UNREF_CHECK
   1753     PyObject *p;
   1754 #endif
   1755     if (op->ob_refcnt < 0)
   1756         Py_FatalError("UNREF negative refcnt");
   1757     if (op == &refchain ||
   1758         op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) {
   1759         fprintf(stderr, "* ob\n");
   1760         _PyObject_Dump(op);
   1761         fprintf(stderr, "* op->_ob_prev->_ob_next\n");
   1762         _PyObject_Dump(op->_ob_prev->_ob_next);
   1763         fprintf(stderr, "* op->_ob_next->_ob_prev\n");
   1764         _PyObject_Dump(op->_ob_next->_ob_prev);
   1765         Py_FatalError("UNREF invalid object");
   1766     }
   1767 #ifdef SLOW_UNREF_CHECK
   1768     for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
   1769         if (p == op)
   1770             break;
   1771     }
   1772     if (p == &refchain) /* Not found */
   1773         Py_FatalError("UNREF unknown object");
   1774 #endif
   1775     op->_ob_next->_ob_prev = op->_ob_prev;
   1776     op->_ob_prev->_ob_next = op->_ob_next;
   1777     op->_ob_next = op->_ob_prev = NULL;
   1778     _Py_INC_TPFREES(op);
   1779 }
   1780 
   1781 void
   1782 _Py_Dealloc(PyObject *op)
   1783 {
   1784     destructor dealloc = Py_TYPE(op)->tp_dealloc;
   1785     _Py_ForgetReference(op);
   1786     (*dealloc)(op);
   1787 }
   1788 
   1789 /* Print all live objects.  Because PyObject_Print is called, the
   1790  * interpreter must be in a healthy state.
   1791  */
   1792 void
   1793 _Py_PrintReferences(FILE *fp)
   1794 {
   1795     PyObject *op;
   1796     fprintf(fp, "Remaining objects:\n");
   1797     for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
   1798         fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt);
   1799         if (PyObject_Print(op, fp, 0) != 0)
   1800             PyErr_Clear();
   1801         putc('\n', fp);
   1802     }
   1803 }
   1804 
   1805 /* Print the addresses of all live objects.  Unlike _Py_PrintReferences, this
   1806  * doesn't make any calls to the Python C API, so is always safe to call.
   1807  */
   1808 void
   1809 _Py_PrintReferenceAddresses(FILE *fp)
   1810 {
   1811     PyObject *op;
   1812     fprintf(fp, "Remaining object addresses:\n");
   1813     for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
   1814         fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op,
   1815             op->ob_refcnt, Py_TYPE(op)->tp_name);
   1816 }
   1817 
   1818 PyObject *
   1819 _Py_GetObjects(PyObject *self, PyObject *args)
   1820 {
   1821     int i, n;
   1822     PyObject *t = NULL;
   1823     PyObject *res, *op;
   1824 
   1825     if (!PyArg_ParseTuple(args, "i|O", &n, &t))
   1826         return NULL;
   1827     op = refchain._ob_next;
   1828     res = PyList_New(0);
   1829     if (res == NULL)
   1830         return NULL;
   1831     for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
   1832         while (op == self || op == args || op == res || op == t ||
   1833                (t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) {
   1834             op = op->_ob_next;
   1835             if (op == &refchain)
   1836                 return res;
   1837         }
   1838         if (PyList_Append(res, op) < 0) {
   1839             Py_DECREF(res);
   1840             return NULL;
   1841         }
   1842         op = op->_ob_next;
   1843     }
   1844     return res;
   1845 }
   1846 
   1847 #endif
   1848 
   1849 
   1850 /* Hack to force loading of abstract.o */
   1851 Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
   1852 
   1853 
   1854 void
   1855 _PyObject_DebugTypeStats(FILE *out)
   1856 {
   1857     _PyCFunction_DebugMallocStats(out);
   1858     _PyDict_DebugMallocStats(out);
   1859     _PyFloat_DebugMallocStats(out);
   1860     _PyFrame_DebugMallocStats(out);
   1861     _PyList_DebugMallocStats(out);
   1862     _PyMethod_DebugMallocStats(out);
   1863     _PyTuple_DebugMallocStats(out);
   1864 }
   1865 
   1866 /* These methods are used to control infinite recursion in repr, str, print,
   1867    etc.  Container objects that may recursively contain themselves,
   1868    e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
   1869    Py_ReprLeave() to avoid infinite recursion.
   1870 
   1871    Py_ReprEnter() returns 0 the first time it is called for a particular
   1872    object and 1 every time thereafter.  It returns -1 if an exception
   1873    occurred.  Py_ReprLeave() has no return value.
   1874 
   1875    See dictobject.c and listobject.c for examples of use.
   1876 */
   1877 
   1878 int
   1879 Py_ReprEnter(PyObject *obj)
   1880 {
   1881     PyObject *dict;
   1882     PyObject *list;
   1883     Py_ssize_t i;
   1884 
   1885     dict = PyThreadState_GetDict();
   1886     /* Ignore a missing thread-state, so that this function can be called
   1887        early on startup. */
   1888     if (dict == NULL)
   1889         return 0;
   1890     list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
   1891     if (list == NULL) {
   1892         list = PyList_New(0);
   1893         if (list == NULL)
   1894             return -1;
   1895         if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
   1896             return -1;
   1897         Py_DECREF(list);
   1898     }
   1899     i = PyList_GET_SIZE(list);
   1900     while (--i >= 0) {
   1901         if (PyList_GET_ITEM(list, i) == obj)
   1902             return 1;
   1903     }
   1904     if (PyList_Append(list, obj) < 0)
   1905         return -1;
   1906     return 0;
   1907 }
   1908 
   1909 void
   1910 Py_ReprLeave(PyObject *obj)
   1911 {
   1912     PyObject *dict;
   1913     PyObject *list;
   1914     Py_ssize_t i;
   1915     PyObject *error_type, *error_value, *error_traceback;
   1916 
   1917     PyErr_Fetch(&error_type, &error_value, &error_traceback);
   1918 
   1919     dict = PyThreadState_GetDict();
   1920     if (dict == NULL)
   1921         goto finally;
   1922 
   1923     list = _PyDict_GetItemId(dict, &PyId_Py_Repr);
   1924     if (list == NULL || !PyList_Check(list))
   1925         goto finally;
   1926 
   1927     i = PyList_GET_SIZE(list);
   1928     /* Count backwards because we always expect obj to be list[-1] */
   1929     while (--i >= 0) {
   1930         if (PyList_GET_ITEM(list, i) == obj) {
   1931             PyList_SetSlice(list, i, i + 1, NULL);
   1932             break;
   1933         }
   1934     }
   1935 
   1936 finally:
   1937     /* ignore exceptions because there is no way to report them. */
   1938     PyErr_Restore(error_type, error_value, error_traceback);
   1939 }
   1940 
   1941 /* Trashcan support. */
   1942 
   1943 /* Current call-stack depth of tp_dealloc calls. */
   1944 int _PyTrash_delete_nesting = 0;
   1945 
   1946 /* List of objects that still need to be cleaned up, singly linked via their
   1947  * gc headers' gc_prev pointers.
   1948  */
   1949 PyObject *_PyTrash_delete_later = NULL;
   1950 
   1951 /* Add op to the _PyTrash_delete_later list.  Called when the current
   1952  * call-stack depth gets large.  op must be a currently untracked gc'ed
   1953  * object, with refcount 0.  Py_DECREF must already have been called on it.
   1954  */
   1955 void
   1956 _PyTrash_deposit_object(PyObject *op)
   1957 {
   1958     assert(PyObject_IS_GC(op));
   1959     assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
   1960     assert(op->ob_refcnt == 0);
   1961     _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later;
   1962     _PyTrash_delete_later = op;
   1963 }
   1964 
   1965 /* The equivalent API, using per-thread state recursion info */
   1966 void
   1967 _PyTrash_thread_deposit_object(PyObject *op)
   1968 {
   1969     PyThreadState *tstate = PyThreadState_GET();
   1970     assert(PyObject_IS_GC(op));
   1971     assert(_PyGC_REFS(op) == _PyGC_REFS_UNTRACKED);
   1972     assert(op->ob_refcnt == 0);
   1973     _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later;
   1974     tstate->trash_delete_later = op;
   1975 }
   1976 
   1977 /* Dealloccate all the objects in the _PyTrash_delete_later list.  Called when
   1978  * the call-stack unwinds again.
   1979  */
   1980 void
   1981 _PyTrash_destroy_chain(void)
   1982 {
   1983     while (_PyTrash_delete_later) {
   1984         PyObject *op = _PyTrash_delete_later;
   1985         destructor dealloc = Py_TYPE(op)->tp_dealloc;
   1986 
   1987         _PyTrash_delete_later =
   1988             (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
   1989 
   1990         /* Call the deallocator directly.  This used to try to
   1991          * fool Py_DECREF into calling it indirectly, but
   1992          * Py_DECREF was already called on this object, and in
   1993          * assorted non-release builds calling Py_DECREF again ends
   1994          * up distorting allocation statistics.
   1995          */
   1996         assert(op->ob_refcnt == 0);
   1997         ++_PyTrash_delete_nesting;
   1998         (*dealloc)(op);
   1999         --_PyTrash_delete_nesting;
   2000     }
   2001 }
   2002 
   2003 /* The equivalent API, using per-thread state recursion info */
   2004 void
   2005 _PyTrash_thread_destroy_chain(void)
   2006 {
   2007     PyThreadState *tstate = PyThreadState_GET();
   2008     while (tstate->trash_delete_later) {
   2009         PyObject *op = tstate->trash_delete_later;
   2010         destructor dealloc = Py_TYPE(op)->tp_dealloc;
   2011 
   2012         tstate->trash_delete_later =
   2013             (PyObject*) _Py_AS_GC(op)->gc.gc_prev;
   2014 
   2015         /* Call the deallocator directly.  This used to try to
   2016          * fool Py_DECREF into calling it indirectly, but
   2017          * Py_DECREF was already called on this object, and in
   2018          * assorted non-release builds calling Py_DECREF again ends
   2019          * up distorting allocation statistics.
   2020          */
   2021         assert(op->ob_refcnt == 0);
   2022         ++tstate->trash_delete_nesting;
   2023         (*dealloc)(op);
   2024         --tstate->trash_delete_nesting;
   2025     }
   2026 }
   2027 
   2028 #ifndef Py_TRACE_REFS
   2029 /* For Py_LIMITED_API, we need an out-of-line version of _Py_Dealloc.
   2030    Define this here, so we can undefine the macro. */
   2031 #undef _Py_Dealloc
   2032 PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
   2033 void
   2034 _Py_Dealloc(PyObject *op)
   2035 {
   2036     _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA
   2037     (*Py_TYPE(op)->tp_dealloc)(op);
   2038 }
   2039 #endif
   2040 
   2041 #ifdef __cplusplus
   2042 }
   2043 #endif
   2044