LCOV - code coverage report
Current view: top level - Objects - object.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 754 953 79.1 %
Date: 2022-07-07 18:19:46 Functions: 72 86 83.7 %

          Line data    Source code
       1             : 
       2             : /* Generic object operations; and implementation of None */
       3             : 
       4             : #include "Python.h"
       5             : #include "pycore_call.h"          // _PyObject_CallNoArgs()
       6             : #include "pycore_ceval.h"         // _Py_EnterRecursiveCallTstate()
       7             : #include "pycore_context.h"       // _PyContextTokenMissing_Type
       8             : #include "pycore_dict.h"          // _PyObject_MakeDictFromInstanceAttributes()
       9             : #include "pycore_floatobject.h"   // _PyFloat_DebugMallocStats()
      10             : #include "pycore_initconfig.h"    // _PyStatus_EXCEPTION()
      11             : #include "pycore_namespace.h"     // _PyNamespace_Type
      12             : #include "pycore_object.h"        // _PyType_CheckConsistency(), _Py_FatalRefcountError()
      13             : #include "pycore_pyerrors.h"      // _PyErr_Occurred()
      14             : #include "pycore_pymem.h"         // _PyMem_IsPtrFreed()
      15             : #include "pycore_pystate.h"       // _PyThreadState_GET()
      16             : #include "pycore_symtable.h"      // PySTEntry_Type
      17             : #include "pycore_typeobject.h"    // _PyTypes_InitSlotDefs()
      18             : #include "pycore_unionobject.h"   // _PyUnion_Type
      19             : #include "pycore_interpreteridobject.h"  // _PyInterpreterID_Type
      20             : 
      21             : #ifdef Py_LIMITED_API
      22             :    // Prevent recursive call _Py_IncRef() <=> Py_INCREF()
      23             : #  error "Py_LIMITED_API macro must not be defined"
      24             : #endif
      25             : 
      26             : #ifdef __cplusplus
      27             : extern "C" {
      28             : #endif
      29             : 
      30             : /* Defined in tracemalloc.c */
      31             : extern void _PyMem_DumpTraceback(int fd, const void *ptr);
      32             : 
      33             : 
      34             : int
      35           0 : _PyObject_CheckConsistency(PyObject *op, int check_content)
      36             : {
      37             : #define CHECK(expr) \
      38             :     do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
      39             : 
      40           0 :     CHECK(!_PyObject_IsFreed(op));
      41           0 :     CHECK(Py_REFCNT(op) >= 1);
      42             : 
      43           0 :     _PyType_CheckConsistency(Py_TYPE(op));
      44             : 
      45           0 :     if (PyUnicode_Check(op)) {
      46           0 :         _PyUnicode_CheckConsistency(op, check_content);
      47             :     }
      48           0 :     else if (PyDict_Check(op)) {
      49           0 :         _PyDict_CheckConsistency(op, check_content);
      50             :     }
      51           0 :     return 1;
      52             : 
      53             : #undef CHECK
      54             : }
      55             : 
      56             : 
      57             : #ifdef Py_REF_DEBUG
      58             : Py_ssize_t _Py_RefTotal;
      59             : 
      60             : Py_ssize_t
      61         119 : _Py_GetRefTotal(void)
      62             : {
      63         119 :     return _Py_RefTotal;
      64             : }
      65             : 
      66             : void
      67           7 : _PyDebug_PrintTotalRefs(void) {
      68           7 :     fprintf(stderr,
      69             :             "[%zd refs, %zd blocks]\n",
      70             :             _Py_GetRefTotal(), _Py_GetAllocatedBlocks());
      71           7 : }
      72             : #endif /* Py_REF_DEBUG */
      73             : 
      74             : /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
      75             :    These are used by the individual routines for object creation.
      76             :    Do not call them otherwise, they do not initialize the object! */
      77             : 
      78             : #ifdef Py_TRACE_REFS
      79             : /* Head of circular doubly-linked list of all objects.  These are linked
      80             :  * together via the _ob_prev and _ob_next members of a PyObject, which
      81             :  * exist only in a Py_TRACE_REFS build.
      82             :  */
      83             : static PyObject refchain = {&refchain, &refchain};
      84             : 
      85             : /* Insert op at the front of the list of all objects.  If force is true,
      86             :  * op is added even if _ob_prev and _ob_next are non-NULL already.  If
      87             :  * force is false amd _ob_prev or _ob_next are non-NULL, do nothing.
      88             :  * force should be true if and only if op points to freshly allocated,
      89             :  * uninitialized memory, or you've unlinked op from the list and are
      90             :  * relinking it into the front.
      91             :  * Note that objects are normally added to the list via _Py_NewReference,
      92             :  * which is called by PyObject_Init.  Not all objects are initialized that
      93             :  * way, though; exceptions include statically allocated type objects, and
      94             :  * statically allocated singletons (like Py_True and Py_None).
      95             :  */
      96             : void
      97             : _Py_AddToAllObjects(PyObject *op, int force)
      98             : {
      99             : #ifdef  Py_DEBUG
     100             :     if (!force) {
     101             :         /* If it's initialized memory, op must be in or out of
     102             :          * the list unambiguously.
     103             :          */
     104             :         _PyObject_ASSERT(op, (op->_ob_prev == NULL) == (op->_ob_next == NULL));
     105             :     }
     106             : #endif
     107             :     if (force || op->_ob_prev == NULL) {
     108             :         op->_ob_next = refchain._ob_next;
     109             :         op->_ob_prev = &refchain;
     110             :         refchain._ob_next->_ob_prev = op;
     111             :         refchain._ob_next = op;
     112             :     }
     113             : }
     114             : #endif  /* Py_TRACE_REFS */
     115             : 
     116             : #ifdef Py_REF_DEBUG
     117             : /* Log a fatal error; doesn't return. */
     118             : void
     119           0 : _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
     120             : {
     121           0 :     _PyObject_AssertFailed(op, NULL, "object has negative ref count",
     122             :                            filename, lineno, __func__);
     123             : }
     124             : 
     125             : #endif /* Py_REF_DEBUG */
     126             : 
     127             : void
     128           1 : Py_IncRef(PyObject *o)
     129             : {
     130           1 :     Py_XINCREF(o);
     131           1 : }
     132             : 
     133             : void
     134           6 : Py_DecRef(PyObject *o)
     135             : {
     136           6 :     Py_XDECREF(o);
     137           6 : }
     138             : 
     139             : void
     140           6 : _Py_IncRef(PyObject *o)
     141             : {
     142           6 :     Py_INCREF(o);
     143           6 : }
     144             : 
     145             : void
     146          13 : _Py_DecRef(PyObject *o)
     147             : {
     148          13 :     Py_DECREF(o);
     149          13 : }
     150             : 
     151             : PyObject *
     152           0 : PyObject_Init(PyObject *op, PyTypeObject *tp)
     153             : {
     154           0 :     if (op == NULL) {
     155           0 :         return PyErr_NoMemory();
     156             :     }
     157             : 
     158           0 :     _PyObject_Init(op, tp);
     159           0 :     return op;
     160             : }
     161             : 
     162             : PyVarObject *
     163           0 : PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
     164             : {
     165           0 :     if (op == NULL) {
     166           0 :         return (PyVarObject *) PyErr_NoMemory();
     167             :     }
     168             : 
     169           0 :     _PyObject_InitVar(op, tp, size);
     170           0 :     return op;
     171             : }
     172             : 
     173             : PyObject *
     174     4609460 : _PyObject_New(PyTypeObject *tp)
     175             : {
     176     4609460 :     PyObject *op = (PyObject *) PyObject_Malloc(_PyObject_SIZE(tp));
     177     4609460 :     if (op == NULL) {
     178           0 :         return PyErr_NoMemory();
     179             :     }
     180     4609460 :     _PyObject_Init(op, tp);
     181     4609460 :     return op;
     182             : }
     183             : 
     184             : PyVarObject *
     185     9004280 : _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
     186             : {
     187             :     PyVarObject *op;
     188     9004280 :     const size_t size = _PyObject_VAR_SIZE(tp, nitems);
     189     9004280 :     op = (PyVarObject *) PyObject_Malloc(size);
     190     9004280 :     if (op == NULL) {
     191           0 :         return (PyVarObject *)PyErr_NoMemory();
     192             :     }
     193     9004280 :     _PyObject_InitVar(op, tp, nitems);
     194     9004280 :     return op;
     195             : }
     196             : 
     197             : void
     198    22756500 : PyObject_CallFinalizer(PyObject *self)
     199             : {
     200    22756500 :     PyTypeObject *tp = Py_TYPE(self);
     201             : 
     202    22756500 :     if (tp->tp_finalize == NULL)
     203           0 :         return;
     204             :     /* tp_finalize should only be called once. */
     205    22756500 :     if (_PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
     206      225841 :         return;
     207             : 
     208    22530700 :     tp->tp_finalize(self);
     209    22530700 :     if (_PyType_IS_GC(tp)) {
     210    22347800 :         _PyGC_SET_FINALIZED(self);
     211             :     }
     212             : }
     213             : 
     214             : int
     215    22756500 : PyObject_CallFinalizerFromDealloc(PyObject *self)
     216             : {
     217    22756500 :     if (Py_REFCNT(self) != 0) {
     218           0 :         _PyObject_ASSERT_FAILED_MSG(self,
     219             :                                     "PyObject_CallFinalizerFromDealloc called "
     220             :                                     "on object with a non-zero refcount");
     221             :     }
     222             : 
     223             :     /* Temporarily resurrect the object. */
     224    22756500 :     Py_SET_REFCNT(self, 1);
     225             : 
     226    22756500 :     PyObject_CallFinalizer(self);
     227             : 
     228    22756500 :     _PyObject_ASSERT_WITH_MSG(self,
     229             :                               Py_REFCNT(self) > 0,
     230             :                               "refcount is too small");
     231             : 
     232             :     /* Undo the temporary resurrection; can't use DECREF here, it would
     233             :      * cause a recursive call. */
     234    22756500 :     Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
     235    22756500 :     if (Py_REFCNT(self) == 0) {
     236    22756400 :         return 0;         /* this is the normal path out */
     237             :     }
     238             : 
     239             :     /* tp_finalize resurrected it!  Make it look like the original Py_DECREF
     240             :      * never happened. */
     241         111 :     Py_ssize_t refcnt = Py_REFCNT(self);
     242         111 :     _Py_NewReference(self);
     243         111 :     Py_SET_REFCNT(self, refcnt);
     244             : 
     245         111 :     _PyObject_ASSERT(self,
     246             :                      (!_PyType_IS_GC(Py_TYPE(self))
     247             :                       || _PyObject_GC_IS_TRACKED(self)));
     248             :     /* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
     249             :        _Py_RefTotal, so we need to undo that. */
     250             : #ifdef Py_REF_DEBUG
     251         111 :     _Py_RefTotal--;
     252             : #endif
     253         111 :     return -1;
     254             : }
     255             : 
     256             : int
     257         128 : PyObject_Print(PyObject *op, FILE *fp, int flags)
     258             : {
     259         128 :     int ret = 0;
     260         128 :     if (PyErr_CheckSignals())
     261           0 :         return -1;
     262             : #ifdef USE_STACKCHECK
     263             :     if (PyOS_CheckStack()) {
     264             :         PyErr_SetString(PyExc_MemoryError, "stack overflow");
     265             :         return -1;
     266             :     }
     267             : #endif
     268         128 :     clearerr(fp); /* Clear any previous error condition */
     269         128 :     if (op == NULL) {
     270           9 :         Py_BEGIN_ALLOW_THREADS
     271           9 :         fprintf(fp, "<nil>");
     272           9 :         Py_END_ALLOW_THREADS
     273             :     }
     274             :     else {
     275         119 :         if (Py_REFCNT(op) <= 0) {
     276           0 :             Py_BEGIN_ALLOW_THREADS
     277           0 :             fprintf(fp, "<refcnt %zd at %p>", Py_REFCNT(op), (void *)op);
     278           0 :             Py_END_ALLOW_THREADS
     279             :         }
     280             :         else {
     281             :             PyObject *s;
     282         119 :             if (flags & Py_PRINT_RAW)
     283           0 :                 s = PyObject_Str(op);
     284             :             else
     285         119 :                 s = PyObject_Repr(op);
     286         119 :             if (s == NULL)
     287          32 :                 ret = -1;
     288          87 :             else if (PyBytes_Check(s)) {
     289           0 :                 fwrite(PyBytes_AS_STRING(s), 1,
     290           0 :                        PyBytes_GET_SIZE(s), fp);
     291             :             }
     292          87 :             else if (PyUnicode_Check(s)) {
     293             :                 PyObject *t;
     294          87 :                 t = PyUnicode_AsEncodedString(s, "utf-8", "backslashreplace");
     295          87 :                 if (t == NULL) {
     296           0 :                     ret = -1;
     297             :                 }
     298             :                 else {
     299          87 :                     fwrite(PyBytes_AS_STRING(t), 1,
     300          87 :                            PyBytes_GET_SIZE(t), fp);
     301          87 :                     Py_DECREF(t);
     302             :                 }
     303             :             }
     304             :             else {
     305           0 :                 PyErr_Format(PyExc_TypeError,
     306             :                              "str() or repr() returned '%.100s'",
     307           0 :                              Py_TYPE(s)->tp_name);
     308           0 :                 ret = -1;
     309             :             }
     310         119 :             Py_XDECREF(s);
     311             :         }
     312             :     }
     313         128 :     if (ret == 0) {
     314          96 :         if (ferror(fp)) {
     315           0 :             PyErr_SetFromErrno(PyExc_OSError);
     316           0 :             clearerr(fp);
     317           0 :             ret = -1;
     318             :         }
     319             :     }
     320         128 :     return ret;
     321             : }
     322             : 
     323             : /* For debugging convenience.  Set a breakpoint here and call it from your DLL */
     324             : void
     325           0 : _Py_BreakPoint(void)
     326             : {
     327           0 : }
     328             : 
     329             : 
     330             : /* Heuristic checking if the object memory is uninitialized or deallocated.
     331             :    Rely on the debug hooks on Python memory allocators:
     332             :    see _PyMem_IsPtrFreed().
     333             : 
     334             :    The function can be used to prevent segmentation fault on dereferencing
     335             :    pointers like 0xDDDDDDDDDDDDDDDD. */
     336             : int
     337  2894430000 : _PyObject_IsFreed(PyObject *op)
     338             : {
     339  2894430000 :     if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) {
     340           0 :         return 1;
     341             :     }
     342             :     /* ignore op->ob_ref: its value can have be modified
     343             :        by Py_INCREF() and Py_DECREF(). */
     344             : #ifdef Py_TRACE_REFS
     345             :     if (op->_ob_next != NULL && _PyMem_IsPtrFreed(op->_ob_next)) {
     346             :         return 1;
     347             :     }
     348             :     if (op->_ob_prev != NULL && _PyMem_IsPtrFreed(op->_ob_prev)) {
     349             :          return 1;
     350             :      }
     351             : #endif
     352  2894430000 :     return 0;
     353             : }
     354             : 
     355             : 
     356             : /* For debugging convenience.  See Misc/gdbinit for some useful gdb hooks */
     357             : void
     358          33 : _PyObject_Dump(PyObject* op)
     359             : {
     360          33 :     if (_PyObject_IsFreed(op)) {
     361             :         /* It seems like the object memory has been freed:
     362             :            don't access it to prevent a segmentation fault. */
     363           0 :         fprintf(stderr, "<object at %p is freed>\n", op);
     364           0 :         fflush(stderr);
     365           0 :         return;
     366             :     }
     367             : 
     368             :     /* first, write fields which are the least likely to crash */
     369          33 :     fprintf(stderr, "object address  : %p\n", (void *)op);
     370          33 :     fprintf(stderr, "object refcount : %zd\n", Py_REFCNT(op));
     371          33 :     fflush(stderr);
     372             : 
     373          33 :     PyTypeObject *type = Py_TYPE(op);
     374          33 :     fprintf(stderr, "object type     : %p\n", type);
     375          33 :     fprintf(stderr, "object type name: %s\n",
     376             :             type==NULL ? "NULL" : type->tp_name);
     377             : 
     378             :     /* the most dangerous part */
     379          33 :     fprintf(stderr, "object repr     : ");
     380          33 :     fflush(stderr);
     381             : 
     382          33 :     PyGILState_STATE gil = PyGILState_Ensure();
     383             :     PyObject *error_type, *error_value, *error_traceback;
     384          33 :     PyErr_Fetch(&error_type, &error_value, &error_traceback);
     385             : 
     386          33 :     (void)PyObject_Print(op, stderr, 0);
     387          33 :     fflush(stderr);
     388             : 
     389          33 :     PyErr_Restore(error_type, error_value, error_traceback);
     390          33 :     PyGILState_Release(gil);
     391             : 
     392          33 :     fprintf(stderr, "\n");
     393          33 :     fflush(stderr);
     394             : }
     395             : 
     396             : PyObject *
     397     5075680 : PyObject_Repr(PyObject *v)
     398             : {
     399             :     PyObject *res;
     400     5075680 :     if (PyErr_CheckSignals())
     401           0 :         return NULL;
     402             : #ifdef USE_STACKCHECK
     403             :     if (PyOS_CheckStack()) {
     404             :         PyErr_SetString(PyExc_MemoryError, "stack overflow");
     405             :         return NULL;
     406             :     }
     407             : #endif
     408     5075680 :     if (v == NULL)
     409           1 :         return PyUnicode_FromString("<NULL>");
     410     5075680 :     if (Py_TYPE(v)->tp_repr == NULL)
     411           0 :         return PyUnicode_FromFormat("<%s object at %p>",
     412           0 :                                     Py_TYPE(v)->tp_name, v);
     413             : 
     414     5075680 :     PyThreadState *tstate = _PyThreadState_GET();
     415             : #ifdef Py_DEBUG
     416             :     /* PyObject_Repr() must not be called with an exception set,
     417             :        because it can clear it (directly or indirectly) and so the
     418             :        caller loses its exception */
     419     5075680 :     assert(!_PyErr_Occurred(tstate));
     420             : #endif
     421             : 
     422             :     /* It is possible for a type to have a tp_repr representation that loops
     423             :        infinitely. */
     424     5075680 :     if (_Py_EnterRecursiveCallTstate(tstate,
     425             :                                      " while getting the repr of an object")) {
     426           3 :         return NULL;
     427             :     }
     428     5075680 :     res = (*Py_TYPE(v)->tp_repr)(v);
     429     5075680 :     _Py_LeaveRecursiveCallTstate(tstate);
     430             : 
     431     5075680 :     if (res == NULL) {
     432        6216 :         return NULL;
     433             :     }
     434     5069460 :     if (!PyUnicode_Check(res)) {
     435           3 :         _PyErr_Format(tstate, PyExc_TypeError,
     436             :                       "__repr__ returned non-string (type %.200s)",
     437           3 :                       Py_TYPE(res)->tp_name);
     438           3 :         Py_DECREF(res);
     439           3 :         return NULL;
     440             :     }
     441             : #ifndef Py_DEBUG
     442             :     if (PyUnicode_READY(res) < 0) {
     443             :         return NULL;
     444             :     }
     445             : #endif
     446     5069460 :     return res;
     447             : }
     448             : 
     449             : PyObject *
     450     6697080 : PyObject_Str(PyObject *v)
     451             : {
     452             :     PyObject *res;
     453     6697080 :     if (PyErr_CheckSignals())
     454           0 :         return NULL;
     455             : #ifdef USE_STACKCHECK
     456             :     if (PyOS_CheckStack()) {
     457             :         PyErr_SetString(PyExc_MemoryError, "stack overflow");
     458             :         return NULL;
     459             :     }
     460             : #endif
     461     6697080 :     if (v == NULL)
     462           1 :         return PyUnicode_FromString("<NULL>");
     463     6697080 :     if (PyUnicode_CheckExact(v)) {
     464             : #ifndef Py_DEBUG
     465             :         if (PyUnicode_READY(v) < 0)
     466             :             return NULL;
     467             : #endif
     468     2187570 :         Py_INCREF(v);
     469     2187570 :         return v;
     470             :     }
     471     4509510 :     if (Py_TYPE(v)->tp_str == NULL)
     472           0 :         return PyObject_Repr(v);
     473             : 
     474     4509510 :     PyThreadState *tstate = _PyThreadState_GET();
     475             : #ifdef Py_DEBUG
     476             :     /* PyObject_Str() must not be called with an exception set,
     477             :        because it can clear it (directly or indirectly) and so the
     478             :        caller loses its exception */
     479     4509510 :     assert(!_PyErr_Occurred(tstate));
     480             : #endif
     481             : 
     482             :     /* It is possible for a type to have a tp_str representation that loops
     483             :        infinitely. */
     484     4509510 :     if (_Py_EnterRecursiveCallTstate(tstate, " while getting the str of an object")) {
     485           0 :         return NULL;
     486             :     }
     487     4509510 :     res = (*Py_TYPE(v)->tp_str)(v);
     488     4509510 :     _Py_LeaveRecursiveCallTstate(tstate);
     489             : 
     490     4509510 :     if (res == NULL) {
     491          31 :         return NULL;
     492             :     }
     493     4509480 :     if (!PyUnicode_Check(res)) {
     494           1 :         _PyErr_Format(tstate, PyExc_TypeError,
     495             :                       "__str__ returned non-string (type %.200s)",
     496           1 :                       Py_TYPE(res)->tp_name);
     497           1 :         Py_DECREF(res);
     498           1 :         return NULL;
     499             :     }
     500             : #ifndef Py_DEBUG
     501             :     if (PyUnicode_READY(res) < 0) {
     502             :         return NULL;
     503             :     }
     504             : #endif
     505     4509480 :     assert(_PyUnicode_CheckConsistency(res, 1));
     506     4509480 :     return res;
     507             : }
     508             : 
     509             : PyObject *
     510        5983 : PyObject_ASCII(PyObject *v)
     511             : {
     512             :     PyObject *repr, *ascii, *res;
     513             : 
     514        5983 :     repr = PyObject_Repr(v);
     515        5983 :     if (repr == NULL)
     516           1 :         return NULL;
     517             : 
     518        5982 :     if (PyUnicode_IS_ASCII(repr))
     519        2367 :         return repr;
     520             : 
     521             :     /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
     522        3615 :     ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
     523        3615 :     Py_DECREF(repr);
     524        3615 :     if (ascii == NULL)
     525           0 :         return NULL;
     526             : 
     527        7230 :     res = PyUnicode_DecodeASCII(
     528        3615 :         PyBytes_AS_STRING(ascii),
     529             :         PyBytes_GET_SIZE(ascii),
     530             :         NULL);
     531             : 
     532        3615 :     Py_DECREF(ascii);
     533        3615 :     return res;
     534             : }
     535             : 
     536             : PyObject *
     537      820198 : PyObject_Bytes(PyObject *v)
     538             : {
     539             :     PyObject *result, *func;
     540             : 
     541      820198 :     if (v == NULL)
     542           1 :         return PyBytes_FromString("<NULL>");
     543             : 
     544      820197 :     if (PyBytes_CheckExact(v)) {
     545      818634 :         Py_INCREF(v);
     546      818634 :         return v;
     547             :     }
     548             : 
     549        1563 :     func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__));
     550        1563 :     if (func != NULL) {
     551           0 :         result = _PyObject_CallNoArgs(func);
     552           0 :         Py_DECREF(func);
     553           0 :         if (result == NULL)
     554           0 :             return NULL;
     555           0 :         if (!PyBytes_Check(result)) {
     556           0 :             PyErr_Format(PyExc_TypeError,
     557             :                          "__bytes__ returned non-bytes (type %.200s)",
     558           0 :                          Py_TYPE(result)->tp_name);
     559           0 :             Py_DECREF(result);
     560           0 :             return NULL;
     561             :         }
     562           0 :         return result;
     563             :     }
     564        1563 :     else if (PyErr_Occurred())
     565           0 :         return NULL;
     566        1563 :     return PyBytes_FromObject(v);
     567             : }
     568             : 
     569             : 
     570             : /*
     571             : def _PyObject_FunctionStr(x):
     572             :     try:
     573             :         qualname = x.__qualname__
     574             :     except AttributeError:
     575             :         return str(x)
     576             :     try:
     577             :         mod = x.__module__
     578             :         if mod is not None and mod != 'builtins':
     579             :             return f"{x.__module__}.{qualname}()"
     580             :     except AttributeError:
     581             :         pass
     582             :     return qualname
     583             : */
     584             : PyObject *
     585         552 : _PyObject_FunctionStr(PyObject *x)
     586             : {
     587         552 :     assert(!PyErr_Occurred());
     588             :     PyObject *qualname;
     589         552 :     int ret = _PyObject_LookupAttr(x, &_Py_ID(__qualname__), &qualname);
     590         552 :     if (qualname == NULL) {
     591           2 :         if (ret < 0) {
     592           0 :             return NULL;
     593             :         }
     594           2 :         return PyObject_Str(x);
     595             :     }
     596             :     PyObject *module;
     597         550 :     PyObject *result = NULL;
     598         550 :     ret = _PyObject_LookupAttr(x, &_Py_ID(__module__), &module);
     599         550 :     if (module != NULL && module != Py_None) {
     600         147 :         ret = PyObject_RichCompareBool(module, &_Py_ID(builtins), Py_NE);
     601         147 :         if (ret < 0) {
     602             :             // error
     603           0 :             goto done;
     604             :         }
     605         147 :         if (ret > 0) {
     606         128 :             result = PyUnicode_FromFormat("%S.%S()", module, qualname);
     607         128 :             goto done;
     608             :         }
     609             :     }
     610         403 :     else if (ret < 0) {
     611           0 :         goto done;
     612             :     }
     613         422 :     result = PyUnicode_FromFormat("%S()", qualname);
     614         550 : done:
     615         550 :     Py_DECREF(qualname);
     616         550 :     Py_XDECREF(module);
     617         550 :     return result;
     618             : }
     619             : 
     620             : /* For Python 3.0.1 and later, the old three-way comparison has been
     621             :    completely removed in favour of rich comparisons.  PyObject_Compare() and
     622             :    PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
     623             :    The old tp_compare slot has been renamed to tp_as_async, and should no
     624             :    longer be used.  Use tp_richcompare instead.
     625             : 
     626             :    See (*) below for practical amendments.
     627             : 
     628             :    tp_richcompare gets called with a first argument of the appropriate type
     629             :    and a second object of an arbitrary type.  We never do any kind of
     630             :    coercion.
     631             : 
     632             :    The tp_richcompare slot should return an object, as follows:
     633             : 
     634             :     NULL if an exception occurred
     635             :     NotImplemented if the requested comparison is not implemented
     636             :     any other false value if the requested comparison is false
     637             :     any other true value if the requested comparison is true
     638             : 
     639             :   The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
     640             :   NotImplemented.
     641             : 
     642             :   (*) Practical amendments:
     643             : 
     644             :   - If rich comparison returns NotImplemented, == and != are decided by
     645             :     comparing the object pointer (i.e. falling back to the base object
     646             :     implementation).
     647             : 
     648             : */
     649             : 
     650             : /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
     651             : int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
     652             : 
     653             : static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
     654             : 
     655             : /* Perform a rich comparison, raising TypeError when the requested comparison
     656             :    operator is not supported. */
     657             : static PyObject *
     658   197122000 : do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
     659             : {
     660             :     richcmpfunc f;
     661             :     PyObject *res;
     662   197122000 :     int checked_reverse_op = 0;
     663             : 
     664   203289000 :     if (!Py_IS_TYPE(v, Py_TYPE(w)) &&
     665     6167390 :         PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) &&
     666      169946 :         (f = Py_TYPE(w)->tp_richcompare) != NULL) {
     667      169946 :         checked_reverse_op = 1;
     668      169946 :         res = (*f)(w, v, _Py_SwappedOp[op]);
     669      169946 :         if (res != Py_NotImplemented)
     670       54197 :             return res;
     671      115749 :         Py_DECREF(res);
     672             :     }
     673   197068000 :     if ((f = Py_TYPE(v)->tp_richcompare) != NULL) {
     674   197068000 :         res = (*f)(v, w, op);
     675   197068000 :         if (res != Py_NotImplemented)
     676   189709000 :             return res;
     677     7358550 :         Py_DECREF(res);
     678             :     }
     679     7358560 :     if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) {
     680     7242810 :         res = (*f)(w, v, _Py_SwappedOp[op]);
     681     7242810 :         if (res != Py_NotImplemented)
     682      184921 :             return res;
     683     7057890 :         Py_DECREF(res);
     684             :     }
     685             :     /* If neither object implements it, provide a sensible default
     686             :        for == and !=, but raise an exception for ordering. */
     687     7173630 :     switch (op) {
     688     7147130 :     case Py_EQ:
     689     7147130 :         res = (v == w) ? Py_True : Py_False;
     690     7147130 :         break;
     691       24145 :     case Py_NE:
     692       24145 :         res = (v != w) ? Py_True : Py_False;
     693       24145 :         break;
     694        2358 :     default:
     695        2358 :         _PyErr_Format(tstate, PyExc_TypeError,
     696             :                       "'%s' not supported between instances of '%.100s' and '%.100s'",
     697             :                       opstrings[op],
     698        2358 :                       Py_TYPE(v)->tp_name,
     699        2358 :                       Py_TYPE(w)->tp_name);
     700        2358 :         return NULL;
     701             :     }
     702     7171280 :     Py_INCREF(res);
     703     7171280 :     return res;
     704             : }
     705             : 
     706             : /* Perform a rich comparison with object result.  This wraps do_richcompare()
     707             :    with a check for NULL arguments and a recursion check. */
     708             : 
     709             : PyObject *
     710   197122000 : PyObject_RichCompare(PyObject *v, PyObject *w, int op)
     711             : {
     712   197122000 :     PyThreadState *tstate = _PyThreadState_GET();
     713             : 
     714   197122000 :     assert(Py_LT <= op && op <= Py_GE);
     715   197122000 :     if (v == NULL || w == NULL) {
     716           0 :         if (!_PyErr_Occurred(tstate)) {
     717           0 :             PyErr_BadInternalCall();
     718             :         }
     719           0 :         return NULL;
     720             :     }
     721   197122000 :     if (_Py_EnterRecursiveCallTstate(tstate, " in comparison")) {
     722          15 :         return NULL;
     723             :     }
     724   197122000 :     PyObject *res = do_richcompare(tstate, v, w, op);
     725   197122000 :     _Py_LeaveRecursiveCallTstate(tstate);
     726   197122000 :     return res;
     727             : }
     728             : 
     729             : /* Perform a rich comparison with integer result.  This wraps
     730             :    PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
     731             : int
     732   171426000 : PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
     733             : {
     734             :     PyObject *res;
     735             :     int ok;
     736             : 
     737             :     /* Quick result when objects are the same.
     738             :        Guarantees that identity implies equality. */
     739   171426000 :     if (v == w) {
     740    34158200 :         if (op == Py_EQ)
     741    31998900 :             return 1;
     742     2159340 :         else if (op == Py_NE)
     743           8 :             return 0;
     744             :     }
     745             : 
     746   139427000 :     res = PyObject_RichCompare(v, w, op);
     747   139427000 :     if (res == NULL)
     748       20042 :         return -1;
     749   139407000 :     if (PyBool_Check(res))
     750   139407000 :         ok = (res == Py_True);
     751             :     else
     752          16 :         ok = PyObject_IsTrue(res);
     753   139407000 :     Py_DECREF(res);
     754   139407000 :     return ok;
     755             : }
     756             : 
     757             : Py_hash_t
     758         168 : PyObject_HashNotImplemented(PyObject *v)
     759             : {
     760         168 :     PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
     761         168 :                  Py_TYPE(v)->tp_name);
     762         168 :     return -1;
     763             : }
     764             : 
     765             : Py_hash_t
     766   316197000 : PyObject_Hash(PyObject *v)
     767             : {
     768   316197000 :     PyTypeObject *tp = Py_TYPE(v);
     769   316197000 :     if (tp->tp_hash != NULL)
     770   316197000 :         return (*tp->tp_hash)(v);
     771             :     /* To keep to the general practice that inheriting
     772             :      * solely from object in C code should work without
     773             :      * an explicit call to PyType_Ready, we implicitly call
     774             :      * PyType_Ready here and then check the tp_hash slot again
     775             :      */
     776           1 :     if (tp->tp_dict == NULL) {
     777           1 :         if (PyType_Ready(tp) < 0)
     778           0 :             return -1;
     779           1 :         if (tp->tp_hash != NULL)
     780           1 :             return (*tp->tp_hash)(v);
     781             :     }
     782             :     /* Otherwise, the object can't be hashed */
     783           0 :     return PyObject_HashNotImplemented(v);
     784             : }
     785             : 
     786             : PyObject *
     787     4021490 : PyObject_GetAttrString(PyObject *v, const char *name)
     788             : {
     789             :     PyObject *w, *res;
     790             : 
     791     4021490 :     if (Py_TYPE(v)->tp_getattr != NULL)
     792           0 :         return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
     793     4021490 :     w = PyUnicode_FromString(name);
     794     4021490 :     if (w == NULL)
     795          31 :         return NULL;
     796     4021460 :     res = PyObject_GetAttr(v, w);
     797     4021460 :     Py_DECREF(w);
     798     4021460 :     return res;
     799             : }
     800             : 
     801             : int
     802           0 : PyObject_HasAttrString(PyObject *v, const char *name)
     803             : {
     804           0 :     PyObject *res = PyObject_GetAttrString(v, name);
     805           0 :     if (res != NULL) {
     806           0 :         Py_DECREF(res);
     807           0 :         return 1;
     808             :     }
     809           0 :     PyErr_Clear();
     810           0 :     return 0;
     811             : }
     812             : 
     813             : int
     814     1936270 : PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
     815             : {
     816             :     PyObject *s;
     817             :     int res;
     818             : 
     819     1936270 :     if (Py_TYPE(v)->tp_setattr != NULL)
     820           0 :         return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
     821     1936270 :     s = PyUnicode_InternFromString(name);
     822     1936270 :     if (s == NULL)
     823           0 :         return -1;
     824     1936270 :     res = PyObject_SetAttr(v, s, w);
     825     1936270 :     Py_XDECREF(s);
     826     1936270 :     return res;
     827             : }
     828             : 
     829             : int
     830     2012670 : _PyObject_IsAbstract(PyObject *obj)
     831             : {
     832             :     int res;
     833             :     PyObject* isabstract;
     834             : 
     835     2012670 :     if (obj == NULL)
     836       30956 :         return 0;
     837             : 
     838     1981710 :     res = _PyObject_LookupAttr(obj, &_Py_ID(__isabstractmethod__), &isabstract);
     839     1981710 :     if (res > 0) {
     840      282726 :         res = PyObject_IsTrue(isabstract);
     841      282726 :         Py_DECREF(isabstract);
     842             :     }
     843     1981710 :     return res;
     844             : }
     845             : 
     846             : PyObject *
     847       29650 : _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
     848             : {
     849             :     PyObject *result;
     850       29650 :     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
     851       29650 :     if (!oname)
     852           0 :         return NULL;
     853       29650 :     result = PyObject_GetAttr(v, oname);
     854       29650 :     return result;
     855             : }
     856             : 
     857             : int
     858         941 : _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
     859             : {
     860             :     int result;
     861         941 :     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
     862         941 :     if (!oname)
     863           0 :         return -1;
     864         941 :     result = PyObject_SetAttr(v, oname, w);
     865         941 :     return result;
     866             : }
     867             : 
     868             : static inline int
     869     1512360 : set_attribute_error_context(PyObject* v, PyObject* name)
     870             : {
     871     1512360 :     assert(PyErr_Occurred());
     872     1512360 :     if (!PyErr_ExceptionMatches(PyExc_AttributeError)){
     873         740 :         return 0;
     874             :     }
     875             :     // Intercept AttributeError exceptions and augment them to offer suggestions later.
     876             :     PyObject *type, *value, *traceback;
     877     1511620 :     PyErr_Fetch(&type, &value, &traceback);
     878     1511620 :     PyErr_NormalizeException(&type, &value, &traceback);
     879             :     // Check if the normalized exception is indeed an AttributeError
     880     1511620 :     if (!PyErr_GivenExceptionMatches(value, PyExc_AttributeError)) {
     881           1 :         goto restore;
     882             :     }
     883     1511620 :     PyAttributeErrorObject* the_exc = (PyAttributeErrorObject*) value;
     884             :     // Check if this exception was already augmented
     885     1511620 :     if (the_exc->name || the_exc->obj) {
     886         136 :         goto restore;
     887             :     }
     888             :     // Augment the exception with the name and object
     889     3022960 :     if (PyObject_SetAttr(value, &_Py_ID(name), name) ||
     890     1511480 :         PyObject_SetAttr(value, &_Py_ID(obj), v)) {
     891           0 :         return 1;
     892             :     }
     893     1511480 : restore:
     894     1511620 :     PyErr_Restore(type, value, traceback);
     895     1511620 :     return 0;
     896             : }
     897             : 
     898             : PyObject *
     899   171930000 : PyObject_GetAttr(PyObject *v, PyObject *name)
     900             : {
     901   171930000 :     PyTypeObject *tp = Py_TYPE(v);
     902   171930000 :     if (!PyUnicode_Check(name)) {
     903           4 :         PyErr_Format(PyExc_TypeError,
     904             :                      "attribute name must be string, not '%.200s'",
     905           4 :                      Py_TYPE(name)->tp_name);
     906           4 :         return NULL;
     907             :     }
     908             : 
     909   171930000 :     PyObject* result = NULL;
     910   171930000 :     if (tp->tp_getattro != NULL) {
     911   171930000 :         result = (*tp->tp_getattro)(v, name);
     912             :     }
     913           0 :     else if (tp->tp_getattr != NULL) {
     914           0 :         const char *name_str = PyUnicode_AsUTF8(name);
     915           0 :         if (name_str == NULL) {
     916           0 :             return NULL;
     917             :         }
     918           0 :         result = (*tp->tp_getattr)(v, (char *)name_str);
     919             :     }
     920             :     else {
     921           0 :         PyErr_Format(PyExc_AttributeError,
     922             :                     "'%.50s' object has no attribute '%U'",
     923             :                     tp->tp_name, name);
     924             :     }
     925             : 
     926   171930000 :     if (result == NULL) {
     927     1499460 :         set_attribute_error_context(v, name);
     928             :     }
     929   171930000 :     return result;
     930             : }
     931             : 
     932             : int
     933    64810900 : _PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
     934             : {
     935    64810900 :     PyTypeObject *tp = Py_TYPE(v);
     936             : 
     937    64810900 :     if (!PyUnicode_Check(name)) {
     938           2 :         PyErr_Format(PyExc_TypeError,
     939             :                      "attribute name must be string, not '%.200s'",
     940           2 :                      Py_TYPE(name)->tp_name);
     941           2 :         *result = NULL;
     942           2 :         return -1;
     943             :     }
     944             : 
     945    64810900 :     if (tp->tp_getattro == PyObject_GenericGetAttr) {
     946    54141000 :         *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
     947    54141000 :         if (*result != NULL) {
     948    42173700 :             return 1;
     949             :         }
     950    11967400 :         if (PyErr_Occurred()) {
     951         607 :             return -1;
     952             :         }
     953    11966700 :         return 0;
     954             :     }
     955    10669900 :     if (tp->tp_getattro != NULL) {
     956    10669900 :         *result = (*tp->tp_getattro)(v, name);
     957             :     }
     958           0 :     else if (tp->tp_getattr != NULL) {
     959           0 :         const char *name_str = PyUnicode_AsUTF8(name);
     960           0 :         if (name_str == NULL) {
     961           0 :             *result = NULL;
     962           0 :             return -1;
     963             :         }
     964           0 :         *result = (*tp->tp_getattr)(v, (char *)name_str);
     965             :     }
     966             :     else {
     967           0 :         *result = NULL;
     968           0 :         return 0;
     969             :     }
     970             : 
     971    10669900 :     if (*result != NULL) {
     972     8345050 :         return 1;
     973             :     }
     974     2324820 :     if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
     975          26 :         return -1;
     976             :     }
     977     2324790 :     PyErr_Clear();
     978     2324790 :     return 0;
     979             : }
     980             : 
     981             : int
     982       20058 : _PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
     983             : {
     984       20058 :     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
     985       20058 :     if (!oname) {
     986           0 :         *result = NULL;
     987           0 :         return -1;
     988             :     }
     989       20058 :     return  _PyObject_LookupAttr(v, oname, result);
     990             : }
     991             : 
     992             : int
     993         877 : PyObject_HasAttr(PyObject *v, PyObject *name)
     994             : {
     995             :     PyObject *res;
     996         877 :     if (_PyObject_LookupAttr(v, name, &res) < 0) {
     997           0 :         PyErr_Clear();
     998           0 :         return 0;
     999             :     }
    1000         877 :     if (res == NULL) {
    1001         825 :         return 0;
    1002             :     }
    1003          52 :     Py_DECREF(res);
    1004          52 :     return 1;
    1005             : }
    1006             : 
    1007             : int
    1008    70122300 : PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
    1009             : {
    1010    70122300 :     PyTypeObject *tp = Py_TYPE(v);
    1011             :     int err;
    1012             : 
    1013    70122300 :     if (!PyUnicode_Check(name)) {
    1014           4 :         PyErr_Format(PyExc_TypeError,
    1015             :                      "attribute name must be string, not '%.200s'",
    1016           4 :                      Py_TYPE(name)->tp_name);
    1017           4 :         return -1;
    1018             :     }
    1019    70122300 :     Py_INCREF(name);
    1020             : 
    1021    70122300 :     PyUnicode_InternInPlace(&name);
    1022    70122300 :     if (tp->tp_setattro != NULL) {
    1023    70122200 :         err = (*tp->tp_setattro)(v, name, value);
    1024    70122200 :         Py_DECREF(name);
    1025    70122200 :         return err;
    1026             :     }
    1027           5 :     if (tp->tp_setattr != NULL) {
    1028           5 :         const char *name_str = PyUnicode_AsUTF8(name);
    1029           5 :         if (name_str == NULL) {
    1030           0 :             Py_DECREF(name);
    1031           0 :             return -1;
    1032             :         }
    1033           5 :         err = (*tp->tp_setattr)(v, (char *)name_str, value);
    1034           5 :         Py_DECREF(name);
    1035           5 :         return err;
    1036             :     }
    1037           0 :     Py_DECREF(name);
    1038           0 :     _PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
    1039           0 :     if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
    1040           0 :         PyErr_Format(PyExc_TypeError,
    1041             :                      "'%.100s' object has no attributes "
    1042             :                      "(%s .%U)",
    1043             :                      tp->tp_name,
    1044             :                      value==NULL ? "del" : "assign to",
    1045             :                      name);
    1046             :     else
    1047           0 :         PyErr_Format(PyExc_TypeError,
    1048             :                      "'%.100s' object has only read-only attributes "
    1049             :                      "(%s .%U)",
    1050             :                      tp->tp_name,
    1051             :                      value==NULL ? "del" : "assign to",
    1052             :                      name);
    1053           0 :     return -1;
    1054             : }
    1055             : 
    1056             : PyObject **
    1057   364132000 : _PyObject_DictPointer(PyObject *obj)
    1058             : {
    1059             :     Py_ssize_t dictoffset;
    1060   364132000 :     PyTypeObject *tp = Py_TYPE(obj);
    1061             : 
    1062   364132000 :     if (tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
    1063   196652000 :         return _PyObject_ManagedDictPointer(obj);
    1064             :     }
    1065   167481000 :     dictoffset = tp->tp_dictoffset;
    1066   167481000 :     if (dictoffset == 0)
    1067    21648800 :         return NULL;
    1068   145832000 :     if (dictoffset < 0) {
    1069    35126100 :         Py_ssize_t tsize = Py_SIZE(obj);
    1070    35126100 :         if (tsize < 0) {
    1071       37572 :             tsize = -tsize;
    1072             :         }
    1073    35126100 :         size_t size = _PyObject_VAR_SIZE(tp, tsize);
    1074             : 
    1075    35126100 :         dictoffset += (long)size;
    1076    35126100 :         _PyObject_ASSERT(obj, dictoffset > 0);
    1077    35126100 :         _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
    1078             :     }
    1079   145832000 :     return (PyObject **) ((char *)obj + dictoffset);
    1080             : }
    1081             : 
    1082             : /* Helper to get a pointer to an object's __dict__ slot, if any.
    1083             :  * Creates the dict from inline attributes if necessary.
    1084             :  * Does not set an exception. */
    1085             : PyObject **
    1086        1231 : _PyObject_GetDictPtr(PyObject *obj)
    1087             : {
    1088        1231 :     if ((Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) {
    1089          45 :         return _PyObject_DictPointer(obj);
    1090             :     }
    1091        1186 :     PyObject **dict_ptr = _PyObject_ManagedDictPointer(obj);
    1092        1186 :     PyDictValues **values_ptr = _PyObject_ValuesPointer(obj);
    1093        1186 :     if (*values_ptr == NULL) {
    1094         612 :         return dict_ptr;
    1095             :     }
    1096         574 :     assert(*dict_ptr == NULL);
    1097         574 :     PyObject *dict = _PyObject_MakeDictFromInstanceAttributes(obj, *values_ptr);
    1098         574 :     if (dict == NULL) {
    1099           0 :         PyErr_Clear();
    1100           0 :         return NULL;
    1101             :     }
    1102         574 :     *values_ptr = NULL;
    1103         574 :     *dict_ptr = dict;
    1104         574 :     return dict_ptr;
    1105             : }
    1106             : 
    1107             : PyObject *
    1108    22665500 : PyObject_SelfIter(PyObject *obj)
    1109             : {
    1110    22665500 :     Py_INCREF(obj);
    1111    22665500 :     return obj;
    1112             : }
    1113             : 
    1114             : /* Helper used when the __next__ method is removed from a type:
    1115             :    tp_iternext is never NULL and can be safely called without checking
    1116             :    on every iteration.
    1117             :  */
    1118             : 
    1119             : PyObject *
    1120           1 : _PyObject_NextNotImplemented(PyObject *self)
    1121             : {
    1122           1 :     PyErr_Format(PyExc_TypeError,
    1123             :                  "'%.200s' object is not iterable",
    1124           1 :                  Py_TYPE(self)->tp_name);
    1125           1 :     return NULL;
    1126             : }
    1127             : 
    1128             : 
    1129             : /* Specialized version of _PyObject_GenericGetAttrWithDict
    1130             :    specifically for the LOAD_METHOD opcode.
    1131             : 
    1132             :    Return 1 if a method is found, 0 if it's a regular attribute
    1133             :    from __dict__ or something returned by using a descriptor
    1134             :    protocol.
    1135             : 
    1136             :    `method` will point to the resolved attribute or NULL.  In the
    1137             :    latter case, an error will be set.
    1138             : */
    1139             : int
    1140    44283800 : _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
    1141             : {
    1142    44283800 :     int meth_found = 0;
    1143             : 
    1144    44283800 :     assert(*method == NULL);
    1145             : 
    1146    44283800 :     PyTypeObject *tp = Py_TYPE(obj);
    1147    44283800 :     if (!_PyType_IsReady(tp)) {
    1148           0 :         if (PyType_Ready(tp) < 0) {
    1149           0 :             return 0;
    1150             :         }
    1151             :     }
    1152             : 
    1153    44283800 :     if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_CheckExact(name)) {
    1154    13375400 :         *method = PyObject_GetAttr(obj, name);
    1155    13375400 :         return 0;
    1156             :     }
    1157             : 
    1158    30908500 :     PyObject *descr = _PyType_Lookup(tp, name);
    1159    30908500 :     descrgetfunc f = NULL;
    1160    30908500 :     if (descr != NULL) {
    1161    30762800 :         Py_INCREF(descr);
    1162    30762800 :         if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
    1163    29965900 :             meth_found = 1;
    1164             :         } else {
    1165      796882 :             f = Py_TYPE(descr)->tp_descr_get;
    1166      796882 :             if (f != NULL && PyDescr_IsData(descr)) {
    1167       21988 :                 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
    1168       21988 :                 Py_DECREF(descr);
    1169       21988 :                 return 0;
    1170             :             }
    1171             :         }
    1172             :     }
    1173             :     PyDictValues *values;
    1174    30886500 :     if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) &&
    1175    16486500 :         (values = *_PyObject_ValuesPointer(obj)))
    1176     9754990 :     {
    1177     9835390 :         assert(*_PyObject_DictPointer(obj) == NULL);
    1178     9835390 :         PyObject *attr = _PyObject_GetInstanceAttribute(obj, values, name);
    1179     9835390 :         if (attr != NULL) {
    1180       80403 :             *method = attr;
    1181       80403 :             Py_XDECREF(descr);
    1182       80403 :             return 0;
    1183             :         }
    1184             :     }
    1185             :     else {
    1186    21051100 :         PyObject **dictptr = _PyObject_DictPointer(obj);
    1187             :         PyObject *dict;
    1188    21051100 :         if (dictptr != NULL && (dict = *dictptr) != NULL) {
    1189    10615900 :             Py_INCREF(dict);
    1190    10615900 :             PyObject *attr = PyDict_GetItemWithError(dict, name);
    1191    10615900 :             if (attr != NULL) {
    1192       94856 :                 *method = Py_NewRef(attr);
    1193       94856 :                 Py_DECREF(dict);
    1194       94856 :                 Py_XDECREF(descr);
    1195       94856 :                 return 0;
    1196             :             }
    1197    10521000 :             Py_DECREF(dict);
    1198             : 
    1199    10521000 :             if (PyErr_Occurred()) {
    1200           0 :                 Py_XDECREF(descr);
    1201           0 :                 return 0;
    1202             :             }
    1203             :         }
    1204             :     }
    1205             : 
    1206    30711200 :     if (meth_found) {
    1207    29923700 :         *method = descr;
    1208    29923700 :         return 1;
    1209             :     }
    1210             : 
    1211      787538 :     if (f != NULL) {
    1212      366966 :         *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
    1213      366966 :         Py_DECREF(descr);
    1214      366966 :         return 0;
    1215             :     }
    1216             : 
    1217      420572 :     if (descr != NULL) {
    1218      407674 :         *method = descr;
    1219      407674 :         return 0;
    1220             :     }
    1221             : 
    1222       12898 :     PyErr_Format(PyExc_AttributeError,
    1223             :                  "'%.50s' object has no attribute '%U'",
    1224             :                  tp->tp_name, name);
    1225             : 
    1226       12898 :     set_attribute_error_context(obj, name);
    1227       12898 :     return 0;
    1228             : }
    1229             : 
    1230             : /* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
    1231             : 
    1232             : PyObject *
    1233   187070000 : _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
    1234             :                                  PyObject *dict, int suppress)
    1235             : {
    1236             :     /* Make sure the logic of _PyObject_GetMethod is in sync with
    1237             :        this method.
    1238             : 
    1239             :        When suppress=1, this function suppresses AttributeError.
    1240             :     */
    1241             : 
    1242   187070000 :     PyTypeObject *tp = Py_TYPE(obj);
    1243   187070000 :     PyObject *descr = NULL;
    1244   187070000 :     PyObject *res = NULL;
    1245             :     descrgetfunc f;
    1246             :     PyObject **dictptr;
    1247             : 
    1248   187070000 :     if (!PyUnicode_Check(name)){
    1249           0 :         PyErr_Format(PyExc_TypeError,
    1250             :                      "attribute name must be string, not '%.200s'",
    1251           0 :                      Py_TYPE(name)->tp_name);
    1252           0 :         return NULL;
    1253             :     }
    1254   187070000 :     Py_INCREF(name);
    1255             : 
    1256   187070000 :     if (tp->tp_dict == NULL) {
    1257           4 :         if (PyType_Ready(tp) < 0)
    1258           0 :             goto done;
    1259             :     }
    1260             : 
    1261   187070000 :     descr = _PyType_Lookup(tp, name);
    1262             : 
    1263   187070000 :     f = NULL;
    1264   187070000 :     if (descr != NULL) {
    1265   110837000 :         Py_INCREF(descr);
    1266   110837000 :         f = Py_TYPE(descr)->tp_descr_get;
    1267   110837000 :         if (f != NULL && PyDescr_IsData(descr)) {
    1268    49725400 :             res = f(descr, obj, (PyObject *)Py_TYPE(obj));
    1269    49822700 :             if (res == NULL && suppress &&
    1270       97380 :                     PyErr_ExceptionMatches(PyExc_AttributeError)) {
    1271       96773 :                 PyErr_Clear();
    1272             :             }
    1273    49725400 :             goto done;
    1274             :         }
    1275             :     }
    1276   137345000 :     if (dict == NULL) {
    1277   137334000 :         if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) &&
    1278    68271600 :             *_PyObject_ValuesPointer(obj))
    1279    34170700 :         {
    1280    53238200 :             PyDictValues **values_ptr = _PyObject_ValuesPointer(obj);
    1281    53238200 :             if (PyUnicode_CheckExact(name)) {
    1282    53238200 :                 assert(*_PyObject_DictPointer(obj) == NULL);
    1283    53238200 :                 res = _PyObject_GetInstanceAttribute(obj, *values_ptr, name);
    1284    53238200 :                 if (res != NULL) {
    1285    19067500 :                     goto done;
    1286             :                 }
    1287             :             }
    1288             :             else {
    1289           2 :                 dictptr = _PyObject_DictPointer(obj);
    1290           2 :                 assert(dictptr != NULL && *dictptr == NULL);
    1291           2 :                 *dictptr = dict = _PyObject_MakeDictFromInstanceAttributes(obj, *values_ptr);
    1292           2 :                 if (dict == NULL) {
    1293           0 :                     res = NULL;
    1294           0 :                     goto done;
    1295             :                 }
    1296           2 :                 *values_ptr = NULL;
    1297             :             }
    1298             :         }
    1299             :         else {
    1300    84095700 :             dictptr = _PyObject_DictPointer(obj);
    1301    84095700 :             if (dictptr) {
    1302    70999000 :                 dict = *dictptr;
    1303             :             }
    1304             :         }
    1305             :     }
    1306   118277000 :     if (dict != NULL) {
    1307    64266200 :         Py_INCREF(dict);
    1308    64266200 :         res = PyDict_GetItemWithError(dict, name);
    1309    64266200 :         if (res != NULL) {
    1310    50062300 :             Py_INCREF(res);
    1311    50062300 :             Py_DECREF(dict);
    1312    50062300 :             goto done;
    1313             :         }
    1314             :         else {
    1315    14203900 :             Py_DECREF(dict);
    1316    14203900 :             if (PyErr_Occurred()) {
    1317           0 :                 if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
    1318           0 :                     PyErr_Clear();
    1319             :                 }
    1320             :                 else {
    1321           0 :                     goto done;
    1322             :                 }
    1323             :             }
    1324             :         }
    1325             :     }
    1326             : 
    1327    68214900 :     if (f != NULL) {
    1328    30748000 :         res = f(descr, obj, (PyObject *)Py_TYPE(obj));
    1329    30748000 :         if (res == NULL && suppress &&
    1330           0 :                 PyErr_ExceptionMatches(PyExc_AttributeError)) {
    1331           0 :             PyErr_Clear();
    1332             :         }
    1333    30748000 :         goto done;
    1334             :     }
    1335             : 
    1336    37467000 :     if (descr != NULL) {
    1337    22208700 :         res = descr;
    1338    22208700 :         descr = NULL;
    1339    22208700 :         goto done;
    1340             :     }
    1341             : 
    1342    15258300 :     if (!suppress) {
    1343     3388290 :         PyErr_Format(PyExc_AttributeError,
    1344             :                      "'%.50s' object has no attribute '%U'",
    1345             :                      tp->tp_name, name);
    1346             :     }
    1347    11870000 :   done:
    1348   187070000 :     Py_XDECREF(descr);
    1349   187070000 :     Py_DECREF(name);
    1350   187070000 :     return res;
    1351             : }
    1352             : 
    1353             : PyObject *
    1354   132918000 : PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
    1355             : {
    1356   132918000 :     return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
    1357             : }
    1358             : 
    1359             : int
    1360    70063300 : _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
    1361             :                                  PyObject *value, PyObject *dict)
    1362             : {
    1363    70063300 :     PyTypeObject *tp = Py_TYPE(obj);
    1364             :     PyObject *descr;
    1365             :     descrsetfunc f;
    1366    70063300 :     int res = -1;
    1367             : 
    1368    70063300 :     if (!PyUnicode_Check(name)){
    1369           2 :         PyErr_Format(PyExc_TypeError,
    1370             :                      "attribute name must be string, not '%.200s'",
    1371           2 :                      Py_TYPE(name)->tp_name);
    1372           2 :         return -1;
    1373             :     }
    1374             : 
    1375    70063300 :     if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
    1376           0 :         return -1;
    1377             : 
    1378    70063300 :     Py_INCREF(name);
    1379    70063300 :     Py_INCREF(tp);
    1380    70063300 :     descr = _PyType_Lookup(tp, name);
    1381             : 
    1382    70063300 :     if (descr != NULL) {
    1383    17825500 :         Py_INCREF(descr);
    1384    17825500 :         f = Py_TYPE(descr)->tp_descr_set;
    1385    17825500 :         if (f != NULL) {
    1386     4679110 :             res = f(descr, obj, value);
    1387     4679110 :             goto done;
    1388             :         }
    1389             :     }
    1390             : 
    1391    65384200 :     if (dict == NULL) {
    1392    65369400 :         if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) && *_PyObject_ValuesPointer(obj)) {
    1393     7700960 :             res = _PyObject_StoreInstanceAttribute(obj, *_PyObject_ValuesPointer(obj), name, value);
    1394             :         }
    1395             :         else {
    1396    57668400 :             PyObject **dictptr = _PyObject_DictPointer(obj);
    1397    57668400 :             if (dictptr == NULL) {
    1398          97 :                 if (descr == NULL) {
    1399          90 :                     PyErr_Format(PyExc_AttributeError,
    1400             :                                 "'%.100s' object has no attribute '%U'",
    1401             :                                 tp->tp_name, name);
    1402             :                 }
    1403             :                 else {
    1404           7 :                     PyErr_Format(PyExc_AttributeError,
    1405             :                                 "'%.50s' object attribute '%U' is read-only",
    1406             :                                 tp->tp_name, name);
    1407             :                 }
    1408          97 :                 goto done;
    1409             :             }
    1410             :             else {
    1411    57668300 :                 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
    1412             :             }
    1413             :         }
    1414             :     }
    1415             :     else {
    1416       14856 :         Py_INCREF(dict);
    1417       14856 :         if (value == NULL)
    1418         165 :             res = PyDict_DelItem(dict, name);
    1419             :         else
    1420       14691 :             res = PyDict_SetItem(dict, name, value);
    1421       14856 :         Py_DECREF(dict);
    1422             :     }
    1423    65384100 :     if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
    1424          37 :         if (PyType_IsSubtype(tp, &PyType_Type)) {
    1425          14 :             PyErr_Format(PyExc_AttributeError,
    1426             :                          "type object '%.50s' has no attribute '%U'",
    1427             :                          ((PyTypeObject*)obj)->tp_name, name);
    1428             :         }
    1429             :         else {
    1430          23 :             PyErr_Format(PyExc_AttributeError,
    1431             :                          "'%.100s' object has no attribute '%U'",
    1432             :                          tp->tp_name, name);
    1433             :         }
    1434             :     }
    1435    65384100 :   done:
    1436    70063300 :     Py_XDECREF(descr);
    1437    70063300 :     Py_DECREF(tp);
    1438    70063300 :     Py_DECREF(name);
    1439    70063300 :     return res;
    1440             : }
    1441             : 
    1442             : int
    1443    60011000 : PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
    1444             : {
    1445    60011000 :     return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
    1446             : }
    1447             : 
    1448             : int
    1449          18 : PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
    1450             : {
    1451          18 :     PyObject **dictptr = _PyObject_GetDictPtr(obj);
    1452          18 :     if (dictptr == NULL) {
    1453           0 :         if (_PyType_HasFeature(Py_TYPE(obj), Py_TPFLAGS_MANAGED_DICT) &&
    1454           0 :             *_PyObject_ValuesPointer(obj) != NULL)
    1455             :         {
    1456             :             /* Was unable to convert to dict */
    1457           0 :             PyErr_NoMemory();
    1458             :         }
    1459             :         else {
    1460           0 :             PyErr_SetString(PyExc_AttributeError,
    1461             :                             "This object has no __dict__");
    1462             :         }
    1463           0 :         return -1;
    1464             :     }
    1465          18 :     if (value == NULL) {
    1466          11 :         PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
    1467          11 :         return -1;
    1468             :     }
    1469           7 :     if (!PyDict_Check(value)) {
    1470           2 :         PyErr_Format(PyExc_TypeError,
    1471             :                      "__dict__ must be set to a dictionary, "
    1472           2 :                      "not a '%.200s'", Py_TYPE(value)->tp_name);
    1473           2 :         return -1;
    1474             :     }
    1475           5 :     Py_INCREF(value);
    1476           5 :     Py_XSETREF(*dictptr, value);
    1477           5 :     return 0;
    1478             : }
    1479             : 
    1480             : 
    1481             : /* Test a value used as condition, e.g., in a while or if statement.
    1482             :    Return -1 if an error occurred */
    1483             : 
    1484             : int
    1485   127707000 : PyObject_IsTrue(PyObject *v)
    1486             : {
    1487             :     Py_ssize_t res;
    1488   127707000 :     if (v == Py_True)
    1489     5641860 :         return 1;
    1490   122065000 :     if (v == Py_False)
    1491     7448700 :         return 0;
    1492   114616000 :     if (v == Py_None)
    1493    30941700 :         return 0;
    1494    83674800 :     else if (Py_TYPE(v)->tp_as_number != NULL &&
    1495    55951000 :              Py_TYPE(v)->tp_as_number->nb_bool != NULL)
    1496    21913300 :         res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
    1497    61761400 :     else if (Py_TYPE(v)->tp_as_mapping != NULL &&
    1498    56148100 :              Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
    1499    47590700 :         res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
    1500    14170700 :     else if (Py_TYPE(v)->tp_as_sequence != NULL &&
    1501     9622940 :              Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
    1502     1065560 :         res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
    1503             :     else
    1504    13105100 :         return 1;
    1505             :     /* if it is negative, it should be either -1 or -2 */
    1506    70569600 :     return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
    1507             : }
    1508             : 
    1509             : /* equivalent of 'not v'
    1510             :    Return -1 if an error occurred */
    1511             : 
    1512             : int
    1513        9645 : PyObject_Not(PyObject *v)
    1514             : {
    1515             :     int res;
    1516        9645 :     res = PyObject_IsTrue(v);
    1517        9645 :     if (res < 0)
    1518           1 :         return res;
    1519        9644 :     return res == 0;
    1520             : }
    1521             : 
    1522             : /* Test whether an object can be called */
    1523             : 
    1524             : int
    1525   255069000 : PyCallable_Check(PyObject *x)
    1526             : {
    1527   255069000 :     if (x == NULL)
    1528           0 :         return 0;
    1529   255069000 :     return Py_TYPE(x)->tp_call != NULL;
    1530             : }
    1531             : 
    1532             : 
    1533             : /* Helper for PyObject_Dir without arguments: returns the local scope. */
    1534             : static PyObject *
    1535         763 : _dir_locals(void)
    1536             : {
    1537             :     PyObject *names;
    1538             :     PyObject *locals;
    1539             : 
    1540         763 :     locals = PyEval_GetLocals();
    1541         763 :     if (locals == NULL)
    1542           0 :         return NULL;
    1543             : 
    1544         763 :     names = PyMapping_Keys(locals);
    1545         763 :     if (!names)
    1546           1 :         return NULL;
    1547         762 :     if (!PyList_Check(names)) {
    1548           0 :         PyErr_Format(PyExc_TypeError,
    1549             :             "dir(): expected keys() of locals to be a list, "
    1550           0 :             "not '%.200s'", Py_TYPE(names)->tp_name);
    1551           0 :         Py_DECREF(names);
    1552           0 :         return NULL;
    1553             :     }
    1554         762 :     if (PyList_Sort(names)) {
    1555           0 :         Py_DECREF(names);
    1556           0 :         return NULL;
    1557             :     }
    1558             :     /* the locals don't need to be DECREF'd */
    1559         762 :     return names;
    1560             : }
    1561             : 
    1562             : /* Helper for PyObject_Dir: object introspection. */
    1563             : static PyObject *
    1564       26269 : _dir_object(PyObject *obj)
    1565             : {
    1566             :     PyObject *result, *sorted;
    1567       26269 :     PyObject *dirfunc = _PyObject_LookupSpecial(obj, &_Py_ID(__dir__));
    1568             : 
    1569       26269 :     assert(obj != NULL);
    1570       26269 :     if (dirfunc == NULL) {
    1571           1 :         if (!PyErr_Occurred())
    1572           0 :             PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
    1573           1 :         return NULL;
    1574             :     }
    1575             :     /* use __dir__ */
    1576       26268 :     result = _PyObject_CallNoArgs(dirfunc);
    1577       26268 :     Py_DECREF(dirfunc);
    1578       26268 :     if (result == NULL)
    1579           5 :         return NULL;
    1580             :     /* return sorted(result) */
    1581       26263 :     sorted = PySequence_List(result);
    1582       26263 :     Py_DECREF(result);
    1583       26263 :     if (sorted == NULL)
    1584           1 :         return NULL;
    1585       26262 :     if (PyList_Sort(sorted)) {
    1586           0 :         Py_DECREF(sorted);
    1587           0 :         return NULL;
    1588             :     }
    1589       26262 :     return sorted;
    1590             : }
    1591             : 
    1592             : /* Implementation of dir() -- if obj is NULL, returns the names in the current
    1593             :    (local) scope.  Otherwise, performs introspection of the object: returns a
    1594             :    sorted list of attribute names (supposedly) accessible from the object
    1595             : */
    1596             : PyObject *
    1597       27032 : PyObject_Dir(PyObject *obj)
    1598             : {
    1599       27032 :     return (obj == NULL) ? _dir_locals() : _dir_object(obj);
    1600             : }
    1601             : 
    1602             : /*
    1603             : None is a non-NULL undefined value.
    1604             : There is (and should be!) no way to create other objects of this type,
    1605             : so there is exactly one (which is indestructible, by the way).
    1606             : */
    1607             : 
    1608             : /* ARGSUSED */
    1609             : static PyObject *
    1610      210640 : none_repr(PyObject *op)
    1611             : {
    1612      210640 :     return PyUnicode_FromString("None");
    1613             : }
    1614             : 
    1615             : static void _Py_NO_RETURN
    1616           0 : none_dealloc(PyObject* Py_UNUSED(ignore))
    1617             : {
    1618           0 :     _Py_FatalRefcountError("deallocating None");
    1619             : }
    1620             : 
    1621             : static PyObject *
    1622           3 : none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
    1623             : {
    1624           3 :     if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
    1625           2 :         PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
    1626           2 :         return NULL;
    1627             :     }
    1628           1 :     Py_RETURN_NONE;
    1629             : }
    1630             : 
    1631             : static int
    1632           0 : none_bool(PyObject *v)
    1633             : {
    1634           0 :     return 0;
    1635             : }
    1636             : 
    1637             : static PyNumberMethods none_as_number = {
    1638             :     0,                          /* nb_add */
    1639             :     0,                          /* nb_subtract */
    1640             :     0,                          /* nb_multiply */
    1641             :     0,                          /* nb_remainder */
    1642             :     0,                          /* nb_divmod */
    1643             :     0,                          /* nb_power */
    1644             :     0,                          /* nb_negative */
    1645             :     0,                          /* nb_positive */
    1646             :     0,                          /* nb_absolute */
    1647             :     (inquiry)none_bool,         /* nb_bool */
    1648             :     0,                          /* nb_invert */
    1649             :     0,                          /* nb_lshift */
    1650             :     0,                          /* nb_rshift */
    1651             :     0,                          /* nb_and */
    1652             :     0,                          /* nb_xor */
    1653             :     0,                          /* nb_or */
    1654             :     0,                          /* nb_int */
    1655             :     0,                          /* nb_reserved */
    1656             :     0,                          /* nb_float */
    1657             :     0,                          /* nb_inplace_add */
    1658             :     0,                          /* nb_inplace_subtract */
    1659             :     0,                          /* nb_inplace_multiply */
    1660             :     0,                          /* nb_inplace_remainder */
    1661             :     0,                          /* nb_inplace_power */
    1662             :     0,                          /* nb_inplace_lshift */
    1663             :     0,                          /* nb_inplace_rshift */
    1664             :     0,                          /* nb_inplace_and */
    1665             :     0,                          /* nb_inplace_xor */
    1666             :     0,                          /* nb_inplace_or */
    1667             :     0,                          /* nb_floor_divide */
    1668             :     0,                          /* nb_true_divide */
    1669             :     0,                          /* nb_inplace_floor_divide */
    1670             :     0,                          /* nb_inplace_true_divide */
    1671             :     0,                          /* nb_index */
    1672             : };
    1673             : 
    1674             : PyTypeObject _PyNone_Type = {
    1675             :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
    1676             :     "NoneType",
    1677             :     0,
    1678             :     0,
    1679             :     none_dealloc,       /*tp_dealloc*/ /*never called*/
    1680             :     0,                  /*tp_vectorcall_offset*/
    1681             :     0,                  /*tp_getattr*/
    1682             :     0,                  /*tp_setattr*/
    1683             :     0,                  /*tp_as_async*/
    1684             :     none_repr,          /*tp_repr*/
    1685             :     &none_as_number,    /*tp_as_number*/
    1686             :     0,                  /*tp_as_sequence*/
    1687             :     0,                  /*tp_as_mapping*/
    1688             :     0,                  /*tp_hash */
    1689             :     0,                  /*tp_call */
    1690             :     0,                  /*tp_str */
    1691             :     0,                  /*tp_getattro */
    1692             :     0,                  /*tp_setattro */
    1693             :     0,                  /*tp_as_buffer */
    1694             :     Py_TPFLAGS_DEFAULT, /*tp_flags */
    1695             :     0,                  /*tp_doc */
    1696             :     0,                  /*tp_traverse */
    1697             :     0,                  /*tp_clear */
    1698             :     0,                  /*tp_richcompare */
    1699             :     0,                  /*tp_weaklistoffset */
    1700             :     0,                  /*tp_iter */
    1701             :     0,                  /*tp_iternext */
    1702             :     0,                  /*tp_methods */
    1703             :     0,                  /*tp_members */
    1704             :     0,                  /*tp_getset */
    1705             :     0,                  /*tp_base */
    1706             :     0,                  /*tp_dict */
    1707             :     0,                  /*tp_descr_get */
    1708             :     0,                  /*tp_descr_set */
    1709             :     0,                  /*tp_dictoffset */
    1710             :     0,                  /*tp_init */
    1711             :     0,                  /*tp_alloc */
    1712             :     none_new,           /*tp_new */
    1713             : };
    1714             : 
    1715             : PyObject _Py_NoneStruct = {
    1716             :   _PyObject_EXTRA_INIT
    1717             :   1, &_PyNone_Type
    1718             : };
    1719             : 
    1720             : /* NotImplemented is an object that can be used to signal that an
    1721             :    operation is not implemented for the given type combination. */
    1722             : 
    1723             : static PyObject *
    1724          22 : NotImplemented_repr(PyObject *op)
    1725             : {
    1726          22 :     return PyUnicode_FromString("NotImplemented");
    1727             : }
    1728             : 
    1729             : static PyObject *
    1730          72 : NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
    1731             : {
    1732          72 :     return PyUnicode_FromString("NotImplemented");
    1733             : }
    1734             : 
    1735             : static PyMethodDef notimplemented_methods[] = {
    1736             :     {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
    1737             :     {NULL, NULL}
    1738             : };
    1739             : 
    1740             : static PyObject *
    1741           3 : notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
    1742             : {
    1743           3 :     if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
    1744           2 :         PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
    1745           2 :         return NULL;
    1746             :     }
    1747           1 :     Py_RETURN_NOTIMPLEMENTED;
    1748             : }
    1749             : 
    1750             : static void _Py_NO_RETURN
    1751           0 : notimplemented_dealloc(PyObject* ignore)
    1752             : {
    1753             :     /* This should never get called, but we also don't want to SEGV if
    1754             :      * we accidentally decref NotImplemented out of existence.
    1755             :      */
    1756           0 :     Py_FatalError("deallocating NotImplemented");
    1757             : }
    1758             : 
    1759             : static int
    1760           3 : notimplemented_bool(PyObject *v)
    1761             : {
    1762           3 :     if (PyErr_WarnEx(PyExc_DeprecationWarning,
    1763             :                      "NotImplemented should not be used in a boolean context",
    1764             :                      1) < 0)
    1765             :     {
    1766           0 :         return -1;
    1767             :     }
    1768           3 :     return 1;
    1769             : }
    1770             : 
    1771             : static PyNumberMethods notimplemented_as_number = {
    1772             :     .nb_bool = notimplemented_bool,
    1773             : };
    1774             : 
    1775             : PyTypeObject _PyNotImplemented_Type = {
    1776             :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
    1777             :     "NotImplementedType",
    1778             :     0,
    1779             :     0,
    1780             :     notimplemented_dealloc,       /*tp_dealloc*/ /*never called*/
    1781             :     0,                  /*tp_vectorcall_offset*/
    1782             :     0,                  /*tp_getattr*/
    1783             :     0,                  /*tp_setattr*/
    1784             :     0,                  /*tp_as_async*/
    1785             :     NotImplemented_repr,        /*tp_repr*/
    1786             :     &notimplemented_as_number,  /*tp_as_number*/
    1787             :     0,                  /*tp_as_sequence*/
    1788             :     0,                  /*tp_as_mapping*/
    1789             :     0,                  /*tp_hash */
    1790             :     0,                  /*tp_call */
    1791             :     0,                  /*tp_str */
    1792             :     0,                  /*tp_getattro */
    1793             :     0,                  /*tp_setattro */
    1794             :     0,                  /*tp_as_buffer */
    1795             :     Py_TPFLAGS_DEFAULT, /*tp_flags */
    1796             :     0,                  /*tp_doc */
    1797             :     0,                  /*tp_traverse */
    1798             :     0,                  /*tp_clear */
    1799             :     0,                  /*tp_richcompare */
    1800             :     0,                  /*tp_weaklistoffset */
    1801             :     0,                  /*tp_iter */
    1802             :     0,                  /*tp_iternext */
    1803             :     notimplemented_methods, /*tp_methods */
    1804             :     0,                  /*tp_members */
    1805             :     0,                  /*tp_getset */
    1806             :     0,                  /*tp_base */
    1807             :     0,                  /*tp_dict */
    1808             :     0,                  /*tp_descr_get */
    1809             :     0,                  /*tp_descr_set */
    1810             :     0,                  /*tp_dictoffset */
    1811             :     0,                  /*tp_init */
    1812             :     0,                  /*tp_alloc */
    1813             :     notimplemented_new, /*tp_new */
    1814             : };
    1815             : 
    1816             : PyObject _Py_NotImplementedStruct = {
    1817             :     _PyObject_EXTRA_INIT
    1818             :     1, &_PyNotImplemented_Type
    1819             : };
    1820             : 
    1821             : PyStatus
    1822        3134 : _PyTypes_InitState(PyInterpreterState *interp)
    1823             : {
    1824        3134 :     if (!_Py_IsMainInterpreter(interp)) {
    1825         171 :         return _PyStatus_OK();
    1826             :     }
    1827             : 
    1828        2963 :     PyStatus status = _PyTypes_InitSlotDefs();
    1829        2963 :     if (_PyStatus_EXCEPTION(status)) {
    1830           0 :         return status;
    1831             :     }
    1832             : 
    1833        2963 :     return _PyStatus_OK();
    1834             : }
    1835             : 
    1836             : 
    1837             : 
    1838             : #ifdef MS_WINDOWS
    1839             : extern PyTypeObject PyHKEY_Type;
    1840             : #endif
    1841             : extern PyTypeObject _Py_GenericAliasIterType;
    1842             : extern PyTypeObject _PyMemoryIter_Type;
    1843             : extern PyTypeObject _PyLineIterator;
    1844             : extern PyTypeObject _PyPositionsIterator;
    1845             : 
    1846             : static PyTypeObject* static_types[] = {
    1847             :     // The two most important base types: must be initialized first and
    1848             :     // deallocated last.
    1849             :     &PyBaseObject_Type,
    1850             :     &PyType_Type,
    1851             : 
    1852             :     // Static types with base=&PyBaseObject_Type
    1853             :     &PyAsyncGen_Type,
    1854             :     &PyByteArrayIter_Type,
    1855             :     &PyByteArray_Type,
    1856             :     &PyBytesIter_Type,
    1857             :     &PyBytes_Type,
    1858             :     &PyCFunction_Type,
    1859             :     &PyCallIter_Type,
    1860             :     &PyCapsule_Type,
    1861             :     &PyCell_Type,
    1862             :     &PyClassMethodDescr_Type,
    1863             :     &PyClassMethod_Type,
    1864             :     &PyCode_Type,
    1865             :     &PyComplex_Type,
    1866             :     &PyContextToken_Type,
    1867             :     &PyContextVar_Type,
    1868             :     &PyContext_Type,
    1869             :     &PyCoro_Type,
    1870             :     &PyDictItems_Type,
    1871             :     &PyDictIterItem_Type,
    1872             :     &PyDictIterKey_Type,
    1873             :     &PyDictIterValue_Type,
    1874             :     &PyDictKeys_Type,
    1875             :     &PyDictProxy_Type,
    1876             :     &PyDictRevIterItem_Type,
    1877             :     &PyDictRevIterKey_Type,
    1878             :     &PyDictRevIterValue_Type,
    1879             :     &PyDictValues_Type,
    1880             :     &PyDict_Type,
    1881             :     &PyEllipsis_Type,
    1882             :     &PyEnum_Type,
    1883             :     &PyFilter_Type,
    1884             :     &PyFloat_Type,
    1885             :     &PyFrame_Type,
    1886             :     &PyFrozenSet_Type,
    1887             :     &PyFunction_Type,
    1888             :     &PyGen_Type,
    1889             :     &PyGetSetDescr_Type,
    1890             : #ifdef MS_WINDOWS
    1891             :     &PyHKEY_Type,
    1892             : #endif
    1893             :     &PyInstanceMethod_Type,
    1894             :     &PyListIter_Type,
    1895             :     &PyListRevIter_Type,
    1896             :     &PyList_Type,
    1897             :     &PyLongRangeIter_Type,
    1898             :     &PyLong_Type,
    1899             :     &PyMap_Type,
    1900             :     &PyMemberDescr_Type,
    1901             :     &PyMemoryView_Type,
    1902             :     &PyMethodDescr_Type,
    1903             :     &PyMethod_Type,
    1904             :     &PyModuleDef_Type,
    1905             :     &PyModule_Type,
    1906             :     &PyODictIter_Type,
    1907             :     &PyPickleBuffer_Type,
    1908             :     &PyProperty_Type,
    1909             :     &PyRangeIter_Type,
    1910             :     &PyRange_Type,
    1911             :     &PyReversed_Type,
    1912             :     &PySTEntry_Type,
    1913             :     &PySeqIter_Type,
    1914             :     &PySetIter_Type,
    1915             :     &PySet_Type,
    1916             :     &PySlice_Type,
    1917             :     &PyStaticMethod_Type,
    1918             :     &PyStdPrinter_Type,
    1919             :     &PySuper_Type,
    1920             :     &PyTraceBack_Type,
    1921             :     &PyTupleIter_Type,
    1922             :     &PyTuple_Type,
    1923             :     &PyUnicodeIter_Type,
    1924             :     &PyUnicode_Type,
    1925             :     &PyWrapperDescr_Type,
    1926             :     &PyZip_Type,
    1927             :     &Py_GenericAliasType,
    1928             :     &_PyAnextAwaitable_Type,
    1929             :     &_PyAsyncGenASend_Type,
    1930             :     &_PyAsyncGenAThrow_Type,
    1931             :     &_PyAsyncGenWrappedValue_Type,
    1932             :     &_PyContextTokenMissing_Type,
    1933             :     &_PyCoroWrapper_Type,
    1934             :     &_Py_GenericAliasIterType,
    1935             :     &_PyHamtItems_Type,
    1936             :     &_PyHamtKeys_Type,
    1937             :     &_PyHamtValues_Type,
    1938             :     &_PyHamt_ArrayNode_Type,
    1939             :     &_PyHamt_BitmapNode_Type,
    1940             :     &_PyHamt_CollisionNode_Type,
    1941             :     &_PyHamt_Type,
    1942             :     &_PyInterpreterID_Type,
    1943             :     &_PyLineIterator,
    1944             :     &_PyManagedBuffer_Type,
    1945             :     &_PyMemoryIter_Type,
    1946             :     &_PyMethodWrapper_Type,
    1947             :     &_PyNamespace_Type,
    1948             :     &_PyNone_Type,
    1949             :     &_PyNotImplemented_Type,
    1950             :     &_PyPositionsIterator,
    1951             :     &_PyUnicodeASCIIIter_Type,
    1952             :     &_PyUnion_Type,
    1953             :     &_PyWeakref_CallableProxyType,
    1954             :     &_PyWeakref_ProxyType,
    1955             :     &_PyWeakref_RefType,
    1956             : 
    1957             :     // subclasses: _PyTypes_FiniTypes() deallocates them before their base
    1958             :     // class
    1959             :     &PyBool_Type,         // base=&PyLong_Type
    1960             :     &PyCMethod_Type,      // base=&PyCFunction_Type
    1961             :     &PyODictItems_Type,   // base=&PyDictItems_Type
    1962             :     &PyODictKeys_Type,    // base=&PyDictKeys_Type
    1963             :     &PyODictValues_Type,  // base=&PyDictValues_Type
    1964             :     &PyODict_Type,        // base=&PyDict_Type
    1965             : };
    1966             : 
    1967             : 
    1968             : PyStatus
    1969        3134 : _PyTypes_InitTypes(PyInterpreterState *interp)
    1970             : {
    1971        3134 :     if (!_Py_IsMainInterpreter(interp)) {
    1972         171 :         return _PyStatus_OK();
    1973             :     }
    1974             : 
    1975             :     // All other static types (unless initialized elsewhere)
    1976      322967 :     for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) {
    1977      320004 :         PyTypeObject *type = static_types[i];
    1978      320004 :         if (PyType_Ready(type) < 0) {
    1979           0 :             return _PyStatus_ERR("Can't initialize types");
    1980             :         }
    1981      320004 :         if (type == &PyType_Type) {
    1982             :             // Sanitify checks of the two most important types
    1983        2963 :             assert(PyBaseObject_Type.tp_base == NULL);
    1984        2963 :             assert(PyType_Type.tp_base == &PyBaseObject_Type);
    1985             :         }
    1986             :     }
    1987             : 
    1988        2963 :     return _PyStatus_OK();
    1989             : }
    1990             : 
    1991             : 
    1992             : // Best-effort function clearing static types.
    1993             : //
    1994             : // Don't deallocate a type if it still has subclasses. If a Py_Finalize()
    1995             : // sub-function is interrupted by CTRL+C or fails with MemoryError, some
    1996             : // subclasses are not cleared properly. Leave the static type unchanged in this
    1997             : // case.
    1998             : void
    1999        3120 : _PyTypes_FiniTypes(PyInterpreterState *interp)
    2000             : {
    2001        3120 :     if (!_Py_IsMainInterpreter(interp)) {
    2002         169 :         return;
    2003             :     }
    2004             : 
    2005             :     // Deallocate types in the reverse order to deallocate subclasses before
    2006             :     // their base classes.
    2007      321659 :     for (Py_ssize_t i=Py_ARRAY_LENGTH(static_types)-1; i>=0; i--) {
    2008      318708 :         PyTypeObject *type = static_types[i];
    2009      318708 :         _PyStaticType_Dealloc(type);
    2010             :     }
    2011             : }
    2012             : 
    2013             : 
    2014             : void
    2015  1286110000 : _Py_NewReference(PyObject *op)
    2016             : {
    2017  1286110000 :     if (_Py_tracemalloc_config.tracing) {
    2018     1164430 :         _PyTraceMalloc_NewReference(op);
    2019             :     }
    2020             : #ifdef Py_REF_DEBUG
    2021  1286110000 :     _Py_RefTotal++;
    2022             : #endif
    2023  1286110000 :     Py_SET_REFCNT(op, 1);
    2024             : #ifdef Py_TRACE_REFS
    2025             :     _Py_AddToAllObjects(op, 1);
    2026             : #endif
    2027  1286110000 : }
    2028             : 
    2029             : 
    2030             : #ifdef Py_TRACE_REFS
    2031             : void
    2032             : _Py_ForgetReference(PyObject *op)
    2033             : {
    2034             :     if (Py_REFCNT(op) < 0) {
    2035             :         _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
    2036             :     }
    2037             : 
    2038             :     if (op == &refchain ||
    2039             :         op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
    2040             :     {
    2041             :         _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain");
    2042             :     }
    2043             : 
    2044             : #ifdef SLOW_UNREF_CHECK
    2045             :     PyObject *p;
    2046             :     for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
    2047             :         if (p == op) {
    2048             :             break;
    2049             :         }
    2050             :     }
    2051             :     if (p == &refchain) {
    2052             :         /* Not found */
    2053             :         _PyObject_ASSERT_FAILED_MSG(op,
    2054             :                                     "object not found in the objects list");
    2055             :     }
    2056             : #endif
    2057             : 
    2058             :     op->_ob_next->_ob_prev = op->_ob_prev;
    2059             :     op->_ob_prev->_ob_next = op->_ob_next;
    2060             :     op->_ob_next = op->_ob_prev = NULL;
    2061             : }
    2062             : 
    2063             : /* Print all live objects.  Because PyObject_Print is called, the
    2064             :  * interpreter must be in a healthy state.
    2065             :  */
    2066             : void
    2067             : _Py_PrintReferences(FILE *fp)
    2068             : {
    2069             :     PyObject *op;
    2070             :     fprintf(fp, "Remaining objects:\n");
    2071             :     for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
    2072             :         fprintf(fp, "%p [%zd] ", (void *)op, Py_REFCNT(op));
    2073             :         if (PyObject_Print(op, fp, 0) != 0) {
    2074             :             PyErr_Clear();
    2075             :         }
    2076             :         putc('\n', fp);
    2077             :     }
    2078             : }
    2079             : 
    2080             : /* Print the addresses of all live objects.  Unlike _Py_PrintReferences, this
    2081             :  * doesn't make any calls to the Python C API, so is always safe to call.
    2082             :  */
    2083             : void
    2084             : _Py_PrintReferenceAddresses(FILE *fp)
    2085             : {
    2086             :     PyObject *op;
    2087             :     fprintf(fp, "Remaining object addresses:\n");
    2088             :     for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
    2089             :         fprintf(fp, "%p [%zd] %s\n", (void *)op,
    2090             :             Py_REFCNT(op), Py_TYPE(op)->tp_name);
    2091             : }
    2092             : 
    2093             : PyObject *
    2094             : _Py_GetObjects(PyObject *self, PyObject *args)
    2095             : {
    2096             :     int i, n;
    2097             :     PyObject *t = NULL;
    2098             :     PyObject *res, *op;
    2099             : 
    2100             :     if (!PyArg_ParseTuple(args, "i|O", &n, &t))
    2101             :         return NULL;
    2102             :     op = refchain._ob_next;
    2103             :     res = PyList_New(0);
    2104             :     if (res == NULL)
    2105             :         return NULL;
    2106             :     for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
    2107             :         while (op == self || op == args || op == res || op == t ||
    2108             :                (t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) {
    2109             :             op = op->_ob_next;
    2110             :             if (op == &refchain)
    2111             :                 return res;
    2112             :         }
    2113             :         if (PyList_Append(res, op) < 0) {
    2114             :             Py_DECREF(res);
    2115             :             return NULL;
    2116             :         }
    2117             :         op = op->_ob_next;
    2118             :     }
    2119             :     return res;
    2120             : }
    2121             : 
    2122             : #endif
    2123             : 
    2124             : 
    2125             : /* Hack to force loading of abstract.o */
    2126             : Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
    2127             : 
    2128             : 
    2129             : void
    2130           1 : _PyObject_DebugTypeStats(FILE *out)
    2131             : {
    2132           1 :     _PyDict_DebugMallocStats(out);
    2133           1 :     _PyFloat_DebugMallocStats(out);
    2134           1 :     _PyList_DebugMallocStats(out);
    2135           1 :     _PyTuple_DebugMallocStats(out);
    2136           1 : }
    2137             : 
    2138             : /* These methods are used to control infinite recursion in repr, str, print,
    2139             :    etc.  Container objects that may recursively contain themselves,
    2140             :    e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
    2141             :    Py_ReprLeave() to avoid infinite recursion.
    2142             : 
    2143             :    Py_ReprEnter() returns 0 the first time it is called for a particular
    2144             :    object and 1 every time thereafter.  It returns -1 if an exception
    2145             :    occurred.  Py_ReprLeave() has no return value.
    2146             : 
    2147             :    See dictobject.c and listobject.c for examples of use.
    2148             : */
    2149             : 
    2150             : int
    2151      119261 : Py_ReprEnter(PyObject *obj)
    2152             : {
    2153             :     PyObject *dict;
    2154             :     PyObject *list;
    2155             :     Py_ssize_t i;
    2156             : 
    2157      119261 :     dict = PyThreadState_GetDict();
    2158             :     /* Ignore a missing thread-state, so that this function can be called
    2159             :        early on startup. */
    2160      119261 :     if (dict == NULL)
    2161           0 :         return 0;
    2162      119261 :     list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr));
    2163      119261 :     if (list == NULL) {
    2164         398 :         if (PyErr_Occurred()) {
    2165           0 :             return -1;
    2166             :         }
    2167         398 :         list = PyList_New(0);
    2168         398 :         if (list == NULL)
    2169           0 :             return -1;
    2170         398 :         if (PyDict_SetItem(dict, &_Py_ID(Py_Repr), list) < 0)
    2171           0 :             return -1;
    2172         398 :         Py_DECREF(list);
    2173             :     }
    2174      119261 :     i = PyList_GET_SIZE(list);
    2175     1655630 :     while (--i >= 0) {
    2176     1536420 :         if (PyList_GET_ITEM(list, i) == obj)
    2177          48 :             return 1;
    2178             :     }
    2179      119213 :     if (PyList_Append(list, obj) < 0)
    2180           0 :         return -1;
    2181      119213 :     return 0;
    2182             : }
    2183             : 
    2184             : void
    2185      119214 : Py_ReprLeave(PyObject *obj)
    2186             : {
    2187             :     PyObject *dict;
    2188             :     PyObject *list;
    2189             :     Py_ssize_t i;
    2190             :     PyObject *error_type, *error_value, *error_traceback;
    2191             : 
    2192      119214 :     PyErr_Fetch(&error_type, &error_value, &error_traceback);
    2193             : 
    2194      119214 :     dict = PyThreadState_GetDict();
    2195      119214 :     if (dict == NULL)
    2196           0 :         goto finally;
    2197             : 
    2198      119214 :     list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr));
    2199      119214 :     if (list == NULL || !PyList_Check(list))
    2200           0 :         goto finally;
    2201             : 
    2202      119214 :     i = PyList_GET_SIZE(list);
    2203             :     /* Count backwards because we always expect obj to be list[-1] */
    2204      119214 :     while (--i >= 0) {
    2205      119213 :         if (PyList_GET_ITEM(list, i) == obj) {
    2206      119213 :             PyList_SetSlice(list, i, i + 1, NULL);
    2207      119213 :             break;
    2208             :         }
    2209             :     }
    2210             : 
    2211           1 : finally:
    2212             :     /* ignore exceptions because there is no way to report them. */
    2213      119214 :     PyErr_Restore(error_type, error_value, error_traceback);
    2214      119214 : }
    2215             : 
    2216             : /* Trashcan support. */
    2217             : 
    2218             : #define _PyTrash_UNWIND_LEVEL 50
    2219             : 
    2220             : /* Add op to the gcstate->trash_delete_later list.  Called when the current
    2221             :  * call-stack depth gets large.  op must be a currently untracked gc'ed
    2222             :  * object, with refcount 0.  Py_DECREF must already have been called on it.
    2223             :  */
    2224             : static void
    2225      231121 : _PyTrash_thread_deposit_object(PyObject *op)
    2226             : {
    2227      231121 :     PyThreadState *tstate = _PyThreadState_GET();
    2228      231121 :     _PyObject_ASSERT(op, _PyObject_IS_GC(op));
    2229      231121 :     _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
    2230      231121 :     _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
    2231      231121 :     _PyGCHead_SET_PREV(_Py_AS_GC(op), (PyGC_Head*)tstate->trash_delete_later);
    2232      231121 :     tstate->trash_delete_later = op;
    2233      231121 : }
    2234             : 
    2235             : /* Deallocate all the objects in the gcstate->trash_delete_later list.
    2236             :  * Called when the call-stack unwinds again. */
    2237             : static void
    2238         356 : _PyTrash_thread_destroy_chain(void)
    2239             : {
    2240         356 :     PyThreadState *tstate = _PyThreadState_GET();
    2241             :     /* We need to increase trash_delete_nesting here, otherwise,
    2242             :        _PyTrash_thread_destroy_chain will be called recursively
    2243             :        and then possibly crash.  An example that may crash without
    2244             :        increase:
    2245             :            N = 500000  # need to be large enough
    2246             :            ob = object()
    2247             :            tups = [(ob,) for i in range(N)]
    2248             :            for i in range(49):
    2249             :                tups = [(tup,) for tup in tups]
    2250             :            del tups
    2251             :     */
    2252         356 :     assert(tstate->trash_delete_nesting == 0);
    2253         356 :     ++tstate->trash_delete_nesting;
    2254      231477 :     while (tstate->trash_delete_later) {
    2255      231121 :         PyObject *op = tstate->trash_delete_later;
    2256      231121 :         destructor dealloc = Py_TYPE(op)->tp_dealloc;
    2257             : 
    2258      231121 :         tstate->trash_delete_later =
    2259      231121 :             (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
    2260             : 
    2261             :         /* Call the deallocator directly.  This used to try to
    2262             :          * fool Py_DECREF into calling it indirectly, but
    2263             :          * Py_DECREF was already called on this object, and in
    2264             :          * assorted non-release builds calling Py_DECREF again ends
    2265             :          * up distorting allocation statistics.
    2266             :          */
    2267      231121 :         _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
    2268      231121 :         (*dealloc)(op);
    2269      231121 :         assert(tstate->trash_delete_nesting == 1);
    2270             :     }
    2271         356 :     --tstate->trash_delete_nesting;
    2272         356 : }
    2273             : 
    2274             : 
    2275             : int
    2276   457042000 : _PyTrash_begin(PyThreadState *tstate, PyObject *op)
    2277             : {
    2278   457042000 :     if (tstate->trash_delete_nesting >= _PyTrash_UNWIND_LEVEL) {
    2279             :         /* Store the object (to be deallocated later) and jump past
    2280             :          * Py_TRASHCAN_END, skipping the body of the deallocator */
    2281      231121 :         _PyTrash_thread_deposit_object(op);
    2282      231121 :         return 1;
    2283             :     }
    2284   456811000 :     ++tstate->trash_delete_nesting;
    2285   456811000 :     return 0;
    2286             : }
    2287             : 
    2288             : 
    2289             : void
    2290   456811000 : _PyTrash_end(PyThreadState *tstate)
    2291             : {
    2292   456811000 :     --tstate->trash_delete_nesting;
    2293   456811000 :     if (tstate->trash_delete_later && tstate->trash_delete_nesting <= 0) {
    2294         356 :         _PyTrash_thread_destroy_chain();
    2295             :     }
    2296   456811000 : }
    2297             : 
    2298             : 
    2299             : /* bpo-40170: It's only be used in Py_TRASHCAN_BEGIN macro to hide
    2300             :    implementation details. */
    2301             : int
    2302   469522000 : _PyTrash_cond(PyObject *op, destructor dealloc)
    2303             : {
    2304   469522000 :     return Py_TYPE(op)->tp_dealloc == dealloc;
    2305             : }
    2306             : 
    2307             : 
    2308             : void _Py_NO_RETURN
    2309           0 : _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
    2310             :                        const char *file, int line, const char *function)
    2311             : {
    2312           0 :     fprintf(stderr, "%s:%d: ", file, line);
    2313           0 :     if (function) {
    2314           0 :         fprintf(stderr, "%s: ", function);
    2315             :     }
    2316           0 :     fflush(stderr);
    2317             : 
    2318           0 :     if (expr) {
    2319           0 :         fprintf(stderr, "Assertion \"%s\" failed", expr);
    2320             :     }
    2321             :     else {
    2322           0 :         fprintf(stderr, "Assertion failed");
    2323             :     }
    2324           0 :     fflush(stderr);
    2325             : 
    2326           0 :     if (msg) {
    2327           0 :         fprintf(stderr, ": %s", msg);
    2328             :     }
    2329           0 :     fprintf(stderr, "\n");
    2330           0 :     fflush(stderr);
    2331             : 
    2332           0 :     if (_PyObject_IsFreed(obj)) {
    2333             :         /* It seems like the object memory has been freed:
    2334             :            don't access it to prevent a segmentation fault. */
    2335           0 :         fprintf(stderr, "<object at %p is freed>\n", obj);
    2336           0 :         fflush(stderr);
    2337             :     }
    2338             :     else {
    2339             :         /* Display the traceback where the object has been allocated.
    2340             :            Do it before dumping repr(obj), since repr() is more likely
    2341             :            to crash than dumping the traceback. */
    2342             :         void *ptr;
    2343           0 :         PyTypeObject *type = Py_TYPE(obj);
    2344           0 :         if (_PyType_IS_GC(type)) {
    2345           0 :             ptr = (void *)((char *)obj - sizeof(PyGC_Head));
    2346             :         }
    2347             :         else {
    2348           0 :             ptr = (void *)obj;
    2349             :         }
    2350           0 :         _PyMem_DumpTraceback(fileno(stderr), ptr);
    2351             : 
    2352             :         /* This might succeed or fail, but we're about to abort, so at least
    2353             :            try to provide any extra info we can: */
    2354           0 :         _PyObject_Dump(obj);
    2355             : 
    2356           0 :         fprintf(stderr, "\n");
    2357           0 :         fflush(stderr);
    2358             :     }
    2359             : 
    2360           0 :     Py_FatalError("_PyObject_AssertFailed");
    2361             : }
    2362             : 
    2363             : 
    2364             : void
    2365  1200790000 : _Py_Dealloc(PyObject *op)
    2366             : {
    2367  1200790000 :     PyTypeObject *type = Py_TYPE(op);
    2368  1200790000 :     destructor dealloc = type->tp_dealloc;
    2369             : #ifdef Py_DEBUG
    2370  1200790000 :     PyThreadState *tstate = _PyThreadState_GET();
    2371  1200790000 :     PyObject *old_exc_type = tstate->curexc_type;
    2372             :     // Keep the old exception type alive to prevent undefined behavior
    2373             :     // on (tstate->curexc_type != old_exc_type) below
    2374  1200790000 :     Py_XINCREF(old_exc_type);
    2375             :     // Make sure that type->tp_name remains valid
    2376  1200790000 :     Py_INCREF(type);
    2377             : #endif
    2378             : 
    2379             : #ifdef Py_TRACE_REFS
    2380             :     _Py_ForgetReference(op);
    2381             : #endif
    2382  1200790000 :     (*dealloc)(op);
    2383             : 
    2384             : #ifdef Py_DEBUG
    2385             :     // gh-89373: The tp_dealloc function must leave the current exception
    2386             :     // unchanged.
    2387  1200790000 :     if (tstate->curexc_type != old_exc_type) {
    2388             :         const char *err;
    2389           0 :         if (old_exc_type == NULL) {
    2390           0 :             err = "Deallocator of type '%s' raised an exception";
    2391             :         }
    2392           0 :         else if (tstate->curexc_type == NULL) {
    2393           0 :             err = "Deallocator of type '%s' cleared the current exception";
    2394             :         }
    2395             :         else {
    2396             :             // It can happen if dealloc() normalized the current exception.
    2397             :             // A deallocator function must not change the current exception,
    2398             :             // not even normalize it.
    2399           0 :             err = "Deallocator of type '%s' overrode the current exception";
    2400             :         }
    2401           0 :         _Py_FatalErrorFormat(__func__, err, type->tp_name);
    2402             :     }
    2403  1200790000 :     Py_XDECREF(old_exc_type);
    2404  1200790000 :     Py_DECREF(type);
    2405             : #endif
    2406  1200790000 : }
    2407             : 
    2408             : 
    2409             : PyObject **
    2410           0 : PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
    2411             : {
    2412           0 :     return _PyObject_GET_WEAKREFS_LISTPTR(op);
    2413             : }
    2414             : 
    2415             : 
    2416             : #undef Py_NewRef
    2417             : #undef Py_XNewRef
    2418             : 
    2419             : // Export Py_NewRef() and Py_XNewRef() as regular functions for the stable ABI.
    2420             : PyObject*
    2421          11 : Py_NewRef(PyObject *obj)
    2422             : {
    2423          11 :     return _Py_NewRef(obj);
    2424             : }
    2425             : 
    2426             : PyObject*
    2427           2 : Py_XNewRef(PyObject *obj)
    2428             : {
    2429           2 :     return _Py_XNewRef(obj);
    2430             : }
    2431             : 
    2432             : #undef Py_Is
    2433             : #undef Py_IsNone
    2434             : #undef Py_IsTrue
    2435             : #undef Py_IsFalse
    2436             : 
    2437             : // Export Py_Is(), Py_IsNone(), Py_IsTrue(), Py_IsFalse() as regular functions
    2438             : // for the stable ABI.
    2439          10 : int Py_Is(PyObject *x, PyObject *y)
    2440             : {
    2441          10 :     return (x == y);
    2442             : }
    2443             : 
    2444           0 : int Py_IsNone(PyObject *x)
    2445             : {
    2446           0 :     return Py_Is(x, Py_None);
    2447             : }
    2448             : 
    2449           0 : int Py_IsTrue(PyObject *x)
    2450             : {
    2451           0 :     return Py_Is(x, Py_True);
    2452             : }
    2453             : 
    2454           0 : int Py_IsFalse(PyObject *x)
    2455             : {
    2456           0 :     return Py_Is(x, Py_False);
    2457             : }
    2458             : 
    2459             : #ifdef __cplusplus
    2460             : }
    2461             : #endif

Generated by: LCOV version 1.14