mont29
mont29

Reputation: 398

CPython: Usage of `tp_finalize` in C-defined static types with no custom `tp_dealloc`

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:

Questions

Assuming that I am understanding the current behavior of CPython correctly:

Upvotes: 3

Views: 73

Answers (1)

mont29
mont29

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

Related Questions