diff --git a/Lib/test/test_capi/test_type.py b/Lib/test/test_capi/test_type.py index 9aca0d4741dd60..8ed000963c796d 100644 --- a/Lib/test/test_capi/test_type.py +++ b/Lib/test/test_capi/test_type.py @@ -1,3 +1,4 @@ +from test import support from test.support import import_helper, Py_GIL_DISABLED, refleak_helper import unittest @@ -228,6 +229,19 @@ class H2(int): pass with self.assertRaises(TypeError): _testcapi.pytype_getmodulebytoken(H2, token) + def test_get_module_by_token_in_dealloc(self): + # gh-160840: PyType_GetModuleByToken() called from tp_dealloc + # of a subclass instance that's in a reference cycle with its class + # (and with itself, so it outlives the class's tp_clear) + class C(_testlimitedcapi.GetModuleByTokenInDealloc): + pass + C.inst = C() + C.inst.self = C.inst + with support.catch_unraisable_exception() as cm: + del C + support.gc_collect() + self.assertIsNone(cm.unraisable) + def test_freeze(self): # test PyType_Freeze() type_freeze = _testlimitedcapi.type_freeze diff --git a/Misc/NEWS.d/next/C_API/2026-11-17-17-54-53.gh-issue-160840.IaFtRu.rst b/Misc/NEWS.d/next/C_API/2026-11-17-17-54-53.gh-issue-160840.IaFtRu.rst new file mode 100644 index 00000000000000..9d1287bd2f010c --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-11-17-17-54-53.gh-issue-160840.IaFtRu.rst @@ -0,0 +1,4 @@ +When a type's internal :term:`method resolution order` data is cleared as +the type is being torn down during garbage collection, +:c:func:`PyType_GetModuleByToken` and :c:func:`PyType_GetModuleByDef` can +now still retrieve its module. diff --git a/Modules/_testlimitedcapi.c b/Modules/_testlimitedcapi.c index 1aee3bc9bf3c1d..abbb528a5ec6fc 100644 --- a/Modules/_testlimitedcapi.c +++ b/Modules/_testlimitedcapi.c @@ -103,7 +103,7 @@ module_exec(PyObject *mod) return 0; } -static struct PyModuleDef _testlimitedcapimodule_def = { +PyModuleDef _Py_TestLimitedCAPI_ModuleDef = { PyModuleDef_HEAD_INIT, .m_name = "_testlimitedcapi", .m_size = 0, @@ -119,5 +119,5 @@ static struct PyModuleDef _testlimitedcapimodule_def = { PyMODINIT_FUNC PyInit__testlimitedcapi(void) { - return PyModuleDef_Init(&_testlimitedcapimodule_def); + return PyModuleDef_Init(&_Py_TestLimitedCAPI_ModuleDef); } diff --git a/Modules/_testlimitedcapi/parts.h b/Modules/_testlimitedcapi/parts.h index 1e9d140899cecf..b738a577f52d72 100644 --- a/Modules/_testlimitedcapi/parts.h +++ b/Modules/_testlimitedcapi/parts.h @@ -24,6 +24,9 @@ # error "Py_BUILD_CORE macro must not be defined" #endif +// Definition (and token) of the _testlimitedcapi module +extern PyModuleDef _Py_TestLimitedCAPI_ModuleDef; + int _PyTestLimitedCAPI_Init_Abstract(PyObject *module); int _PyTestLimitedCAPI_Init_ByteArray(PyObject *module); int _PyTestLimitedCAPI_Init_Bytes(PyObject *module); diff --git a/Modules/_testlimitedcapi/type.c b/Modules/_testlimitedcapi/type.c index 640468ae08dcbb..19d84cdb905ee1 100644 --- a/Modules/_testlimitedcapi/type.c +++ b/Modules/_testlimitedcapi/type.c @@ -2,7 +2,7 @@ // Do no check PyType_Check() so Python tests can pass arbitrary objects, // even if it's likely to crash. -// Need limited C API version 3.14 for PyType_Freeze() +// Need limited C API version 3.15 for PyType_Freeze() #include "pyconfig.h" // Py_GIL_DISABLED #if !defined(Py_GIL_DISABLED) && !defined(Py_LIMITED_API) # define Py_LIMITED_API 0x030e0000 @@ -160,6 +160,40 @@ type_issubtype(PyObject *module, PyObject *args) } +// Type whose tp_dealloc calls PyType_GetModuleByToken() (gh-160840) +static int +getmodulebytoken_dealloc_traverse(PyObject *op, visitproc visit, void *arg) +{ + Py_VISIT(Py_TYPE(op)); + return 0; +} + +static void +getmodulebytoken_dealloc_dealloc(PyObject *op) +{ + PyTypeObject *tp = Py_TYPE(op); + PyObject_GC_UnTrack(op); + + PyObject *mod = PyType_GetModuleByDef(tp, &_Py_TestLimitedCAPI_ModuleDef); + if (mod == NULL) { + PyErr_WriteUnraisable(NULL); + } + + freefunc tp_free = (freefunc)PyType_GetSlot(tp, Py_tp_free); + tp_free(op); + Py_DECREF(tp); +} + +static PyType_Spec getmodulebytoken_dealloc_spec = { + .name = "_testlimitedcapi.GetModuleByTokenInDealloc", + .flags = Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, + .slots = (PyType_Slot[]) { + {Py_tp_traverse, getmodulebytoken_dealloc_traverse}, + {Py_tp_dealloc, getmodulebytoken_dealloc_dealloc}, + {0}, + }, +}; + static PyMethodDef test_methods[] = { {"get_heaptype_for_name", get_heaptype_for_name, METH_NOARGS}, {"get_type_name", get_type_name, METH_O}, @@ -182,6 +216,12 @@ _PyTestLimitedCAPI_Init_Type(PyObject *m) return -1; } + PyObject *type = PyType_FromModuleAndSpec( + m, &getmodulebytoken_dealloc_spec, NULL); + if (PyModule_Add(m, "GetModuleByTokenInDealloc", type) < 0) { + return -1; + } + #define ADD_INT(macro) \ do { \ if (PyModule_AddIntConstant(m, #macro, macro) < 0) { \ diff --git a/Objects/typeobject.c b/Objects/typeobject.c index ee8c2090e6397d..980441a289f336 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -5906,6 +5906,29 @@ PyType_GetModuleState(PyTypeObject *type) return _PyModule_GetState(m); } +static PyObject * +get_module_by_token_recursive(PyObject *bases, const void *token) +{ + assert(bases != NULL); + Py_ssize_t n = PyTuple_GET_SIZE(bases); + for (Py_ssize_t i = 0; i < n; i++) { + PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, i)); + if (!_PyType_HasFeature(base, Py_TPFLAGS_HEAPTYPE)) { + continue; + } + PyObject *module = ((PyHeapTypeObject*)base)->ht_module; + if (module && _PyModule_GetToken(module) == token) { + return module; + break; + } + module = get_module_by_token_recursive(lookup_tp_bases(base), token); + if (module != NULL) { + return module; + } + } + return NULL; +} + /* Return borrowed ref to the module of the first superclass where the module * has the given token. */ @@ -5931,28 +5954,33 @@ PyType_GetModuleByToken_DuringGC(PyTypeObject *type, const void *token) BEGIN_TYPE_LOCK(); PyObject *mro = lookup_tp_mro(type); - // The type must be ready - assert(mro != NULL); - assert(PyTuple_Check(mro)); - // mro_invoke() ensures that the type MRO cannot be empty. - assert(PyTuple_GET_SIZE(mro) >= 1); - // Also, the first item in the MRO is the type itself, which - // we already checked above. We skip it in the loop. - assert(PyTuple_GET_ITEM(mro, 0) == (PyObject *)type); + if (mro == NULL) { + // If the MRO is already cleared during garbage collection, fall + // back to walking tp_bases. + res = get_module_by_token_recursive(lookup_tp_bases(type), token); + } + else { + assert(PyTuple_Check(mro)); + // mro_invoke() ensures that the type MRO cannot be empty. + assert(PyTuple_GET_SIZE(mro) >= 1); + // Also, the first item in the MRO is the type itself, which + // we already checked above. We skip it in the loop. + assert(PyTuple_GET_ITEM(mro, 0) == (PyObject *)type); - Py_ssize_t n = PyTuple_GET_SIZE(mro); - for (Py_ssize_t i = 1; i < n; i++) { - PyObject *super = PyTuple_GET_ITEM(mro, i); - if (!_PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) { - // Static types in the MRO need to be skipped - continue; - } + Py_ssize_t n = PyTuple_GET_SIZE(mro); + for (Py_ssize_t i = 1; i < n; i++) { + PyObject *super = PyTuple_GET_ITEM(mro, i); + if (!_PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) { + // Static types in the MRO need to be skipped + continue; + } - PyHeapTypeObject *ht = (PyHeapTypeObject*)super; - PyObject *module = ht->ht_module; - if (module && _PyModule_GetToken(module) == token) { - res = module; - break; + PyHeapTypeObject *ht = (PyHeapTypeObject*)super; + PyObject *module = ht->ht_module; + if (module && _PyModule_GetToken(module) == token) { + res = module; + break; + } } } END_TYPE_LOCK();