[Python-Dev] C new-style classes and GC
Jeremy Hylton
jeremy@zope.com
16 May 2003 14:12:33 -0400
On Fri, 2003-05-16 at 13:37, Guido van Rossum wrote:
> > 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.
>
> But calling tp_free is more correct. This allows a subclass to change
> the memory allocation policy. (This is also important if a base class
> is not collectible but a subclass is -- then it's essential that the
> base class dealloc handler calls tp_free.)
There are dozens of objects in Python that do not call tp_free. For
example, range object's have a tp_dealloc that is set to PyObject_Del().
Should we change those? Or should we say that it's okay to call
PyObject_Del() and PyObject_GC_Del() from objects that are not intended
to be subclassed?
(patch pending :-)
Jeremy