3

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:

  • Static types with no custom 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.
    • Therefore, tp_finalize will never be called for these objects.
  • Types defined on the heap using PyType_FromSpec and similar will inherit by default the subtype_dealloc deallocator.
    • subtype_dealloc is much more complex and will call PyObject_CallFinalizerFromDealloc.

Questions

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

  • Is it expected that PyBaseObject_Type deallocator does not call tp_finalize?
  • If yes, what are the reasons for this exception?
  • And would it make sense then to expose subtype_dealloc as a generic dealloc callback for C-defined static types?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.