diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-03-28 11:15:16 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-28 11:15:16 (GMT) |
commit | 552b2646b75d079e9b31d4e800fbb2234624ed44 (patch) | |
tree | c38d67350c2c50072c6e117d497b32702d38334c /Lib/importlib/util.py | |
parent | 8d42c57789b11f838da40cdc3f129b2115368dd9 (diff) | |
download | cpython-552b2646b75d079e9b31d4e800fbb2234624ed44.zip cpython-552b2646b75d079e9b31d4e800fbb2234624ed44.tar.gz cpython-552b2646b75d079e9b31d4e800fbb2234624ed44.tar.bz2 |
[3.12] gh-117178: Recover lazy loading of self-referential modules (GH-117179) (#117319)
Co-authored-by: Chris Markiewicz <effigies@gmail.com>
Diffstat (limited to 'Lib/importlib/util.py')
-rw-r--r-- | Lib/importlib/util.py | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py index f1bd064..3743e6a 100644 --- a/Lib/importlib/util.py +++ b/Lib/importlib/util.py @@ -178,12 +178,11 @@ class _LazyModule(types.ModuleType): # Only the first thread to get the lock should trigger the load # and reset the module's class. The rest can now getattr(). if object.__getattribute__(self, '__class__') is _LazyModule: - # The first thread comes here multiple times as it descends the - # call stack. The first time, it sets is_loading and triggers - # exec_module(), which will access module.__dict__, module.__name__, - # and/or module.__spec__, reentering this method. These accesses - # need to be allowed to proceed without triggering the load again. - if loader_state['is_loading'] and attr.startswith('__') and attr.endswith('__'): + # Reentrant calls from the same thread must be allowed to proceed without + # triggering the load again. + # exec_module() and self-referential imports are the primary ways this can + # happen, but in any case we must return something to avoid deadlock. + if loader_state['is_loading']: return object.__getattribute__(self, attr) loader_state['is_loading'] = True |