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