[Python-Dev] Avoiding reference leaks in heap types with custom tp_dealloc

Petr Viktorin encukou at gmail.com
Mon Jun 1 17:52:13 CEST 2015

On Mon, Jun 1, 2015 at 5:33 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> On Mon, 1 Jun 2015 16:38:35 +0200
> Petr Viktorin <encukou at gmail.com> wrote:
>> The nice way out would be taking advantage of PEP 442: xxlimited.Xxo
>> can ditch tp_dealloc in favor of tp_traverse and tp_finalize (the
>> former of which it needs anyway to behave correctly). Unfortunately,
>> tp_finalize is not available in the stable ABI (issue24345). I think
>> it should be added; is it too late for 3.5?
> Well, but.... the stable ABI is supposed to be a subset of the API
> that's safe to program against, regardless of the Python version (at
> least from the point where the stable ABI was introduced).

That part's not true. From the PEP:
During evolution of Python, new ABI functions will be added.
Applications using them will then have a requirement on a minimum
version of Python; this PEP provides no mechanism for such
applications to fall back when the Python library is too old.

> What happens
> if you define a Py_tp_finalize and run your C extension type on a
> pre-3.5 version? Do you get an error at definition time? A resource
> leak? A crash?

Looking at PyType_FromSpecWithBases code, you should get
RuntimeError("invalid slot offset") in this case.

More information about the Python-Dev mailing list