[Cython] Hooking tp_clear()

Stefan Behnel stefan_ml at behnel.de
Wed Jan 9 16:12:09 EST 2019


Jeroen Demeyer schrieb am 09.01.19 um 15:43:
> (reviving this thread after I thought about it some more...)
> 
> As I mentioned in the original post, I want something like __dealloc__ but
> with access to a particular cdef attribute (representing a Python object).
> Since Python attributes of cdef classes may have been cleared by tp_clear,
> they cannot be accessed in __dealloc__.
> 
> I suggested hooking tp_clear to do the cleanup but maybe there is a simpler
> solution: do that cleanup in __dealloc__ anyway but make sure that this
> specific attribute is not cleared in tp_clear.
> 
> So really I want something like @cython.no_gc_clear (just found out about
> that now) but for a specific attribute only.
> 
> There could be new syntax like
> 
> cdef class X:
>     cdef no_gc_clear object attr
> 
> This is a better solution than a blanket @cython.no_gc_clear: in my code,
> I'm sure that there are no reference cycles involving only that attribute,
> but there may be reference cycles involving other attributes.
> 
> This should be fairly easy to implement. Any comments before I submit a PR?

I would like to avoid adding Cython specific features that are better
served by the general finalisation mechanism of PEP-442.

Stefan


More information about the cython-devel mailing list