8000 gh-117182: Allow modules to modify their own __class__ when lazily lo… · python/cpython@3778005 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3778005

Browse files
committed
gh-117182: Allow modules to modify their own __class__ when lazily loaded
1 parent 4fb1a22 commit 3778005

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Lib/importlib/util.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,17 @@ def __getattribute__(self, attr):
178178
# Only the first thread to get the lock should trigger the load
179179
# and reset the module's class. The rest can now getattr().
180180
if object.__getattribute__(self, '__class__') is _LazyModule:
181+
__class__ = loader_state['__class__']
182+
181183
# Reentrant calls from the same thread must be allowed to proceed without
182184
# triggering the load again.
183185
# exec_module() and self-referential imports are the primary ways this can
184186
# happen, but in any case we must return something to avoid deadlock.
185187
if loader_state['is_loading']:
186-
return object.__getattribute__(self, attr)
188+
return __class__.__getattribute__(self, attr)
187189
loader_state['is_loading'] = True
188190

189-
__dict__ = object.__getattribute__(self, '__dict__')
191+
__dict__ = __class__.__getattribute__(self, '__dict__')
190192

191193
# All module metadata must be gathered from __spec__ in order to avoid
192194
# using mutated values.
@@ -216,8 +218,10 @@ def __getattribute__(self, attr):
216218
# Update after loading since that's what would happen in an eager
217219
# loading situation.
218220
__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__)
221225

222226
return getattr(self, attr)
223227

0 commit comments

Comments
 (0)
0