[Python-Dev] C new-style classes and GC

Jeremy Hylton jeremy@zope.com
16 May 2003 12:42:03 -0400


On Fri, 2003-05-16 at 11:46, Jim Fulton wrote:
> So given that we have a new style type, to add support for GC, we need
> to:
> 
> - Set the Py_TPFLAGS_HAVE_GC type flag,
> 
> - Provide implementations of tp_traverse and tp_clear, as described in
>    the section "Supporting the Cycle Collector" section of the docs.
> 
> - Call PyObject_GC_UnTrack at the beginning of the deallocator,
>    before decrefing any members.
> 
> I think that that is *all* we have to do.
> 
> In particular, since we have a new style type that inherits the
> standard allocator, we don't need to fool with PyObject_GC_New, and
> PyObject_GC_DEL, because the default tp_alloc and tp_free take care of
> that for us. Similarly, we don't need to call PyObject_GC_Track,
> because that is done by the default allocator. (Because of that, our
> traverse function has to check for null object pointers in our
> object's members.)

It depends on how the objects are used in C code.  I've upgraded a lot
of C extensions to make their types collectable recently.  In several
cases, it was necessary to change PyObject_New to PyObject_GC_New and
add a PyObject_GC_Track.  I think the docs ought to explain how to do
this.

It's not clear to me what the one right way to implement a tp_dealloc
slot is.  I've seen two common patterns in the Python source: call
obj->ob_type->tp_free or call PyObject_GC_Del.  The type object
initializes tp_free to PyObject_GC_Del, so in most cases the two
spellings are equivalent.  Calling PyObject_GC_Del feels more
straightforward to me.

This question isn't specific to GC.  Perhaps it's a question of what
tp_free is used for and when it should be called.  Pure-Python classes
and instances have tp_dealloc implementations that call tp_free.  I'm
not sure if that's a generic recommendation for all types written in C.

> Did I get this right? I intend to update the docs to reflect this
> understanding (or a corrected one, of course).

The three items you listed were sufficient for all the types I've worked
on, expecting the issues I noted above.

Jeremy