[issue40142] Modify _PyObject_GC_TRACK() to ensure that newly tracked object is valid

STINNER Victor report at bugs.python.org
Mon Jun 28 21:43:49 EDT 2021


STINNER Victor <vstinner at python.org> added the comment:

PyType_GenericAlloc() cannot traverse the object since the object members are not initialized yet. For example, dict_traverse() can only be called when PyDict_New() completes.

A different approach would be to:

* (1) Add PyType_AllocNoTrack(), use it in built-in types, and call _PyObject_GC_TRACK() on the instance once it is fully initialized.
* (2) Modify PyType_GenericAlloc() to use a new variant of _PyObject_GC_TRACK() which will not traverse the instance.
* (3) Modify _PyObject_GC_TRACK() to traverse the instance.

In short, PyType_GenericAlloc() cannot be modified for backward compatibility.

Moreover, _PyObject_GC_TRACK() should only be used inside Python internals, since it's part the internal C API.

----------

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


More information about the Python-bugs-list mailing list