@@ -178,15 +178,17 @@ def __getattribute__(self, attr):
178
178
# Only the first thread to get the lock should trigger the load
179
179
# and reset the module's class. The rest can now getattr().
180
180
if object .__getattribute__ (self , '__class__' ) is _LazyModule :
181
+ __class__ = loader_state ['__class__' ]
182
+
181
183
# Reentrant calls from the same thread must be allowed to proceed without
182
184
# triggering the load again.
183
185
# exec_module() and self-referential imports are the primary ways this can
184
186
# happen, but in any case we must return something to avoid deadlock.
185
187
if loader_state ['is_loading' ]:
186
- return object .__getattribute__ (self , attr )
188
+ return __class__ .__getattribute__ (self , attr )
187
189
loader_state ['is_loading' ] = True
188
190
189
- __dict__ = object .__getattribute__ (self , '__dict__' )
191
+ __dict__ = __class__ .__getattribute__ (self , '__dict__' )
190
192
191
193
# All module metadata must be gathered from __spec__ in order to avoid
192
194
# using mutated values.
@@ -216,8 +218,10 @@ def __getattribute__(self, attr):
216
218
# Update after loading since that's what would happen in an eager
217
219
# loading situation.
218
220
__dict__ .update (attrs_updated )
219
- # Finally, stop triggering this method.
220
- self .__class__ = types .ModuleType
221
+ # Finally, stop triggering this method, if the module did not
222
+ # already update its own __class__.
223
+ if isinstance (self , _LazyModule ):
224
+ object .__setattr__ (self , '__class__' , __class__ )
221
225
222
226
return getattr (self , attr )
223
227
0 commit comments