[Python-Dev] another Py_TPFLAGS_HEAPTYPE question (original) (raw)

Joshua Haberman joshua at reverberate.org
Mon Aug 17 01:01:58 CEST 2009


On Sun, Aug 16, 2009 at 3:37 PM, "Martin v. Löwis"<martin at v.loewis.de> wrote:

So where does the PyDECREF() for the above PyINCREF() live?  I expected to find this code snippet somewhere, but couldn't:

if (type->tpflags & PyTPFLAGSHEAPTYPE) PyDECREF(type); For a regular heaptype, it's in subtypedealloc:  /* Can't reference self beyond this point */  PyDECREF(type);

Thanks for the pointer. I noticed that subtype_dealloc is only called for types that are allocated using type_new(). Does this mean that it is not safe to create types in C using just PyType_Ready() and set Py_TPFLAGS_HEAPTYPE on them? The documentation is not clear on this point.

Here is what I would like to do when I create my types dynamically:

Does this seem safe? I notice that subtype_dealloc() does some funky GC/trashcan stuff. Is it safe for me not to call subtype_dealloc? Can I safely implement my tp_dealloc function like this?

void my_tp_dealloc(PyObject *obj) { obj->ob_type->tp_free(obj); Py_DECREF(obj->ob_type); }

Thanks, Josh



More information about the Python-Dev mailing list