Skip to content

Commit ca9363e

Browse files
Update importlib.util._LazyModule.
Allow `importlib.util._LazyModule` to special-case its `__spec__` attribute so that the regular import machinery requesting that attribute doesn't trigger the full module load.
1 parent 2cdfb41 commit ca9363e

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

Lib/importlib/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ class _LazyModule(types.ModuleType):
171171
def __getattribute__(self, attr):
172172
"""Trigger the load of the module and return the attribute."""
173173
__spec__ = object.__getattribute__(self, '__spec__')
174+
175+
# Allow __spec__ to go through without triggering the load, since the import
176+
# machinery uses it to determine if a cached module is still initialzing
177+
# (see importlib._bootstrap._find_and_load()).
178+
if attr == "__spec__":
179+
return __spec__
180+
174181
loader_state = __spec__.loader_state
175182
with loader_state['lock']:
176183
# Only the first thread to get the lock should trigger the load

0 commit comments

Comments
 (0)