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
24 extern "C" {
25 #endif
26
27 /* Defined in tracemalloc.c */
28 extern 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
36 int
_PyObject_CheckConsistency(PyObject * op,int check_content)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
60 Py_ssize_t _Py_RefTotal;
61
62 Py_ssize_t
_Py_GetRefTotal(void)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
73 void
_PyDebug_PrintTotalRefs(void)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 */
90 static 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 */
103 void
_Py_AddToAllObjects(PyObject * op,int force)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. */
125 void
_Py_NegativeRefcount(const char * filename,int lineno,PyObject * op)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
134 void
Py_IncRef(PyObject * o)135 Py_IncRef(PyObject *o)
136 {
137 Py_XINCREF(o);
138 }
139
140 void
Py_DecRef(PyObject * o)141 Py_DecRef(PyObject *o)
142 {
143 Py_XDECREF(o);
144 }
145
146 void
_Py_IncRef(PyObject * o)147 _Py_IncRef(PyObject *o)
148 {
149 Py_INCREF(o);
150 }
151
152 void
_Py_DecRef(PyObject * o)153 _Py_DecRef(PyObject *o)
154 {
155 Py_DECREF(o);
156 }
157
158 PyObject *
PyObject_Init(PyObject * op,PyTypeObject * tp)159 PyObject_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
169 PyVarObject *
PyObject_InitVar(PyVarObject * op,PyTypeObject * tp,Py_ssize_t size)170 PyObject_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
180 PyObject *
_PyObject_New(PyTypeObject * tp)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
191 PyVarObject *
_PyObject_NewVar(PyTypeObject * tp,Py_ssize_t nitems)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
204 void
PyObject_CallFinalizer(PyObject * self)205 PyObject_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
221 int
PyObject_CallFinalizerFromDealloc(PyObject * self)222 PyObject_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
263 int
PyObject_Print(PyObject * op,FILE * fp,int flags)264 PyObject_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 */
334 void
_Py_BreakPoint(void)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. */
346 int
_PyObject_IsFreed(PyObject * op)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 */
367 void
_PyObject_Dump(PyObject * op)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
408 PyObject *
PyObject_Repr(PyObject * v)409 PyObject_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
461 PyObject *
PyObject_Str(PyObject * v)462 PyObject_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
521 PyObject *
PyObject_ASCII(PyObject * v)522 PyObject_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
548 PyObject *
PyObject_Bytes(PyObject * v)549 PyObject_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 /*
583 def _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 */
596 PyObject *
_PyObject_FunctionStr(PyObject * x)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);
633 done:
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 */
670 int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
671
672 static const char * const opstrings[] = {"<", "<=", "==", "!=", ">", ">="};
673
674 /* Perform a rich comparison, raising TypeError when the requested comparison
675 operator is not supported. */
676 static PyObject *
do_richcompare(PyThreadState * tstate,PyObject * v,PyObject * w,int op)677 do_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
728 PyObject *
PyObject_RichCompare(PyObject * v,PyObject * w,int op)729 PyObject_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. */
750 int
PyObject_RichCompareBool(PyObject * v,PyObject * w,int op)751 PyObject_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
776 Py_hash_t
PyObject_HashNotImplemented(PyObject * v)777 PyObject_HashNotImplemented(PyObject *v)
778 {
779 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'",
780 Py_TYPE(v)->tp_name);
781 return -1;
782 }
783
784 Py_hash_t
PyObject_Hash(PyObject * v)785 PyObject_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
805 PyObject *
PyObject_GetAttrString(PyObject * v,const char * name)806 PyObject_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
820 int
PyObject_HasAttrString(PyObject * v,const char * name)821 PyObject_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
832 int
PyObject_SetAttrString(PyObject * v,const char * name,PyObject * w)833 PyObject_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
848 int
_PyObject_IsAbstract(PyObject * obj)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
865 PyObject *
_PyObject_GetAttrId(PyObject * v,_Py_Identifier * name)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
876 int
_PyObject_SetAttrId(PyObject * v,_Py_Identifier * name,PyObject * w)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
887 static inline int
set_attribute_error_context(PyObject * v,PyObject * name)888 set_attribute_error_context(PyObject* v, PyObject* name)
889 {
890 assert(PyErr_Occurred());
891 _Py_IDENTIFIER(name);
892 _Py_IDENTIFIER(obj);
893 // Intercept AttributeError exceptions and augment them to offer
894 // suggestions later.
895 if (PyErr_ExceptionMatches(PyExc_AttributeError)){
896 PyObject *type, *value, *traceback;
897 PyErr_Fetch(&type, &value, &traceback);
898 PyErr_NormalizeException(&type, &value, &traceback);
899 if (PyErr_GivenExceptionMatches(value, PyExc_AttributeError) &&
900 (_PyObject_SetAttrId(value, &PyId_name, name) ||
901 _PyObject_SetAttrId(value, &PyId_obj, v))) {
902 return 1;
903 }
904 PyErr_Restore(type, value, traceback);
905 }
906 return 0;
907 }
908
909 PyObject *
PyObject_GetAttr(PyObject * v,PyObject * name)910 PyObject_GetAttr(PyObject *v, PyObject *name)
911 {
912 PyTypeObject *tp = Py_TYPE(v);
913 if (!PyUnicode_Check(name)) {
914 PyErr_Format(PyExc_TypeError,
915 "attribute name must be string, not '%.200s'",
916 Py_TYPE(name)->tp_name);
917 return NULL;
918 }
919
920 PyObject* result = NULL;
921 if (tp->tp_getattro != NULL) {
922 result = (*tp->tp_getattro)(v, name);
923 }
924 else if (tp->tp_getattr != NULL) {
925 const char *name_str = PyUnicode_AsUTF8(name);
926 if (name_str == NULL) {
927 return NULL;
928 }
929 result = (*tp->tp_getattr)(v, (char *)name_str);
930 }
931 else {
932 PyErr_Format(PyExc_AttributeError,
933 "'%.50s' object has no attribute '%U'",
934 tp->tp_name, name);
935 }
936
937 if (result == NULL) {
938 set_attribute_error_context(v, name);
939 }
940 return result;
941 }
942
943 int
_PyObject_LookupAttr(PyObject * v,PyObject * name,PyObject ** result)944 _PyObject_LookupAttr(PyObject *v, PyObject *name, PyObject **result)
945 {
946 PyTypeObject *tp = Py_TYPE(v);
947
948 if (!PyUnicode_Check(name)) {
949 PyErr_Format(PyExc_TypeError,
950 "attribute name must be string, not '%.200s'",
951 Py_TYPE(name)->tp_name);
952 *result = NULL;
953 return -1;
954 }
955
956 if (tp->tp_getattro == PyObject_GenericGetAttr) {
957 *result = _PyObject_GenericGetAttrWithDict(v, name, NULL, 1);
958 if (*result != NULL) {
959 return 1;
960 }
961 if (PyErr_Occurred()) {
962 return -1;
963 }
964 return 0;
965 }
966 if (tp->tp_getattro != NULL) {
967 *result = (*tp->tp_getattro)(v, name);
968 }
969 else if (tp->tp_getattr != NULL) {
970 const char *name_str = PyUnicode_AsUTF8(name);
971 if (name_str == NULL) {
972 *result = NULL;
973 return -1;
974 }
975 *result = (*tp->tp_getattr)(v, (char *)name_str);
976 }
977 else {
978 *result = NULL;
979 return 0;
980 }
981
982 if (*result != NULL) {
983 return 1;
984 }
985 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
986 return -1;
987 }
988 PyErr_Clear();
989 return 0;
990 }
991
992 int
_PyObject_LookupAttrId(PyObject * v,_Py_Identifier * name,PyObject ** result)993 _PyObject_LookupAttrId(PyObject *v, _Py_Identifier *name, PyObject **result)
994 {
995 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
996 if (!oname) {
997 *result = NULL;
998 return -1;
999 }
1000 return _PyObject_LookupAttr(v, oname, result);
1001 }
1002
1003 int
PyObject_HasAttr(PyObject * v,PyObject * name)1004 PyObject_HasAttr(PyObject *v, PyObject *name)
1005 {
1006 PyObject *res;
1007 if (_PyObject_LookupAttr(v, name, &res) < 0) {
1008 PyErr_Clear();
1009 return 0;
1010 }
1011 if (res == NULL) {
1012 return 0;
1013 }
1014 Py_DECREF(res);
1015 return 1;
1016 }
1017
1018 int
PyObject_SetAttr(PyObject * v,PyObject * name,PyObject * value)1019 PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
1020 {
1021 PyTypeObject *tp = Py_TYPE(v);
1022 int err;
1023
1024 if (!PyUnicode_Check(name)) {
1025 PyErr_Format(PyExc_TypeError,
1026 "attribute name must be string, not '%.200s'",
1027 Py_TYPE(name)->tp_name);
1028 return -1;
1029 }
1030 Py_INCREF(name);
1031
1032 PyUnicode_InternInPlace(&name);
1033 if (tp->tp_setattro != NULL) {
1034 err = (*tp->tp_setattro)(v, name, value);
1035 Py_DECREF(name);
1036 return err;
1037 }
1038 if (tp->tp_setattr != NULL) {
1039 const char *name_str = PyUnicode_AsUTF8(name);
1040 if (name_str == NULL) {
1041 Py_DECREF(name);
1042 return -1;
1043 }
1044 err = (*tp->tp_setattr)(v, (char *)name_str, value);
1045 Py_DECREF(name);
1046 return err;
1047 }
1048 Py_DECREF(name);
1049 _PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
1050 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
1051 PyErr_Format(PyExc_TypeError,
1052 "'%.100s' object has no attributes "
1053 "(%s .%U)",
1054 tp->tp_name,
1055 value==NULL ? "del" : "assign to",
1056 name);
1057 else
1058 PyErr_Format(PyExc_TypeError,
1059 "'%.100s' object has only read-only attributes "
1060 "(%s .%U)",
1061 tp->tp_name,
1062 value==NULL ? "del" : "assign to",
1063 name);
1064 return -1;
1065 }
1066
1067 /* Helper to get a pointer to an object's __dict__ slot, if any */
1068
1069 PyObject **
_PyObject_GetDictPtr(PyObject * obj)1070 _PyObject_GetDictPtr(PyObject *obj)
1071 {
1072 Py_ssize_t dictoffset;
1073 PyTypeObject *tp = Py_TYPE(obj);
1074
1075 dictoffset = tp->tp_dictoffset;
1076 if (dictoffset == 0)
1077 return NULL;
1078 if (dictoffset < 0) {
1079 Py_ssize_t tsize = Py_SIZE(obj);
1080 if (tsize < 0) {
1081 tsize = -tsize;
1082 }
1083 size_t size = _PyObject_VAR_SIZE(tp, tsize);
1084
1085 dictoffset += (long)size;
1086 _PyObject_ASSERT(obj, dictoffset > 0);
1087 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
1088 }
1089 return (PyObject **) ((char *)obj + dictoffset);
1090 }
1091
1092 PyObject *
PyObject_SelfIter(PyObject * obj)1093 PyObject_SelfIter(PyObject *obj)
1094 {
1095 Py_INCREF(obj);
1096 return obj;
1097 }
1098
1099 /* Helper used when the __next__ method is removed from a type:
1100 tp_iternext is never NULL and can be safely called without checking
1101 on every iteration.
1102 */
1103
1104 PyObject *
_PyObject_NextNotImplemented(PyObject * self)1105 _PyObject_NextNotImplemented(PyObject *self)
1106 {
1107 PyErr_Format(PyExc_TypeError,
1108 "'%.200s' object is not iterable",
1109 Py_TYPE(self)->tp_name);
1110 return NULL;
1111 }
1112
1113
1114 /* Specialized version of _PyObject_GenericGetAttrWithDict
1115 specifically for the LOAD_METHOD opcode.
1116
1117 Return 1 if a method is found, 0 if it's a regular attribute
1118 from __dict__ or something returned by using a descriptor
1119 protocol.
1120
1121 `method` will point to the resolved attribute or NULL. In the
1122 latter case, an error will be set.
1123 */
1124 int
_PyObject_GetMethod(PyObject * obj,PyObject * name,PyObject ** method)1125 _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method)
1126 {
1127 PyTypeObject *tp = Py_TYPE(obj);
1128 PyObject *descr;
1129 descrgetfunc f = NULL;
1130 PyObject **dictptr, *dict;
1131 PyObject *attr;
1132 int meth_found = 0;
1133
1134 assert(*method == NULL);
1135
1136 if (Py_TYPE(obj)->tp_getattro != PyObject_GenericGetAttr
1137 || !PyUnicode_Check(name)) {
1138 *method = PyObject_GetAttr(obj, name);
1139 return 0;
1140 }
1141
1142 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1143 return 0;
1144
1145 descr = _PyType_Lookup(tp, name);
1146 if (descr != NULL) {
1147 Py_INCREF(descr);
1148 if (_PyType_HasFeature(Py_TYPE(descr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
1149 meth_found = 1;
1150 } else {
1151 f = Py_TYPE(descr)->tp_descr_get;
1152 if (f != NULL && PyDescr_IsData(descr)) {
1153 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1154 Py_DECREF(descr);
1155 return 0;
1156 }
1157 }
1158 }
1159
1160 dictptr = _PyObject_GetDictPtr(obj);
1161 if (dictptr != NULL && (dict = *dictptr) != NULL) {
1162 Py_INCREF(dict);
1163 attr = PyDict_GetItemWithError(dict, name);
1164 if (attr != NULL) {
1165 Py_INCREF(attr);
1166 *method = attr;
1167 Py_DECREF(dict);
1168 Py_XDECREF(descr);
1169 return 0;
1170 }
1171 else {
1172 Py_DECREF(dict);
1173 if (PyErr_Occurred()) {
1174 Py_XDECREF(descr);
1175 return 0;
1176 }
1177 }
1178 }
1179
1180 if (meth_found) {
1181 *method = descr;
1182 return 1;
1183 }
1184
1185 if (f != NULL) {
1186 *method = f(descr, obj, (PyObject *)Py_TYPE(obj));
1187 Py_DECREF(descr);
1188 return 0;
1189 }
1190
1191 if (descr != NULL) {
1192 *method = descr;
1193 return 0;
1194 }
1195
1196 PyErr_Format(PyExc_AttributeError,
1197 "'%.50s' object has no attribute '%U'",
1198 tp->tp_name, name);
1199
1200 set_attribute_error_context(obj, name);
1201 return 0;
1202 }
1203
1204 /* Generic GetAttr functions - put these in your tp_[gs]etattro slot. */
1205
1206 PyObject *
_PyObject_GenericGetAttrWithDict(PyObject * obj,PyObject * name,PyObject * dict,int suppress)1207 _PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name,
1208 PyObject *dict, int suppress)
1209 {
1210 /* Make sure the logic of _PyObject_GetMethod is in sync with
1211 this method.
1212
1213 When suppress=1, this function suppresses AttributeError.
1214 */
1215
1216 PyTypeObject *tp = Py_TYPE(obj);
1217 PyObject *descr = NULL;
1218 PyObject *res = NULL;
1219 descrgetfunc f;
1220 Py_ssize_t dictoffset;
1221 PyObject **dictptr;
1222
1223 if (!PyUnicode_Check(name)){
1224 PyErr_Format(PyExc_TypeError,
1225 "attribute name must be string, not '%.200s'",
1226 Py_TYPE(name)->tp_name);
1227 return NULL;
1228 }
1229 Py_INCREF(name);
1230
1231 if (tp->tp_dict == NULL) {
1232 if (PyType_Ready(tp) < 0)
1233 goto done;
1234 }
1235
1236 descr = _PyType_Lookup(tp, name);
1237
1238 f = NULL;
1239 if (descr != NULL) {
1240 Py_INCREF(descr);
1241 f = Py_TYPE(descr)->tp_descr_get;
1242 if (f != NULL && PyDescr_IsData(descr)) {
1243 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
1244 if (res == NULL && suppress &&
1245 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1246 PyErr_Clear();
1247 }
1248 goto done;
1249 }
1250 }
1251
1252 if (dict == NULL) {
1253 /* Inline _PyObject_GetDictPtr */
1254 dictoffset = tp->tp_dictoffset;
1255 if (dictoffset != 0) {
1256 if (dictoffset < 0) {
1257 Py_ssize_t tsize = Py_SIZE(obj);
1258 if (tsize < 0) {
1259 tsize = -tsize;
1260 }
1261 size_t size = _PyObject_VAR_SIZE(tp, tsize);
1262 _PyObject_ASSERT(obj, size <= PY_SSIZE_T_MAX);
1263
1264 dictoffset += (Py_ssize_t)size;
1265 _PyObject_ASSERT(obj, dictoffset > 0);
1266 _PyObject_ASSERT(obj, dictoffset % SIZEOF_VOID_P == 0);
1267 }
1268 dictptr = (PyObject **) ((char *)obj + dictoffset);
1269 dict = *dictptr;
1270 }
1271 }
1272 if (dict != NULL) {
1273 Py_INCREF(dict);
1274 res = PyDict_GetItemWithError(dict, name);
1275 if (res != NULL) {
1276 Py_INCREF(res);
1277 Py_DECREF(dict);
1278 goto done;
1279 }
1280 else {
1281 Py_DECREF(dict);
1282 if (PyErr_Occurred()) {
1283 if (suppress && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1284 PyErr_Clear();
1285 }
1286 else {
1287 goto done;
1288 }
1289 }
1290 }
1291 }
1292
1293 if (f != NULL) {
1294 res = f(descr, obj, (PyObject *)Py_TYPE(obj));
1295 if (res == NULL && suppress &&
1296 PyErr_ExceptionMatches(PyExc_AttributeError)) {
1297 PyErr_Clear();
1298 }
1299 goto done;
1300 }
1301
1302 if (descr != NULL) {
1303 res = descr;
1304 descr = NULL;
1305 goto done;
1306 }
1307
1308 if (!suppress) {
1309 PyErr_Format(PyExc_AttributeError,
1310 "'%.50s' object has no attribute '%U'",
1311 tp->tp_name, name);
1312 }
1313 done:
1314 Py_XDECREF(descr);
1315 Py_DECREF(name);
1316 return res;
1317 }
1318
1319 PyObject *
PyObject_GenericGetAttr(PyObject * obj,PyObject * name)1320 PyObject_GenericGetAttr(PyObject *obj, PyObject *name)
1321 {
1322 return _PyObject_GenericGetAttrWithDict(obj, name, NULL, 0);
1323 }
1324
1325 int
_PyObject_GenericSetAttrWithDict(PyObject * obj,PyObject * name,PyObject * value,PyObject * dict)1326 _PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name,
1327 PyObject *value, PyObject *dict)
1328 {
1329 PyTypeObject *tp = Py_TYPE(obj);
1330 PyObject *descr;
1331 descrsetfunc f;
1332 PyObject **dictptr;
1333 int res = -1;
1334
1335 if (!PyUnicode_Check(name)){
1336 PyErr_Format(PyExc_TypeError,
1337 "attribute name must be string, not '%.200s'",
1338 Py_TYPE(name)->tp_name);
1339 return -1;
1340 }
1341
1342 if (tp->tp_dict == NULL && PyType_Ready(tp) < 0)
1343 return -1;
1344
1345 Py_INCREF(name);
1346
1347 descr = _PyType_Lookup(tp, name);
1348
1349 if (descr != NULL) {
1350 Py_INCREF(descr);
1351 f = Py_TYPE(descr)->tp_descr_set;
1352 if (f != NULL) {
1353 res = f(descr, obj, value);
1354 goto done;
1355 }
1356 }
1357
1358 /* XXX [Steve Dower] These are really noisy - worth it? */
1359 /*if (PyType_Check(obj) || PyModule_Check(obj)) {
1360 if (value && PySys_Audit("object.__setattr__", "OOO", obj, name, value) < 0)
1361 return -1;
1362 if (!value && PySys_Audit("object.__delattr__", "OO", obj, name) < 0)
1363 return -1;
1364 }*/
1365
1366 if (dict == NULL) {
1367 dictptr = _PyObject_GetDictPtr(obj);
1368 if (dictptr == NULL) {
1369 if (descr == NULL) {
1370 PyErr_Format(PyExc_AttributeError,
1371 "'%.100s' object has no attribute '%U'",
1372 tp->tp_name, name);
1373 }
1374 else {
1375 PyErr_Format(PyExc_AttributeError,
1376 "'%.50s' object attribute '%U' is read-only",
1377 tp->tp_name, name);
1378 }
1379 goto done;
1380 }
1381 res = _PyObjectDict_SetItem(tp, dictptr, name, value);
1382 }
1383 else {
1384 Py_INCREF(dict);
1385 if (value == NULL)
1386 res = PyDict_DelItem(dict, name);
1387 else
1388 res = PyDict_SetItem(dict, name, value);
1389 Py_DECREF(dict);
1390 }
1391 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
1392 PyErr_SetObject(PyExc_AttributeError, name);
1393
1394 done:
1395 Py_XDECREF(descr);
1396 Py_DECREF(name);
1397 return res;
1398 }
1399
1400 int
PyObject_GenericSetAttr(PyObject * obj,PyObject * name,PyObject * value)1401 PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value)
1402 {
1403 return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL);
1404 }
1405
1406 int
PyObject_GenericSetDict(PyObject * obj,PyObject * value,void * context)1407 PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
1408 {
1409 PyObject **dictptr = _PyObject_GetDictPtr(obj);
1410 if (dictptr == NULL) {
1411 PyErr_SetString(PyExc_AttributeError,
1412 "This object has no __dict__");
1413 return -1;
1414 }
1415 if (value == NULL) {
1416 PyErr_SetString(PyExc_TypeError, "cannot delete __dict__");
1417 return -1;
1418 }
1419 if (!PyDict_Check(value)) {
1420 PyErr_Format(PyExc_TypeError,
1421 "__dict__ must be set to a dictionary, "
1422 "not a '%.200s'", Py_TYPE(value)->tp_name);
1423 return -1;
1424 }
1425 Py_INCREF(value);
1426 Py_XSETREF(*dictptr, value);
1427 return 0;
1428 }
1429
1430
1431 /* Test a value used as condition, e.g., in a while or if statement.
1432 Return -1 if an error occurred */
1433
1434 int
PyObject_IsTrue(PyObject * v)1435 PyObject_IsTrue(PyObject *v)
1436 {
1437 Py_ssize_t res;
1438 if (v == Py_True)
1439 return 1;
1440 if (v == Py_False)
1441 return 0;
1442 if (v == Py_None)
1443 return 0;
1444 else if (Py_TYPE(v)->tp_as_number != NULL &&
1445 Py_TYPE(v)->tp_as_number->nb_bool != NULL)
1446 res = (*Py_TYPE(v)->tp_as_number->nb_bool)(v);
1447 else if (Py_TYPE(v)->tp_as_mapping != NULL &&
1448 Py_TYPE(v)->tp_as_mapping->mp_length != NULL)
1449 res = (*Py_TYPE(v)->tp_as_mapping->mp_length)(v);
1450 else if (Py_TYPE(v)->tp_as_sequence != NULL &&
1451 Py_TYPE(v)->tp_as_sequence->sq_length != NULL)
1452 res = (*Py_TYPE(v)->tp_as_sequence->sq_length)(v);
1453 else
1454 return 1;
1455 /* if it is negative, it should be either -1 or -2 */
1456 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int);
1457 }
1458
1459 /* equivalent of 'not v'
1460 Return -1 if an error occurred */
1461
1462 int
PyObject_Not(PyObject * v)1463 PyObject_Not(PyObject *v)
1464 {
1465 int res;
1466 res = PyObject_IsTrue(v);
1467 if (res < 0)
1468 return res;
1469 return res == 0;
1470 }
1471
1472 /* Test whether an object can be called */
1473
1474 int
PyCallable_Check(PyObject * x)1475 PyCallable_Check(PyObject *x)
1476 {
1477 if (x == NULL)
1478 return 0;
1479 return Py_TYPE(x)->tp_call != NULL;
1480 }
1481
1482
1483 /* Helper for PyObject_Dir without arguments: returns the local scope. */
1484 static PyObject *
_dir_locals(void)1485 _dir_locals(void)
1486 {
1487 PyObject *names;
1488 PyObject *locals;
1489
1490 locals = PyEval_GetLocals();
1491 if (locals == NULL)
1492 return NULL;
1493
1494 names = PyMapping_Keys(locals);
1495 if (!names)
1496 return NULL;
1497 if (!PyList_Check(names)) {
1498 PyErr_Format(PyExc_TypeError,
1499 "dir(): expected keys() of locals to be a list, "
1500 "not '%.200s'", Py_TYPE(names)->tp_name);
1501 Py_DECREF(names);
1502 return NULL;
1503 }
1504 if (PyList_Sort(names)) {
1505 Py_DECREF(names);
1506 return NULL;
1507 }
1508 /* the locals don't need to be DECREF'd */
1509 return names;
1510 }
1511
1512 /* Helper for PyObject_Dir: object introspection. */
1513 static PyObject *
_dir_object(PyObject * obj)1514 _dir_object(PyObject *obj)
1515 {
1516 PyObject *result, *sorted;
1517 PyObject *dirfunc = _PyObject_LookupSpecial(obj, &PyId___dir__);
1518
1519 assert(obj != NULL);
1520 if (dirfunc == NULL) {
1521 if (!PyErr_Occurred())
1522 PyErr_SetString(PyExc_TypeError, "object does not provide __dir__");
1523 return NULL;
1524 }
1525 /* use __dir__ */
1526 result = _PyObject_CallNoArg(dirfunc);
1527 Py_DECREF(dirfunc);
1528 if (result == NULL)
1529 return NULL;
1530 /* return sorted(result) */
1531 sorted = PySequence_List(result);
1532 Py_DECREF(result);
1533 if (sorted == NULL)
1534 return NULL;
1535 if (PyList_Sort(sorted)) {
1536 Py_DECREF(sorted);
1537 return NULL;
1538 }
1539 return sorted;
1540 }
1541
1542 /* Implementation of dir() -- if obj is NULL, returns the names in the current
1543 (local) scope. Otherwise, performs introspection of the object: returns a
1544 sorted list of attribute names (supposedly) accessible from the object
1545 */
1546 PyObject *
PyObject_Dir(PyObject * obj)1547 PyObject_Dir(PyObject *obj)
1548 {
1549 return (obj == NULL) ? _dir_locals() : _dir_object(obj);
1550 }
1551
1552 /*
1553 None is a non-NULL undefined value.
1554 There is (and should be!) no way to create other objects of this type,
1555 so there is exactly one (which is indestructible, by the way).
1556 */
1557
1558 /* ARGSUSED */
1559 static PyObject *
none_repr(PyObject * op)1560 none_repr(PyObject *op)
1561 {
1562 return PyUnicode_FromString("None");
1563 }
1564
1565 /* ARGUSED */
1566 static void _Py_NO_RETURN
none_dealloc(PyObject * ignore)1567 none_dealloc(PyObject* ignore)
1568 {
1569 /* This should never get called, but we also don't want to SEGV if
1570 * we accidentally decref None out of existence.
1571 */
1572 Py_FatalError("deallocating None");
1573 }
1574
1575 static PyObject *
none_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)1576 none_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1577 {
1578 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
1579 PyErr_SetString(PyExc_TypeError, "NoneType takes no arguments");
1580 return NULL;
1581 }
1582 Py_RETURN_NONE;
1583 }
1584
1585 static int
none_bool(PyObject * v)1586 none_bool(PyObject *v)
1587 {
1588 return 0;
1589 }
1590
1591 static PyNumberMethods none_as_number = {
1592 0, /* nb_add */
1593 0, /* nb_subtract */
1594 0, /* nb_multiply */
1595 0, /* nb_remainder */
1596 0, /* nb_divmod */
1597 0, /* nb_power */
1598 0, /* nb_negative */
1599 0, /* nb_positive */
1600 0, /* nb_absolute */
1601 (inquiry)none_bool, /* nb_bool */
1602 0, /* nb_invert */
1603 0, /* nb_lshift */
1604 0, /* nb_rshift */
1605 0, /* nb_and */
1606 0, /* nb_xor */
1607 0, /* nb_or */
1608 0, /* nb_int */
1609 0, /* nb_reserved */
1610 0, /* nb_float */
1611 0, /* nb_inplace_add */
1612 0, /* nb_inplace_subtract */
1613 0, /* nb_inplace_multiply */
1614 0, /* nb_inplace_remainder */
1615 0, /* nb_inplace_power */
1616 0, /* nb_inplace_lshift */
1617 0, /* nb_inplace_rshift */
1618 0, /* nb_inplace_and */
1619 0, /* nb_inplace_xor */
1620 0, /* nb_inplace_or */
1621 0, /* nb_floor_divide */
1622 0, /* nb_true_divide */
1623 0, /* nb_inplace_floor_divide */
1624 0, /* nb_inplace_true_divide */
1625 0, /* nb_index */
1626 };
1627
1628 PyTypeObject _PyNone_Type = {
1629 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1630 "NoneType",
1631 0,
1632 0,
1633 none_dealloc, /*tp_dealloc*/ /*never called*/
1634 0, /*tp_vectorcall_offset*/
1635 0, /*tp_getattr*/
1636 0, /*tp_setattr*/
1637 0, /*tp_as_async*/
1638 none_repr, /*tp_repr*/
1639 &none_as_number, /*tp_as_number*/
1640 0, /*tp_as_sequence*/
1641 0, /*tp_as_mapping*/
1642 0, /*tp_hash */
1643 0, /*tp_call */
1644 0, /*tp_str */
1645 0, /*tp_getattro */
1646 0, /*tp_setattro */
1647 0, /*tp_as_buffer */
1648 Py_TPFLAGS_DEFAULT, /*tp_flags */
1649 0, /*tp_doc */
1650 0, /*tp_traverse */
1651 0, /*tp_clear */
1652 0, /*tp_richcompare */
1653 0, /*tp_weaklistoffset */
1654 0, /*tp_iter */
1655 0, /*tp_iternext */
1656 0, /*tp_methods */
1657 0, /*tp_members */
1658 0, /*tp_getset */
1659 0, /*tp_base */
1660 0, /*tp_dict */
1661 0, /*tp_descr_get */
1662 0, /*tp_descr_set */
1663 0, /*tp_dictoffset */
1664 0, /*tp_init */
1665 0, /*tp_alloc */
1666 none_new, /*tp_new */
1667 };
1668
1669 PyObject _Py_NoneStruct = {
1670 _PyObject_EXTRA_INIT
1671 1, &_PyNone_Type
1672 };
1673
1674 /* NotImplemented is an object that can be used to signal that an
1675 operation is not implemented for the given type combination. */
1676
1677 static PyObject *
NotImplemented_repr(PyObject * op)1678 NotImplemented_repr(PyObject *op)
1679 {
1680 return PyUnicode_FromString("NotImplemented");
1681 }
1682
1683 static PyObject *
NotImplemented_reduce(PyObject * op,PyObject * Py_UNUSED (ignored))1684 NotImplemented_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
1685 {
1686 return PyUnicode_FromString("NotImplemented");
1687 }
1688
1689 static PyMethodDef notimplemented_methods[] = {
1690 {"__reduce__", NotImplemented_reduce, METH_NOARGS, NULL},
1691 {NULL, NULL}
1692 };
1693
1694 static PyObject *
notimplemented_new(PyTypeObject * type,PyObject * args,PyObject * kwargs)1695 notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
1696 {
1697 if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
1698 PyErr_SetString(PyExc_TypeError, "NotImplementedType takes no arguments");
1699 return NULL;
1700 }
1701 Py_RETURN_NOTIMPLEMENTED;
1702 }
1703
1704 static void _Py_NO_RETURN
notimplemented_dealloc(PyObject * ignore)1705 notimplemented_dealloc(PyObject* ignore)
1706 {
1707 /* This should never get called, but we also don't want to SEGV if
1708 * we accidentally decref NotImplemented out of existence.
1709 */
1710 Py_FatalError("deallocating NotImplemented");
1711 }
1712
1713 static int
notimplemented_bool(PyObject * v)1714 notimplemented_bool(PyObject *v)
1715 {
1716 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1717 "NotImplemented should not be used in a boolean context",
1718 1) < 0)
1719 {
1720 return -1;
1721 }
1722 return 1;
1723 }
1724
1725 static PyNumberMethods notimplemented_as_number = {
1726 .nb_bool = notimplemented_bool,
1727 };
1728
1729 PyTypeObject _PyNotImplemented_Type = {
1730 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1731 "NotImplementedType",
1732 0,
1733 0,
1734 notimplemented_dealloc, /*tp_dealloc*/ /*never called*/
1735 0, /*tp_vectorcall_offset*/
1736 0, /*tp_getattr*/
1737 0, /*tp_setattr*/
1738 0, /*tp_as_async*/
1739 NotImplemented_repr, /*tp_repr*/
1740 ¬implemented_as_number, /*tp_as_number*/
1741 0, /*tp_as_sequence*/
1742 0, /*tp_as_mapping*/
1743 0, /*tp_hash */
1744 0, /*tp_call */
1745 0, /*tp_str */
1746 0, /*tp_getattro */
1747 0, /*tp_setattro */
1748 0, /*tp_as_buffer */
1749 Py_TPFLAGS_DEFAULT, /*tp_flags */
1750 0, /*tp_doc */
1751 0, /*tp_traverse */
1752 0, /*tp_clear */
1753 0, /*tp_richcompare */
1754 0, /*tp_weaklistoffset */
1755 0, /*tp_iter */
1756 0, /*tp_iternext */
1757 notimplemented_methods, /*tp_methods */
1758 0, /*tp_members */
1759 0, /*tp_getset */
1760 0, /*tp_base */
1761 0, /*tp_dict */
1762 0, /*tp_descr_get */
1763 0, /*tp_descr_set */
1764 0, /*tp_dictoffset */
1765 0, /*tp_init */
1766 0, /*tp_alloc */
1767 notimplemented_new, /*tp_new */
1768 };
1769
1770 PyObject _Py_NotImplementedStruct = {
1771 _PyObject_EXTRA_INIT
1772 1, &_PyNotImplemented_Type
1773 };
1774
1775 PyStatus
_PyTypes_Init(void)1776 _PyTypes_Init(void)
1777 {
1778 PyStatus status = _PyTypes_InitSlotDefs();
1779 if (_PyStatus_EXCEPTION(status)) {
1780 return status;
1781 }
1782
1783 #define INIT_TYPE(TYPE) \
1784 do { \
1785 if (PyType_Ready(&(TYPE)) < 0) { \
1786 return _PyStatus_ERR("Can't initialize " #TYPE " type"); \
1787 } \
1788 } while (0)
1789
1790 // Base types
1791 INIT_TYPE(PyBaseObject_Type);
1792 INIT_TYPE(PyType_Type);
1793 assert(PyBaseObject_Type.tp_base == NULL);
1794 assert(PyType_Type.tp_base == &PyBaseObject_Type);
1795
1796 // All other static types
1797 INIT_TYPE(PyAsyncGen_Type);
1798 INIT_TYPE(PyBool_Type);
1799 INIT_TYPE(PyByteArrayIter_Type);
1800 INIT_TYPE(PyByteArray_Type);
1801 INIT_TYPE(PyBytesIter_Type);
1802 INIT_TYPE(PyBytes_Type);
1803 INIT_TYPE(PyCFunction_Type);
1804 INIT_TYPE(PyCMethod_Type);
1805 INIT_TYPE(PyCallIter_Type);
1806 INIT_TYPE(PyCapsule_Type);
1807 INIT_TYPE(PyCell_Type);
1808 INIT_TYPE(PyClassMethodDescr_Type);
1809 INIT_TYPE(PyClassMethod_Type);
1810 INIT_TYPE(PyCode_Type);
1811 INIT_TYPE(PyComplex_Type);
1812 INIT_TYPE(PyCoro_Type);
1813 INIT_TYPE(PyDictItems_Type);
1814 INIT_TYPE(PyDictIterItem_Type);
1815 INIT_TYPE(PyDictIterKey_Type);
1816 INIT_TYPE(PyDictIterValue_Type);
1817 INIT_TYPE(PyDictKeys_Type);
1818 INIT_TYPE(PyDictProxy_Type);
1819 INIT_TYPE(PyDictRevIterItem_Type);
1820 INIT_TYPE(PyDictRevIterKey_Type);
1821 INIT_TYPE(PyDictRevIterValue_Type);
1822 INIT_TYPE(PyDictValues_Type);
1823 INIT_TYPE(PyDict_Type);
1824 INIT_TYPE(PyEllipsis_Type);
1825 INIT_TYPE(PyEnum_Type);
1826 INIT_TYPE(PyFloat_Type);
1827 INIT_TYPE(PyFrame_Type);
1828 INIT_TYPE(PyFrozenSet_Type);
1829 INIT_TYPE(PyFunction_Type);
1830 INIT_TYPE(PyGen_Type);
1831 INIT_TYPE(PyGetSetDescr_Type);
1832 INIT_TYPE(PyInstanceMethod_Type);
1833 INIT_TYPE(PyListIter_Type);
1834 INIT_TYPE(PyListRevIter_Type);
1835 INIT_TYPE(PyList_Type);
1836 INIT_TYPE(PyLongRangeIter_Type);
1837 INIT_TYPE(PyLong_Type);
1838 INIT_TYPE(PyMemberDescr_Type);
1839 INIT_TYPE(PyMemoryView_Type);
1840 INIT_TYPE(PyMethodDescr_Type);
1841 INIT_TYPE(PyMethod_Type);
1842 INIT_TYPE(PyModuleDef_Type);
1843 INIT_TYPE(PyModule_Type);
1844 INIT_TYPE(PyODictItems_Type);
1845 INIT_TYPE(PyODictIter_Type);
1846 INIT_TYPE(PyODictKeys_Type);
1847 INIT_TYPE(PyODictValues_Type);
1848 INIT_TYPE(PyODict_Type);
1849 INIT_TYPE(PyPickleBuffer_Type);
1850 INIT_TYPE(PyProperty_Type);
1851 INIT_TYPE(PyRangeIter_Type);
1852 INIT_TYPE(PyRange_Type);
1853 INIT_TYPE(PyReversed_Type);
1854 INIT_TYPE(PySTEntry_Type);
1855 INIT_TYPE(PySeqIter_Type);
1856 INIT_TYPE(PySetIter_Type);
1857 INIT_TYPE(PySet_Type);
1858 INIT_TYPE(PySlice_Type);
1859 INIT_TYPE(PyStaticMethod_Type);
1860 INIT_TYPE(PyStdPrinter_Type);
1861 INIT_TYPE(PySuper_Type);
1862 INIT_TYPE(PyTraceBack_Type);
1863 INIT_TYPE(PyTupleIter_Type);
1864 INIT_TYPE(PyTuple_Type);
1865 INIT_TYPE(PyUnicodeIter_Type);
1866 INIT_TYPE(PyUnicode_Type);
1867 INIT_TYPE(PyWrapperDescr_Type);
1868 INIT_TYPE(Py_GenericAliasType);
1869 INIT_TYPE(_PyAnextAwaitable_Type);
1870 INIT_TYPE(_PyAsyncGenASend_Type);
1871 INIT_TYPE(_PyAsyncGenAThrow_Type);
1872 INIT_TYPE(_PyAsyncGenWrappedValue_Type);
1873 INIT_TYPE(_PyCoroWrapper_Type);
1874 INIT_TYPE(_PyInterpreterID_Type);
1875 INIT_TYPE(_PyManagedBuffer_Type);
1876 INIT_TYPE(_PyMethodWrapper_Type);
1877 INIT_TYPE(_PyNamespace_Type);
1878 INIT_TYPE(_PyNone_Type);
1879 INIT_TYPE(_PyNotImplemented_Type);
1880 INIT_TYPE(_PyWeakref_CallableProxyType);
1881 INIT_TYPE(_PyWeakref_ProxyType);
1882 INIT_TYPE(_PyWeakref_RefType);
1883 INIT_TYPE(_PyUnion_Type);
1884
1885 return _PyStatus_OK();
1886 #undef INIT_TYPE
1887 }
1888
1889
1890 void
_Py_NewReference(PyObject * op)1891 _Py_NewReference(PyObject *op)
1892 {
1893 if (_Py_tracemalloc_config.tracing) {
1894 _PyTraceMalloc_NewReference(op);
1895 }
1896 #ifdef Py_REF_DEBUG
1897 _Py_RefTotal++;
1898 #endif
1899 Py_SET_REFCNT(op, 1);
1900 #ifdef Py_TRACE_REFS
1901 _Py_AddToAllObjects(op, 1);
1902 #endif
1903 }
1904
1905
1906 #ifdef Py_TRACE_REFS
1907 void
_Py_ForgetReference(PyObject * op)1908 _Py_ForgetReference(PyObject *op)
1909 {
1910 if (Py_REFCNT(op) < 0) {
1911 _PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
1912 }
1913
1914 if (op == &refchain ||
1915 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
1916 {
1917 _PyObject_ASSERT_FAILED_MSG(op, "invalid object chain");
1918 }
1919
1920 #ifdef SLOW_UNREF_CHECK
1921 PyObject *p;
1922 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
1923 if (p == op) {
1924 break;
1925 }
1926 }
1927 if (p == &refchain) {
1928 /* Not found */
1929 _PyObject_ASSERT_FAILED_MSG(op,
1930 "object not found in the objects list");
1931 }
1932 #endif
1933
1934 op->_ob_next->_ob_prev = op->_ob_prev;
1935 op->_ob_prev->_ob_next = op->_ob_next;
1936 op->_ob_next = op->_ob_prev = NULL;
1937 }
1938
1939 /* Print all live objects. Because PyObject_Print is called, the
1940 * interpreter must be in a healthy state.
1941 */
1942 void
_Py_PrintReferences(FILE * fp)1943 _Py_PrintReferences(FILE *fp)
1944 {
1945 PyObject *op;
1946 fprintf(fp, "Remaining objects:\n");
1947 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1948 fprintf(fp, "%p [%zd] ", (void *)op, Py_REFCNT(op));
1949 if (PyObject_Print(op, fp, 0) != 0) {
1950 PyErr_Clear();
1951 }
1952 putc('\n', fp);
1953 }
1954 }
1955
1956 /* Print the addresses of all live objects. Unlike _Py_PrintReferences, this
1957 * doesn't make any calls to the Python C API, so is always safe to call.
1958 */
1959 void
_Py_PrintReferenceAddresses(FILE * fp)1960 _Py_PrintReferenceAddresses(FILE *fp)
1961 {
1962 PyObject *op;
1963 fprintf(fp, "Remaining object addresses:\n");
1964 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
1965 fprintf(fp, "%p [%zd] %s\n", (void *)op,
1966 Py_REFCNT(op), Py_TYPE(op)->tp_name);
1967 }
1968
1969 PyObject *
_Py_GetObjects(PyObject * self,PyObject * args)1970 _Py_GetObjects(PyObject *self, PyObject *args)
1971 {
1972 int i, n;
1973 PyObject *t = NULL;
1974 PyObject *res, *op;
1975
1976 if (!PyArg_ParseTuple(args, "i|O", &n, &t))
1977 return NULL;
1978 op = refchain._ob_next;
1979 res = PyList_New(0);
1980 if (res == NULL)
1981 return NULL;
1982 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
1983 while (op == self || op == args || op == res || op == t ||
1984 (t != NULL && !Py_IS_TYPE(op, (PyTypeObject *) t))) {
1985 op = op->_ob_next;
1986 if (op == &refchain)
1987 return res;
1988 }
1989 if (PyList_Append(res, op) < 0) {
1990 Py_DECREF(res);
1991 return NULL;
1992 }
1993 op = op->_ob_next;
1994 }
1995 return res;
1996 }
1997
1998 #endif
1999
2000
2001 /* Hack to force loading of abstract.o */
2002 Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size;
2003
2004
2005 void
_PyObject_DebugTypeStats(FILE * out)2006 _PyObject_DebugTypeStats(FILE *out)
2007 {
2008 _PyDict_DebugMallocStats(out);
2009 _PyFloat_DebugMallocStats(out);
2010 _PyFrame_DebugMallocStats(out);
2011 _PyList_DebugMallocStats(out);
2012 _PyTuple_DebugMallocStats(out);
2013 }
2014
2015 /* These methods are used to control infinite recursion in repr, str, print,
2016 etc. Container objects that may recursively contain themselves,
2017 e.g. builtin dictionaries and lists, should use Py_ReprEnter() and
2018 Py_ReprLeave() to avoid infinite recursion.
2019
2020 Py_ReprEnter() returns 0 the first time it is called for a particular
2021 object and 1 every time thereafter. It returns -1 if an exception
2022 occurred. Py_ReprLeave() has no return value.
2023
2024 See dictobject.c and listobject.c for examples of use.
2025 */
2026
2027 int
Py_ReprEnter(PyObject * obj)2028 Py_ReprEnter(PyObject *obj)
2029 {
2030 PyObject *dict;
2031 PyObject *list;
2032 Py_ssize_t i;
2033
2034 dict = PyThreadState_GetDict();
2035 /* Ignore a missing thread-state, so that this function can be called
2036 early on startup. */
2037 if (dict == NULL)
2038 return 0;
2039 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
2040 if (list == NULL) {
2041 if (PyErr_Occurred()) {
2042 return -1;
2043 }
2044 list = PyList_New(0);
2045 if (list == NULL)
2046 return -1;
2047 if (_PyDict_SetItemId(dict, &PyId_Py_Repr, list) < 0)
2048 return -1;
2049 Py_DECREF(list);
2050 }
2051 i = PyList_GET_SIZE(list);
2052 while (--i >= 0) {
2053 if (PyList_GET_ITEM(list, i) == obj)
2054 return 1;
2055 }
2056 if (PyList_Append(list, obj) < 0)
2057 return -1;
2058 return 0;
2059 }
2060
2061 void
Py_ReprLeave(PyObject * obj)2062 Py_ReprLeave(PyObject *obj)
2063 {
2064 PyObject *dict;
2065 PyObject *list;
2066 Py_ssize_t i;
2067 PyObject *error_type, *error_value, *error_traceback;
2068
2069 PyErr_Fetch(&error_type, &error_value, &error_traceback);
2070
2071 dict = PyThreadState_GetDict();
2072 if (dict == NULL)
2073 goto finally;
2074
2075 list = _PyDict_GetItemIdWithError(dict, &PyId_Py_Repr);
2076 if (list == NULL || !PyList_Check(list))
2077 goto finally;
2078
2079 i = PyList_GET_SIZE(list);
2080 /* Count backwards because we always expect obj to be list[-1] */
2081 while (--i >= 0) {
2082 if (PyList_GET_ITEM(list, i) == obj) {
2083 PyList_SetSlice(list, i, i + 1, NULL);
2084 break;
2085 }
2086 }
2087
2088 finally:
2089 /* ignore exceptions because there is no way to report them. */
2090 PyErr_Restore(error_type, error_value, error_traceback);
2091 }
2092
2093 /* Trashcan support. */
2094
2095 /* Add op to the _PyTrash_delete_later list. Called when the current
2096 * call-stack depth gets large. op must be a currently untracked gc'ed
2097 * object, with refcount 0. Py_DECREF must already have been called on it.
2098 */
2099 void
_PyTrash_deposit_object(PyObject * op)2100 _PyTrash_deposit_object(PyObject *op)
2101 {
2102 PyInterpreterState *interp = _PyInterpreterState_GET();
2103 struct _gc_runtime_state *gcstate = &interp->gc;
2104
2105 _PyObject_ASSERT(op, _PyObject_IS_GC(op));
2106 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
2107 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
2108 _PyGCHead_SET_PREV(_Py_AS_GC(op), gcstate->trash_delete_later);
2109 gcstate->trash_delete_later = op;
2110 }
2111
2112 /* The equivalent API, using per-thread state recursion info */
2113 void
_PyTrash_thread_deposit_object(PyObject * op)2114 _PyTrash_thread_deposit_object(PyObject *op)
2115 {
2116 PyThreadState *tstate = _PyThreadState_GET();
2117 _PyObject_ASSERT(op, _PyObject_IS_GC(op));
2118 _PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
2119 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
2120 _PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
2121 tstate->trash_delete_later = op;
2122 }
2123
2124 /* Deallocate all the objects in the _PyTrash_delete_later list. Called when
2125 * the call-stack unwinds again.
2126 */
2127 void
_PyTrash_destroy_chain(void)2128 _PyTrash_destroy_chain(void)
2129 {
2130 PyInterpreterState *interp = _PyInterpreterState_GET();
2131 struct _gc_runtime_state *gcstate = &interp->gc;
2132
2133 while (gcstate->trash_delete_later) {
2134 PyObject *op = gcstate->trash_delete_later;
2135 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2136
2137 gcstate->trash_delete_later =
2138 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
2139
2140 /* Call the deallocator directly. This used to try to
2141 * fool Py_DECREF into calling it indirectly, but
2142 * Py_DECREF was already called on this object, and in
2143 * assorted non-release builds calling Py_DECREF again ends
2144 * up distorting allocation statistics.
2145 */
2146 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
2147 ++gcstate->trash_delete_nesting;
2148 (*dealloc)(op);
2149 --gcstate->trash_delete_nesting;
2150 }
2151 }
2152
2153 /* The equivalent API, using per-thread state recursion info */
2154 void
_PyTrash_thread_destroy_chain(void)2155 _PyTrash_thread_destroy_chain(void)
2156 {
2157 PyThreadState *tstate = _PyThreadState_GET();
2158 /* We need to increase trash_delete_nesting here, otherwise,
2159 _PyTrash_thread_destroy_chain will be called recursively
2160 and then possibly crash. An example that may crash without
2161 increase:
2162 N = 500000 # need to be large enough
2163 ob = object()
2164 tups = [(ob,) for i in range(N)]
2165 for i in range(49):
2166 tups = [(tup,) for tup in tups]
2167 del tups
2168 */
2169 assert(tstate->trash_delete_nesting == 0);
2170 ++tstate->trash_delete_nesting;
2171 while (tstate->trash_delete_later) {
2172 PyObject *op = tstate->trash_delete_later;
2173 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2174
2175 tstate->trash_delete_later =
2176 (PyObject*) _PyGCHead_PREV(_Py_AS_GC(op));
2177
2178 /* Call the deallocator directly. This used to try to
2179 * fool Py_DECREF into calling it indirectly, but
2180 * Py_DECREF was already called on this object, and in
2181 * assorted non-release builds calling Py_DECREF again ends
2182 * up distorting allocation statistics.
2183 */
2184 _PyObject_ASSERT(op, Py_REFCNT(op) == 0);
2185 (*dealloc)(op);
2186 assert(tstate->trash_delete_nesting == 1);
2187 }
2188 --tstate->trash_delete_nesting;
2189 }
2190
2191
2192 int
_PyTrash_begin(PyThreadState * tstate,PyObject * op)2193 _PyTrash_begin(PyThreadState *tstate, PyObject *op)
2194 {
2195 if (tstate->trash_delete_nesting >= PyTrash_UNWIND_LEVEL) {
2196 /* Store the object (to be deallocated later) and jump past
2197 * Py_TRASHCAN_END, skipping the body of the deallocator */
2198 _PyTrash_thread_deposit_object(op);
2199 return 1;
2200 }
2201 ++tstate->trash_delete_nesting;
2202 return 0;
2203 }
2204
2205
2206 void
_PyTrash_end(PyThreadState * tstate)2207 _PyTrash_end(PyThreadState *tstate)
2208 {
2209 --tstate->trash_delete_nesting;
2210 if (tstate->trash_delete_later && tstate->trash_delete_nesting <= 0) {
2211 _PyTrash_thread_destroy_chain();
2212 }
2213 }
2214
2215
2216 /* bpo-40170: It's only be used in Py_TRASHCAN_BEGIN macro to hide
2217 implementation details. */
2218 int
_PyTrash_cond(PyObject * op,destructor dealloc)2219 _PyTrash_cond(PyObject *op, destructor dealloc)
2220 {
2221 return Py_TYPE(op)->tp_dealloc == dealloc;
2222 }
2223
2224
2225 void _Py_NO_RETURN
_PyObject_AssertFailed(PyObject * obj,const char * expr,const char * msg,const char * file,int line,const char * function)2226 _PyObject_AssertFailed(PyObject *obj, const char *expr, const char *msg,
2227 const char *file, int line, const char *function)
2228 {
2229 fprintf(stderr, "%s:%d: ", file, line);
2230 if (function) {
2231 fprintf(stderr, "%s: ", function);
2232 }
2233 fflush(stderr);
2234
2235 if (expr) {
2236 fprintf(stderr, "Assertion \"%s\" failed", expr);
2237 }
2238 else {
2239 fprintf(stderr, "Assertion failed");
2240 }
2241 fflush(stderr);
2242
2243 if (msg) {
2244 fprintf(stderr, ": %s", msg);
2245 }
2246 fprintf(stderr, "\n");
2247 fflush(stderr);
2248
2249 if (_PyObject_IsFreed(obj)) {
2250 /* It seems like the object memory has been freed:
2251 don't access it to prevent a segmentation fault. */
2252 fprintf(stderr, "<object at %p is freed>\n", obj);
2253 fflush(stderr);
2254 }
2255 else {
2256 /* Display the traceback where the object has been allocated.
2257 Do it before dumping repr(obj), since repr() is more likely
2258 to crash than dumping the traceback. */
2259 void *ptr;
2260 PyTypeObject *type = Py_TYPE(obj);
2261 if (_PyType_IS_GC(type)) {
2262 ptr = (void *)((char *)obj - sizeof(PyGC_Head));
2263 }
2264 else {
2265 ptr = (void *)obj;
2266 }
2267 _PyMem_DumpTraceback(fileno(stderr), ptr);
2268
2269 /* This might succeed or fail, but we're about to abort, so at least
2270 try to provide any extra info we can: */
2271 _PyObject_Dump(obj);
2272
2273 fprintf(stderr, "\n");
2274 fflush(stderr);
2275 }
2276
2277 Py_FatalError("_PyObject_AssertFailed");
2278 }
2279
2280
2281 void
_Py_Dealloc(PyObject * op)2282 _Py_Dealloc(PyObject *op)
2283 {
2284 destructor dealloc = Py_TYPE(op)->tp_dealloc;
2285 #ifdef Py_TRACE_REFS
2286 _Py_ForgetReference(op);
2287 #endif
2288 (*dealloc)(op);
2289 }
2290
2291
2292 PyObject **
PyObject_GET_WEAKREFS_LISTPTR(PyObject * op)2293 PyObject_GET_WEAKREFS_LISTPTR(PyObject *op)
2294 {
2295 return _PyObject_GET_WEAKREFS_LISTPTR(op);
2296 }
2297
2298
2299 #undef Py_NewRef
2300 #undef Py_XNewRef
2301
2302 // Export Py_NewRef() and Py_XNewRef() as regular functions for the stable ABI.
2303 PyObject*
Py_NewRef(PyObject * obj)2304 Py_NewRef(PyObject *obj)
2305 {
2306 return _Py_NewRef(obj);
2307 }
2308
2309 PyObject*
Py_XNewRef(PyObject * obj)2310 Py_XNewRef(PyObject *obj)
2311 {
2312 return _Py_XNewRef(obj);
2313 }
2314
2315 #undef Py_Is
2316 #undef Py_IsNone
2317 #undef Py_IsTrue
2318 #undef Py_IsFalse
2319
2320 // Export Py_Is(), Py_IsNone(), Py_IsTrue(), Py_IsFalse() as regular functions
2321 // for the stable ABI.
Py_Is(PyObject * x,PyObject * y)2322 int Py_Is(PyObject *x, PyObject *y)
2323 {
2324 return (x == y);
2325 }
2326
Py_IsNone(PyObject * x)2327 int Py_IsNone(PyObject *x)
2328 {
2329 return Py_Is(x, Py_None);
2330 }
2331
Py_IsTrue(PyObject * x)2332 int Py_IsTrue(PyObject *x)
2333 {
2334 return Py_Is(x, Py_True);
2335 }
2336
Py_IsFalse(PyObject * x)2337 int Py_IsFalse(PyObject *x)
2338 {
2339 return Py_Is(x, Py_False);
2340 }
2341
2342 #ifdef __cplusplus
2343 }
2344 #endif
2345