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
> 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
- for heap types created by PyType_FromSpecWithBases, the default
value is subtype_dealloc, which seems to do a lot of complex logic which
I don't fully understand.
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  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.
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 and I found only a match, in Cython-generated code
; 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.
So, back to my original problem:
1. Is the default value of tp_dealloc actually correct?
2. Is it actually possible to write a custom tp_dealloc which behaves
3. If (2) is true, what is the simplest way to do it?