diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-07-21 11:00:46 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-21 11:00:46 (GMT) |
commit | e72b1359f81d1dd42bd8a5c5cc2b3928b74f8023 (patch) | |
tree | 845e7c5701752a30063a95e02228c47e23c3375e /Lib/importlib/_bootstrap.py | |
parent | 3913bad4957ac30e58a7ffe9279333ad176a8eea (diff) | |
download | cpython-e72b1359f81d1dd42bd8a5c5cc2b3928b74f8023.zip cpython-e72b1359f81d1dd42bd8a5c5cc2b3928b74f8023.tar.gz cpython-e72b1359f81d1dd42bd8a5c5cc2b3928b74f8023.tar.bz2 |
bpo-30891: Fix again importlib _find_and_load() (#2665)
Use sys.modules.get() in the "with _ModuleLockManager(name):" block
to protect the dictionary key with the module lock and use an atomic
get to prevent race condition.
Remove also _bootstrap._POPULATE since it was unused
(_bootstrap_external now has its own _POPULATE object), add a new
_SENTINEL object instead.
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index a269eee..493c272 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -446,9 +446,6 @@ def spec_from_loader(name, loader, *, origin=None, is_package=None): return ModuleSpec(name, loader, origin=origin, is_package=is_package) -_POPULATE = object() - - def _spec_from_module(module, loader=None, origin=None): # This function is meant for use in _setup(). try: @@ -953,13 +950,16 @@ def _find_and_load_unlocked(name, import_): return module +_NEEDS_LOADING = object() + + def _find_and_load(name, import_): """Find and load the module.""" with _ModuleLockManager(name): - if name not in sys.modules: + module = sys.modules.get(name, _NEEDS_LOADING) + if module is _NEEDS_LOADING: return _find_and_load_unlocked(name, import_) - module = sys.modules[name] if module is None: message = ('import of {} halted; ' 'None in sys.modules'.format(name)) |