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