Reputation: 398
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_finalize
is 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:
tp_dealloc
will inherit the deallocator from the base python type (PyBaseObject_Type
), which is object_dealloc
.
object_dealloc
is extremely simple, it just calls the tp_free
of the given object's type.tp_finalize
will never be called for these objects.PyType_FromSpec
and similar will inherit by default the subtype_dealloc
deallocator.
subtype_dealloc
is much more complex and will call PyObject_CallFinalizerFromDealloc
.Assuming that I am understanding the current behavior of CPython correctly:
PyBaseObject_Type
deallocator does not call tp_finalize
?subtype_dealloc
as a generic dealloc callback for C-defined static types?Upvotes: 3
Views: 73
Reputation: 398
Ended us asking the question on the Python discussion channel.
Please follow above link for details. The TL;DR: would be that this could be an overlooked aspect when PEP 442 was implemented (or later). However, trying to fix it could be more troublesome than the current issue, and using tp_dealloc
instead of tp_finalize
should be fine and safe, as long as no python code is called on self
itself.
Upvotes: 2