[Cython] Hooking tp_clear()
Greg Ewing
greg.ewing at canterbury.ac.nz
Fri Sep 7 01:59:40 EDT 2018
Jeroen Demeyer wrote:
> I have a concrete use case where I want something like __dealloc__ but
> *before* Python attributes are cleared. So this really belongs in tp_clear().
Are you sure you can't do it in __del__? From what I gather,
the presence of __del__ no longer prevents cyclic garbage
collection.
> I never really understood the technical difference between
> tp_clear() and tp_dealloc(). It seems to me that these serve a very
> similar purpose: why can't the garbage collector just call tp_dealloc()?
tp_dealloc is the inverse of tp_alloc -- its purpose is to
free the memory occupied by the object. This must not be done
until there are no more references to the object.
tp_clear is used to break reference cycles. After calling it,
there may still be references to the object from other objects
in the cycle, so tp_dealloc can't be done at that point.
--
Greg
More information about the cython-devel
mailing list