[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-capi-sig
Subject:    [capi-sig] tp_dealloc and heap types
From:       Antonio Cuni <anto.cuni () gmail ! com>
Date:       2021-07-30 19:23:18
Message-ID: CAHPg78xf1kZ-Z-QLrf3AwN+cwZwWWwejw3hwSDniz0+BJqVTTQ () mail ! gmail ! com
[Download RAW message or body]

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/0cd919943a007f95f4bf8510e667c \
fff5bd059fc/top1000/0314-gevent-21.1.2/src/gevent/_generated_include/CythonFunctionShared_impl_b8d451d411168c5bfd24309f8017c779ed1ebdda.h#L611


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
correctly?
3. If (2) is true, what is the simplest way to do it?

Cheers,
Antonio
_______________________________________________
capi-sig mailing list -- capi-sig@python.org
To unsubscribe send an email to capi-sig-leave@python.org
https://mail.python.org/mailman3/lists/capi-sig.python.org/
Member address: python-capi-sig@progressive-comp.com


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic