diff options
author | Eddie Elizondo <eduardo.elizondorueda@gmail.com> | 2019-03-27 11:52:18 (GMT) |
---|---|---|
committer | Petr Viktorin <encukou@gmail.com> | 2019-03-27 11:52:18 (GMT) |
commit | 364f0b0f19cc3f0d5e63f571ec9163cf41c62958 (patch) | |
tree | 977c0c418780824cca9ef9b4d920b9d423253e84 /Doc | |
parent | 1fc5bf2ff27b898e8d9460d0fbc791e83009ed71 (diff) | |
download | cpython-364f0b0f19cc3f0d5e63f571ec9163cf41c62958.zip cpython-364f0b0f19cc3f0d5e63f571ec9163cf41c62958.tar.gz cpython-364f0b0f19cc3f0d5e63f571ec9163cf41c62958.tar.bz2 |
bpo-35810: Incref heap-allocated types in PyObject_Init (GH-11661)
* Incref heap-allocated types in PyObject_Init
* Add documentation and porting notes to What's New
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/whatsnew/3.8.rst | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 6ab7991..0ffbcab 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -509,6 +509,12 @@ Build and C API Changes ``1`` for objects implementing ``__index__()``. (Contributed by Serhiy Storchaka in :issue:`36048`.) +* Heap-allocated type objects will now increase their reference count + in :c:func:`PyObject_Init` (and its parallel macro ``PyObject_INIT``) + instead of in :c:func:`PyType_GenericAlloc`. Types that modify instance + allocation or deallocation may need to be adjusted. + (Contributed by Eddie Elizondo in :issue:`35810`.) + Deprecated ========== @@ -732,6 +738,67 @@ Changes in the C API (Contributed by Inada Naoki in :issue:`36381`.) +Changes in the C API +-------------------------- + +* Instances of heap-allocated types (such as those created with + :c:func:`PyType_FromSpec`) hold a reference to their type object. + Increasing the reference count of these type objects has been moved from + :c:func:`PyType_GenericAlloc` to the more low-level functions, + :c:func:`PyObject_Init` and :c:func:`PyObject_INIT`. + This makes types created through :c:func:`PyType_FromSpec` behave like + other classes in managed code. + + Statically allocated types are not affected. + + For the vast majority of cases, there should be no side effect. + However, types that manually increase the reference count after allocating + an instance (perhaps to work around the bug) may now become immortal. + To avoid this, these classes need to call Py_DECREF on the type object + during instance deallocation. + + To correctly port these types into 3.8, please apply the following + changes: + + * Remove :c:macro:`Py_INCREF` on the type object after allocating an + instance - if any. + This may happen after calling :c:func:`PyObject_New`, + :c:func:`PyObject_NewVar`, :c:func:`PyObject_GC_New`, + :c:func:`PyObject_GC_NewVar`, or any other custom allocator that uses + :c:func:`PyObject_Init` or :c:func:`PyObject_INIT`. + + Example:: + + static foo_struct * + foo_new(PyObject *type) { + foo_struct *foo = PyObject_GC_New(foo_struct, (PyTypeObject *) type); + if (foo == NULL) + return NULL; + #if PY_VERSION_HEX < 0x03080000 + // Workaround for Python issue 35810; no longer necessary in Python 3.8 + PY_INCREF(type) + #endif + return foo; + } + + * Ensure that all custom ``tp_dealloc`` functions of heap-allocated types + decrease the type's reference count. + + Example:: + + static void + foo_dealloc(foo_struct *instance) { + PyObject *type = Py_TYPE(instance); + PyObject_GC_Del(instance); + #if PY_VERSION_HEX >= 0x03080000 + // This was not needed before Python 3.8 (Python issue 35810) + Py_DECREF(type); + #endif + } + + (Contributed by Eddie Elizondo in :issue:`35810`.) + + CPython bytecode changes ------------------------ |