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