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