diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h index fa789611133a6f..4caa323a15a420 100644 --- a/Include/internal/pycore_object.h +++ b/Include/internal/pycore_object.h @@ -266,8 +266,9 @@ _PyObject_Init(PyObject *op, PyTypeObject *typeobj) { assert(op != NULL); Py_SET_TYPE(op, typeobj); - assert(_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE) || _Py_IsImmortal(typeobj)); - Py_INCREF(typeobj); + if (_PyType_HasFeature(typeobj, Py_TPFLAGS_HEAPTYPE) || _Py_IsImmortal(typeobj)) { + Py_INCREF(typeobj); + } _Py_NewReference(op); } diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-07-10-03-13-19.gh-issue-121528._Bo7rw.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-07-10-03-13-19.gh-issue-121528._Bo7rw.rst new file mode 100644 index 00000000000000..f571f5961b2805 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-07-10-03-13-19.gh-issue-121528._Bo7rw.rst @@ -0,0 +1,2 @@ +Fix crash when loading extension modules that are compiled with an older +version of Python.