@@ -5924,6 +5924,29 @@ PyType_GetModuleState(PyTypeObject *type)
59245924 return _PyModule_GetState (m );
59255925}
59265926
5927+ static PyObject *
5928+ get_module_by_token_recursive (PyObject * bases , const void * token )
5929+ {
5930+ assert (bases != NULL );
5931+ Py_ssize_t n = PyTuple_GET_SIZE (bases );
5932+ for (Py_ssize_t i = 0 ; i < n ; i ++ ) {
5933+ PyTypeObject * base = _PyType_CAST (PyTuple_GET_ITEM (bases , i ));
5934+ if (!_PyType_HasFeature (base , Py_TPFLAGS_HEAPTYPE )) {
5935+ continue ;
5936+ }
5937+ PyObject * module = ((PyHeapTypeObject * )base )-> ht_module ;
5938+ if (module && _PyModule_GetToken (module ) == token ) {
5939+ return module ;
5940+ break ;
5941+ }
5942+ module = get_module_by_token_recursive (lookup_tp_bases (base ), token );
5943+ if (module != NULL ) {
5944+ return module ;
5945+ }
5946+ }
5947+ return NULL ;
5948+ }
5949+
59275950/* Return borrowed ref to the module of the first superclass where the module
59285951 * has the given token.
59295952 */
@@ -5949,28 +5972,33 @@ PyType_GetModuleByToken_DuringGC(PyTypeObject *type, const void *token)
59495972 BEGIN_TYPE_LOCK ();
59505973
59515974 PyObject * mro = lookup_tp_mro (type );
5952- // The type must be ready
5953- assert (mro != NULL );
5954- assert (PyTuple_Check (mro ));
5955- // mro_invoke() ensures that the type MRO cannot be empty.
5956- assert (PyTuple_GET_SIZE (mro ) >= 1 );
5957- // Also, the first item in the MRO is the type itself, which
5958- // we already checked above. We skip it in the loop.
5959- assert (PyTuple_GET_ITEM (mro , 0 ) == (PyObject * )type );
5975+ if (mro == NULL ) {
5976+ // If the MRO is already cleared during garbage collection, fall
5977+ // back to walking tp_bases.
5978+ res = get_module_by_token_recursive (lookup_tp_bases (type ), token );
5979+ }
5980+ else {
5981+ assert (PyTuple_Check (mro ));
5982+ // mro_invoke() ensures that the type MRO cannot be empty.
5983+ assert (PyTuple_GET_SIZE (mro ) >= 1 );
5984+ // Also, the first item in the MRO is the type itself, which
5985+ // we already checked above. We skip it in the loop.
5986+ assert (PyTuple_GET_ITEM (mro , 0 ) == (PyObject * )type );
59605987
5961- Py_ssize_t n = PyTuple_GET_SIZE (mro );
5962- for (Py_ssize_t i = 1 ; i < n ; i ++ ) {
5963- PyObject * super = PyTuple_GET_ITEM (mro , i );
5964- if (!_PyType_HasFeature ((PyTypeObject * )super , Py_TPFLAGS_HEAPTYPE )) {
5965- // Static types in the MRO need to be skipped
5966- continue ;
5967- }
5988+ Py_ssize_t n = PyTuple_GET_SIZE (mro );
5989+ for (Py_ssize_t i = 1 ; i < n ; i ++ ) {
5990+ PyObject * super = PyTuple_GET_ITEM (mro , i );
5991+ if (!_PyType_HasFeature ((PyTypeObject * )super , Py_TPFLAGS_HEAPTYPE )) {
5992+ // Static types in the MRO need to be skipped
5993+ continue ;
5994+ }
59685995
5969- PyHeapTypeObject * ht = (PyHeapTypeObject * )super ;
5970- PyObject * module = ht -> ht_module ;
5971- if (module && _PyModule_GetToken (module ) == token ) {
5972- res = module ;
5973- break ;
5996+ PyHeapTypeObject * ht = (PyHeapTypeObject * )super ;
5997+ PyObject * module = ht -> ht_module ;
5998+ if (module && _PyModule_GetToken (module ) == token ) {
5999+ res = module ;
6000+ break ;
6001+ }
59746002 }
59756003 }
59766004 END_TYPE_LOCK ();
0 commit comments