@@ -5981,6 +5981,29 @@ PyType_GetModuleState(PyTypeObject *type)
59815981 return _PyModule_GetState (m );
59825982}
59835983
5984+ static PyObject *
5985+ get_module_by_token_recursive (PyObject * bases , const void * token )
5986+ {
5987+ assert (bases != NULL );
5988+ Py_ssize_t n = PyTuple_GET_SIZE (bases );
5989+ for (Py_ssize_t i = 0 ; i < n ; i ++ ) {
5990+ PyTypeObject * base = _PyType_CAST (PyTuple_GET_ITEM (bases , i ));
5991+ if (!_PyType_HasFeature (base , Py_TPFLAGS_HEAPTYPE )) {
5992+ continue ;
5993+ }
5994+ PyObject * module = ((PyHeapTypeObject * )base )-> ht_module ;
5995+ if (module && _PyModule_GetToken (module ) == token ) {
5996+ return module ;
5997+ break ;
5998+ }
5999+ module = get_module_by_token_recursive (lookup_tp_bases (base ), token );
6000+ if (module != NULL ) {
6001+ return module ;
6002+ }
6003+ }
6004+ return NULL ;
6005+ }
6006+
59846007/* Return borrowed ref to the module of the first superclass where the module
59856008 * has the given token.
59866009 */
@@ -6006,28 +6029,33 @@ PyType_GetModuleByToken_DuringGC(PyTypeObject *type, const void *token)
60066029 BEGIN_TYPE_LOCK ();
60076030
60086031 PyObject * mro = lookup_tp_mro (type );
6009- // The type must be ready
6010- assert (mro != NULL );
6011- assert (PyTuple_Check (mro ));
6012- // mro_invoke() ensures that the type MRO cannot be empty.
6013- assert (PyTuple_GET_SIZE (mro ) >= 1 );
6014- // Also, the first item in the MRO is the type itself, which
6015- // we already checked above. We skip it in the loop.
6016- assert (PyTuple_GET_ITEM (mro , 0 ) == (PyObject * )type );
6032+ if (mro == NULL ) {
6033+ // If the MRO is already cleared during garbage collection, fall
6034+ // back to walking tp_bases.
6035+ res = get_module_by_token_recursive (lookup_tp_bases (type ), token );
6036+ }
6037+ else {
6038+ assert (PyTuple_Check (mro ));
6039+ // mro_invoke() ensures that the type MRO cannot be empty.
6040+ assert (PyTuple_GET_SIZE (mro ) >= 1 );
6041+ // Also, the first item in the MRO is the type itself, which
6042+ // we already checked above. We skip it in the loop.
6043+ assert (PyTuple_GET_ITEM (mro , 0 ) == (PyObject * )type );
60176044
6018- Py_ssize_t n = PyTuple_GET_SIZE (mro );
6019- for (Py_ssize_t i = 1 ; i < n ; i ++ ) {
6020- PyObject * super = PyTuple_GET_ITEM (mro , i );
6021- if (!_PyType_HasFeature ((PyTypeObject * )super , Py_TPFLAGS_HEAPTYPE )) {
6022- // Static types in the MRO need to be skipped
6023- continue ;
6024- }
6045+ Py_ssize_t n = PyTuple_GET_SIZE (mro );
6046+ for (Py_ssize_t i = 1 ; i < n ; i ++ ) {
6047+ PyObject * super = PyTuple_GET_ITEM (mro , i );
6048+ if (!_PyType_HasFeature ((PyTypeObject * )super , Py_TPFLAGS_HEAPTYPE )) {
6049+ // Static types in the MRO need to be skipped
6050+ continue ;
6051+ }
60256052
6026- PyHeapTypeObject * ht = (PyHeapTypeObject * )super ;
6027- PyObject * module = ht -> ht_module ;
6028- if (module && _PyModule_GetToken (module ) == token ) {
6029- res = module ;
6030- break ;
6053+ PyHeapTypeObject * ht = (PyHeapTypeObject * )super ;
6054+ PyObject * module = ht -> ht_module ;
6055+ if (module && _PyModule_GetToken (module ) == token ) {
6056+ res = module ;
6057+ break ;
6058+ }
60316059 }
60326060 }
60336061 END_TYPE_LOCK ();
0 commit comments