[Python-Dev] another Py_TPFLAGS_HEAPTYPE question

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 Py_DECREF() for the above Py_INCREF() live?  I
>> expected to find this code snippet somewhere, but couldn't:
>>
>>   if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
>>     Py_DECREF(type);
>
> For a regular heaptype, it's in subtype_dealloc:
>
>                /* Can't reference self beyond this point */
>                Py_DECREF(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:

- implement tp_alloc and tp_dealloc() to INCREF and DECREF the type.
- not set Py_TPFLAGS_HEAPTYPE.
- set Py_TPFLAGS_HAVE_GC (because instances of my obj can create cycles)

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