Search code examples
pythonmemory-managementcython

Does Cython guarantee __dealloc__ order in __mro__?


Does Cython respect the __mro__ in case of __dealloc__?

For example, in the case of inheritence:

cdef class A:
  def __dealloc__(self):
      # Deallocate

cdef class B(A):
  def __dealloc__(self):
      # Deallocate some more

Is B.__dealloc__ guaranteed to be called before A.__dealloc__?

I wish to create a threadsafe child class that takes a lock during deallocation, and just calls the parent __dealloc__ within that lock.


Solution

  • The documentation doesn't specify this explicitly.

    However, it does say that __dealloc__() is the inverse of __cinit__(), and that documentation says

    any existing __cinit__() methods in the base type hierarchy are automatically called before your __cinit__() method.

    So if this is truly the inverse, I would expect them to be called in the reverse order.