ยปCore Development>Code coverage>Objects/object.c

Python code coverage for Objects/object.c

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