Skip to content

Commit 96c08c5

Browse files
committed
Improve comments around the branching
1 parent 5424d0c commit 96c08c5

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Lib/importlib/util.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,14 @@ def __getattribute__(self, attr):
174174
__spec__ = object.__getattribute__(self, '__spec__')
175175
loader_state = __spec__.loader_state
176176
with loader_state['lock']:
177+
# Only the first thread to get the lock should trigger the load
178+
# and reset the module's class. The rest can now getattr().
177179
if object.__getattribute__(self, '__class__') is _LazyModule:
178-
# exec_module() will access dunder attributes, so we use a reentrant
179-
# lock and an event to prevent infinite recursion.
180+
# The first thread comes here multiple times as it descends the
181+
# call stack. The first time, it sets is_loading and triggers
182+
# exec_module(), which will access module.__dict__, module.__name__,
183+
# and/or module.__spec__, reentering this method. These accesses
184+
# need to be allowed to proceed without triggering the load again.
180185
if loader_state['is_loading'].is_set() and attr[:2] == attr[-2:] == '__':
181186
return object.__getattribute__(self, attr)
182187
loader_state['is_loading'].set()

0 commit comments

Comments
 (0)