PEP 442 introduced the tp_finalize callback to Python type definitions (as a one-to-one equivalent of Pythons classes' __del__ function), and recommends using this for any non-trivial destruction.
The official API doc states:
If
tp_finalizeis set, the interpreter calls it once when finalizing an instance.
However, this does not seem to be true for C-defined static Python types that do not define a custom deallocator (either directly or inherited from their base). From my understanding:
- Static types with no custom
tp_deallocwill inherit the deallocator from the base python type (PyBaseObject_Type), which isobject_dealloc.object_deallocis extremely simple, it just calls thetp_freeof the given object's type.- Therefore,
tp_finalizewill never be called for these objects.
- Types defined on the heap using
PyType_FromSpecand similar will inherit by default thesubtype_deallocdeallocator.subtype_deallocis much more complex and will callPyObject_CallFinalizerFromDealloc.
Questions
Assuming that I am understanding the current behavior of CPython correctly:
- Is it expected that
PyBaseObject_Typedeallocator does not calltp_finalize? - If yes, what are the reasons for this exception?
- And would it make sense then to expose
subtype_deallocas a generic dealloc callback for C-defined static types?