• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* Generic object operations; and implementation of None */
3 
4 #include "Python.h"
5 #include "pycore_brc.h"           // _Py_brc_queue_object()
6 #include "pycore_call.h"          // _PyObject_CallNoArgs()
7 #include "pycore_ceval.h"         // _Py_EnterRecursiveCallTstate()
8 #include "pycore_context.h"       // _PyContextTokenMissing_Type
9 #include "pycore_critical_section.h"     // Py_BEGIN_CRITICAL_SECTION, Py_END_CRITICAL_SECTION
10 #include "pycore_descrobject.h"   // _PyMethodWrapper_Type
11 #include "pycore_dict.h"          // _PyObject_MakeDictFromInstanceAttributes()
12 #include "pycore_floatobject.h"   // _PyFloat_DebugMallocStats()
13 #include "pycore_initconfig.h"    // _PyStatus_EXCEPTION()
14 #include "pycore_instruction_sequence.h" // _PyInstructionSequence_Type
15 #include "pycore_hashtable.h"     // _Py_hashtable_new()
16 #include "pycore_memoryobject.h"  // _PyManagedBuffer_Type
17 #include "pycore_namespace.h"     // _PyNamespace_Type
18 #include "pycore_object.h"        // PyAPI_DATA() _Py_SwappedOp definition
19 #include "pycore_long.h"          // _PyLong_GetZero()
20 #include "pycore_optimizer.h"     // _PyUOpExecutor_Type, _PyUOpOptimizer_Type, ...
21 #include "pycore_pyerrors.h"      // _PyErr_Occurred()
22 #include "pycore_pymem.h"         // _PyMem_IsPtrFreed()
23 #include "pycore_pystate.h"       // _PyThreadState_GET()
24 #include "pycore_symtable.h"      // PySTEntry_Type
25 #include "pycore_typeobject.h"    // _PyBufferWrapper_Type
26 #include "pycore_typevarobject.h" // _PyTypeAlias_Type, _Py_initialize_generic
27 #include "pycore_unionobject.h"   // _PyUnion_Type
28 
29 
30 #ifdef Py_LIMITED_API
31    // Prevent recursive call _Py_IncRef() <=> Py_INCREF()
32 #  error "Py_LIMITED_API macro must not be defined"
33 #endif
34 
35 /* Defined in tracemalloc.c */
36 extern void _PyMem_DumpTraceback(int fd, const void *ptr);
37 
38 
39 int
_PyObject_CheckConsistency(PyObject * op,int check_content)40 _PyObject_CheckConsistency(PyObject *op, int check_content)
41 {
42 #define CHECK(expr) \
43     do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG(op, Py_STRINGIFY(expr)); } } while (0)
44 
45     CHECK(!_PyObject_IsFreed(op));
46     CHECK(Py_REFCNT(op) >= 1);
47 
48     _PyType_CheckConsistency(Py_TYPE(op));
49 
50     if (PyUnicode_Check(op)) {
51         _PyUnicode_CheckConsistency(op, check_content);
52     }
53     else if (PyDict_Check(op)) {
54         _PyDict_CheckConsistency(op, check_content);
55     }
56     return 1;
57 
58 #undef CHECK
59 }
60 
61 
62 #ifdef Py_REF_DEBUG
63 /* We keep the legacy symbol around for backward compatibility. */
64 Py_ssize_t _Py_RefTotal;
65 
66 static inline Py_ssize_t
get_legacy_reftotal(void)67 get_legacy_reftotal(void)
68 {
69     return _Py_RefTotal;
70 }
71 #endif
72 
73 #ifdef Py_REF_DEBUG
74 
75 #  define REFTOTAL(interp) \
76     interp->object_state.reftotal
77 
78 static inline void
reftotal_add(PyThreadState * tstate,Py_ssize_t n)79 reftotal_add(PyThreadState *tstate, Py_ssize_t n)
80 {
81 #ifdef Py_GIL_DISABLED
82     _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;
83     // relaxed store to avoid data race with read in get_reftotal()
84     Py_ssize_t reftotal = tstate_impl->reftotal + n;
85     _Py_atomic_store_ssize_relaxed(&tstate_impl->reftotal, reftotal);
86 #else
87     REFTOTAL(tstate->interp) += n;
88 #endif
89 }
90 
91 static inline Py_ssize_t get_global_reftotal(_PyRuntimeState *);
92 
93 /* We preserve the number of refs leaked during runtime finalization,
94    so they can be reported if the runtime is initialized again. */
95 // XXX We don't lose any information by dropping this,
96 // so we should consider doing so.
97 static Py_ssize_t last_final_reftotal = 0;
98 
99 void
_Py_FinalizeRefTotal(_PyRuntimeState * runtime)100 _Py_FinalizeRefTotal(_PyRuntimeState *runtime)
101 {
102     last_final_reftotal = get_global_reftotal(runtime);
103     runtime->object_state.interpreter_leaks = 0;
104 }
105 
106 void
_PyInterpreterState_FinalizeRefTotal(PyInterpreterState * interp)107 _PyInterpreterState_FinalizeRefTotal(PyInterpreterState *interp)
108 {
109     interp->runtime->object_state.interpreter_leaks += REFTOTAL(interp);
110     REFTOTAL(interp) = 0;
111 }
112 
113 static inline Py_ssize_t
get_reftotal(PyInterpreterState * interp)114 get_reftotal(PyInterpreterState *interp)
115 {
116     /* For a single interpreter, we ignore the legacy _Py_RefTotal,
117        since we can't determine which interpreter updated it. */
118     Py_ssize_t total = REFTOTAL(interp);
119 #ifdef Py_GIL_DISABLED
120     for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
121         /* This may race with other threads modifications to their reftotal */
122         _PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)p;
123         total += _Py_atomic_load_ssize_relaxed(&tstate_impl->reftotal);
124     }
125 #endif
126     return total;
127 }
128 
129 static inline Py_ssize_t
get_global_reftotal(_PyRuntimeState * runtime)130 get_global_reftotal(_PyRuntimeState *runtime)
131 {
132     Py_ssize_t total = 0;
133 
134     /* Add up the total from each interpreter. */
135     HEAD_LOCK(&_PyRuntime);
136     PyInterpreterState *interp = PyInterpreterState_Head();
137     for (; interp != NULL; interp = PyInterpreterState_Next(interp)) {
138         total += get_reftotal(interp);
139     }
140     HEAD_UNLOCK(&_PyRuntime);
141 
142     /* Add in the updated value from the legacy _Py_RefTotal. */
143     total += get_legacy_reftotal();
144     total += last_final_reftotal;
145     total += runtime->object_state.interpreter_leaks;
146 
147     return total;
148 }
149 
150 #undef REFTOTAL
151 
152 void
_PyDebug_PrintTotalRefs(void)153 _PyDebug_PrintTotalRefs(void) {
154     _PyRuntimeState *runtime = &_PyRuntime;
155     fprintf(stderr,
156             "[%zd refs, %zd blocks]\n",
157             get_global_reftotal(runtime), _Py_GetGlobalAllocatedBlocks());
158     /* It may be helpful to also print the "legacy" reftotal separately.
159        Likewise for the total for each interpreter. */
160 }
161 #endif /* Py_REF_DEBUG */
162 
163 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
164    These are used by the individual routines for object creation.
165    Do not call them otherwise, they do not initialize the object! */
166 
167 #ifdef Py_TRACE_REFS
168 
169 #define REFCHAIN(interp) interp->object_state.refchain
170 #define REFCHAIN_VALUE ((void*)(uintptr_t)1)
171 
172 static inline int
has_own_refchain(PyInterpreterState * interp)173 has_own_refchain(PyInterpreterState *interp)
174 {
175     if (interp->feature_flags & Py_RTFLAGS_USE_MAIN_OBMALLOC) {
176         return (_Py_IsMainInterpreter(interp)
177             || _PyInterpreterState_Main() == NULL);
178     }
179     return 1;
180 }
181 
182 static int
refchain_init(PyInterpreterState * interp)183 refchain_init(PyInterpreterState *interp)
184 {
185     if (!has_own_refchain(interp)) {
186         // Legacy subinterpreters share a refchain with the main interpreter.
187         REFCHAIN(interp) = REFCHAIN(_PyInterpreterState_Main());
188         return 0;
189     }
190     _Py_hashtable_allocator_t alloc = {
191         // Don't use default PyMem_Malloc() and PyMem_Free() which
192         // require the caller to hold the GIL.
193         .malloc = PyMem_RawMalloc,
194         .free = PyMem_RawFree,
195     };
196     REFCHAIN(interp) = _Py_hashtable_new_full(
197         _Py_hashtable_hash_ptr, _Py_hashtable_compare_direct,
198         NULL, NULL, &alloc);
199     if (REFCHAIN(interp) == NULL) {
200         return -1;
201     }
202     return 0;
203 }
204 
205 static void
refchain_fini(PyInterpreterState * interp)206 refchain_fini(PyInterpreterState *interp)
207 {
208     if (has_own_refchain(interp) && REFCHAIN(interp) != NULL) {
209         _Py_hashtable_destroy(REFCHAIN(interp));
210     }
211     REFCHAIN(interp) = NULL;
212 }
213 
214 bool
_PyRefchain_IsTraced(PyInterpreterState * interp,PyObject * obj)215 _PyRefchain_IsTraced(PyInterpreterState *interp, PyObject *obj)
216 {
217     return (_Py_hashtable_get(REFCHAIN(interp), obj) == REFCHAIN_VALUE);
218 }
219 
220 
221 static void
_PyRefchain_Trace(PyInterpreterState * interp,PyObject * obj)222 _PyRefchain_Trace(PyInterpreterState *interp, PyObject *obj)
223 {
224     if (_Py_hashtable_set(REFCHAIN(interp), obj, REFCHAIN_VALUE) < 0) {
225         // Use a fatal error because _Py_NewReference() cannot report
226         // the error to the caller.
227         Py_FatalError("_Py_hashtable_set() memory allocation failed");
228     }
229 }
230 
231 
232 static void
_PyRefchain_Remove(PyInterpreterState * interp,PyObject * obj)233 _PyRefchain_Remove(PyInterpreterState *interp, PyObject *obj)
234 {
235     void *value = _Py_hashtable_steal(REFCHAIN(interp), obj);
236 #ifndef NDEBUG
237     assert(value == REFCHAIN_VALUE);
238 #else
239     (void)value;
240 #endif
241 }
242 
243 
244 /* Add an object to the refchain hash table.
245  *
246  * Note that objects are normally added to the list by PyObject_Init()
247  * indirectly.  Not all objects are initialized that way, though; exceptions
248  * include statically allocated type objects, and statically allocated
249  * singletons (like Py_True and Py_None). */
250 void
_Py_AddToAllObjects(PyObject * op)251 _Py_AddToAllObjects(PyObject *op)
252 {
253     PyInterpreterState *interp = _PyInterpreterState_GET();
254     if (!_PyRefchain_IsTraced(interp, op)) {
255         _PyRefchain_Trace(interp, op);
256     }
257 }
258 #endif  /* Py_TRACE_REFS */
259 
260 #ifdef Py_REF_DEBUG
261 /* Log a fatal error; doesn't return. */
262 void
_Py_NegativeRefcount(const char * filename,int lineno,PyObject * op)263 _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
264 {
265     _PyObject_AssertFailed(op, NULL, "object has negative ref count",
266                            filename, lineno, __func__);
267 }
268 
269 /* This is used strictly by Py_INCREF(). */
270 void
_Py_INCREF_IncRefTotal(void)271 _Py_INCREF_IncRefTotal(void)
272 {
273     reftotal_add(_PyThreadState_GET(), 1);
274 }
275 
276 /* This is used strictly by Py_DECREF(). */
277 void
_Py_DECREF_DecRefTotal(void)278 _Py_DECREF_DecRefTotal(void)
279 {
280     reftotal_add(_PyThreadState_GET(), -1);
281 }
282 
283 void
_Py_IncRefTotal(PyThreadState * tstate)284 _Py_IncRefTotal(PyThreadState *tstate)
285 {
286     reftotal_add(tstate, 1);
287 }
288 
289 void
_Py_DecRefTotal(PyThreadState * tstate)290 _Py_DecRefTotal(PyThreadState *tstate)
291 {
292     reftotal_add(tstate, -1);
293 }
294 
295 void
_Py_AddRefTotal(PyThreadState * tstate,Py_ssize_t n)296 _Py_AddRefTotal(PyThreadState *tstate, Py_ssize_t n)
297 {
298     reftotal_add(tstate, n);
299 }
300 
301 /* This includes the legacy total
302    and any carried over from the last runtime init/fini cycle. */
303 Py_ssize_t
_Py_GetGlobalRefTotal(void)304 _Py_GetGlobalRefTotal(void)
305 {
306     return get_global_reftotal(&_PyRuntime);
307 }
308 
309 Py_ssize_t
_Py_GetLegacyRefTotal(void)310 _Py_GetLegacyRefTotal(void)
311 {
312     return get_legacy_reftotal();
313 }
314 
315 Py_ssize_t
_PyInterpreterState_GetRefTotal(PyInterpreterState * interp)316 _PyInterpreterState_GetRefTotal(PyInterpreterState *interp)
317 {
318     HEAD_LOCK(&_PyRuntime);
319     Py_ssize_t total = get_reftotal(interp);
320     HEAD_UNLOCK(&_PyRuntime);
321     return total;
322 }
323 
324 #endif /* Py_REF_DEBUG */
325 
326 void
Py_IncRef(PyObject * o)327 Py_IncRef(PyObject *o)
328 {
329     Py_XINCREF(o);
330 }
331 
332 void
Py_DecRef(PyObject * o)333 Py_DecRef(PyObject *o)
334 {
335     Py_XDECREF(o);
336 }
337 
338 void
_Py_IncRef(PyObject * o)339 _Py_IncRef(PyObject *o)
340 {
341     Py_INCREF(o);
342 }
343 
344 void
_Py_DecRef(PyObject * o)345 _Py_DecRef(PyObject *o)
346 {
347     Py_DECREF(o);
348 }
349 
350 #ifdef Py_GIL_DISABLED
351 # ifdef Py_REF_DEBUG
352 static int
is_dead(PyObject * o)353 is_dead(PyObject *o)
354 {
355 #  if SIZEOF_SIZE_T == 8
356     return (uintptr_t)o->ob_type == 0xDDDDDDDDDDDDDDDD;
357 #  else
358     return (uintptr_t)o->ob_type == 0xDDDDDDDD;
359 #  endif
360 }
361 # endif
362 
363 void
_Py_DecRefSharedDebug(PyObject * o,const char * filename,int lineno)364 _Py_DecRefSharedDebug(PyObject *o, const char *filename, int lineno)
365 {
366     // Should we queue the object for the owning thread to merge?
367     int should_queue;
368 
369     Py_ssize_t new_shared;
370     Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&o->ob_ref_shared);
371     do {
372         should_queue = (shared == 0 || shared == _Py_REF_MAYBE_WEAKREF);
373 
374         if (should_queue) {
375             // If the object had refcount zero, not queued, and not merged,
376             // then we enqueue the object to be merged by the owning thread.
377             // In this case, we don't subtract one from the reference count
378             // because the queue holds a reference.
379             new_shared = _Py_REF_QUEUED;
380         }
381         else {
382             // Otherwise, subtract one from the reference count. This might
383             // be negative!
384             new_shared = shared - (1 << _Py_REF_SHARED_SHIFT);
385         }
386 
387 #ifdef Py_REF_DEBUG
388         if ((new_shared < 0 && _Py_REF_IS_MERGED(new_shared)) ||
389             (should_queue && is_dead(o)))
390         {
391             _Py_NegativeRefcount(filename, lineno, o);
392         }
393 #endif
394     } while (!_Py_atomic_compare_exchange_ssize(&o->ob_ref_shared,
395                                                 &shared, new_shared));
396 
397     if (should_queue) {
398 #ifdef Py_REF_DEBUG
399         _Py_IncRefTotal(_PyThreadState_GET());
400 #endif
401         _Py_brc_queue_object(o);
402     }
403     else if (new_shared == _Py_REF_MERGED) {
404         // refcount is zero AND merged
405         _Py_Dealloc(o);
406     }
407 }
408 
409 void
_Py_DecRefShared(PyObject * o)410 _Py_DecRefShared(PyObject *o)
411 {
412     _Py_DecRefSharedDebug(o, NULL, 0);
413 }
414 
415 void
_Py_MergeZeroLocalRefcount(PyObject * op)416 _Py_MergeZeroLocalRefcount(PyObject *op)
417 {
418     assert(op->ob_ref_local == 0);
419 
420     Py_ssize_t shared = _Py_atomic_load_ssize_acquire(&op->ob_ref_shared);
421     if (shared == 0) {
422         // Fast-path: shared refcount is zero (including flags)
423         _Py_Dealloc(op);
424         return;
425     }
426 
427     // gh-121794: This must be before the store to `ob_ref_shared` (gh-119999),
428     // but should outside the fast-path to maintain the invariant that
429     // a zero `ob_tid` implies a merged refcount.
430     _Py_atomic_store_uintptr_relaxed(&op->ob_tid, 0);
431 
432     // Slow-path: atomically set the flags (low two bits) to _Py_REF_MERGED.
433     Py_ssize_t new_shared;
434     do {
435         new_shared = (shared & ~_Py_REF_SHARED_FLAG_MASK) | _Py_REF_MERGED;
436     } while (!_Py_atomic_compare_exchange_ssize(&op->ob_ref_shared,
437                                                 &shared, new_shared));
438 
439     if (new_shared == _Py_REF_MERGED) {
440         // i.e., the shared refcount is zero (only the flags are set) so we
441         // deallocate the object.
442         _Py_Dealloc(op);
443     }
444 }
445 
446 Py_ssize_t
_Py_ExplicitMergeRefcount(PyObject * op,Py_ssize_t extra)447 _Py_ExplicitMergeRefcount(PyObject *op, Py_ssize_t extra)
448 {
449     assert(!_Py_IsImmortal(op));
450 
451 #ifdef Py_REF_DEBUG
452     _Py_AddRefTotal(_PyThreadState_GET(), extra);
453 #endif
454 
455     // gh-119999: Write to ob_ref_local and ob_tid before merging the refcount.
456     Py_ssize_t local = (Py_ssize_t)op->ob_ref_local;
457     _Py_atomic_store_uint32_relaxed(&op->ob_ref_local, 0);
458     _Py_atomic_store_uintptr_relaxed(&op->ob_tid, 0);
459 
460     Py_ssize_t refcnt;
461     Py_ssize_t new_shared;
462     Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&op->ob_ref_shared);
463     do {
464         refcnt = Py_ARITHMETIC_RIGHT_SHIFT(Py_ssize_t, shared, _Py_REF_SHARED_SHIFT);
465         refcnt += local;
466         refcnt += extra;
467 
468         new_shared = _Py_REF_SHARED(refcnt, _Py_REF_MERGED);
469     } while (!_Py_atomic_compare_exchange_ssize(&op->ob_ref_shared,
470                                                 &shared, new_shared));
471     return refcnt;
472 }
473 #endif  /* Py_GIL_DISABLED */
474 
475 
476 /**************************************/
477 
478 PyObject *
PyObject_Init(PyObject * op,PyTypeObject * tp)479 PyObject_Init(PyObject *op, PyTypeObject *tp)
480 {
481     if (op == NULL) {
482         return PyErr_NoMemory();
483     }
484 
485     _PyObject_Init(op, tp);
486     return op;
487 }
488 
489 PyVarObject *
PyObject_InitVar(PyVarObject * op,PyTypeObject * tp,Py_ssize_t size)490 PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size)
491 {
492     if (op == NULL) {
493         return (PyVarObject *) PyErr_NoMemory();
494     }
495 
496     _PyObject_InitVar(op, tp, size);
497     return op;
498 }
499 
500 PyObject *
_PyObject_New(PyTypeObject * tp)501 _PyObject_New(PyTypeObject *tp)
502 {
503     PyObject *op = (PyObject *) PyObject_Malloc(_PyObject_SIZE(tp));
504     if (op == NULL) {
505         return PyErr_NoMemory();
506     }
507     _PyObject_Init(op, tp);
508     return op;
509 }
510 
511 PyVarObject *
_PyObject_NewVar(PyTypeObject * tp,Py_ssize_t nitems)512 _PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems)
513 {
514     PyVarObject *op;
515     const size_t size = _PyObject_VAR_SIZE(tp, nitems);
516     op = (PyVarObject *) PyObject_Malloc(size);
517     if (op == NULL) {
518         return (PyVarObject *)PyErr_NoMemory();
519     }
520     _PyObject_InitVar(op, tp, nitems);
521     return op;
522 }
523 
524 void
PyObject_CallFinalizer(PyObject * self)525 PyObject_CallFinalizer(PyObject *self)
526 {
527     PyTypeObject *tp = Py_TYPE(self);
528 
529     if (tp->tp_finalize == NULL)
530         return;
531     /* tp_finalize should only be called once. */
532     if (_PyType_IS_GC(tp) && _PyGC_FINALIZED(self))
533         return;
534 
535     tp->tp_finalize(self);
536     if (_PyType_IS_GC(tp)) {
537         _PyGC_SET_FINALIZED(self);
538     }
539 }
540 
541 int
PyObject_CallFinalizerFromDealloc(PyObject * self)542 PyObject_CallFinalizerFromDealloc(PyObject *self)
543 {
544     if (Py_REFCNT(self) != 0) {
545         _PyObject_ASSERT_FAILED_MSG(self,
546                                     "PyObject_CallFinalizerFromDealloc called "
547                                     "on object with a non-zero refcount");
548     }
549 
550     /* Temporarily resurrect the object. */
551     Py_SET_REFCNT(self, 1);
552 
553     PyObject_CallFinalizer(self);
554 
555     _PyObject_ASSERT_WITH_MSG(self,
556                               Py_REFCNT(self) > 0,
557                               "refcount is too small");
558 
559     /* Undo the temporary resurrection; can't use DECREF here, it would
560      * cause a recursive call. */
561     Py_SET_REFCNT(self, Py_REFCNT(self) - 1);
562     if (Py_REFCNT(self) == 0) {
563         return 0;         /* this is the normal path out */
564     }
565 
566     /* tp_finalize resurrected it!  Make it look like the original Py_DECREF
567      * never happened. */
568     _Py_ResurrectReference(self);
569 
570     _PyObject_ASSERT(self,
571                      (!_PyType_IS_GC(Py_TYPE(self))
572                       || _PyObject_GC_IS_TRACKED(self)));
573     return -1;
574 }
575 
576 int
PyObject_Print(PyObject * op,FILE * fp,int flags)577 PyObject_Print(PyObject *op, FILE *fp, int flags)
578 {
579     int ret = 0;
580     int write_error = 0;
581     if (PyErr_CheckSignals())
582         return -1;
583 #ifdef USE_STACKCHECK
584     if (PyOS_CheckStack()) {
585         PyErr_SetString(PyExc_MemoryError, "stack overflow");
586         return -1;
587     }
588 #endif
589     clearerr(fp); /* Clear any previous error condition */
590     if (op == NULL) {
591         Py_BEGIN_ALLOW_THREADS
592         fprintf(fp, "<nil>");
593         Py_END_ALLOW_THREADS
594     }
595     else {
596         if (Py_REFCNT(op) <= 0) {
597             Py_BEGIN_ALLOW_THREADS
598             fprintf(fp, "<refcnt %zd at %p>", Py_REFCNT(op), (void *)op);
599             Py_END_ALLOW_THREADS
600         }
601         else {
602             PyObject *s;
603             if (flags & Py_PRINT_RAW)
604                 s = PyObject_Str(op);
605             else
606                 s = PyObject_Repr(op);
607             if (s == NULL) {
608                 ret = -1;
609             }
610             else {
611                 assert(PyUnicode_Check(s));
612                 const char *t;
613                 Py_ssize_t len;
614                 t = PyUnicode_AsUTF8AndSize(s, &len);
615                 if (t == NULL) {
616                     ret = -1;
617                 }
618                 else {
619                     /* Versions of Android and OpenBSD from before 2023 fail to
620                        set the `ferror` indicator when writing to a read-only
621                        stream, so we need to check the return value.
622                        (https://github.com/openbsd/src/commit/fc99cf9338942ecd9adc94ea08bf6188f0428c15) */
623                     if (fwrite(t, 1, len, fp) != (size_t)len) {
624                         write_error = 1;
625                     }
626                 }
627                 Py_DECREF(s);
628             }
629         }
630     }
631     if (ret == 0) {
632         if (write_error || ferror(fp)) {
633             PyErr_SetFromErrno(PyExc_OSError);
634             clearerr(fp);
635             ret = -1;
636         }
637     }
638     return ret;
639 }
640 
641 /* For debugging convenience.  Set a breakpoint here and call it from your DLL */
642 void
_Py_BreakPoint(void)643 _Py_BreakPoint(void)
644 {
645 }
646 
647 
648 /* Heuristic checking if the object memory is uninitialized or deallocated.
649    Rely on the debug hooks on Python memory allocators:
650    see _PyMem_IsPtrFreed().
651 
652    The function can be used to prevent segmentation fault on dereferencing
653    pointers like 0xDDDDDDDDDDDDDDDD. */
654 int
_PyObject_IsFreed(PyObject * op)655 _PyObject_IsFreed(PyObject *op)
656 {
657     if (_PyMem_IsPtrFreed(op) || _PyMem_IsPtrFreed(Py_TYPE(op))) {
658         return 1;
659     }
660     return 0;
661 }
662 
663 
664 /* For debugging convenience.  See Misc/gdbinit for some useful gdb hooks */
665 void
_PyObject_Dump(PyObject * op)666 _PyObject_Dump(PyObject* op)
667 {
668     if (_PyObject_IsFreed(op)) {
669         /* It seems like the object memory has been freed:
670            don't access it to prevent a segmentation fault. */
671         fprintf(stderr, "<object at %p is freed>\n", op);
672         fflush(stderr);
673         return;
674     }
675 
676     /* first, write fields which are the least likely to crash */
677     fprintf(stderr, "object address  : %p\n", (void *)op);
678     fprintf(stderr, "object refcount : %zd\n", Py_REFCNT(op));
679     fflush(stderr);
680 
681     PyTypeObject *type = Py_TYPE(op);
682     fprintf(stderr, "object type     : %p\n", type);
683     fprintf(stderr, "object type name: %s\n",
684             type==NULL ? "NULL" : type->tp_name);
685 
686     /* the most dangerous part */
687     fprintf(stderr, "object repr     : ");
688     fflush(stderr);
689 
690     PyGILState_STATE gil = PyGILState_Ensure();
691     PyObject *exc = PyErr_GetRaisedException();
692 
693     (void)PyObject_Print(op, stderr, 0);
694     fflush(stderr);
695 
696     PyErr_SetRaisedException(exc);
697     PyGILState_Release(gil);
698 
699     fprintf(stderr, "\n");
700     fflush(stderr);
701 }
702 
703 PyObject *
PyObject_Repr(PyObject * v)704 PyObject_Repr(PyObject *v)
705 {
706     PyObject *res;
707     if (PyErr_CheckSignals())
708         return NULL;
709 #ifdef USE_STACKCHECK
710     if (PyOS_CheckStack()) {
711         PyErr_SetString(PyExc_MemoryError, "stack overflow");
712         return NULL;
713     }
714 #endif
715     if (v == NULL)
716         return PyUnicode_FromString("<NULL>");
717     if (Py_TYPE(v)->tp_repr == NULL)
718         return PyUnicode_FromFormat("<%s object at %p>",
719                                     Py_TYPE(v)->tp_name, v);
720 
721     PyThreadState *tstate = _PyThreadState_GET();
722 #ifdef Py_DEBUG
723     /* PyObject_Repr() must not be called with an exception set,
724        because it can clear it (directly or indirectly) and so the
725        caller loses its exception */
726     assert(!_PyErr_Occurred(tstate));
727 #endif
728 
729     /* It is possible for a type to have a tp_repr representation that loops
730        infinitely. */
731     if (_Py_EnterRecursiveCallTstate(tstate,
732                                      " while getting the repr of an object")) {
733         return NULL;
734     }
735     res = (*Py_TYPE(v)->tp_repr)(v);
736     _Py_LeaveRecursiveCallTstate(tstate);
737 
738     if (res == NULL) {
739         return NULL;
740     }
741     if (!PyUnicode_Check(res)) {
742         _PyErr_Format(tstate, PyExc_TypeError,
743                       "__repr__ returned non-string (type %.200s)",
744                       Py_TYPE(res)->tp_name);
745         Py_DECREF(res);
746         return NULL;
747     }
748     return res;
749 }
750 
751 PyObject *
PyObject_Str(PyObject * v)752 PyObject_Str(PyObject *v)
753 {
754     PyObject *res;
755     if (PyErr_CheckSignals())
756         return NULL;
757 #ifdef USE_STACKCHECK
758     if (PyOS_CheckStack()) {
759         PyErr_SetString(PyExc_MemoryError, "stack overflow");
760         return NULL;
761     }
762 #endif
763     if (v == NULL)
764         return PyUnicode_FromString("<NULL>");
765     if (PyUnicode_CheckExact(v)) {
766         return Py_NewRef(v);
767     }
768     if (Py_TYPE(v)->tp_str == NULL)
769         return PyObject_Repr(v);
770 
771     PyThreadState *tstate = _PyThreadState_GET();
772 #ifdef Py_DEBUG
773     /* PyObject_Str() must not be called with an exception set,
774        because it can clear it (directly or indirectly) and so the
775        caller loses its exception */
776     assert(!_PyErr_Occurred(tstate));
777 #endif
778 
779     /* It is possible for a type to have a tp_str representation that loops
780        infinitely. */
781     if (_Py_EnterRecursiveCallTstate(tstate, " while getting the str of an object")) {
782         return NULL;
783     }
784     res = (*Py_TYPE(v)->tp_str)(v);
785     _Py_LeaveRecursiveCallTstate(tstate);
786 
787     if (res == NULL) {
788         return NULL;
789     }
790     if (!PyUnicode_Check(res)) {
791         _PyErr_Format(tstate, PyExc_TypeError,
792                       "__str__ returned non-string (type %.200s)",
793                       Py_TYPE(res)->tp_name);
794         Py_DECREF(res);
795         return NULL;
796     }
797     assert(_PyUnicode_CheckConsistency(res, 1));
798     return res;
799 }
800 
801 PyObject *
PyObject_ASCII(PyObject * v)802 PyObject_ASCII(PyObject *v)
803 {
804     PyObject *repr, *ascii, *res;
805 
806     repr = PyObject_Repr(v);
807     if (repr == NULL)
808         return NULL;
809 
810     if (PyUnicode_IS_ASCII(repr))
811         return repr;
812 
813     /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
814     ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
815     Py_DECREF(repr);
816     if (ascii == NULL)
817         return NULL;
818 
819     res = PyUnicode_DecodeASCII(
820         PyBytes_AS_STRING(ascii),
821         PyBytes_GET_SIZE(ascii),
822         NULL);
823 
824     Py_DECREF(ascii);
825     return res;
826 }
827 
828 PyObject *
PyObject_Bytes(PyObject * v)829 PyObject_Bytes(PyObject *v)
830 {
831     PyObject *result, *func;
832 
833     if (v == NULL)
834         return PyBytes_FromString("<NULL>");
835 
836     if (PyBytes_CheckExact(v)) {
837         return Py_NewRef(v);
838     }
839 
840     func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__));
841     if (func != NULL) {
842         result = _PyObject_CallNoArgs(func);
843         Py_DECREF(func);
844         if (result == NULL)
845             return NULL;
846         if (!PyBytes_Check(result)) {
847             PyErr_Format(PyExc_TypeError,
848                          "__bytes__ returned non-bytes (type %.200s)",
849                          Py_TYPE(result)->tp_name);
850             Py_DECREF(result);
851             return NULL;
852         }
853         return result;
854     }
855     else if (PyErr_Occurred())
856         return NULL;
857     return PyBytes_FromObject(v);
858 }
859 
860 void
_PyObject_ClearFreeLists(struct _Py_object_freelists * freelists,int is_finalization)861 _PyObject_ClearFreeLists(struct _Py_object_freelists *freelists, int is_finalization)
862 {
863     // In the free-threaded build, freelists are per-PyThreadState and cleared in PyThreadState_Clear()
864     // In the default build, freelists are per-interpreter and cleared in finalize_interp_types()
865     _PyFloat_ClearFreeList(freelists, is_finalization);
866     _PyTuple_ClearFreeList(freelists, is_finalization);
867     _PyList_ClearFreeList(freelists, is_finalization);
868     _PyDict_ClearFreeList(freelists, is_finalization);
869     _PyContext_ClearFreeList(freelists, is_finalization);
870     _PyAsyncGen_ClearFreeLists(freelists, is_finalization);
871     // Only be cleared if is_finalization is true.
872     _PyObjectStackChunk_ClearFreeList(freelists, is_finalization);
873     _PySlice_ClearFreeList(freelists, is_finalization);
874 }
875 
876 /*
877 def _PyObject_FunctionStr(x):
878     try:
879         qualname = x.__qualname__
880     except AttributeError:
881         return str(x)
882     try:
883         mod = x.__module__
884         if mod is not None and mod != 'builtins':
885             return f"{x.__module__}.{qualname}()"
886     except AttributeError:
887         pass
888     return qualname
889 */
890 PyObject *
_PyObject_FunctionStr(PyObject * x)891 _PyObject_FunctionStr(PyObject *x)
892 {
893     assert(!PyErr_Occurred());
894     PyObject *qualname;
895     int ret = PyObject_GetOptionalAttr(x, &_Py_ID(__qualname__), &qualname);
896     if (qualname == NULL) {
897         if (ret < 0) {
898             return NULL;
899         }
900         return PyObject_Str(x);
901     }
902     PyObject *module;
903     PyObject *result = NULL;
904     ret = PyObject_GetOptionalAttr(x, &_Py_ID(__module__), &module);
905     if (module != NULL && module != Py_None) {
906         ret = PyObject_RichCompareBool(module, &_Py_ID(builtins), Py_NE);
907         if (ret < 0) {
908             // error
909             goto done;
910         }
911         if (ret > 0) {
912             result = PyUnicode_FromFormat("%S.%S()", module, qualname);
913             goto done;
914         }
915     }
916     else if (ret < 0) {
917         goto done;
918     }
919     result = PyUnicode_FromFormat("%S()", qualname);
920 done:
921     Py_DECREF(qualname);
922     Py_XDECREF(module);
923     return result;
924 }
925 
926 /* For Python 3.0.1 and later, the old three-way comparison has been
927    completely removed in favour of rich comparisons.  PyObject_Compare() and
928    PyObject_Cmp() are gone, and the builtin cmp function no longer exists.
929    The old tp_compare slot has been renamed to tp_as_async, and should no
930    longer be used.  Use tp_richcompare instead.
931 
932    See (*) below for practical amendments.
933 
934    tp_richcompare gets called with a first argument of the appropriate type
935    and a second object of an arbitrary type.  We never do any kind of
936    coercion.
937 
938    The tp_richcompare slot should return an object, as follows:
939 
940     NULL if an exception occurred
941     NotImplemented if the requested comparison is not implemented
942     any other false value if the requested comparison is false
943     any other true value if the requested comparison is true
944 
945   The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get
946   NotImplemented.
947 
948   (*) Practical amendments:
949 
950   - If rich comparison returns NotImplemented, == and != are decided by
951     comparing the object pointer (i.e. falling back to the base object
952     implementation).
953 
954 */
955 
956 /* Map rich comparison operators to their swapped version, e.g. LT <--> GT */
957 int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
958 
959 static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
960 
961 /* Perform a rich comparison, raising TypeError when the requested comparison
962    operator is not supported. */
963 static PyObject *
do_richcompare(PyThreadState * tstate,PyObject * v,PyObject * w,int op)964 do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
965 {
966     richcmpfunc f;
967     PyObject *res;
968     int checked_reverse_op = 0;
969 
970     if (!Py_IS_TYPE(v, Py_TYPE(w)) &&
971         PyType_IsSubtype(Py_TYPE(w), Py_TYPE(v)) &&
972         (f = Py_TYPE(w)->tp_richcompare) != NULL) {
973         checked_reverse_op = 1;
974         res = (*f)(w, v, _Py_SwappedOp[op]);
975         if (res != Py_NotImplemented)
976             return res;
977         Py_DECREF(res);
978     }
979     if ((f = Py_TYPE(v)->tp_richcompare) != NULL) {
980         res = (*f)(v, w, op);
981         if (res != Py_NotImplemented)
982             return res;
983         Py_DECREF(res);
984     }
985     if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) {
986         res = (*f)(w, v, _Py_SwappedOp[op]);
987         if (res != Py_NotImplemented)
988             return res;
989         Py_DECREF(res);
990     }
991     /* If neither object implements it, provide a sensible default
992        for == and !=, but raise an exception for ordering. */
993     switch (op) {
994     case Py_EQ:
995         res = (v == w) ? Py_True : Py_False;
996         break;
997     case Py_NE:
998         res = (v != w) ? Py_True : Py_False;
999         break;
1000     default:
1001         _PyErr_Format(tstate, PyExc_TypeError,
1002                       "'%s' not supported between instances of '%.100s' and '%.100s'",
1003                       opstrings[op],
1004                       Py_TYPE(v)->tp_name,
1005                       Py_TYPE(w)->tp_name);
1006         return NULL;
1007     }
1008     return Py_NewRef(res);
1009 }
1010 
1011 /* Perform a rich comparison with object result.  This wraps do_richcompare()
1012    with a check for NULL arguments and a recursion check. */
1013 
1014 PyObject *
PyObject_RichCompare(PyObject * v,PyObject * w,int op)1015 PyObject_RichCompare(PyObject *v, PyObject *w, int op)
1016 {
1017     PyThreadState *tstate = _PyThreadState_GET();
1018 
1019     assert(Py_LT <= op && op <= Py_GE);
1020     if (v == NULL || w == NULL) {
1021         if (!_PyErr_Occurred(tstate)) {
1022             PyErr_BadInternalCall();
1023         }
1024         return NULL;
1025     }
1026     if (_Py_EnterRecursiveCallTstate(tstate, " in comparison")) {
1027         return NULL;
1028     }
1029     PyObject *res = do_richcompare(tstate, v, w, op);
1030     _Py_LeaveRecursiveCallTstate(tstate);
1031     return res;
1032 }
1033 
1034 /* Perform a rich comparison with integer result.  This wraps
1035    PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */
1036 int
PyObject_RichCompareBool(PyObject * v,PyObject * w,int op)1037 PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
1038 {
1039     PyObject *res;
1040     int ok;
1041 
1042     /* Quick result when objects are the same.
1043        Guarantees that identity implies equality. */
1044     if (v == w) {
1045         if (op == Py_EQ)
1046             return 1;
1047         else if (op == Py_NE)
1048             return 0;
1049     }
1050 
1051     res = PyObject_RichCompare(v, w, op);
1052     if (res == NULL)
1053         return -1;
1054     if (PyBool_Check(res))
1055         ok = (res == Py_True);
1056     else
1057         ok = PyObject_IsTrue(res);
1058     Py_DECREF(res);
1059     return ok;
1060 }
1061 
1062 Py_hash_t
PyObject_HashNotImplemented(PyObject * v)1063 PyObject_HashNotImplemented(PyObject *v)
1064 {
1065     PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
1066                  Py_TYPE(v)->tp_name);
1067     return -1;
1068 }
1069 
1070 Py_hash_t
PyObject_Hash(PyObject * v)1071 PyObject_Hash(PyObject *v)
1072 {
1073     PyTypeObject *tp = Py_TYPE(v);
1074     if (tp->tp_hash != NULL)
1075         return (*tp->tp_hash)(v);
1076     /* To keep to the general practice that inheriting
1077      * solely from object in C code should work without
1078      * an explicit call to PyType_Ready, we implicitly call
1079      * PyType_Ready here and then check the tp_hash slot again
1080      */
1081     if (!_PyType_IsReady(tp)) {
1082         if (PyType_Ready(tp) < 0)
1083             return -1;
1084         if (tp->tp_hash != NULL)
1085             return (*tp->tp_hash)(v);
1086     }
1087     /* Otherwise, the object can't be hashed */
1088     return PyObject_HashNotImplemented(v);
1089 }
1090 
1091 PyObject *
PyObject_GetAttrString(PyObject * v,const char * name)1092 PyObject_GetAttrString(PyObject *v, const char *name)
1093 {
1094     PyObject *w, *res;
1095 
1096     if (Py_TYPE(v)->tp_getattr != NULL)
1097         return (*Py_TYPE(v)->tp_getattr)(v, (char*)name);
1098     w = PyUnicode_FromString(name);
1099     if (w == NULL)
1100         return NULL;
1101     res = PyObject_GetAttr(v, w);
1102     Py_DECREF(w);
1103     return res;
1104 }
1105 
1106 int
PyObject_HasAttrStringWithError(PyObject * obj,const char * name)1107 PyObject_HasAttrStringWithError(PyObject *obj, const char *name)
1108 {
1109     PyObject *res;
1110     int rc = PyObject_GetOptionalAttrString(obj, name, &res);
1111     Py_XDECREF(res);
1112     return rc;
1113 }
1114 
1115 
1116 int
PyObject_HasAttrString(PyObject * obj,const char * name)1117 PyObject_HasAttrString(PyObject *obj, const char *name)
1118 {
1119     int rc = PyObject_HasAttrStringWithError(obj, name);
1120     if (rc < 0) {
1121         PyErr_FormatUnraisable(
1122             "Exception ignored in PyObject_HasAttrString(); consider using "
1123             "PyObject_HasAttrStringWithError(), "
1124             "PyObject_GetOptionalAttrString() or PyObject_GetAttrString()");
1125         return 0;
1126     }
1127     return rc;
1128 }
1129 
1130 int
PyObject_SetAttrString(PyObject * v,const char * name,PyObject * w)1131 PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w)
1132 {
1133     PyObject *s;
1134     int res;
1135 
1136     if (Py_TYPE(v)->tp_setattr != NULL)
1137         return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w);
1138     s = PyUnicode_InternFromString(name);
1139     if (s == NULL)
1140         return -1;
1141     res = PyObject_SetAttr(v, s, w);
1142     Py_XDECREF(s);
1143     return res;
1144 }
1145 
1146 int
PyObject_DelAttrString(PyObject * v,const char * name)1147 PyObject_DelAttrString(PyObject *v, const char *name)
1148 {
1149     return PyObject_SetAttrString(v, name, NULL);
1150 }
1151 
1152 int
_PyObject_IsAbstract(PyObject * obj)1153 _PyObject_IsAbstract(PyObject *obj)
1154 {
1155     int res;
1156     PyObject* isabstract;
1157 
1158     if (obj == NULL)
1159         return 0;
1160 
1161     res = PyObject_GetOptionalAttr(obj, &_Py_ID(__isabstractmethod__), &isabstract);
1162     if (res > 0) {
1163         res = PyObject_IsTrue(isabstract);
1164         Py_DECREF(isabstract);
1165     }
1166     return res;
1167 }
1168 
1169 PyObject *
_PyObject_GetAttrId(PyObject * v,_Py_Identifier * name)1170 _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
1171 {
1172     PyObject *result;
1173     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
1174     if (!oname)
1175         return NULL;
1176     result = PyObject_GetAttr(v, oname);
1177     return result;
1178 }
1179 
1180 int
_PyObject_SetAttrId(PyObject * v,_Py_Identifier * name,PyObject * w)1181 _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
1182 {
1183     int result;
1184     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
1185     if (!oname)
1186         return -1;
1187     result = PyObject_SetAttr(v, oname, w);
1188     return result;
1189 }
1190 
1191 int
_PyObject_SetAttributeErrorContext(PyObject * v,PyObject * name)1192 _PyObject_SetAttributeErrorContext(PyObject* v, PyObject* name)
1193 {
1194     assert(PyErr_Occurred());
1195     if (!PyErr_ExceptionMatches(PyExc_AttributeError)){
1196         return 0;
1197     }
1198     // Intercept AttributeError exceptions and augment them to offer suggestions later.
1199     PyObject *exc = PyErr_GetRaisedException();
1200     if (!PyErr_GivenExceptionMatches(exc, PyExc_AttributeError)) {
1201         goto restore;
1202     }
1203     PyAttributeErrorObject* the_exc = (PyAttributeErrorObject*) exc;
1204     // Check if this exception was already augmented
1205     if (the_exc->name || the_exc->obj) {
1206         goto restore;
1207     }
1208     // Augment the exception with the name and object
1209     if (PyObject_SetAttr(exc, &_Py_ID(name), name) ||
1210         PyObject_SetAttr(exc, &_Py_ID(obj), v)) {
1211         return 1;
1212     }
1213 restore:
1214     PyErr_SetRaisedException(exc);
1215     return 0;
1216 }
1217 
1218 PyObject *
PyObject_GetAttr(PyObject * v,PyObject * name)1219 PyObject_GetAttr(PyObject *v, PyObject *name)
1220 {
1221     PyTypeObject *tp = Py_TYPE(v);
1222     if (!PyUnicode_Check(name)) {
1223         PyErr_Format(PyExc_TypeError,
1224                      "attribute name must be string, not '%.200s'",
1225                      Py_TYPE(name)->tp_name);
1226         return NULL;
1227     }
1228 
1229     PyObject* result = NULL;
1230     if (tp->tp_getattro != NULL) {
1231         result = (*tp->tp_getattro)(v, name);
1232     }
1233     else if (tp->tp_getattr != NULL) {
1234         const char *name_str = PyUnicode_AsUTF8(name);
1235         if (name_str == NULL) {
1236             return NULL;
1237         }
1238         result = (*tp->tp_getattr)(v, (char *)name_str);
1239     }
1240     else {
1241         PyErr_Format(PyExc_AttributeError,
1242                     "'%.100s' object has no attribute '%U'",
1243                     tp->tp_name, name);
1244     }
1245 
1246     if (result == NULL) {
1247         _PyObject_SetAttributeErrorContext(v, name);
1248     }
1249     return result;
1250 }
1251 
1252 int
PyObject_GetOptionalAttr(PyObject * v,PyObject * name,PyObject ** result)1253 PyObject_GetOptionalAttr(PyObject *v, PyObject *name, PyObject **result)
1254 {
1255     PyTypeObject *tp = Py_TYPE(v);
1256 
1257     if (!PyUnicode_Check(name)) {
1258         PyErr_Format(PyExc_TypeError,
1259                      "attribute name must be string, not '%.200s'",
1260                      Py_TYPE(name)->tp_name);
1261         *result = NULL;
1262         return -1;
1263     }
1264 
1265     if (tp->tp_getattro == PyObject_GenericGetAttr) {
1266         *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
1267         if (*result != NULL) {
1268             return 1;
1269         }
1270         if (PyErr_Occurred()) {
1271             return -1;
1272         }
1273         return 0;
1274     }
1275     if (tp->tp_getattro == _Py_type_getattro) {
1276         int supress_missing_attribute_exception = 0;
1277         *result = _Py_type_getattro_impl((PyTypeObject*)v, name, &supress_missing_attribute_exception);
1278         if (supress_missing_attribute_exception) {
1279             // return 0 without having to clear the exception
1280             return 0;
1281         }
1282     }
1283     else if (tp->tp_getattro == (getattrofunc)_Py_module_getattro) {
1284         // optimization: suppress attribute error from module getattro method
1285         *result = _Py_module_getattro_impl((PyModuleObject*)v, name, 1);
1286         if (*result != NULL) {
1287             return 1;
1288         }
1289         if (PyErr_Occurred()) {
1290             return -1;
1291         }
1292         return 0;
1293     }
1294     else if (tp->tp_getattro != NULL) {
1295         *result = (*tp->tp_getattro)(v, name);
1296     }
1297     else if (tp->tp_getattr != NULL) {
1298         const char *name_str = PyUnicode_AsUTF8(name);
1299         if (name_str == NULL) {
1300             *result = NULL;
1301             return -1;
1302         }
1303         *result = (*tp->tp_getattr)(v, (char *)name_str);
1304     }
1305     else {
1306         *result = NULL;
1307         return 0;
1308     }
1309 
1310     if (*result != NULL) {
1311         return 1;
1312     }
1313     if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
1314         return -1;
1315     }
1316     PyErr_Clear();
1317     return 0;
1318 }
1319 
1320 int
PyObject_GetOptionalAttrString(PyObject * obj,const char * name,PyObject ** result)1321 PyObject_GetOptionalAttrString(PyObject *obj, const char *name, PyObject **result)
1322 {
1323     if (Py_TYPE(obj)->tp_getattr == NULL) {
1324         PyObject *oname = PyUnicode_FromString(name);
1325         if (oname == NULL) {
1326             *result = NULL;
1327             return -1;
1328         }
1329         int rc = PyObject_GetOptionalAttr(obj, oname, result);
1330         Py_DECREF(oname);
1331         return rc;
1332     }
1333 
1334     *result = (*Py_TYPE(obj)->tp_getattr)(obj, (char*)name);
1335     if (*result != NULL) {
1336         return 1;
1337     }
1338     if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
1339         return -1;
1340     }
1341     PyErr_Clear();
1342     return 0;
1343 }
1344 
1345 int
PyObject_HasAttrWithError(PyObject * obj,PyObject * name)1346 PyObject_HasAttrWithError(PyObject *obj, PyObject *name)
1347 {
1348     PyObject *res;
1349     int rc = PyObject_GetOptionalAttr(obj, name, &res);
1350     Py_XDECREF(res);
1351     return rc;
1352 }
1353 
1354 int
PyObject_HasAttr(PyObject * obj,PyObject * name)1355 PyObject_HasAttr(PyObject *obj, PyObject *name)
1356 {
1357     int rc = PyObject_HasAttrWithError(obj, name);
1358     if (rc < 0) {
1359         PyErr_FormatUnraisable(
1360             "Exception ignored in PyObject_HasAttr(); consider using "
1361             "PyObject_HasAttrWithError(), "
1362             "PyObject_GetOptionalAttr() or PyObject_GetAttr()");
1363         return 0;
1364     }
1365     return rc;
1366 }
1367 
1368 int
PyObject_SetAttr(PyObject * v,PyObject * name,PyObject * value)1369 PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
1370 {
1371     PyTypeObject *tp = Py_TYPE(v);
1372     int err;
1373 
1374     if (!PyUnicode_Check(name)) {
1375         PyErr_Format(PyExc_TypeError,
1376                      "attribute name must be string, not '%.200s'",
1377                      Py_TYPE(name)->tp_name);
1378         return -1;
1379     }
1380     Py_INCREF(name);
1381 
1382     PyInterpreterState *interp = _PyInterpreterState_GET();
1383     _PyUnicode_InternMortal(interp, &name);
1384     if (tp->tp_setattro != NULL) {
1385         err = (*tp->tp_setattro)(v, name, value);
1386         Py_DECREF(name);
1387         return err;
1388     }
1389     if (tp->tp_setattr != NULL) {
1390         const char *name_str = PyUnicode_AsUTF8(name);
1391         if (name_str == NULL) {
1392             Py_DECREF(name);
1393             return -1;
1394         }
1395         err = (*tp->tp_setattr)(v, (char *)name_str, value);
1396         Py_DECREF(name);
1397         return err;
1398     }
1399     Py_DECREF(name);
1400     _PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
1401     if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1402         PyErr_Format(PyExc_TypeError,
1403                      "'%.100s' object has no attributes "
1404                      "(%s .%U)",
1405                      tp->tp_name,
1406                      value==NULL ? "del" : "assign to",
1407                      name);
1408     else
1409         PyErr_Format(PyExc_TypeError,
1410                      "'%.100s' object has only read-only attributes "
1411                      "(%s .%U)",
1412                      tp->tp_name,
1413                      value==NULL ? "del" : "assign to",
1414                      name);
1415     return -1;
1416 }
1417 
1418 int
PyObject_DelAttr(PyObject * v,PyObject * name)1419 PyObject_DelAttr(PyObject *v, PyObject *name)
1420 {
1421     return PyObject_SetAttr(v, name, NULL);
1422 }
1423 
1424 PyObject **
_PyObject_ComputedDictPointer(PyObject * obj)1425 _PyObject_ComputedDictPointer(PyObject *obj)
1426 {
1427     PyTypeObject *tp = Py_TYPE(obj);
1428     assert((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
1429 
1430     Py_ssize_t dictoffset = tp->tp_dictoffset;
1431     if (dictoffset == 0) {
1432         return NULL;
1433     }
1434 
1435     if (dictoffset < 0) {
1436         assert(dictoffset != -1);
1437 
1438         Py_ssize_t tsize = Py_SIZE(obj);
1439         if (tsize < 0) {
1440             tsize = -tsize;
1441         }
1442         size_t size = _PyObject_VAR_SIZE(tp, tsize);
1443         assert(size <= (size_t)PY_SSIZE_T_MAX);
1444         dictoffset += (Py_ssize_t)size;
1445 
1446         _PyObject_ASSERT(obj, dictoffset > 0);
1447         _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
1448     }
1449     return (PyObject **) ((char *)obj + dictoffset);
1450 }
1451 
1452 /* Helper to get a pointer to an object's __dict__ slot, if any.
1453  * Creates the dict from inline attributes if necessary.
1454  * Does not set an exception.
1455  *
1456  * Note that the tp_dictoffset docs used to recommend this function,
1457  * so it should be treated as part of the public API.
1458  */
1459 PyObject **
_PyObject_GetDictPtr(PyObject * obj)1460 _PyObject_GetDictPtr(PyObject *obj)
1461 {
1462     if ((Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) {
1463         return _PyObject_ComputedDictPointer(obj);
1464     }
1465     PyDictObject *dict = _PyObject_GetManagedDict(obj);
1466     if (dict == NULL && Py_TYPE(obj)->tp_flags & Py_TPFLAGS_INLINE_VALUES) {
1467         dict = _PyObject_MaterializeManagedDict(obj);
1468         if (dict == NULL) {
1469             PyErr_Clear();
1470             return NULL;
1471         }
1472     }
1473     return (PyObject **)&_PyObject_ManagedDictPointer(obj)->dict;
1474 }
1475 
1476 PyObject *
PyObject_SelfIter(PyObject * obj)1477 PyObject_SelfIter(PyObject *obj)
1478 {
1479     return Py_NewRef(obj);
1480 }
1481 
1482 /* Helper used when the __next__ method is removed from a type:
1483    tp_iternext is never NULL and can be safely called without checking
1484    on every iteration.
1485  */
1486 
1487 PyObject *
_PyObject_NextNotImplemented(PyObject * self)1488 _PyObject_NextNotImplemented(PyObject *self)
1489 {
1490     PyErr_Format(PyExc_TypeError,
1491                  "'%.200s' object is not iterable",
1492                  Py_TYPE(self)->tp_name);
1493     return NULL;
1494 }
1495 
1496 
1497 /* Specialized version of _PyObject_GenericGetAttrWithDict
1498    specifically for the LOAD_METHOD opcode.
1499 
1500    Return 1 if a method is found, 0 if it's a regular attribute
1501    from __dict__ or something returned by using a descriptor
1502    protocol.
1503 
1504    `method` will point to the resolved attribute or NULL.  In the
1505    latter case, an error will be set.
1506 */
1507 int
_PyObject_GetMethod(PyObject * obj,PyObject * name,PyObject ** method)1508 _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1509 {
1510     int meth_found = 0;
1511 
1512     assert(*method == NULL);
1513 
1514     PyTypeObject *tp = Py_TYPE(obj);
1515     if (!_PyType_IsReady(tp)) {
1516         if (PyType_Ready(tp) < 0) {
1517             return 0;
1518         }
1519     }
1520 
1521     if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_CheckExact(name)) {
1522         *method = PyObject_GetAttr(obj, name);
1523         return 0;
1524     }
1525 
1526     PyObject *descr = _PyType_LookupRef(tp, name);
1527     descrgetfunc f = NULL;
1528     if (descr != NULL) {
1529         if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
1530             meth_found = 1;
1531         }
1532         else {
1533             f = Py_TYPE(descr)->tp_descr_get;
1534             if (f != NULL && PyDescr_IsData(descr)) {
1535                 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1536                 Py_DECREF(descr);
1537                 return 0;
1538             }
1539         }
1540     }
1541     PyObject *dict, *attr;
1542     if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES) &&
1543          _PyObject_TryGetInstanceAttribute(obj, name, &attr)) {
1544         if (attr != NULL) {
1545             *method = attr;
1546             Py_XDECREF(descr);
1547             return 0;
1548         }
1549         dict = NULL;
1550     }
1551     else if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) {
1552         dict = (PyObject *)_PyObject_GetManagedDict(obj);
1553     }
1554     else {
1555         PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
1556         if (dictptr != NULL) {
1557             dict = *dictptr;
1558         }
1559         else {
1560             dict = NULL;
1561         }
1562     }
1563     if (dict != NULL) {
1564         Py_INCREF(dict);
1565         if (PyDict_GetItemRef(dict, name, method) != 0) {
1566             // found or error
1567             Py_DECREF(dict);
1568             Py_XDECREF(descr);
1569             return 0;
1570         }
1571         // not found
1572         Py_DECREF(dict);
1573     }
1574 
1575     if (meth_found) {
1576         *method = descr;
1577         return 1;
1578     }
1579 
1580     if (f != NULL) {
1581         *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1582         Py_DECREF(descr);
1583         return 0;
1584     }
1585 
1586     if (descr != NULL) {
1587         *method = descr;
1588         return 0;
1589     }
1590 
1591     PyErr_Format(PyExc_AttributeError,
1592                  "'%.100s' object has no attribute '%U'",
1593                  tp->tp_name, name);
1594 
1595     _PyObject_SetAttributeErrorContext(obj, name);
1596     return 0;
1597 }
1598 
1599 /* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
1600 
1601 PyObject *
_PyObject_GenericGetAttrWithDict(PyObject * obj,PyObject * name,PyObject * dict,int suppress)1602 _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1603                                  PyObject *dict, int suppress)
1604 {
1605     /* Make sure the logic of _PyObject_GetMethod is in sync with
1606        this method.
1607 
1608        When suppress=1, this function suppresses AttributeError.
1609     */
1610 
1611     PyTypeObject *tp = Py_TYPE(obj);
1612     PyObject *descr = NULL;
1613     PyObject *res = NULL;
1614     descrgetfunc f;
1615 
1616     if (!PyUnicode_Check(name)){
1617         PyErr_Format(PyExc_TypeError,
1618                      "attribute name must be string, not '%.200s'",
1619                      Py_TYPE(name)->tp_name);
1620         return NULL;
1621     }
1622     Py_INCREF(name);
1623 
1624     if (!_PyType_IsReady(tp)) {
1625         if (PyType_Ready(tp) < 0)
1626             goto done;
1627     }
1628 
1629     descr = _PyType_LookupRef(tp, name);
1630 
1631     f = NULL;
1632     if (descr != NULL) {
1633         f = Py_TYPE(descr)->tp_descr_get;
1634         if (f != NULL && PyDescr_IsData(descr)) {
1635             res = f(descr, obj, (PyObject *)Py_TYPE(obj));
1636             if (res == NULL && suppress &&
1637                     PyErr_ExceptionMatches(PyExc_AttributeError)) {
1638                 PyErr_Clear();
1639             }
1640             goto done;
1641         }
1642     }
1643     if (dict == NULL) {
1644         if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES)) {
1645             if (PyUnicode_CheckExact(name) &&
1646                 _PyObject_TryGetInstanceAttribute(obj, name, &res)) {
1647                 if (res != NULL) {
1648                     goto done;
1649                 }
1650             }
1651             else {
1652                 dict = (PyObject *)_PyObject_MaterializeManagedDict(obj);
1653                 if (dict == NULL) {
1654                     res = NULL;
1655                     goto done;
1656                 }
1657             }
1658         }
1659         else if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) {
1660             dict = (PyObject *)_PyObject_GetManagedDict(obj);
1661         }
1662         else {
1663             PyObject **dictptr = _PyObject_ComputedDictPointer(obj);
1664             if (dictptr) {
1665                 dict = *dictptr;
1666             }
1667         }
1668     }
1669     if (dict != NULL) {
1670         Py_INCREF(dict);
1671         int rc = PyDict_GetItemRef(dict, name, &res);
1672         Py_DECREF(dict);
1673         if (res != NULL) {
1674             goto done;
1675         }
1676         else if (rc < 0) {
1677             if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1678                 PyErr_Clear();
1679             }
1680             else {
1681                 goto done;
1682             }
1683         }
1684     }
1685 
1686     if (f != NULL) {
1687         res = f(descr, obj, (PyObject *)Py_TYPE(obj));
1688         if (res == NULL && suppress &&
1689                 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1690             PyErr_Clear();
1691         }
1692         goto done;
1693     }
1694 
1695     if (descr != NULL) {
1696         res = descr;
1697         descr = NULL;
1698         goto done;
1699     }
1700 
1701     if (!suppress) {
1702         PyErr_Format(PyExc_AttributeError,
1703                      "'%.100s' object has no attribute '%U'",
1704                      tp->tp_name, name);
1705 
1706         _PyObject_SetAttributeErrorContext(obj, name);
1707     }
1708   done:
1709     Py_XDECREF(descr);
1710     Py_DECREF(name);
1711     return res;
1712 }
1713 
1714 PyObject *
PyObject_GenericGetAttr(PyObject * obj,PyObject * name)1715 PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1716 {
1717     return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
1718 }
1719 
1720 int
_PyObject_GenericSetAttrWithDict(PyObject * obj,PyObject * name,PyObject * value,PyObject * dict)1721 _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1722                                  PyObject *value, PyObject *dict)
1723 {
1724     PyTypeObject *tp = Py_TYPE(obj);
1725     PyObject *descr;
1726     descrsetfunc f;
1727     int res = -1;
1728 
1729     assert(!PyType_IsSubtype(tp, &PyType_Type));
1730     if (!PyUnicode_Check(name)){
1731         PyErr_Format(PyExc_TypeError,
1732                      "attribute name must be string, not '%.200s'",
1733                      Py_TYPE(name)->tp_name);
1734         return -1;
1735     }
1736 
1737     if (!_PyType_IsReady(tp) && PyType_Ready(tp) < 0) {
1738         return -1;
1739     }
1740 
1741     Py_INCREF(name);
1742     Py_INCREF(tp);
1743     descr = _PyType_LookupRef(tp, name);
1744 
1745     if (descr != NULL) {
1746         f = Py_TYPE(descr)->tp_descr_set;
1747         if (f != NULL) {
1748             res = f(descr, obj, value);
1749             goto done;
1750         }
1751     }
1752 
1753     if (dict == NULL) {
1754         PyObject **dictptr;
1755 
1756         if ((tp->tp_flags & Py_TPFLAGS_INLINE_VALUES)) {
1757             res = _PyObject_StoreInstanceAttribute(obj, name, value);
1758             goto error_check;
1759         }
1760 
1761         if ((tp->tp_flags & Py_TPFLAGS_MANAGED_DICT)) {
1762             PyManagedDictPointer *managed_dict = _PyObject_ManagedDictPointer(obj);
1763             dictptr = (PyObject **)&managed_dict->dict;
1764         }
1765         else {
1766             dictptr = _PyObject_ComputedDictPointer(obj);
1767         }
1768         if (dictptr == NULL) {
1769             if (descr == NULL) {
1770                 if (tp->tp_setattro == PyObject_GenericSetAttr) {
1771                     PyErr_Format(PyExc_AttributeError,
1772                                 "'%.100s' object has no attribute '%U' and no "
1773                                 "__dict__ for setting new attributes",
1774                                 tp->tp_name, name);
1775                 }
1776                 else {
1777                     PyErr_Format(PyExc_AttributeError,
1778                                 "'%.100s' object has no attribute '%U'",
1779                                 tp->tp_name, name);
1780                 }
1781                 _PyObject_SetAttributeErrorContext(obj, name);
1782             }
1783             else {
1784                 PyErr_Format(PyExc_AttributeError,
1785                             "'%.100s' object attribute '%U' is read-only",
1786                             tp->tp_name, name);
1787             }
1788             goto done;
1789         }
1790         else {
1791             res = _PyObjectDict_SetItem(tp, obj, dictptr, name, value);
1792         }
1793     }
1794     else {
1795         Py_INCREF(dict);
1796         if (value == NULL)
1797             res = PyDict_DelItem(dict, name);
1798         else
1799             res = PyDict_SetItem(dict, name, value);
1800         Py_DECREF(dict);
1801     }
1802   error_check:
1803     if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) {
1804         PyErr_Format(PyExc_AttributeError,
1805                         "'%.100s' object has no attribute '%U'",
1806                         tp->tp_name, name);
1807         _PyObject_SetAttributeErrorContext(obj, name);
1808     }
1809   done:
1810     Py_XDECREF(descr);
1811     Py_DECREF(tp);
1812     Py_DECREF(name);
1813     return res;
1814 }
1815 
1816 int
PyObject_GenericSetAttr(PyObject * obj,PyObject * name,PyObject * value)1817 PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1818 {
1819     return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1820 }
1821 
1822 int
PyObject_GenericSetDict(PyObject * obj,PyObject * value,void * context)1823 PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1824 {
1825     PyObject **dictptr = _PyObject_GetDictPtr(obj);
1826     if (dictptr == NULL) {
1827         if (_PyType_HasFeature(Py_TYPE(obj), Py_TPFLAGS_INLINE_VALUES) &&
1828             _PyObject_GetManagedDict(obj) == NULL
1829         ) {
1830             /* Was unable to convert to dict */
1831             PyErr_NoMemory();
1832         }
1833         else {
1834             PyErr_SetString(PyExc_AttributeError,
1835                             "This object has no __dict__");
1836         }
1837         return -1;
1838     }
1839     if (value == NULL) {
1840         PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1841         return -1;
1842     }
1843     if (!PyDict_Check(value)) {
1844         PyErr_Format(PyExc_TypeError,
1845                      "__dict__ must be set to a dictionary, "
1846                      "not a '%.200s'", Py_TYPE(value)->tp_name);
1847         return -1;
1848     }
1849     Py_BEGIN_CRITICAL_SECTION(obj);
1850     Py_XSETREF(*dictptr, Py_NewRef(value));
1851     Py_END_CRITICAL_SECTION();
1852     return 0;
1853 }
1854 
1855 
1856 /* Test a value used as condition, e.g., in a while or if statement.
1857    Return -1 if an error occurred */
1858 
1859 int
PyObject_IsTrue(PyObject * v)1860 PyObject_IsTrue(PyObject *v)
1861 {
1862     Py_ssize_t res;
1863     if (v == Py_True)
1864         return 1;
1865     if (v == Py_False)
1866         return 0;
1867     if (v == Py_None)
1868         return 0;
1869     else if (Py_TYPE(v)->tp_as_number != NULL &&
1870              Py_TYPE(v)->tp_as_number->nb_bool != NULL)
1871         res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
1872     else if (Py_TYPE(v)->tp_as_mapping != NULL &&
1873              Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
1874         res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
1875     else if (Py_TYPE(v)->tp_as_sequence != NULL &&
1876              Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
1877         res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
1878     else
1879         return 1;
1880     /* if it is negative, it should be either -1 or -2 */
1881     return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
1882 }
1883 
1884 /* equivalent of 'not v'
1885    Return -1 if an error occurred */
1886 
1887 int
PyObject_Not(PyObject * v)1888 PyObject_Not(PyObject *v)
1889 {
1890     int res;
1891     res = PyObject_IsTrue(v);
1892     if (res < 0)
1893         return res;
1894     return res == 0;
1895 }
1896 
1897 /* Test whether an object can be called */
1898 
1899 int
PyCallable_Check(PyObject * x)1900 PyCallable_Check(PyObject *x)
1901 {
1902     if (x == NULL)
1903         return 0;
1904     return Py_TYPE(x)->tp_call != NULL;
1905 }
1906 
1907 
1908 /* Helper for PyObject_Dir without arguments: returns the local scope. */
1909 static PyObject *
_dir_locals(void)1910 _dir_locals(void)
1911 {
1912     PyObject *names;
1913     PyObject *locals;
1914 
1915     locals = _PyEval_GetFrameLocals();
1916     if (locals == NULL)
1917         return NULL;
1918 
1919     names = PyMapping_Keys(locals);
1920     Py_DECREF(locals);
1921     if (!names) {
1922         return NULL;
1923     }
1924     if (!PyList_Check(names)) {
1925         PyErr_Format(PyExc_TypeError,
1926             "dir(): expected keys() of locals to be a list, "
1927             "not '%.200s'", Py_TYPE(names)->tp_name);
1928         Py_DECREF(names);
1929         return NULL;
1930     }
1931     if (PyList_Sort(names)) {
1932         Py_DECREF(names);
1933         return NULL;
1934     }
1935     return names;
1936 }
1937 
1938 /* Helper for PyObject_Dir: object introspection. */
1939 static PyObject *
_dir_object(PyObject * obj)1940 _dir_object(PyObject *obj)
1941 {
1942     PyObject *result, *sorted;
1943     PyObject *dirfunc = _PyObject_LookupSpecial(obj, &_Py_ID(__dir__));
1944 
1945     assert(obj != NULL);
1946     if (dirfunc == NULL) {
1947         if (!PyErr_Occurred())
1948             PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1949         return NULL;
1950     }
1951     /* use __dir__ */
1952     result = _PyObject_CallNoArgs(dirfunc);
1953     Py_DECREF(dirfunc);
1954     if (result == NULL)
1955         return NULL;
1956     /* return sorted(result) */
1957     sorted = PySequence_List(result);
1958     Py_DECREF(result);
1959     if (sorted == NULL)
1960         return NULL;
1961     if (PyList_Sort(sorted)) {
1962         Py_DECREF(sorted);
1963         return NULL;
1964     }
1965     return sorted;
1966 }
1967 
1968 /* Implementation of dir() -- if obj is NULL, returns the names in the current
1969    (local) scope.  Otherwise, performs introspection of the object: returns a
1970    sorted list of attribute names (supposedly) accessible from the object
1971 */
1972 PyObject *
PyObject_Dir(PyObject * obj)1973 PyObject_Dir(PyObject *obj)
1974 {
1975     return (obj == NULL) ? _dir_locals() : _dir_object(obj);
1976 }
1977 
1978 /*
1979 None is a non-NULL undefined value.
1980 There is (and should be!) no way to create other objects of this type,
1981 so there is exactly one (which is indestructible, by the way).
1982 */
1983 
1984 /* ARGSUSED */
1985 static PyObject *
none_repr(PyObject * op)1986 none_repr(PyObject *op)
1987 {
1988     return PyUnicode_FromString("None");
1989 }
1990 
1991 static void
none_dealloc(PyObject * none)1992 none_dealloc(PyObject* none)
1993 {
1994     /* This should never get called, but we also don't want to SEGV if
1995      * we accidentally decref None out of existence. Instead,
1996      * since None is an immortal object, re-set the reference count.
1997      */
1998     _Py_SetImmortal(none);
1999 }
2000 
2001 static PyObject *
none_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)2002 none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2003 {
2004     if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
2005         PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
2006         return NULL;
2007     }
2008     Py_RETURN_NONE;
2009 }
2010 
2011 static int
none_bool(PyObject * v)2012 none_bool(PyObject *v)
2013 {
2014     return 0;
2015 }
2016 
none_hash(PyObject * v)2017 static Py_hash_t none_hash(PyObject *v)
2018 {
2019     return 0xFCA86420;
2020 }
2021 
2022 static PyNumberMethods none_as_number = {
2023     0,                          /* nb_add */
2024     0,                          /* nb_subtract */
2025     0,                          /* nb_multiply */
2026     0,                          /* nb_remainder */
2027     0,                          /* nb_divmod */
2028     0,                          /* nb_power */
2029     0,                          /* nb_negative */
2030     0,                          /* nb_positive */
2031     0,                          /* nb_absolute */
2032     (inquiry)none_bool,         /* nb_bool */
2033     0,                          /* nb_invert */
2034     0,                          /* nb_lshift */
2035     0,                          /* nb_rshift */
2036     0,                          /* nb_and */
2037     0,                          /* nb_xor */
2038     0,                          /* nb_or */
2039     0,                          /* nb_int */
2040     0,                          /* nb_reserved */
2041     0,                          /* nb_float */
2042     0,                          /* nb_inplace_add */
2043     0,                          /* nb_inplace_subtract */
2044     0,                          /* nb_inplace_multiply */
2045     0,                          /* nb_inplace_remainder */
2046     0,                          /* nb_inplace_power */
2047     0,                          /* nb_inplace_lshift */
2048     0,                          /* nb_inplace_rshift */
2049     0,                          /* nb_inplace_and */
2050     0,                          /* nb_inplace_xor */
2051     0,                          /* nb_inplace_or */
2052     0,                          /* nb_floor_divide */
2053     0,                          /* nb_true_divide */
2054     0,                          /* nb_inplace_floor_divide */
2055     0,                          /* nb_inplace_true_divide */
2056     0,                          /* nb_index */
2057 };
2058 
2059 PyDoc_STRVAR(none_doc,
2060 "NoneType()\n"
2061 "--\n\n"
2062 "The type of the None singleton.");
2063 
2064 PyTypeObject _PyNone_Type = {
2065     PyVarObject_HEAD_INIT(&PyType_Type, 0)
2066     "NoneType",
2067     0,
2068     0,
2069     none_dealloc,       /*tp_dealloc*/
2070     0,                  /*tp_vectorcall_offset*/
2071     0,                  /*tp_getattr*/
2072     0,                  /*tp_setattr*/
2073     0,                  /*tp_as_async*/
2074     none_repr,          /*tp_repr*/
2075     &none_as_number,    /*tp_as_number*/
2076     0,                  /*tp_as_sequence*/
2077     0,                  /*tp_as_mapping*/
2078     (hashfunc)none_hash,/*tp_hash */
2079     0,                  /*tp_call */
2080     0,                  /*tp_str */
2081     0,                  /*tp_getattro */
2082     0,                  /*tp_setattro */
2083     0,                  /*tp_as_buffer */
2084     Py_TPFLAGS_DEFAULT, /*tp_flags */
2085     none_doc,           /*tp_doc */
2086     0,                  /*tp_traverse */
2087     0,                  /*tp_clear */
2088     _Py_BaseObject_RichCompare, /*tp_richcompare */
2089     0,                  /*tp_weaklistoffset */
2090     0,                  /*tp_iter */
2091     0,                  /*tp_iternext */
2092     0,                  /*tp_methods */
2093     0,                  /*tp_members */
2094     0,                  /*tp_getset */
2095     0,                  /*tp_base */
2096     0,                  /*tp_dict */
2097     0,                  /*tp_descr_get */
2098     0,                  /*tp_descr_set */
2099     0,                  /*tp_dictoffset */
2100     0,                  /*tp_init */
2101     0,                  /*tp_alloc */
2102     none_new,           /*tp_new */
2103 };
2104 
2105 PyObject _Py_NoneStruct = _PyObject_HEAD_INIT(&_PyNone_Type);
2106 
2107 /* NotImplemented is an object that can be used to signal that an
2108    operation is not implemented for the given type combination. */
2109 
2110 static PyObject *
NotImplemented_repr(PyObject * op)2111 NotImplemented_repr(PyObject *op)
2112 {
2113     return PyUnicode_FromString("NotImplemented");
2114 }
2115 
2116 static PyObject *
NotImplemented_reduce(PyObject * op,PyObject * Py_UNUSED (ignored))2117 NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
2118 {
2119     return PyUnicode_FromString("NotImplemented");
2120 }
2121 
2122 static PyMethodDef notimplemented_methods[] = {
2123     {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
2124     {NULL, NULL}
2125 };
2126 
2127 static PyObject *
notimplemented_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)2128 notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
2129 {
2130     if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
2131         PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
2132         return NULL;
2133     }
2134     Py_RETURN_NOTIMPLEMENTED;
2135 }
2136 
2137 static void
notimplemented_dealloc(PyObject * notimplemented)2138 notimplemented_dealloc(PyObject *notimplemented)
2139 {
2140     /* This should never get called, but we also don't want to SEGV if
2141      * we accidentally decref NotImplemented out of existence. Instead,
2142      * since Notimplemented is an immortal object, re-set the reference count.
2143      */
2144     _Py_SetImmortal(notimplemented);
2145 }
2146 
2147 static int
notimplemented_bool(PyObject * v)2148 notimplemented_bool(PyObject *v)
2149 {
2150     if (PyErr_WarnEx(PyExc_DeprecationWarning,
2151                      "NotImplemented should not be used in a boolean context",
2152                      1) < 0)
2153     {
2154         return -1;
2155     }
2156     return 1;
2157 }
2158 
2159 static PyNumberMethods notimplemented_as_number = {
2160     .nb_bool = notimplemented_bool,
2161 };
2162 
2163 PyDoc_STRVAR(notimplemented_doc,
2164 "NotImplementedType()\n"
2165 "--\n\n"
2166 "The type of the NotImplemented singleton.");
2167 
2168 PyTypeObject _PyNotImplemented_Type = {
2169     PyVarObject_HEAD_INIT(&PyType_Type, 0)
2170     "NotImplementedType",
2171     0,
2172     0,
2173     notimplemented_dealloc,       /*tp_dealloc*/ /*never called*/
2174     0,                  /*tp_vectorcall_offset*/
2175     0,                  /*tp_getattr*/
2176     0,                  /*tp_setattr*/
2177     0,                  /*tp_as_async*/
2178     NotImplemented_repr,        /*tp_repr*/
2179     &notimplemented_as_number,  /*tp_as_number*/
2180     0,                  /*tp_as_sequence*/
2181     0,                  /*tp_as_mapping*/
2182     0,                  /*tp_hash */
2183     0,                  /*tp_call */
2184     0,                  /*tp_str */
2185     0,                  /*tp_getattro */
2186     0,                  /*tp_setattro */
2187     0,                  /*tp_as_buffer */
2188     Py_TPFLAGS_DEFAULT, /*tp_flags */
2189     notimplemented_doc, /*tp_doc */
2190     0,                  /*tp_traverse */
2191     0,                  /*tp_clear */
2192     0,                  /*tp_richcompare */
2193     0,                  /*tp_weaklistoffset */
2194     0,                  /*tp_iter */
2195     0,                  /*tp_iternext */
2196     notimplemented_methods, /*tp_methods */
2197     0,                  /*tp_members */
2198     0,                  /*tp_getset */
2199     0,                  /*tp_base */
2200     0,                  /*tp_dict */
2201     0,                  /*tp_descr_get */
2202     0,                  /*tp_descr_set */
2203     0,                  /*tp_dictoffset */
2204     0,                  /*tp_init */
2205     0,                  /*tp_alloc */
2206     notimplemented_new, /*tp_new */
2207 };
2208 
2209 PyObject _Py_NotImplementedStruct = _PyObject_HEAD_INIT(&_PyNotImplemented_Type);
2210 
2211 
2212 PyStatus
_PyObject_InitState(PyInterpreterState * interp)2213 _PyObject_InitState(PyInterpreterState *interp)
2214 {
2215 #ifdef Py_TRACE_REFS
2216     if (refchain_init(interp) < 0) {
2217         return _PyStatus_NO_MEMORY();
2218     }
2219 #endif
2220     return _PyStatus_OK();
2221 }
2222 
2223 void
_PyObject_FiniState(PyInterpreterState * interp)2224 _PyObject_FiniState(PyInterpreterState *interp)
2225 {
2226 #ifdef Py_TRACE_REFS
2227     refchain_fini(interp);
2228 #endif
2229 }
2230 
2231 
2232 extern PyTypeObject _PyAnextAwaitable_Type;
2233 extern PyTypeObject _PyLegacyEventHandler_Type;
2234 extern PyTypeObject _PyLineIterator;
2235 extern PyTypeObject _PyMemoryIter_Type;
2236 extern PyTypeObject _PyPositionsIterator;
2237 extern PyTypeObject _Py_GenericAliasIterType;
2238 
2239 static PyTypeObject* static_types[] = {
2240     // The two most important base types: must be initialized first and
2241     // deallocated last.
2242     &PyBaseObject_Type,
2243     &PyType_Type,
2244 
2245     // Static types with base=&PyBaseObject_Type
2246     &PyAsyncGen_Type,
2247     &PyByteArrayIter_Type,
2248     &PyByteArray_Type,
2249     &PyBytesIter_Type,
2250     &PyBytes_Type,
2251     &PyCFunction_Type,
2252     &PyCallIter_Type,
2253     &PyCapsule_Type,
2254     &PyCell_Type,
2255     &PyClassMethodDescr_Type,
2256     &PyClassMethod_Type,
2257     &PyCode_Type,
2258     &PyComplex_Type,
2259     &PyContextToken_Type,
2260     &PyContextVar_Type,
2261     &PyContext_Type,
2262     &PyCoro_Type,
2263     &PyDictItems_Type,
2264     &PyDictIterItem_Type,
2265     &PyDictIterKey_Type,
2266     &PyDictIterValue_Type,
2267     &PyDictKeys_Type,
2268     &PyDictProxy_Type,
2269     &PyDictRevIterItem_Type,
2270     &PyDictRevIterKey_Type,
2271     &PyDictRevIterValue_Type,
2272     &PyDictValues_Type,
2273     &PyDict_Type,
2274     &PyEllipsis_Type,
2275     &PyEnum_Type,
2276     &PyFilter_Type,
2277     &PyFloat_Type,
2278     &PyFrame_Type,
2279     &PyFrameLocalsProxy_Type,
2280     &PyFrozenSet_Type,
2281     &PyFunction_Type,
2282     &PyGen_Type,
2283     &PyGetSetDescr_Type,
2284     &PyInstanceMethod_Type,
2285     &PyListIter_Type,
2286     &PyListRevIter_Type,
2287     &PyList_Type,
2288     &PyLongRangeIter_Type,
2289     &PyLong_Type,
2290     &PyMap_Type,
2291     &PyMemberDescr_Type,
2292     &PyMemoryView_Type,
2293     &PyMethodDescr_Type,
2294     &PyMethod_Type,
2295     &PyModuleDef_Type,
2296     &PyModule_Type,
2297     &PyODictIter_Type,
2298     &PyPickleBuffer_Type,
2299     &PyProperty_Type,
2300     &PyRangeIter_Type,
2301     &PyRange_Type,
2302     &PyReversed_Type,
2303     &PySTEntry_Type,
2304     &PySeqIter_Type,
2305     &PySetIter_Type,
2306     &PySet_Type,
2307     &PySlice_Type,
2308     &PyStaticMethod_Type,
2309     &PyStdPrinter_Type,
2310     &PySuper_Type,
2311     &PyTraceBack_Type,
2312     &PyTupleIter_Type,
2313     &PyTuple_Type,
2314     &PyUnicodeIter_Type,
2315     &PyUnicode_Type,
2316     &PyWrapperDescr_Type,
2317     &PyZip_Type,
2318     &Py_GenericAliasType,
2319     &_PyAnextAwaitable_Type,
2320     &_PyAsyncGenASend_Type,
2321     &_PyAsyncGenAThrow_Type,
2322     &_PyAsyncGenWrappedValue_Type,
2323     &_PyBufferWrapper_Type,
2324     &_PyContextTokenMissing_Type,
2325     &_PyCoroWrapper_Type,
2326 #ifdef _Py_TIER2
2327     &_PyCounterExecutor_Type,
2328     &_PyCounterOptimizer_Type,
2329     &_PyDefaultOptimizer_Type,
2330 #endif
2331     &_Py_GenericAliasIterType,
2332     &_PyHamtItems_Type,
2333     &_PyHamtKeys_Type,
2334     &_PyHamtValues_Type,
2335     &_PyHamt_ArrayNode_Type,
2336     &_PyHamt_BitmapNode_Type,
2337     &_PyHamt_CollisionNode_Type,
2338     &_PyHamt_Type,
2339     &_PyInstructionSequence_Type,
2340     &_PyLegacyEventHandler_Type,
2341     &_PyLineIterator,
2342     &_PyManagedBuffer_Type,
2343     &_PyMemoryIter_Type,
2344     &_PyMethodWrapper_Type,
2345     &_PyNamespace_Type,
2346     &_PyNone_Type,
2347     &_PyNotImplemented_Type,
2348     &_PyPositionsIterator,
2349     &_PyUnicodeASCIIIter_Type,
2350     &_PyUnion_Type,
2351 #ifdef _Py_TIER2
2352     &_PyUOpExecutor_Type,
2353     &_PyUOpOptimizer_Type,
2354 #endif
2355     &_PyWeakref_CallableProxyType,
2356     &_PyWeakref_ProxyType,
2357     &_PyWeakref_RefType,
2358     &_PyTypeAlias_Type,
2359     &_PyNoDefault_Type,
2360 
2361     // subclasses: _PyTypes_FiniTypes() deallocates them before their base
2362     // class
2363     &PyBool_Type,         // base=&PyLong_Type
2364     &PyCMethod_Type,      // base=&PyCFunction_Type
2365     &PyODictItems_Type,   // base=&PyDictItems_Type
2366     &PyODictKeys_Type,    // base=&PyDictKeys_Type
2367     &PyODictValues_Type,  // base=&PyDictValues_Type
2368     &PyODict_Type,        // base=&PyDict_Type
2369 };
2370 
2371 
2372 PyStatus
_PyTypes_InitTypes(PyInterpreterState * interp)2373 _PyTypes_InitTypes(PyInterpreterState *interp)
2374 {
2375     // All other static types (unless initialized elsewhere)
2376     for (size_t i=0; i < Py_ARRAY_LENGTH(static_types); i++) {
2377         PyTypeObject *type = static_types[i];
2378         if (_PyStaticType_InitBuiltin(interp, type) < 0) {
2379             return _PyStatus_ERR("Can't initialize builtin type");
2380         }
2381         if (type == &PyType_Type) {
2382             // Sanitify checks of the two most important types
2383             assert(PyBaseObject_Type.tp_base == NULL);
2384             assert(PyType_Type.tp_base == &PyBaseObject_Type);
2385         }
2386     }
2387 
2388     // Cache __reduce__ from PyBaseObject_Type object
2389     PyObject *baseobj_dict = _PyType_GetDict(&PyBaseObject_Type);
2390     PyObject *baseobj_reduce = PyDict_GetItemWithError(baseobj_dict, &_Py_ID(__reduce__));
2391     if (baseobj_reduce == NULL && PyErr_Occurred()) {
2392         return _PyStatus_ERR("Can't get __reduce__ from base object");
2393     }
2394     _Py_INTERP_CACHED_OBJECT(interp, objreduce) = baseobj_reduce;
2395 
2396     // Must be after static types are initialized
2397     if (_Py_initialize_generic(interp) < 0) {
2398         return _PyStatus_ERR("Can't initialize generic types");
2399     }
2400 
2401     return _PyStatus_OK();
2402 }
2403 
2404 
2405 // Best-effort function clearing static types.
2406 //
2407 // Don't deallocate a type if it still has subclasses. If a Py_Finalize()
2408 // sub-function is interrupted by CTRL+C or fails with MemoryError, some
2409 // subclasses are not cleared properly. Leave the static type unchanged in this
2410 // case.
2411 void
_PyTypes_FiniTypes(PyInterpreterState * interp)2412 _PyTypes_FiniTypes(PyInterpreterState *interp)
2413 {
2414     // Deallocate types in the reverse order to deallocate subclasses before
2415     // their base classes.
2416     for (Py_ssize_t i=Py_ARRAY_LENGTH(static_types)-1; i>=0; i--) {
2417         PyTypeObject *type = static_types[i];
2418         _PyStaticType_FiniBuiltin(interp, type);
2419     }
2420 }
2421 
2422 
2423 static inline void
new_reference(PyObject * op)2424 new_reference(PyObject *op)
2425 {
2426     // Skip the immortal object check in Py_SET_REFCNT; always set refcnt to 1
2427 #if !defined(Py_GIL_DISABLED)
2428     op->ob_refcnt = 1;
2429 #else
2430     op->ob_tid = _Py_ThreadId();
2431     op->_padding = 0;
2432     op->ob_mutex = (PyMutex){ 0 };
2433     op->ob_gc_bits = 0;
2434     op->ob_ref_local = 1;
2435     op->ob_ref_shared = 0;
2436 #endif
2437 #ifdef Py_TRACE_REFS
2438     _Py_AddToAllObjects(op);
2439 #endif
2440     struct _reftracer_runtime_state *tracer = &_PyRuntime.ref_tracer;
2441     if (tracer->tracer_func != NULL) {
2442         void* data = tracer->tracer_data;
2443         tracer->tracer_func(op, PyRefTracer_CREATE, data);
2444     }
2445 }
2446 
2447 void
_Py_NewReference(PyObject * op)2448 _Py_NewReference(PyObject *op)
2449 {
2450 #ifdef Py_REF_DEBUG
2451     _Py_IncRefTotal(_PyThreadState_GET());
2452 #endif
2453     new_reference(op);
2454 }
2455 
2456 void
_Py_NewReferenceNoTotal(PyObject * op)2457 _Py_NewReferenceNoTotal(PyObject *op)
2458 {
2459     new_reference(op);
2460 }
2461 
2462 void
_Py_SetImmortalUntracked(PyObject * op)2463 _Py_SetImmortalUntracked(PyObject *op)
2464 {
2465 #ifdef Py_DEBUG
2466     // For strings, use _PyUnicode_InternImmortal instead.
2467     if (PyUnicode_CheckExact(op)) {
2468         assert(PyUnicode_CHECK_INTERNED(op) == SSTATE_INTERNED_IMMORTAL
2469             || PyUnicode_CHECK_INTERNED(op) == SSTATE_INTERNED_IMMORTAL_STATIC);
2470     }
2471 #endif
2472 #ifdef Py_GIL_DISABLED
2473     op->ob_tid = _Py_UNOWNED_TID;
2474     op->ob_ref_local = _Py_IMMORTAL_REFCNT_LOCAL;
2475     op->ob_ref_shared = 0;
2476 #else
2477     op->ob_refcnt = _Py_IMMORTAL_REFCNT;
2478 #endif
2479 }
2480 
2481 void
_Py_SetImmortal(PyObject * op)2482 _Py_SetImmortal(PyObject *op)
2483 {
2484     if (PyObject_IS_GC(op) && _PyObject_GC_IS_TRACKED(op)) {
2485         _PyObject_GC_UNTRACK(op);
2486     }
2487     _Py_SetImmortalUntracked(op);
2488 }
2489 
2490 void
_PyObject_SetDeferredRefcount(PyObject * op)2491 _PyObject_SetDeferredRefcount(PyObject *op)
2492 {
2493 #ifdef Py_GIL_DISABLED
2494     assert(PyType_IS_GC(Py_TYPE(op)));
2495     assert(_Py_IsOwnedByCurrentThread(op));
2496     assert(op->ob_ref_shared == 0);
2497     _PyObject_SET_GC_BITS(op, _PyGC_BITS_DEFERRED);
2498     PyInterpreterState *interp = _PyInterpreterState_GET();
2499     if (_Py_atomic_load_int_relaxed(&interp->gc.immortalize) == 1) {
2500         // gh-117696: immortalize objects instead of using deferred reference
2501         // counting for now.
2502         _Py_SetImmortal(op);
2503         return;
2504     }
2505     op->ob_ref_local += 1;
2506     op->ob_ref_shared = _Py_REF_QUEUED;
2507 #endif
2508 }
2509 
2510 void
_Py_ResurrectReference(PyObject * op)2511 _Py_ResurrectReference(PyObject *op)
2512 {
2513 #ifdef Py_TRACE_REFS
2514     _Py_AddToAllObjects(op);
2515 #endif
2516     if (_PyRuntime.ref_tracer.tracer_func != NULL) {
2517         void* data = _PyRuntime.ref_tracer.tracer_data;
2518         _PyRuntime.ref_tracer.tracer_func(op, PyRefTracer_CREATE, data);
2519     }
2520 }
2521 
2522 
2523 #ifdef Py_TRACE_REFS
2524 void
_Py_ForgetReference(PyObject * op)2525 _Py_ForgetReference(PyObject *op)
2526 {
2527     if (Py_REFCNT(op) < 0) {
2528         _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
2529     }
2530 
2531     PyInterpreterState *interp = _PyInterpreterState_GET();
2532 
2533 #ifdef SLOW_UNREF_CHECK
2534     if (!_PyRefchain_Get(interp, op)) {
2535         /* Not found */
2536         _PyObject_ASSERT_FAILED_MSG(op,
2537                                     "object not found in the objects list");
2538     }
2539 #endif
2540 
2541     _PyRefchain_Remove(interp, op);
2542 }
2543 
2544 static int
_Py_PrintReference(_Py_hashtable_t * ht,const void * key,const void * value,void * user_data)2545 _Py_PrintReference(_Py_hashtable_t *ht,
2546                    const void *key, const void *value,
2547                    void *user_data)
2548 {
2549     PyObject *op = (PyObject*)key;
2550     FILE *fp = (FILE *)user_data;
2551     fprintf(fp, "%p [%zd] ", (void *)op, Py_REFCNT(op));
2552     if (PyObject_Print(op, fp, 0) != 0) {
2553         PyErr_Clear();
2554     }
2555     putc('\n', fp);
2556     return 0;
2557 }
2558 
2559 
2560 /* Print all live objects.  Because PyObject_Print is called, the
2561  * interpreter must be in a healthy state.
2562  */
2563 void
_Py_PrintReferences(PyInterpreterState * interp,FILE * fp)2564 _Py_PrintReferences(PyInterpreterState *interp, FILE *fp)
2565 {
2566     if (interp == NULL) {
2567         interp = _PyInterpreterState_Main();
2568     }
2569     fprintf(fp, "Remaining objects:\n");
2570     _Py_hashtable_foreach(REFCHAIN(interp), _Py_PrintReference, fp);
2571 }
2572 
2573 
2574 static int
_Py_PrintReferenceAddress(_Py_hashtable_t * ht,const void * key,const void * value,void * user_data)2575 _Py_PrintReferenceAddress(_Py_hashtable_t *ht,
2576                           const void *key, const void *value,
2577                           void *user_data)
2578 {
2579     PyObject *op = (PyObject*)key;
2580     FILE *fp = (FILE *)user_data;
2581     fprintf(fp, "%p [%zd] %s\n",
2582             (void *)op, Py_REFCNT(op), Py_TYPE(op)->tp_name);
2583     return 0;
2584 }
2585 
2586 
2587 /* Print the addresses of all live objects.  Unlike _Py_PrintReferences, this
2588  * doesn't make any calls to the Python C API, so is always safe to call.
2589  */
2590 // XXX This function is not safe to use if the interpreter has been
2591 // freed or is in an unhealthy state (e.g. late in finalization).
2592 // The call in Py_FinalizeEx() is okay since the main interpreter
2593 // is statically allocated.
2594 void
_Py_PrintReferenceAddresses(PyInterpreterState * interp,FILE * fp)2595 _Py_PrintReferenceAddresses(PyInterpreterState *interp, FILE *fp)
2596 {
2597     fprintf(fp, "Remaining object addresses:\n");
2598     _Py_hashtable_foreach(REFCHAIN(interp), _Py_PrintReferenceAddress, fp);
2599 }
2600 
2601 
2602 typedef struct {
2603     PyObject *self;
2604     PyObject *args;
2605     PyObject *list;
2606     PyObject *type;
2607     Py_ssize_t limit;
2608 } _Py_GetObjectsData;
2609 
2610 enum {
2611     _PY_GETOBJECTS_IGNORE = 0,
2612     _PY_GETOBJECTS_ERROR = 1,
2613     _PY_GETOBJECTS_STOP = 2,
2614 };
2615 
2616 static int
_Py_GetObject(_Py_hashtable_t * ht,const void * key,const void * value,void * user_data)2617 _Py_GetObject(_Py_hashtable_t *ht,
2618               const void *key, const void *value,
2619               void *user_data)
2620 {
2621     PyObject *op = (PyObject *)key;
2622     _Py_GetObjectsData *data = user_data;
2623     if (data->limit > 0) {
2624         if (PyList_GET_SIZE(data->list) >= data->limit) {
2625             return _PY_GETOBJECTS_STOP;
2626         }
2627     }
2628 
2629     if (op == data->self) {
2630         return _PY_GETOBJECTS_IGNORE;
2631     }
2632     if (op == data->args) {
2633         return _PY_GETOBJECTS_IGNORE;
2634     }
2635     if (op == data->list) {
2636         return _PY_GETOBJECTS_IGNORE;
2637     }
2638     if (data->type != NULL) {
2639         if (op == data->type) {
2640             return _PY_GETOBJECTS_IGNORE;
2641         }
2642         if (!Py_IS_TYPE(op, (PyTypeObject *)data->type)) {
2643             return _PY_GETOBJECTS_IGNORE;
2644         }
2645     }
2646 
2647     if (PyList_Append(data->list, op) < 0) {
2648         return _PY_GETOBJECTS_ERROR;
2649     }
2650     return 0;
2651 }
2652 
2653 
2654 /* The implementation of sys.getobjects(). */
2655 PyObject *
_Py_GetObjects(PyObject * self,PyObject * args)2656 _Py_GetObjects(PyObject *self, PyObject *args)
2657 {
2658     Py_ssize_t limit;
2659     PyObject *type = NULL;
2660     if (!PyArg_ParseTuple(args, "n|O", &limit, &type)) {
2661         return NULL;
2662     }
2663 
2664     PyObject *list = PyList_New(0);
2665     if (list == NULL) {
2666         return NULL;
2667     }
2668 
2669     _Py_GetObjectsData data = {
2670         .self = self,
2671         .args = args,
2672         .list = list,
2673         .type = type,
2674         .limit = limit,
2675     };
2676     PyInterpreterState *interp = _PyInterpreterState_GET();
2677     int res = _Py_hashtable_foreach(REFCHAIN(interp), _Py_GetObject, &data);
2678     if (res == _PY_GETOBJECTS_ERROR) {
2679         Py_DECREF(list);
2680         return NULL;
2681     }
2682     return list;
2683 }
2684 
2685 #undef REFCHAIN
2686 #undef REFCHAIN_VALUE
2687 
2688 #endif  /* Py_TRACE_REFS */
2689 
2690 
2691 /* Hack to force loading of abstract.o */
2692 Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
2693 
2694 
2695 void
_PyObject_DebugTypeStats(FILE * out)2696 _PyObject_DebugTypeStats(FILE *out)
2697 {
2698     _PyDict_DebugMallocStats(out);
2699     _PyFloat_DebugMallocStats(out);
2700     _PyList_DebugMallocStats(out);
2701     _PyTuple_DebugMallocStats(out);
2702 }
2703 
2704 /* These methods are used to control infinite recursion in repr, str, print,
2705    etc.  Container objects that may recursively contain themselves,
2706    e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
2707    Py_ReprLeave() to avoid infinite recursion.
2708 
2709    Py_ReprEnter() returns 0 the first time it is called for a particular
2710    object and 1 every time thereafter.  It returns -1 if an exception
2711    occurred.  Py_ReprLeave() has no return value.
2712 
2713    See dictobject.c and listobject.c for examples of use.
2714 */
2715 
2716 int
Py_ReprEnter(PyObject * obj)2717 Py_ReprEnter(PyObject *obj)
2718 {
2719     PyObject *dict;
2720     PyObject *list;
2721     Py_ssize_t i;
2722 
2723     dict = PyThreadState_GetDict();
2724     /* Ignore a missing thread-state, so that this function can be called
2725        early on startup. */
2726     if (dict == NULL)
2727         return 0;
2728     list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr));
2729     if (list == NULL) {
2730         if (PyErr_Occurred()) {
2731             return -1;
2732         }
2733         list = PyList_New(0);
2734         if (list == NULL)
2735             return -1;
2736         if (PyDict_SetItem(dict, &_Py_ID(Py_Repr), list) < 0)
2737             return -1;
2738         Py_DECREF(list);
2739     }
2740     i = PyList_GET_SIZE(list);
2741     while (--i >= 0) {
2742         if (PyList_GET_ITEM(list, i) == obj)
2743             return 1;
2744     }
2745     if (PyList_Append(list, obj) < 0)
2746         return -1;
2747     return 0;
2748 }
2749 
2750 void
Py_ReprLeave(PyObject * obj)2751 Py_ReprLeave(PyObject *obj)
2752 {
2753     PyObject *dict;
2754     PyObject *list;
2755     Py_ssize_t i;
2756 
2757     PyObject *exc = PyErr_GetRaisedException();
2758 
2759     dict = PyThreadState_GetDict();
2760     if (dict == NULL)
2761         goto finally;
2762 
2763     list = PyDict_GetItemWithError(dict, &_Py_ID(Py_Repr));
2764     if (list == NULL || !PyList_Check(list))
2765         goto finally;
2766 
2767     i = PyList_GET_SIZE(list);
2768     /* Count backwards because we always expect obj to be list[-1] */
2769     while (--i >= 0) {
2770         if (PyList_GET_ITEM(list, i) == obj) {
2771             PyList_SetSlice(list, i, i + 1, NULL);
2772             break;
2773         }
2774     }
2775 
2776 finally:
2777     /* ignore exceptions because there is no way to report them. */
2778     PyErr_SetRaisedException(exc);
2779 }
2780 
2781 /* Trashcan support. */
2782 
2783 /* Add op to the gcstate->trash_delete_later list.  Called when the current
2784  * call-stack depth gets large.  op must be a currently untracked gc'ed
2785  * object, with refcount 0.  Py_DECREF must already have been called on it.
2786  */
2787 void
_PyTrash_thread_deposit_object(PyThreadState * tstate,PyObject * op)2788 _PyTrash_thread_deposit_object(PyThreadState *tstate, PyObject *op)
2789 {
2790     _PyObject_ASSERT(op, _PyObject_IS_GC(op));
2791     _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
2792     _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
2793 #ifdef Py_GIL_DISABLED
2794     op->ob_tid = (uintptr_t)tstate->delete_later;
2795 #else
2796     _PyGCHead_SET_PREV(_Py_AS_GC(op), (PyGC_Head*)tstate->delete_later);
2797 #endif
2798     tstate->delete_later = op;
2799 }
2800 
2801 /* Deallocate all the objects in the gcstate->trash_delete_later list.
2802  * Called when the call-stack unwinds again. */
2803 void
_PyTrash_thread_destroy_chain(PyThreadState * tstate)2804 _PyTrash_thread_destroy_chain(PyThreadState *tstate)
2805 {
2806     /* We need to increase c_recursion_remaining here, otherwise,
2807        _PyTrash_thread_destroy_chain will be called recursively
2808        and then possibly crash.  An example that may crash without
2809        increase:
2810            N = 500000  # need to be large enough
2811            ob = object()
2812            tups = [(ob,) for i in range(N)]
2813            for i in range(49):
2814                tups = [(tup,) for tup in tups]
2815            del tups
2816     */
2817     assert(tstate->c_recursion_remaining > Py_TRASHCAN_HEADROOM);
2818     tstate->c_recursion_remaining--;
2819     while (tstate->delete_later) {
2820         PyObject *op = tstate->delete_later;
2821         destructor dealloc = Py_TYPE(op)->tp_dealloc;
2822 
2823 #ifdef Py_GIL_DISABLED
2824         tstate->delete_later = (PyObject*) op->ob_tid;
2825         op->ob_tid = 0;
2826         _Py_atomic_store_ssize_relaxed(&op->ob_ref_shared, _Py_REF_MERGED);
2827 #else
2828         tstate->delete_later = (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
2829 #endif
2830 
2831         /* Call the deallocator directly.  This used to try to
2832          * fool Py_DECREF into calling it indirectly, but
2833          * Py_DECREF was already called on this object, and in
2834          * assorted non-release builds calling Py_DECREF again ends
2835          * up distorting allocation statistics.
2836          */
2837         _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
2838         (*dealloc)(op);
2839     }
2840     tstate->c_recursion_remaining++;
2841 }
2842 
2843 void _Py_NO_RETURN
_PyObject_AssertFailed(PyObject * obj,const char * expr,const char * msg,const char * file,int line,const char * function)2844 _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
2845                        const char *file, int line, const char *function)
2846 {
2847     fprintf(stderr, "%s:%d: ", file, line);
2848     if (function) {
2849         fprintf(stderr, "%s: ", function);
2850     }
2851     fflush(stderr);
2852 
2853     if (expr) {
2854         fprintf(stderr, "Assertion \"%s\" failed", expr);
2855     }
2856     else {
2857         fprintf(stderr, "Assertion failed");
2858     }
2859     fflush(stderr);
2860 
2861     if (msg) {
2862         fprintf(stderr, ": %s", msg);
2863     }
2864     fprintf(stderr, "\n");
2865     fflush(stderr);
2866 
2867     if (_PyObject_IsFreed(obj)) {
2868         /* It seems like the object memory has been freed:
2869            don't access it to prevent a segmentation fault. */
2870         fprintf(stderr, "<object at %p is freed>\n", obj);
2871         fflush(stderr);
2872     }
2873     else {
2874         /* Display the traceback where the object has been allocated.
2875            Do it before dumping repr(obj), since repr() is more likely
2876            to crash than dumping the traceback. */
2877         PyTypeObject *type = Py_TYPE(obj);
2878         const size_t presize = _PyType_PreHeaderSize(type);
2879         void *ptr = (void *)((char *)obj - presize);
2880         _PyMem_DumpTraceback(fileno(stderr), ptr);
2881 
2882         /* This might succeed or fail, but we're about to abort, so at least
2883            try to provide any extra info we can: */
2884         _PyObject_Dump(obj);
2885 
2886         fprintf(stderr, "\n");
2887         fflush(stderr);
2888     }
2889 
2890     Py_FatalError("_PyObject_AssertFailed");
2891 }
2892 
2893 
2894 void
_Py_Dealloc(PyObject * op)2895 _Py_Dealloc(PyObject *op)
2896 {
2897     PyTypeObject *type = Py_TYPE(op);
2898     destructor dealloc = type->tp_dealloc;
2899 #ifdef Py_DEBUG
2900     PyThreadState *tstate = _PyThreadState_GET();
2901     PyObject *old_exc = tstate != NULL ? tstate->current_exception : NULL;
2902     // Keep the old exception type alive to prevent undefined behavior
2903     // on (tstate->curexc_type != old_exc_type) below
2904     Py_XINCREF(old_exc);
2905     // Make sure that type->tp_name remains valid
2906     Py_INCREF(type);
2907 #endif
2908 
2909     struct _reftracer_runtime_state *tracer = &_PyRuntime.ref_tracer;
2910     if (tracer->tracer_func != NULL) {
2911         void* data = tracer->tracer_data;
2912         tracer->tracer_func(op, PyRefTracer_DESTROY, data);
2913     }
2914 
2915 #ifdef Py_TRACE_REFS
2916     _Py_ForgetReference(op);
2917 #endif
2918     (*dealloc)(op);
2919 
2920 #ifdef Py_DEBUG
2921     // gh-89373: The tp_dealloc function must leave the current exception
2922     // unchanged.
2923     if (tstate != NULL && tstate->current_exception != old_exc) {
2924         const char *err;
2925         if (old_exc == NULL) {
2926             err = "Deallocator of type '%s' raised an exception";
2927         }
2928         else if (tstate->current_exception == NULL) {
2929             err = "Deallocator of type '%s' cleared the current exception";
2930         }
2931         else {
2932             // It can happen if dealloc() normalized the current exception.
2933             // A deallocator function must not change the current exception,
2934             // not even normalize it.
2935             err = "Deallocator of type '%s' overrode the current exception";
2936         }
2937         _Py_FatalErrorFormat(__func__, err, type->tp_name);
2938     }
2939     Py_XDECREF(old_exc);
2940     Py_DECREF(type);
2941 #endif
2942 }
2943 
2944 
2945 PyObject **
PyObject_GET_WEAKREFS_LISTPTR(PyObject * op)2946 PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
2947 {
2948     return _PyObject_GET_WEAKREFS_LISTPTR(op);
2949 }
2950 
2951 
2952 #undef Py_NewRef
2953 #undef Py_XNewRef
2954 
2955 // Export Py_NewRef() and Py_XNewRef() as regular functions for the stable ABI.
2956 PyObject*
Py_NewRef(PyObject * obj)2957 Py_NewRef(PyObject *obj)
2958 {
2959     return _Py_NewRef(obj);
2960 }
2961 
2962 PyObject*
Py_XNewRef(PyObject * obj)2963 Py_XNewRef(PyObject *obj)
2964 {
2965     return _Py_XNewRef(obj);
2966 }
2967 
2968 #undef Py_Is
2969 #undef Py_IsNone
2970 #undef Py_IsTrue
2971 #undef Py_IsFalse
2972 
2973 // Export Py_Is(), Py_IsNone(), Py_IsTrue(), Py_IsFalse() as regular functions
2974 // for the stable ABI.
Py_Is(PyObject * x,PyObject * y)2975 int Py_Is(PyObject *x, PyObject *y)
2976 {
2977     return (x == y);
2978 }
2979 
Py_IsNone(PyObject * x)2980 int Py_IsNone(PyObject *x)
2981 {
2982     return Py_Is(x, Py_None);
2983 }
2984 
Py_IsTrue(PyObject * x)2985 int Py_IsTrue(PyObject *x)
2986 {
2987     return Py_Is(x, Py_True);
2988 }
2989 
Py_IsFalse(PyObject * x)2990 int Py_IsFalse(PyObject *x)
2991 {
2992     return Py_Is(x, Py_False);
2993 }
2994 
2995 
2996 // Py_SET_REFCNT() implementation for stable ABI
2997 void
_Py_SetRefcnt(PyObject * ob,Py_ssize_t refcnt)2998 _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt)
2999 {
3000     Py_SET_REFCNT(ob, refcnt);
3001 }
3002 
PyRefTracer_SetTracer(PyRefTracer tracer,void * data)3003 int PyRefTracer_SetTracer(PyRefTracer tracer, void *data) {
3004     assert(PyGILState_Check());
3005     _PyRuntime.ref_tracer.tracer_func = tracer;
3006     _PyRuntime.ref_tracer.tracer_data = data;
3007     return 0;
3008 }
3009 
PyRefTracer_GetTracer(void ** data)3010 PyRefTracer PyRefTracer_GetTracer(void** data) {
3011     assert(PyGILState_Check());
3012     if (data != NULL) {
3013         *data = _PyRuntime.ref_tracer.tracer_data;
3014     }
3015     return _PyRuntime.ref_tracer.tracer_func;
3016 }
3017 
3018 
3019 
3020 static PyObject* constants[] = {
3021     &_Py_NoneStruct,                   // Py_CONSTANT_NONE
3022     (PyObject*)(&_Py_FalseStruct),     // Py_CONSTANT_FALSE
3023     (PyObject*)(&_Py_TrueStruct),      // Py_CONSTANT_TRUE
3024     &_Py_EllipsisObject,               // Py_CONSTANT_ELLIPSIS
3025     &_Py_NotImplementedStruct,         // Py_CONSTANT_NOT_IMPLEMENTED
3026     NULL,  // Py_CONSTANT_ZERO
3027     NULL,  // Py_CONSTANT_ONE
3028     NULL,  // Py_CONSTANT_EMPTY_STR
3029     NULL,  // Py_CONSTANT_EMPTY_BYTES
3030     NULL,  // Py_CONSTANT_EMPTY_TUPLE
3031 };
3032 
3033 void
_Py_GetConstant_Init(void)3034 _Py_GetConstant_Init(void)
3035 {
3036     constants[Py_CONSTANT_ZERO] = _PyLong_GetZero();
3037     constants[Py_CONSTANT_ONE] = _PyLong_GetOne();
3038     constants[Py_CONSTANT_EMPTY_STR] = PyUnicode_New(0, 0);
3039     constants[Py_CONSTANT_EMPTY_BYTES] = PyBytes_FromStringAndSize(NULL, 0);
3040     constants[Py_CONSTANT_EMPTY_TUPLE] = PyTuple_New(0);
3041 #ifndef NDEBUG
3042     for (size_t i=0; i < Py_ARRAY_LENGTH(constants); i++) {
3043         assert(constants[i] != NULL);
3044         assert(_Py_IsImmortal(constants[i]));
3045     }
3046 #endif
3047 }
3048 
3049 PyObject*
Py_GetConstant(unsigned int constant_id)3050 Py_GetConstant(unsigned int constant_id)
3051 {
3052     if (constant_id < Py_ARRAY_LENGTH(constants)) {
3053         return constants[constant_id];
3054     }
3055     else {
3056         PyErr_BadInternalCall();
3057         return NULL;
3058     }
3059 }
3060 
3061 
3062 PyObject*
Py_GetConstantBorrowed(unsigned int constant_id)3063 Py_GetConstantBorrowed(unsigned int constant_id)
3064 {
3065     // All constants are immortal
3066     return Py_GetConstant(constant_id);
3067 }
3068