Skip to content

Commit e72b135

Browse files
authored
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.
1 parent 3913bad commit e72b135

File tree

2 files changed

+357
-356
lines changed

2 files changed

+357
-356
lines changed

Lib/importlib/_bootstrap.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,6 @@ def spec_from_loader(name, loader, *, origin=None, is_package=None):
446446
return ModuleSpec(name, loader, origin=origin, is_package=is_package)
447447

448448

449-
_POPULATE = object()
450-
451-
452449
def _spec_from_module(module, loader=None, origin=None):
453450
# This function is meant for use in _setup().
454451
try:
@@ -953,13 +950,16 @@ def _find_and_load_unlocked(name, import_):
953950
return module
954951

955952

953+
_NEEDS_LOADING = object()
954+
955+
956956
def _find_and_load(name, import_):
957957
"""Find and load the module."""
958958
with _ModuleLockManager(name):
959-
if name not in sys.modules:
959+
module = sys.modules.get(name, _NEEDS_LOADING)
960+
if module is _NEEDS_LOADING:
960961
return _find_and_load_unlocked(name, import_)
961962

962-
module = sys.modules[name]
963963
if module is None:
964964
message = ('import of {} halted; '
965965
'None in sys.modules'.format(name))

0 commit comments

Comments
 (0)