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