cpyext: PyObject_Init reference counting issue
Dear PyPy team (& @mattip),
I noticed that the cpyext
implementation of the function PyObject_Init
lacks an important behavior of recent Python versions. Python 3.8-3.12 (and potentially older versions as well) increase the reference count of the type object when allocating an instance of a heap type. Excerpt from CPython:
if (PyType_GetFlags(tp) & Py_TPFLAGS_HEAPTYPE) {
Py_INCREF(tp);
}
This is clearly missing in PyPy's much shorter version of this function that just sets the reference count, type object pointer, and link field.
I noticed this while developing an extension that follows the recommended approach of decreasing the reference count of the type object in tp_dealloc
, which then crashed PyPy because of an underflow.
Thanks!