[issue40989] [C API] Remove _Py_NewReference() and _Py_ForgetReference() from the public C API

Stefan Behnel report at bugs.python.org
Wed Jun 17 03:24:04 EDT 2020


Stefan Behnel <stefan_ml at behnel.de> added the comment:

I looked into the freelists a bit more and (as always) it's not quite that simple. Calling _Py_NewReference() allows keeping the "ob_type" pointer in place, whereas PyObject_INIT() requires a blank object in order to set the type and properly ref-count heap types.

I think what that means is that there are at least two different cases for freelists: those that only keep the bare object memory alive (and can also support subtypes of the same size), and those that keep the (cleared) object alive, including its type. For the first, PyObject_INIT() works. For the latter, _Py_NewReference() seems the right helper function.

The advantage of keeping the object as it is is the much simpler freelist code in tp_dealloc(). All it needs to do is 1) clear the ref-counted object fields (if any) and 2) put it in the freelist. No other C-API interaction is needed. If we only want to keep the object memory, then we need C-API support in both tp_new() and tp_dealloc().

If _Py_NewReference() is removed/hidden, then it would be nice if there was a replacement for the use case of initialising an object from a freelist that already knows its type.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40989>
_______________________________________


More information about the Python-bugs-list mailing list