2 /* Generic object operations; and implementation of None (NoObject) */
10 /* just for trashcan: */
12 #include "frameobject.h"
13 #include "traceback.h"
15 #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
16 DL_IMPORT(long) _Py_RefTotal
;
19 DL_IMPORT(int) Py_DivisionWarningFlag
;
21 /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
22 These are used by the individual routines for object creation.
23 Do not call them otherwise, they do not initialize the object! */
26 static PyTypeObject
*type_list
;
27 extern int tuple_zero_allocs
, fast_tuple_allocs
;
28 extern int quick_int_allocs
, quick_neg_int_allocs
;
29 extern int null_strings
, one_strings
;
35 for (tp
= type_list
; tp
; tp
= tp
->tp_next
)
36 fprintf(stderr
, "%s alloc'd: %d, freed: %d, max in use: %d\n",
37 tp
->tp_name
, tp
->tp_allocs
, tp
->tp_frees
,
39 fprintf(stderr
, "fast tuple allocs: %d, empty: %d\n",
40 fast_tuple_allocs
, tuple_zero_allocs
);
41 fprintf(stderr
, "fast int allocs: pos: %d, neg: %d\n",
42 quick_int_allocs
, quick_neg_int_allocs
);
43 fprintf(stderr
, "null strings: %d, 1-strings: %d\n",
44 null_strings
, one_strings
);
54 result
= PyList_New(0);
57 for (tp
= type_list
; tp
; tp
= tp
->tp_next
) {
58 v
= Py_BuildValue("(siii)", tp
->tp_name
, tp
->tp_allocs
,
59 tp
->tp_frees
, tp
->tp_maxalloc
);
64 if (PyList_Append(result
, v
) < 0) {
75 inc_count(PyTypeObject
*tp
)
77 if (tp
->tp_allocs
== 0) {
78 /* first time; insert in linked list */
79 if (tp
->tp_next
!= NULL
) /* sanity check */
80 Py_FatalError("XXX inc_count sanity check");
81 tp
->tp_next
= type_list
;
85 if (tp
->tp_allocs
- tp
->tp_frees
> tp
->tp_maxalloc
)
86 tp
->tp_maxalloc
= tp
->tp_allocs
- tp
->tp_frees
;
91 PyObject_Init(PyObject
*op
, PyTypeObject
*tp
)
94 PyErr_SetString(PyExc_SystemError
,
95 "NULL object passed to PyObject_Init");
98 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */
100 _Py_NewReference(op
);
105 PyObject_InitVar(PyVarObject
*op
, PyTypeObject
*tp
, int size
)
108 PyErr_SetString(PyExc_SystemError
,
109 "NULL object passed to PyObject_InitVar");
112 /* Any changes should be reflected in PyObject_INIT_VAR */
115 _Py_NewReference((PyObject
*)op
);
120 _PyObject_New(PyTypeObject
*tp
)
123 op
= (PyObject
*) PyObject_MALLOC(_PyObject_SIZE(tp
));
125 return PyErr_NoMemory();
126 return PyObject_INIT(op
, tp
);
130 _PyObject_NewVar(PyTypeObject
*tp
, int nitems
)
133 const size_t size
= _PyObject_VAR_SIZE(tp
, nitems
);
134 op
= (PyVarObject
*) PyObject_MALLOC(size
);
136 return (PyVarObject
*)PyErr_NoMemory();
137 return PyObject_INIT_VAR(op
, tp
, nitems
);
141 _PyObject_Del(PyObject
*op
)
147 PyObject_Print(PyObject
*op
, FILE *fp
, int flags
)
150 if (PyErr_CheckSignals())
152 #ifdef USE_STACKCHECK
153 if (PyOS_CheckStack()) {
154 PyErr_SetString(PyExc_MemoryError
, "stack overflow");
158 clearerr(fp
); /* Clear any previous error condition */
160 fprintf(fp
, "<nil>");
163 if (op
->ob_refcnt
<= 0)
164 fprintf(fp
, "<refcnt %u at %p>",
166 else if (op
->ob_type
->tp_print
== NULL
) {
168 if (flags
& Py_PRINT_RAW
)
169 s
= PyObject_Str(op
);
171 s
= PyObject_Repr(op
);
175 ret
= PyObject_Print(s
, fp
, Py_PRINT_RAW
);
180 ret
= (*op
->ob_type
->tp_print
)(op
, fp
, flags
);
184 PyErr_SetFromErrno(PyExc_IOError
);
192 /* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */
193 void _PyObject_Dump(PyObject
* op
)
196 fprintf(stderr
, "NULL\n");
198 fprintf(stderr
, "object : ");
199 (void)PyObject_Print(op
, stderr
, 0);
204 op
->ob_type
==NULL
? "NULL" : op
->ob_type
->tp_name
,
211 PyObject_Repr(PyObject
*v
)
213 if (PyErr_CheckSignals())
215 #ifdef USE_STACKCHECK
216 if (PyOS_CheckStack()) {
217 PyErr_SetString(PyExc_MemoryError
, "stack overflow");
222 return PyString_FromString("<NULL>");
223 else if (v
->ob_type
->tp_repr
== NULL
)
224 return PyString_FromFormat("<%s object at %p>",
225 v
->ob_type
->tp_name
, v
);
228 res
= (*v
->ob_type
->tp_repr
)(v
);
231 #ifdef Py_USING_UNICODE
232 if (PyUnicode_Check(res
)) {
234 str
= PyUnicode_AsUnicodeEscapeString(res
);
242 if (!PyString_Check(res
)) {
243 PyErr_Format(PyExc_TypeError
,
244 "__repr__ returned non-string (type %.200s)",
245 res
->ob_type
->tp_name
);
254 PyObject_Str(PyObject
*v
)
259 return PyString_FromString("<NULL>");
260 if (PyString_CheckExact(v
)) {
264 if (v
->ob_type
->tp_str
== NULL
)
265 return PyObject_Repr(v
);
267 res
= (*v
->ob_type
->tp_str
)(v
);
270 #ifdef Py_USING_UNICODE
271 if (PyUnicode_Check(res
)) {
273 str
= PyUnicode_AsEncodedString(res
, NULL
, NULL
);
281 if (!PyString_Check(res
)) {
282 PyErr_Format(PyExc_TypeError
,
283 "__str__ returned non-string (type %.200s)",
284 res
->ob_type
->tp_name
);
291 #ifdef Py_USING_UNICODE
293 PyObject_Unicode(PyObject
*v
)
298 res
= PyString_FromString("<NULL>");
299 if (PyUnicode_CheckExact(v
)) {
303 if (PyUnicode_Check(v
)) {
304 /* For a Unicode subtype that's not a Unicode object,
305 return a true Unicode object with the same data. */
306 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v
),
307 PyUnicode_GET_SIZE(v
));
309 if (PyString_Check(v
)) {
315 static PyObject
*unicodestr
;
316 /* XXX As soon as we have a tp_unicode slot, we should
317 check this before trying the __unicode__
319 if (unicodestr
== NULL
) {
320 unicodestr
= PyString_InternFromString(
322 if (unicodestr
== NULL
)
325 func
= PyObject_GetAttr(v
, unicodestr
);
327 res
= PyEval_CallObject(func
, (PyObject
*)NULL
);
332 if (v
->ob_type
->tp_str
!= NULL
)
333 res
= (*v
->ob_type
->tp_str
)(v
);
335 res
= PyObject_Repr(v
);
340 if (!PyUnicode_Check(res
)) {
342 str
= PyUnicode_FromEncodedObject(res
, NULL
, "strict");
354 /* Macro to get the tp_richcompare field of a type if defined */
355 #define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
356 ? (t)->tp_richcompare : NULL)
358 /* Map rich comparison operators to their swapped version, e.g. LT --> GT */
359 static int swapped_op
[] = {Py_GT
, Py_GE
, Py_EQ
, Py_NE
, Py_LT
, Py_LE
};
361 /* Try a genuine rich comparison, returning an object. Return:
363 NotImplemented if this particular rich comparison is not implemented or
365 some object not equal to NotImplemented if it is implemented
366 (this latter object may not be a Boolean).
369 try_rich_compare(PyObject
*v
, PyObject
*w
, int op
)
374 if (v
->ob_type
!= w
->ob_type
&&
375 PyType_IsSubtype(w
->ob_type
, v
->ob_type
) &&
376 (f
= RICHCOMPARE(w
->ob_type
)) != NULL
) {
377 res
= (*f
)(w
, v
, swapped_op
[op
]);
378 if (res
!= Py_NotImplemented
)
382 if ((f
= RICHCOMPARE(v
->ob_type
)) != NULL
) {
383 res
= (*f
)(v
, w
, op
);
384 if (res
!= Py_NotImplemented
)
388 if ((f
= RICHCOMPARE(w
->ob_type
)) != NULL
) {
389 return (*f
)(w
, v
, swapped_op
[op
]);
391 res
= Py_NotImplemented
;
396 /* Try a genuine rich comparison, returning an int. Return:
397 -1 for exception (including the case where try_rich_compare() returns an
398 object that's not a Boolean);
399 0 if the outcome is false;
400 1 if the outcome is true;
401 2 if this particular rich comparison is not implemented or undefined.
404 try_rich_compare_bool(PyObject
*v
, PyObject
*w
, int op
)
409 if (RICHCOMPARE(v
->ob_type
) == NULL
&& RICHCOMPARE(w
->ob_type
) == NULL
)
410 return 2; /* Shortcut, avoid INCREF+DECREF */
411 res
= try_rich_compare(v
, w
, op
);
414 if (res
== Py_NotImplemented
) {
418 ok
= PyObject_IsTrue(res
);
423 /* Try rich comparisons to determine a 3-way comparison. Return:
428 2 if this particular rich comparison is not implemented or undefined.
431 try_rich_to_3way_compare(PyObject
*v
, PyObject
*w
)
433 static struct { int op
; int outcome
; } tries
[3] = {
434 /* Try this operator, and if it is true, use this outcome: */
441 if (RICHCOMPARE(v
->ob_type
) == NULL
&& RICHCOMPARE(w
->ob_type
) == NULL
)
442 return 2; /* Shortcut */
444 for (i
= 0; i
< 3; i
++) {
445 switch (try_rich_compare_bool(v
, w
, tries
[i
].op
)) {
449 return tries
[i
].outcome
;
456 /* Try a 3-way comparison, returning an int. Return:
461 2 if this particular 3-way comparison is not implemented or undefined.
464 try_3way_compare(PyObject
*v
, PyObject
*w
)
469 /* Comparisons involving instances are given to instance_compare,
470 which has the same return conventions as this function. */
472 f
= v
->ob_type
->tp_compare
;
473 if (PyInstance_Check(v
))
475 if (PyInstance_Check(w
))
476 return (*w
->ob_type
->tp_compare
)(v
, w
);
478 /* If both have the same (non-NULL) tp_compare, use it. */
479 if (f
!= NULL
&& f
== w
->ob_type
->tp_compare
) {
481 if (c
< 0 && PyErr_Occurred())
483 return c
< 0 ? -1 : c
> 0 ? 1 : 0;
486 /* If either tp_compare is _PyObject_SlotCompare, that's safe. */
487 if (f
== _PyObject_SlotCompare
||
488 w
->ob_type
->tp_compare
== _PyObject_SlotCompare
)
489 return _PyObject_SlotCompare(v
, w
);
491 /* Try coercion; if it fails, give up */
492 c
= PyNumber_CoerceEx(&v
, &w
);
498 /* Try v's comparison, if defined */
499 if ((f
= v
->ob_type
->tp_compare
) != NULL
) {
503 if (c
< 0 && PyErr_Occurred())
505 return c
< 0 ? -1 : c
> 0 ? 1 : 0;
508 /* Try w's comparison, if defined */
509 if ((f
= w
->ob_type
->tp_compare
) != NULL
) {
510 c
= (*f
)(w
, v
); /* swapped! */
513 if (c
< 0 && PyErr_Occurred())
515 return c
< 0 ? 1 : c
> 0 ? -1 : 0; /* negated! */
518 /* No comparison defined */
524 /* Final fallback 3-way comparison, returning an int. Return:
525 -2 if an error occurred;
531 default_3way_compare(PyObject
*v
, PyObject
*w
)
536 if (v
->ob_type
== w
->ob_type
) {
537 /* When comparing these pointers, they must be cast to
538 * integer types (i.e. Py_uintptr_t, our spelling of C9X's
539 * uintptr_t). ANSI specifies that pointer compares other
540 * than == and != to non-related structures are undefined.
542 Py_uintptr_t vv
= (Py_uintptr_t
)v
;
543 Py_uintptr_t ww
= (Py_uintptr_t
)w
;
544 return (vv
< ww
) ? -1 : (vv
> ww
) ? 1 : 0;
547 #ifdef Py_USING_UNICODE
548 /* Special case for Unicode */
549 if (PyUnicode_Check(v
) || PyUnicode_Check(w
)) {
550 c
= PyUnicode_Compare(v
, w
);
551 if (!PyErr_Occurred())
553 /* TypeErrors are ignored: if Unicode coercion fails due
554 to one of the arguments not having the right type, we
555 continue as defined by the coercion protocol (see
556 above). Luckily, decoding errors are reported as
557 ValueErrors and are not masked by this technique. */
558 if (!PyErr_ExceptionMatches(PyExc_TypeError
))
564 /* None is smaller than anything */
570 /* different type: compare type names */
571 if (v
->ob_type
->tp_as_number
)
574 vname
= v
->ob_type
->tp_name
;
575 if (w
->ob_type
->tp_as_number
)
578 wname
= w
->ob_type
->tp_name
;
579 c
= strcmp(vname
, wname
);
584 /* Same type name, or (more likely) incomparable numeric types */
585 return ((Py_uintptr_t
)(v
->ob_type
) < (
586 Py_uintptr_t
)(w
->ob_type
)) ? -1 : 1;
589 #define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)
591 /* Do a 3-way comparison, by hook or by crook. Return:
596 If the object implements a tp_compare function, it returns
597 whatever this function returns (whether with an exception or not).
600 do_cmp(PyObject
*v
, PyObject
*w
)
605 if (v
->ob_type
== w
->ob_type
606 && (f
= v
->ob_type
->tp_compare
) != NULL
) {
608 if (c
!= 2 || !PyInstance_Check(v
))
611 c
= try_rich_to_3way_compare(v
, w
);
614 c
= try_3way_compare(v
, w
);
617 return default_3way_compare(v
, w
);
620 /* compare_nesting is incremented before calling compare (for
621 some types) and decremented on exit. If the count exceeds the
622 nesting limit, enable code to detect circular data structures.
624 This is a tunable parameter that should only affect the performance
625 of comparisons, nothing else. Setting it high makes comparing deeply
626 nested non-cyclical data structures faster, but makes comparing cyclical
627 data structures slower.
629 #define NESTING_LIMIT 20
631 static int compare_nesting
= 0;
634 get_inprogress_dict(void)
636 static PyObject
*key
;
637 PyObject
*tstate_dict
, *inprogress
;
640 key
= PyString_InternFromString("cmp_state");
645 tstate_dict
= PyThreadState_GetDict();
646 if (tstate_dict
== NULL
) {
647 PyErr_BadInternalCall();
651 inprogress
= PyDict_GetItem(tstate_dict
, key
);
652 if (inprogress
== NULL
) {
653 inprogress
= PyDict_New();
654 if (inprogress
== NULL
)
656 if (PyDict_SetItem(tstate_dict
, key
, inprogress
) == -1) {
657 Py_DECREF(inprogress
);
660 Py_DECREF(inprogress
);
667 check_recursion(PyObject
*v
, PyObject
*w
, int op
)
669 PyObject
*inprogress
;
671 Py_uintptr_t iv
= (Py_uintptr_t
)v
;
672 Py_uintptr_t iw
= (Py_uintptr_t
)w
;
675 inprogress
= get_inprogress_dict();
676 if (inprogress
== NULL
)
679 token
= PyTuple_New(3);
684 PyTuple_SET_ITEM(token
, 0, x
= PyLong_FromVoidPtr((void *)v
));
685 PyTuple_SET_ITEM(token
, 1, y
= PyLong_FromVoidPtr((void *)w
));
689 PyTuple_SET_ITEM(token
, 0, x
= PyLong_FromVoidPtr((void *)w
));
690 PyTuple_SET_ITEM(token
, 1, y
= PyLong_FromVoidPtr((void *)v
));
692 PyTuple_SET_ITEM(token
, 2, z
= PyInt_FromLong((long)op
));
693 if (x
== NULL
|| y
== NULL
|| z
== NULL
) {
698 if (PyDict_GetItem(inprogress
, token
) != NULL
) {
700 return Py_None
; /* Without INCREF! */
703 if (PyDict_SetItem(inprogress
, token
, token
) < 0) {
712 delete_token(PyObject
*token
)
714 PyObject
*inprogress
;
716 if (token
== NULL
|| token
== Py_None
)
718 inprogress
= get_inprogress_dict();
719 if (inprogress
== NULL
)
722 PyDict_DelItem(inprogress
, token
);
726 /* Compare v to w. Return
727 -1 if v < w or exception (PyErr_Occurred() true in latter case).
730 XXX The docs (C API manual) say the return value is undefined in case
734 PyObject_Compare(PyObject
*v
, PyObject
*w
)
739 #if defined(USE_STACKCHECK)
740 if (PyOS_CheckStack()) {
741 PyErr_SetString(PyExc_MemoryError
, "Stack overflow");
745 if (v
== NULL
|| w
== NULL
) {
746 PyErr_BadInternalCall();
753 if (compare_nesting
> NESTING_LIMIT
&&
755 || (vtp
->tp_as_sequence
756 && !PyString_Check(v
)
757 && !PyTuple_Check(v
)))) {
758 /* try to detect circular data structures */
759 PyObject
*token
= check_recursion(v
, w
, -1);
764 else if (token
== Py_None
) {
765 /* already comparing these objects. assume
766 they're equal until shown otherwise */
770 result
= do_cmp(v
, w
);
775 result
= do_cmp(v
, w
);
778 return result
< 0 ? -1 : result
;
781 /* Return (new reference to) Py_True or Py_False. */
783 convert_3way_to_object(int op
, int c
)
787 case Py_LT
: c
= c
< 0; break;
788 case Py_LE
: c
= c
<= 0; break;
789 case Py_EQ
: c
= c
== 0; break;
790 case Py_NE
: c
= c
!= 0; break;
791 case Py_GT
: c
= c
> 0; break;
792 case Py_GE
: c
= c
>= 0; break;
794 result
= c
? Py_True
: Py_False
;
799 /* We want a rich comparison but don't have one. Try a 3-way cmp instead.
803 Py_False if not (v op w)
806 try_3way_to_rich_compare(PyObject
*v
, PyObject
*w
, int op
)
810 c
= try_3way_compare(v
, w
);
812 c
= default_3way_compare(v
, w
);
815 return convert_3way_to_object(op
, c
);
818 /* Do rich comparison on v and w. Return
820 Else a new reference to an object other than Py_NotImplemented, usually(?):
822 Py_False if not (v op w)
825 do_richcmp(PyObject
*v
, PyObject
*w
, int op
)
829 res
= try_rich_compare(v
, w
, op
);
830 if (res
!= Py_NotImplemented
)
834 return try_3way_to_rich_compare(v
, w
, op
);
839 some object not equal to NotImplemented if it is implemented
840 (this latter object may not be a Boolean).
843 PyObject_RichCompare(PyObject
*v
, PyObject
*w
, int op
)
847 assert(Py_LT
<= op
&& op
<= Py_GE
);
850 if (compare_nesting
> NESTING_LIMIT
&&
851 (v
->ob_type
->tp_as_mapping
852 || (v
->ob_type
->tp_as_sequence
853 && !PyString_Check(v
)
854 && !PyTuple_Check(v
)))) {
856 /* try to detect circular data structures */
857 PyObject
*token
= check_recursion(v
, w
, op
);
862 else if (token
== Py_None
) {
863 /* already comparing these objects with this operator.
864 assume they're equal until shown otherwise */
867 else if (op
== Py_NE
)
870 PyErr_SetString(PyExc_ValueError
,
871 "can't order recursive values");
877 res
= do_richcmp(v
, w
, op
);
883 /* No nesting extremism.
884 If the types are equal, and not old-style instances, try to
885 get out cheap (don't bother with coercions etc.). */
886 if (v
->ob_type
== w
->ob_type
&& !PyInstance_Check(v
)) {
888 richcmpfunc frich
= RICHCOMPARE(v
->ob_type
);
889 /* If the type has richcmp, try it first. try_rich_compare
890 tries it two-sided, which is not needed since we've a
893 res
= (*frich
)(v
, w
, op
);
894 if (res
!= Py_NotImplemented
)
898 /* No richcmp, or this particular richmp not implemented.
900 fcmp
= v
->ob_type
->tp_compare
;
902 int c
= (*fcmp
)(v
, w
);
903 if (c
< 0 && PyErr_Occurred()) {
907 res
= convert_3way_to_object(op
, c
);
912 /* Fast path not taken, or couldn't deliver a useful result. */
913 res
= do_richcmp(v
, w
, op
);
919 /* Return -1 if error; 1 if v op w; 0 if not (v op w). */
921 PyObject_RichCompareBool(PyObject
*v
, PyObject
*w
, int op
)
923 PyObject
*res
= PyObject_RichCompare(v
, w
, op
);
928 ok
= PyObject_IsTrue(res
);
933 /* Set of hash utility functions to help maintaining the invariant that
934 iff a==b then hash(a)==hash(b)
936 All the utility functions (_Py_Hash*()) return "-1" to signify an error.
940 _Py_HashDouble(double v
)
942 double intpart
, fractpart
;
945 long x
; /* the final hash value */
946 /* This is designed so that Python numbers of different types
947 * that compare equal hash to the same value; otherwise comparisons
948 * of mapping keys will turn out weird.
951 #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
954 fractpart
= modf(v
, &e
);
958 fractpart
= modf(v
, &intpart
);
960 if (fractpart
== 0.0) {
961 /* This must return the same hash as an equal int or long. */
962 if (intpart
> LONG_MAX
|| -intpart
> LONG_MAX
) {
963 /* Convert to long and use its hash. */
964 PyObject
*plong
; /* converted to Python long */
965 if (Py_IS_INFINITY(intpart
))
966 /* can't convert to long int -- arbitrary */
967 v
= v
< 0 ? -271828.0 : 314159.0;
968 plong
= PyLong_FromDouble(v
);
971 x
= PyObject_Hash(plong
);
975 /* Fits in a C long == a Python int, so is its own hash. */
981 /* The fractional part is non-zero, so we don't have to worry about
982 * making this match the hash of some other type.
983 * Use frexp to get at the bits in the double.
984 * Since the VAX D double format has 56 mantissa bits, which is the
985 * most of any double format in use, each of these parts may have as
986 * many as (but no more than) 56 significant bits.
987 * So, assuming sizeof(long) >= 4, each part can be broken into two
988 * longs; frexp and multiplication are used to do that.
989 * Also, since the Cray double format has 15 exponent bits, which is
990 * the most of any double format in use, shifting the exponent field
991 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
994 v
*= 2147483648.0; /* 2**31 */
995 hipart
= (long)v
; /* take the top 32 bits */
996 v
= (v
- (double)hipart
) * 2147483648.0; /* get the next 32 bits */
997 x
= hipart
+ (long)v
+ (expo
<< 15);
1004 _Py_HashPointer(void *p
)
1006 #if SIZEOF_LONG >= SIZEOF_VOID_P
1009 /* convert to a Python long and hash that */
1013 if ((longobj
= PyLong_FromVoidPtr(p
)) == NULL
) {
1017 x
= PyObject_Hash(longobj
);
1020 Py_XDECREF(longobj
);
1027 PyObject_Hash(PyObject
*v
)
1029 PyTypeObject
*tp
= v
->ob_type
;
1030 if (tp
->tp_hash
!= NULL
)
1031 return (*tp
->tp_hash
)(v
);
1032 if (tp
->tp_compare
== NULL
&& RICHCOMPARE(tp
) == NULL
) {
1033 return _Py_HashPointer(v
); /* Use address as hash value */
1035 /* If there's a cmp but no hash defined, the object can't be hashed */
1036 PyErr_SetString(PyExc_TypeError
, "unhashable type");
1041 PyObject_GetAttrString(PyObject
*v
, char *name
)
1045 if (v
->ob_type
->tp_getattr
!= NULL
)
1046 return (*v
->ob_type
->tp_getattr
)(v
, name
);
1047 w
= PyString_InternFromString(name
);
1050 res
= PyObject_GetAttr(v
, w
);
1056 PyObject_HasAttrString(PyObject
*v
, char *name
)
1058 PyObject
*res
= PyObject_GetAttrString(v
, name
);
1068 PyObject_SetAttrString(PyObject
*v
, char *name
, PyObject
*w
)
1073 if (v
->ob_type
->tp_setattr
!= NULL
)
1074 return (*v
->ob_type
->tp_setattr
)(v
, name
, w
);
1075 s
= PyString_InternFromString(name
);
1078 res
= PyObject_SetAttr(v
, s
, w
);
1084 PyObject_GetAttr(PyObject
*v
, PyObject
*name
)
1086 PyTypeObject
*tp
= v
->ob_type
;
1088 #ifdef Py_USING_UNICODE
1089 /* The Unicode to string conversion is done here because the
1090 existing tp_getattro slots expect a string object as name
1091 and we wouldn't want to break those. */
1092 if (PyUnicode_Check(name
)) {
1093 name
= _PyUnicode_AsDefaultEncodedString(name
, NULL
);
1099 if (!PyString_Check(name
)) {
1100 PyErr_SetString(PyExc_TypeError
,
1101 "attribute name must be string");
1104 if (tp
->tp_getattro
!= NULL
)
1105 return (*tp
->tp_getattro
)(v
, name
);
1106 if (tp
->tp_getattr
!= NULL
)
1107 return (*tp
->tp_getattr
)(v
, PyString_AS_STRING(name
));
1108 PyErr_Format(PyExc_AttributeError
,
1109 "'%.50s' object has no attribute '%.400s'",
1110 tp
->tp_name
, PyString_AS_STRING(name
));
1115 PyObject_HasAttr(PyObject
*v
, PyObject
*name
)
1117 PyObject
*res
= PyObject_GetAttr(v
, name
);
1127 PyObject_SetAttr(PyObject
*v
, PyObject
*name
, PyObject
*value
)
1129 PyTypeObject
*tp
= v
->ob_type
;
1132 #ifdef Py_USING_UNICODE
1133 /* The Unicode to string conversion is done here because the
1134 existing tp_setattro slots expect a string object as name
1135 and we wouldn't want to break those. */
1136 if (PyUnicode_Check(name
)) {
1137 name
= PyUnicode_AsEncodedString(name
, NULL
, NULL
);
1143 if (!PyString_Check(name
)){
1144 PyErr_SetString(PyExc_TypeError
,
1145 "attribute name must be string");
1151 PyString_InternInPlace(&name
);
1152 if (tp
->tp_setattro
!= NULL
) {
1153 err
= (*tp
->tp_setattro
)(v
, name
, value
);
1157 if (tp
->tp_setattr
!= NULL
) {
1158 err
= (*tp
->tp_setattr
)(v
, PyString_AS_STRING(name
), value
);
1163 if (tp
->tp_getattr
== NULL
&& tp
->tp_getattro
== NULL
)
1164 PyErr_Format(PyExc_TypeError
,
1165 "'%.100s' object has no attributes "
1168 value
==NULL
? "del" : "assign to",
1169 PyString_AS_STRING(name
));
1171 PyErr_Format(PyExc_TypeError
,
1172 "'%.100s' object has only read-only attributes "
1175 value
==NULL
? "del" : "assign to",
1176 PyString_AS_STRING(name
));
1180 /* Helper to get a pointer to an object's __dict__ slot, if any */
1183 _PyObject_GetDictPtr(PyObject
*obj
)
1186 PyTypeObject
*tp
= obj
->ob_type
;
1188 if (!(tp
->tp_flags
& Py_TPFLAGS_HAVE_CLASS
))
1190 dictoffset
= tp
->tp_dictoffset
;
1191 if (dictoffset
== 0)
1193 if (dictoffset
< 0) {
1194 const size_t size
= _PyObject_VAR_SIZE(tp
,
1195 ((PyVarObject
*)obj
)->ob_size
);
1196 dictoffset
+= (long)size
;
1197 assert(dictoffset
> 0);
1198 assert(dictoffset
% SIZEOF_VOID_P
== 0);
1200 return (PyObject
**) ((char *)obj
+ dictoffset
);
1203 /* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
1206 PyObject_GenericGetAttr(PyObject
*obj
, PyObject
*name
)
1208 PyTypeObject
*tp
= obj
->ob_type
;
1210 PyObject
*res
= NULL
;
1214 #ifdef Py_USING_UNICODE
1215 /* The Unicode to string conversion is done here because the
1216 existing tp_setattro slots expect a string object as name
1217 and we wouldn't want to break those. */
1218 if (PyUnicode_Check(name
)) {
1219 name
= PyUnicode_AsEncodedString(name
, NULL
, NULL
);
1225 if (!PyString_Check(name
)){
1226 PyErr_SetString(PyExc_TypeError
,
1227 "attribute name must be string");
1233 if (tp
->tp_dict
== NULL
) {
1234 if (PyType_Ready(tp
) < 0)
1238 descr
= _PyType_Lookup(tp
, name
);
1240 if (descr
!= NULL
) {
1241 f
= descr
->ob_type
->tp_descr_get
;
1242 if (f
!= NULL
&& PyDescr_IsData(descr
)) {
1243 res
= f(descr
, obj
, (PyObject
*)obj
->ob_type
);
1248 dictptr
= _PyObject_GetDictPtr(obj
);
1249 if (dictptr
!= NULL
) {
1250 PyObject
*dict
= *dictptr
;
1252 res
= PyDict_GetItem(dict
, name
);
1261 res
= f(descr
, obj
, (PyObject
*)obj
->ob_type
);
1265 if (descr
!= NULL
) {
1271 PyErr_Format(PyExc_AttributeError
,
1272 "'%.50s' object has no attribute '%.400s'",
1273 tp
->tp_name
, PyString_AS_STRING(name
));
1280 PyObject_GenericSetAttr(PyObject
*obj
, PyObject
*name
, PyObject
*value
)
1282 PyTypeObject
*tp
= obj
->ob_type
;
1288 #ifdef Py_USING_UNICODE
1289 /* The Unicode to string conversion is done here because the
1290 existing tp_setattro slots expect a string object as name
1291 and we wouldn't want to break those. */
1292 if (PyUnicode_Check(name
)) {
1293 name
= PyUnicode_AsEncodedString(name
, NULL
, NULL
);
1299 if (!PyString_Check(name
)){
1300 PyErr_SetString(PyExc_TypeError
,
1301 "attribute name must be string");
1307 if (tp
->tp_dict
== NULL
) {
1308 if (PyType_Ready(tp
) < 0)
1312 descr
= _PyType_Lookup(tp
, name
);
1314 if (descr
!= NULL
) {
1315 f
= descr
->ob_type
->tp_descr_set
;
1316 if (f
!= NULL
&& PyDescr_IsData(descr
)) {
1317 res
= f(descr
, obj
, value
);
1322 dictptr
= _PyObject_GetDictPtr(obj
);
1323 if (dictptr
!= NULL
) {
1324 PyObject
*dict
= *dictptr
;
1325 if (dict
== NULL
&& value
!= NULL
) {
1326 dict
= PyDict_New();
1333 res
= PyDict_DelItem(dict
, name
);
1335 res
= PyDict_SetItem(dict
, name
, value
);
1336 if (res
< 0 && PyErr_ExceptionMatches(PyExc_KeyError
))
1337 PyErr_SetObject(PyExc_AttributeError
, name
);
1343 res
= f(descr
, obj
, value
);
1347 if (descr
== NULL
) {
1348 PyErr_Format(PyExc_AttributeError
,
1349 "'%.50s' object has no attribute '%.400s'",
1350 tp
->tp_name
, PyString_AS_STRING(name
));
1354 PyErr_Format(PyExc_AttributeError
,
1355 "'%.50s' object attribute '%.400s' is read-only",
1356 tp
->tp_name
, PyString_AS_STRING(name
));
1362 /* Test a value used as condition, e.g., in a for or if statement.
1363 Return -1 if an error occurred */
1366 PyObject_IsTrue(PyObject
*v
)
1371 else if (v
->ob_type
->tp_as_number
!= NULL
&&
1372 v
->ob_type
->tp_as_number
->nb_nonzero
!= NULL
)
1373 res
= (*v
->ob_type
->tp_as_number
->nb_nonzero
)(v
);
1374 else if (v
->ob_type
->tp_as_mapping
!= NULL
&&
1375 v
->ob_type
->tp_as_mapping
->mp_length
!= NULL
)
1376 res
= (*v
->ob_type
->tp_as_mapping
->mp_length
)(v
);
1377 else if (v
->ob_type
->tp_as_sequence
!= NULL
&&
1378 v
->ob_type
->tp_as_sequence
->sq_length
!= NULL
)
1379 res
= (*v
->ob_type
->tp_as_sequence
->sq_length
)(v
);
1387 /* equivalent of 'not v'
1388 Return -1 if an error occurred */
1391 PyObject_Not(PyObject
*v
)
1394 res
= PyObject_IsTrue(v
);
1400 /* Coerce two numeric types to the "larger" one.
1401 Increment the reference count on each argument.
1403 -1 if an error occurred;
1404 0 if the coercion succeeded (and then the reference counts are increased);
1405 1 if no coercion is possible (and no error is raised).
1408 PyNumber_CoerceEx(PyObject
**pv
, PyObject
**pw
)
1410 register PyObject
*v
= *pv
;
1411 register PyObject
*w
= *pw
;
1414 if (v
->ob_type
== w
->ob_type
&& !PyInstance_Check(v
)) {
1419 if (v
->ob_type
->tp_as_number
&& v
->ob_type
->tp_as_number
->nb_coerce
) {
1420 res
= (*v
->ob_type
->tp_as_number
->nb_coerce
)(pv
, pw
);
1424 if (w
->ob_type
->tp_as_number
&& w
->ob_type
->tp_as_number
->nb_coerce
) {
1425 res
= (*w
->ob_type
->tp_as_number
->nb_coerce
)(pw
, pv
);
1432 /* Coerce two numeric types to the "larger" one.
1433 Increment the reference count on each argument.
1434 Return -1 and raise an exception if no coercion is possible
1435 (and then no reference count is incremented).
1438 PyNumber_Coerce(PyObject
**pv
, PyObject
**pw
)
1440 int err
= PyNumber_CoerceEx(pv
, pw
);
1443 PyErr_SetString(PyExc_TypeError
, "number coercion failed");
1448 /* Test whether an object can be called */
1451 PyCallable_Check(PyObject
*x
)
1455 if (PyInstance_Check(x
)) {
1456 PyObject
*call
= PyObject_GetAttrString(x
, "__call__");
1461 /* Could test recursively but don't, for fear of endless
1462 recursion if some joker sets self.__call__ = self */
1467 return x
->ob_type
->tp_call
!= NULL
;
1471 /* Helper for PyObject_Dir.
1472 Merge the __dict__ of aclass into dict, and recursively also all
1473 the __dict__s of aclass's base classes. The order of merging isn't
1474 defined, as it's expected that only the final set of dict keys is
1476 Return 0 on success, -1 on error.
1480 merge_class_dict(PyObject
* dict
, PyObject
* aclass
)
1482 PyObject
*classdict
;
1485 assert(PyDict_Check(dict
));
1488 /* Merge in the type's dict (if any). */
1489 classdict
= PyObject_GetAttrString(aclass
, "__dict__");
1490 if (classdict
== NULL
)
1493 int status
= PyDict_Update(dict
, classdict
);
1494 Py_DECREF(classdict
);
1499 /* Recursively merge in the base types' (if any) dicts. */
1500 bases
= PyObject_GetAttrString(aclass
, "__bases__");
1505 assert(PyTuple_Check(bases
));
1506 n
= PyTuple_GET_SIZE(bases
);
1507 for (i
= 0; i
< n
; i
++) {
1508 PyObject
*base
= PyTuple_GET_ITEM(bases
, i
);
1509 if (merge_class_dict(dict
, base
) < 0) {
1519 /* Helper for PyObject_Dir.
1520 If obj has an attr named attrname that's a list, merge its string
1521 elements into keys of dict.
1522 Return 0 on success, -1 on error. Errors due to not finding the attr,
1523 or the attr not being a list, are suppressed.
1527 merge_list_attr(PyObject
* dict
, PyObject
* obj
, char *attrname
)
1532 assert(PyDict_Check(dict
));
1536 list
= PyObject_GetAttrString(obj
, attrname
);
1540 else if (PyList_Check(list
)) {
1542 for (i
= 0; i
< PyList_GET_SIZE(list
); ++i
) {
1543 PyObject
*item
= PyList_GET_ITEM(list
, i
);
1544 if (PyString_Check(item
)) {
1545 result
= PyDict_SetItem(dict
, item
, Py_None
);
1556 /* Like __builtin__.dir(arg). See bltinmodule.c's builtin_dir for the
1557 docstring, which should be kept in synch with this implementation. */
1560 PyObject_Dir(PyObject
*arg
)
1562 /* Set exactly one of these non-NULL before the end. */
1563 PyObject
*result
= NULL
; /* result list */
1564 PyObject
*masterdict
= NULL
; /* result is masterdict.keys() */
1566 /* If NULL arg, return the locals. */
1568 PyObject
*locals
= PyEval_GetLocals();
1571 result
= PyDict_Keys(locals
);
1576 /* Elif this is some form of module, we only want its dict. */
1577 else if (PyModule_Check(arg
)) {
1578 masterdict
= PyObject_GetAttrString(arg
, "__dict__");
1579 if (masterdict
== NULL
)
1581 if (!PyDict_Check(masterdict
)) {
1582 PyErr_SetString(PyExc_TypeError
,
1583 "module.__dict__ is not a dictionary");
1588 /* Elif some form of type or class, grab its dict and its bases.
1589 We deliberately don't suck up its __class__, as methods belonging
1590 to the metaclass would probably be more confusing than helpful. */
1591 else if (PyType_Check(arg
) || PyClass_Check(arg
)) {
1592 masterdict
= PyDict_New();
1593 if (masterdict
== NULL
)
1595 if (merge_class_dict(masterdict
, arg
) < 0)
1599 /* Else look at its dict, and the attrs reachable from its class. */
1602 /* Create a dict to start with. CAUTION: Not everything
1603 responding to __dict__ returns a dict! */
1604 masterdict
= PyObject_GetAttrString(arg
, "__dict__");
1605 if (masterdict
== NULL
) {
1607 masterdict
= PyDict_New();
1609 else if (!PyDict_Check(masterdict
)) {
1610 Py_DECREF(masterdict
);
1611 masterdict
= PyDict_New();
1614 /* The object may have returned a reference to its
1615 dict, so copy it to avoid mutating it. */
1616 PyObject
*temp
= PyDict_Copy(masterdict
);
1617 Py_DECREF(masterdict
);
1620 if (masterdict
== NULL
)
1623 /* Merge in __members__ and __methods__ (if any).
1624 XXX Would like this to go away someday; for now, it's
1625 XXX needed to get at im_self etc of method objects. */
1626 if (merge_list_attr(masterdict
, arg
, "__members__") < 0)
1628 if (merge_list_attr(masterdict
, arg
, "__methods__") < 0)
1631 /* Merge in attrs reachable from its class.
1632 CAUTION: Not all objects have a __class__ attr. */
1633 itsclass
= PyObject_GetAttrString(arg
, "__class__");
1634 if (itsclass
== NULL
)
1637 int status
= merge_class_dict(masterdict
, itsclass
);
1638 Py_DECREF(itsclass
);
1644 assert((result
== NULL
) ^ (masterdict
== NULL
));
1645 if (masterdict
!= NULL
) {
1646 /* The result comes from its keys. */
1647 assert(result
== NULL
);
1648 result
= PyDict_Keys(masterdict
);
1654 if (PyList_Sort(result
) != 0)
1664 Py_XDECREF(masterdict
);
1669 NoObject is usable as a non-NULL undefined value, used by the macro None.
1670 There is (and should be!) no way to create other objects of this type,
1671 so there is exactly one (which is indestructible, by the way).
1672 (XXX This type and the type of NotImplemented below should be unified.)
1677 none_repr(PyObject
*op
)
1679 return PyString_FromString("None");
1684 none_dealloc(PyObject
* ignore
)
1686 /* This should never get called, but we also don't want to SEGV if
1687 * we accidently decref None out of existance.
1693 static PyTypeObject PyNone_Type
= {
1694 PyObject_HEAD_INIT(&PyType_Type
)
1699 (destructor
)none_dealloc
, /*tp_dealloc*/ /*never called*/
1704 (reprfunc
)none_repr
, /*tp_repr*/
1706 0, /*tp_as_sequence*/
1707 0, /*tp_as_mapping*/
1711 PyObject _Py_NoneStruct
= {
1712 PyObject_HEAD_INIT(&PyNone_Type
)
1715 /* NotImplemented is an object that can be used to signal that an
1716 operation is not implemented for the given type combination. */
1719 NotImplemented_repr(PyObject
*op
)
1721 return PyString_FromString("NotImplemented");
1724 static PyTypeObject PyNotImplemented_Type
= {
1725 PyObject_HEAD_INIT(&PyType_Type
)
1727 "NotImplementedType",
1730 (destructor
)none_dealloc
, /*tp_dealloc*/ /*never called*/
1735 (reprfunc
)NotImplemented_repr
, /*tp_repr*/
1737 0, /*tp_as_sequence*/
1738 0, /*tp_as_mapping*/
1742 PyObject _Py_NotImplementedStruct
= {
1743 PyObject_HEAD_INIT(&PyNotImplemented_Type
)
1747 _Py_ReadyTypes(void)
1749 if (PyType_Ready(&PyType_Type
) < 0)
1750 Py_FatalError("Can't initialize 'type'");
1752 if (PyType_Ready(&PyList_Type
) < 0)
1753 Py_FatalError("Can't initialize 'list'");
1755 if (PyType_Ready(&PyNone_Type
) < 0)
1756 Py_FatalError("Can't initialize type(None)");
1758 if (PyType_Ready(&PyNotImplemented_Type
) < 0)
1759 Py_FatalError("Can't initialize type(NotImplemented)");
1763 #ifdef Py_TRACE_REFS
1765 static PyObject refchain
= {&refchain
, &refchain
};
1768 _Py_ResetReferences(void)
1770 refchain
._ob_prev
= refchain
._ob_next
= &refchain
;
1775 _Py_NewReference(PyObject
*op
)
1779 op
->_ob_next
= refchain
._ob_next
;
1780 op
->_ob_prev
= &refchain
;
1781 refchain
._ob_next
->_ob_prev
= op
;
1782 refchain
._ob_next
= op
;
1784 inc_count(op
->ob_type
);
1789 _Py_ForgetReference(register PyObject
*op
)
1791 #ifdef SLOW_UNREF_CHECK
1792 register PyObject
*p
;
1794 if (op
->ob_refcnt
< 0)
1795 Py_FatalError("UNREF negative refcnt");
1796 if (op
== &refchain
||
1797 op
->_ob_prev
->_ob_next
!= op
|| op
->_ob_next
->_ob_prev
!= op
)
1798 Py_FatalError("UNREF invalid object");
1799 #ifdef SLOW_UNREF_CHECK
1800 for (p
= refchain
._ob_next
; p
!= &refchain
; p
= p
->_ob_next
) {
1804 if (p
== &refchain
) /* Not found */
1805 Py_FatalError("UNREF unknown object");
1807 op
->_ob_next
->_ob_prev
= op
->_ob_prev
;
1808 op
->_ob_prev
->_ob_next
= op
->_ob_next
;
1809 op
->_ob_next
= op
->_ob_prev
= NULL
;
1811 op
->ob_type
->tp_frees
++;
1816 _Py_Dealloc(PyObject
*op
)
1818 destructor dealloc
= op
->ob_type
->tp_dealloc
;
1819 _Py_ForgetReference(op
);
1824 _Py_PrintReferences(FILE *fp
)
1827 fprintf(fp
, "Remaining objects:\n");
1828 for (op
= refchain
._ob_next
; op
!= &refchain
; op
= op
->_ob_next
) {
1829 fprintf(fp
, "[%d] ", op
->ob_refcnt
);
1830 if (PyObject_Print(op
, fp
, 0) != 0)
1837 _Py_GetObjects(PyObject
*self
, PyObject
*args
)
1843 if (!PyArg_ParseTuple(args
, "i|O", &n
, &t
))
1845 op
= refchain
._ob_next
;
1846 res
= PyList_New(0);
1849 for (i
= 0; (n
== 0 || i
< n
) && op
!= &refchain
; i
++) {
1850 while (op
== self
|| op
== args
|| op
== res
|| op
== t
||
1851 (t
!= NULL
&& op
->ob_type
!= (PyTypeObject
*) t
)) {
1853 if (op
== &refchain
)
1856 if (PyList_Append(res
, op
) < 0) {
1868 /* Hack to force loading of cobject.o */
1869 PyTypeObject
*_Py_cobject_hack
= &PyCObject_Type
;
1872 /* Hack to force loading of abstract.o */
1873 int (*_Py_abstract_hack
)(PyObject
*) = &PyObject_Size
;
1876 /* Python's malloc wrappers (see pymem.h) */
1879 PyMem_Malloc(size_t nbytes
)
1881 #if _PyMem_EXTRA > 0
1883 nbytes
= _PyMem_EXTRA
;
1885 return PyMem_MALLOC(nbytes
);
1889 PyMem_Realloc(void *p
, size_t nbytes
)
1891 #if _PyMem_EXTRA > 0
1893 nbytes
= _PyMem_EXTRA
;
1895 return PyMem_REALLOC(p
, nbytes
);
1905 /* Python's object malloc wrappers (see objimpl.h) */
1908 PyObject_Malloc(size_t nbytes
)
1910 return PyObject_MALLOC(nbytes
);
1914 PyObject_Realloc(void *p
, size_t nbytes
)
1916 return PyObject_REALLOC(p
, nbytes
);
1920 PyObject_Free(void *p
)
1926 /* These methods are used to control infinite recursion in repr, str, print,
1927 etc. Container objects that may recursively contain themselves,
1928 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
1929 Py_ReprLeave() to avoid infinite recursion.
1931 Py_ReprEnter() returns 0 the first time it is called for a particular
1932 object and 1 every time thereafter. It returns -1 if an exception
1933 occurred. Py_ReprLeave() has no return value.
1935 See dictobject.c and listobject.c for examples of use.
1938 #define KEY "Py_Repr"
1941 Py_ReprEnter(PyObject
*obj
)
1947 dict
= PyThreadState_GetDict();
1950 list
= PyDict_GetItemString(dict
, KEY
);
1952 list
= PyList_New(0);
1955 if (PyDict_SetItemString(dict
, KEY
, list
) < 0)
1959 i
= PyList_GET_SIZE(list
);
1961 if (PyList_GET_ITEM(list
, i
) == obj
)
1964 PyList_Append(list
, obj
);
1969 Py_ReprLeave(PyObject
*obj
)
1975 dict
= PyThreadState_GetDict();
1978 list
= PyDict_GetItemString(dict
, KEY
);
1979 if (list
== NULL
|| !PyList_Check(list
))
1981 i
= PyList_GET_SIZE(list
);
1982 /* Count backwards because we always expect obj to be list[-1] */
1984 if (PyList_GET_ITEM(list
, i
) == obj
) {
1985 PyList_SetSlice(list
, i
, i
+ 1, NULL
);
1994 non-recursively destroy nested objects
1997 everything is now done in a macro.
2000 modified to use functions, after Tim Peter's suggestion.
2003 modified to restore a possible error.
2006 added better safe than sorry check for threadstate
2009 complete rewrite. We now build a chain via ob_type
2010 and save the limited number of types in ob_refcnt.
2011 This is perfect since we don't need any memory.
2012 A patch for free-threading would need just a lock.
2015 #define Py_TRASHCAN_TUPLE 1
2016 #define Py_TRASHCAN_LIST 2
2017 #define Py_TRASHCAN_DICT 3
2018 #define Py_TRASHCAN_FRAME 4
2019 #define Py_TRASHCAN_TRACEBACK 5
2020 /* extend here if other objects want protection */
2022 int _PyTrash_delete_nesting
= 0;
2024 PyObject
* _PyTrash_delete_later
= NULL
;
2027 _PyTrash_deposit_object(PyObject
*op
)
2031 if (PyTuple_Check(op
))
2032 typecode
= Py_TRASHCAN_TUPLE
;
2033 else if (PyList_Check(op
))
2034 typecode
= Py_TRASHCAN_LIST
;
2035 else if (PyDict_Check(op
))
2036 typecode
= Py_TRASHCAN_DICT
;
2037 else if (PyFrame_Check(op
))
2038 typecode
= Py_TRASHCAN_FRAME
;
2039 else if (PyTraceBack_Check(op
))
2040 typecode
= Py_TRASHCAN_TRACEBACK
;
2041 else /* We have a bug here -- those are the only types in GC */ {
2042 Py_FatalError("Type not supported in GC -- internal bug");
2043 return; /* pacify compiler -- execution never here */
2045 op
->ob_refcnt
= typecode
;
2047 op
->ob_type
= (PyTypeObject
*)_PyTrash_delete_later
;
2048 _PyTrash_delete_later
= op
;
2052 _PyTrash_destroy_chain(void)
2054 while (_PyTrash_delete_later
) {
2055 PyObject
*shredder
= _PyTrash_delete_later
;
2056 _PyTrash_delete_later
= (PyObject
*) shredder
->ob_type
;
2058 switch (shredder
->ob_refcnt
) {
2059 case Py_TRASHCAN_TUPLE
:
2060 shredder
->ob_type
= &PyTuple_Type
;
2062 case Py_TRASHCAN_LIST
:
2063 shredder
->ob_type
= &PyList_Type
;
2065 case Py_TRASHCAN_DICT
:
2066 shredder
->ob_type
= &PyDict_Type
;
2068 case Py_TRASHCAN_FRAME
:
2069 shredder
->ob_type
= &PyFrame_Type
;
2071 case Py_TRASHCAN_TRACEBACK
:
2072 shredder
->ob_type
= &PyTraceBack_Type
;
2075 _Py_NewReference(shredder
);
2077 ++_PyTrash_delete_nesting
;
2078 Py_DECREF(shredder
);
2079 --_PyTrash_delete_nesting
;
2083 #ifdef WITH_PYMALLOC
2084 #include "obmalloc.c"