@@ -509,6 +509,12 @@ Build and C API Changes
509509 ``1 `` for objects implementing ``__index__() ``.
510510 (Contributed by Serhiy Storchaka in :issue: `36048 `.)
511511
512+ * Heap-allocated type objects will now increase their reference count
513+ in :c:func: `PyObject_Init ` (and its parallel macro ``PyObject_INIT ``)
514+ instead of in :c:func: `PyType_GenericAlloc `. Types that modify instance
515+ allocation or deallocation may need to be adjusted.
516+ (Contributed by Eddie Elizondo in :issue: `35810 `.)
517+
512518
513519Deprecated
514520==========
@@ -732,6 +738,67 @@ Changes in the C API
732738 (Contributed by Inada Naoki in :issue: `36381 `.)
733739
734740
741+ Changes in the C API
742+ --------------------------
743+
744+ * Instances of heap-allocated types (such as those created with
745+ :c:func: `PyType_FromSpec `) hold a reference to their type object.
746+ Increasing the reference count of these type objects has been moved from
747+ :c:func: `PyType_GenericAlloc ` to the more low-level functions,
748+ :c:func: `PyObject_Init ` and :c:func: `PyObject_INIT `.
749+ This makes types created through :c:func: `PyType_FromSpec ` behave like
750+ other classes in managed code.
751+
752+ Statically allocated types are not affected.
753+
754+ For the vast majority of cases, there should be no side effect.
755+ However, types that manually increase the reference count after allocating
756+ an instance (perhaps to work around the bug) may now become immortal.
757+ To avoid this, these classes need to call Py_DECREF on the type object
758+ during instance deallocation.
759+
760+ To correctly port these types into 3.8, please apply the following
761+ changes:
762+
763+ * Remove :c:macro: `Py_INCREF ` on the type object after allocating an
764+ instance - if any.
765+ This may happen after calling :c:func: `PyObject_New `,
766+ :c:func: `PyObject_NewVar `, :c:func: `PyObject_GC_New `,
767+ :c:func: `PyObject_GC_NewVar `, or any other custom allocator that uses
768+ :c:func: `PyObject_Init ` or :c:func: `PyObject_INIT `.
769+
770+ Example::
771+
772+ static foo_struct *
773+ foo_new(PyObject *type) {
774+ foo_struct *foo = PyObject_GC_New(foo_struct, (PyTypeObject *) type);
775+ if (foo == NULL)
776+ return NULL;
777+ #if PY_VERSION_HEX < 0x03080000
778+ // Workaround for Python issue 35810; no longer necessary in Python 3.8
779+ PY_INCREF(type)
780+ #endif
781+ return foo;
782+ }
783+
784+ * Ensure that all custom ``tp_dealloc `` functions of heap-allocated types
785+ decrease the type's reference count.
786+
787+ Example::
788+
789+ static void
790+ foo_dealloc(foo_struct *instance) {
791+ PyObject *type = Py_TYPE(instance);
792+ PyObject_GC_Del(instance);
793+ #if PY_VERSION_HEX >= 0x03080000
794+ // This was not needed before Python 3.8 (Python issue 35810)
795+ Py_DECREF(type);
796+ #endif
797+ }
798+
799+ (Contributed by Eddie Elizondo in :issue: `35810 `.)
800+
801+
735802CPython bytecode changes
736803------------------------
737804
0 commit comments