[Python-Dev] another Py_TPFLAGS_HEAPTYPE question

Benjamin Peterson benjamin at python.org
Mon Aug 17 03:19:37 CEST 2009

2009/8/16 Joshua Haberman <joshua at reverberate.org>:
> 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)

[Note that this is really starting to get off topic for python-dev.]

Why do you need to set Py_TPFLAGS_HEAPTYPE on your C type? Is a normal
static type not sufficient? The easiest way to create heaptypes is to
simply call PyType_Type.


More information about the Python-Dev mailing list