Hi, I'm trying to understand what is the best/safest/recommended way to implement tp_dealloc on a heap type created by PyType_FromSpec.
The official docs don't say much on the topic. The only note I could find is this:
Finally, if the type is heap allocated (Py_TPFLAGS_HEAPTYPE), the deallocator should decrement the reference count for its type object after calling the type deallocator.
My doubts came after reading the source code. If I don't specify a tp_dealloc, its default value depends on heap vs static types:
for static types, the default is object_dealloc, which simply does a tp->tp_free(self)
for heap types created by PyType_FromSpecWithBases, the default value[1] is subtype_dealloc, which seems to do a lot of complex logic which I don't fully understand. [1] https://github.com/python/cpython/blob/main/Objects/typeobject.c#L3553-L3558
This means that if I create a heap type with a custom tp_dealloc, all the logic implemented by subtype_dealloc will not be executed and that my type will probably behave subtly differently. I also found BPO 26979 [2] where Christian Tismer claims that "The default of PyType_FromSpec for tp_dealloc is wrong!", but it seems that nothing has been done for that. [2] https://bugs.python.org/issue26979
Another interesting data point is that PyType_FromSpec+tp_dealloc does not seem to be used a lot in the wild. I tried to grep for Py_tp_dealloc in the top4000 PyPI packages[3] and I found only a match, in Cython-generated code [4]; but it's code which is behind an "#if CYTHON_COMPILING_IN_LIMITED_API", which makes me to suspect which is not actually used a lot in practice.
[3] https://github.com/hpyproject/top4000-pypi-packages [4] https://github.com/hpyproject/top4000-pypi-packages/blob/0cd919943a007f95f4b...
So, back to my original problem:
- Is the default value of tp_dealloc actually correct?
- Is it actually possible to write a custom tp_dealloc which behaves correctly?
- If (2) is true, what is the simplest way to do it?
Cheers, Antonio
participants (1)
-
Antonio Cuni