@@ -5768,9 +5768,31 @@ PyType_GetModuleState(PyTypeObject *type)
57685768 return _PyModule_GetState (m );
57695769}
57705770
5771+ static PyObject *
5772+ get_module_by_def_recursive (PyObject * bases , PyModuleDef * def )
5773+ {
5774+ assert (bases != NULL );
5775+ Py_ssize_t n = PyTuple_GET_SIZE (bases );
5776+ for (Py_ssize_t i = 0 ; i < n ; i ++ ) {
5777+ PyTypeObject * base = _PyType_CAST (PyTuple_GET_ITEM (bases , i ));
5778+ if (!_PyType_HasFeature (base , Py_TPFLAGS_HEAPTYPE )) {
5779+ continue ;
5780+ }
5781+ PyObject * module = ((PyHeapTypeObject * )base )-> ht_module ;
5782+ if (module && _PyModule_GetDef (module ) == def ) {
5783+ return module ;
5784+ break ;
5785+ }
5786+ module = get_module_by_def_recursive (lookup_tp_bases (base ), def );
5787+ if (module != NULL ) {
5788+ return module ;
5789+ }
5790+ }
5791+ return NULL ;
5792+ }
57715793
5772- /* Get the module of the first superclass where the module has the
5773- * given PyModuleDef.
5794+ /* Return borrowed ref to the module of the first superclass where the module
5795+ * has the given PyModuleDef.
57745796 */
57755797PyObject *
57765798PyType_GetModuleByDef (PyTypeObject * type , PyModuleDef * def )
@@ -5794,28 +5816,33 @@ PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
57945816 BEGIN_TYPE_LOCK ();
57955817
57965818 PyObject * mro = lookup_tp_mro (type );
5797- // The type must be ready
5798- assert (mro != NULL );
5799- assert (PyTuple_Check (mro ));
5800- // mro_invoke() ensures that the type MRO cannot be empty.
5801- assert (PyTuple_GET_SIZE (mro ) >= 1 );
5802- // Also, the first item in the MRO is the type itself, which
5803- // we already checked above. We skip it in the loop.
5804- assert (PyTuple_GET_ITEM (mro , 0 ) == (PyObject * )type );
5819+ if (mro == NULL ) {
5820+ // If the MRO is already cleared during garbage collection, fall
5821+ // back to walking tp_bases.
5822+ res = get_module_by_def_recursive (lookup_tp_bases (type ), def );
5823+ }
5824+ else {
5825+ assert (PyTuple_Check (mro ));
5826+ // mro_invoke() ensures that the type MRO cannot be empty.
5827+ assert (PyTuple_GET_SIZE (mro ) >= 1 );
5828+ // Also, the first item in the MRO is the type itself, which
5829+ // we already checked above. We skip it in the loop.
5830+ assert (PyTuple_GET_ITEM (mro , 0 ) == (PyObject * )type );
58055831
5806- Py_ssize_t n = PyTuple_GET_SIZE (mro );
5807- for (Py_ssize_t i = 1 ; i < n ; i ++ ) {
5808- PyObject * super = PyTuple_GET_ITEM (mro , i );
5809- if (!_PyType_HasFeature ((PyTypeObject * )super , Py_TPFLAGS_HEAPTYPE )) {
5810- // Static types in the MRO need to be skipped
5811- continue ;
5812- }
5832+ Py_ssize_t n = PyTuple_GET_SIZE (mro );
5833+ for (Py_ssize_t i = 1 ; i < n ; i ++ ) {
5834+ PyObject * super = PyTuple_GET_ITEM (mro , i );
5835+ if (!_PyType_HasFeature ((PyTypeObject * )super , Py_TPFLAGS_HEAPTYPE )) {
5836+ // Static types in the MRO need to be skipped
5837+ continue ;
5838+ }
58135839
5814- PyHeapTypeObject * ht = (PyHeapTypeObject * )super ;
5815- PyObject * module = ht -> ht_module ;
5816- if (module && _PyModule_GetDef (module ) == def ) {
5817- res = module ;
5818- break ;
5840+ PyHeapTypeObject * ht = (PyHeapTypeObject * )super ;
5841+ PyObject * module = ht -> ht_module ;
5842+ if (module && _PyModule_GetDef (module ) == def ) {
5843+ res = module ;
5844+ break ;
5845+ }
58195846 }
58205847 }
58215848 END_TYPE_LOCK ();
0 commit comments