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