Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
add custom code to tp_traverse and tp_clear? #2737
I've been looking at Cython with an eye toward replacing some hand written
I find myself wanting the ability to add custom code to tp_traverse and tp_clear
I don't think this is presently possible?
A strawman example of the situation is as follows.
Holder.iface -> PyImpl.held -> Held -> ... -> Holder
cdef extern from "foo.h": cdef cppclass Interface: ... void callback() # pure virtual cdef cppclass PyImpl(Interface): ... void callback() # calls held.callback() object held ctypedef Interface* pInterface cdef class Held: def callback(self, arg): pass cdef class Holder: cdef shared_ptr[Interface] iface # I would like to be able to do something like this: def tp_traverse(self, visit): cdef PyImpl* impl if self.iface: impl = dynamic_cast[pInterface](self.iface.get()) if impl: visit(impl.held)
Could probably be added as two new special methods that get called by
You can already do this by manually casting the type to a
As advertised, it is possible to replace tp_traverse. A working example: https://github.com/mdavidsaver/cython-c--demo
I found that the definition of
The example looks good.
Ah, yes, that makes sense. There aren't many real use cases for translation time environment tests, but this one is reasonable (although your specific use case is clearly better served by a cast). PR welcome that sets