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