Skip to content

Commit

Permalink
remove locking context managers
Browse files Browse the repository at this point in the history
  • Loading branch information
ahgamut committed May 14, 2022
1 parent d5543c8 commit e043046
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
61 changes: 28 additions & 33 deletions third_party/python/Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,24 +531,23 @@ def _module_repr_from_spec(spec):
def _exec(spec, module):
"""Execute the spec's specified module in an existing module's namespace."""
name = spec.name
with _ModuleLockManager(name):
if sys.modules.get(name) is not module:
msg = 'module {!r} not in sys.modules'.format(name)
raise ImportError(msg, name=name)
if spec.loader is None:
if spec.submodule_search_locations is None:
raise ImportError('missing loader', name=spec.name)
# namespace package
_init_module_attrs(spec, module, override=True)
return module
if sys.modules.get(name) is not module:
msg = 'module {!r} not in sys.modules'.format(name)
raise ImportError(msg, name=name)
if spec.loader is None:
if spec.submodule_search_locations is None:
raise ImportError('missing loader', name=spec.name)
# namespace package
_init_module_attrs(spec, module, override=True)
if not hasattr(spec.loader, 'exec_module'):
# (issue19713) Once BuiltinImporter and ExtensionFileLoader
# have exec_module() implemented, we can add a deprecation
# warning here.
spec.loader.load_module(name)
else:
spec.loader.exec_module(module)
return module
_init_module_attrs(spec, module, override=True)
if not hasattr(spec.loader, 'exec_module'):
# (issue19713) Once BuiltinImporter and ExtensionFileLoader
# have exec_module() implemented, we can add a deprecation
# warning here.
spec.loader.load_module(name)
else:
spec.loader.exec_module(module)
return sys.modules[name]


Expand Down Expand Up @@ -613,8 +612,7 @@ def _load(spec):
clobbered.
"""
with _ModuleLockManager(spec.name):
return _load_unlocked(spec)
return _load_unlocked(spec)


# Loaders #####################################################################
Expand Down Expand Up @@ -816,15 +814,14 @@ def _find_spec(name, path, target=None):
# sys.modules provides one.
is_reload = name in sys.modules
for finder in meta_path:
with _ImportLockContext():
try:
find_spec = finder.find_spec
except AttributeError:
spec = _find_spec_legacy(finder, name, path)
if spec is None:
continue
else:
spec = find_spec(name, path, target)
try:
find_spec = finder.find_spec
except AttributeError:
spec = _find_spec_legacy(finder, name, path)
if spec is None:
continue
else:
spec = find_spec(name, path, target)
if spec is not None:
# The parent import may have already imported this module.
if not is_reload and name in sys.modules:
Expand Down Expand Up @@ -911,17 +908,15 @@ def _find_and_load_unlocked(name, import_):

def _find_and_load(name, import_):
"""Find and load the module."""
with _ModuleLockManager(name):
module = sys.modules.get(name, _NEEDS_LOADING)
if module is _NEEDS_LOADING:
return _find_and_load_unlocked(name, import_)
module = sys.modules.get(name, _NEEDS_LOADING)
if module is _NEEDS_LOADING:
return _find_and_load_unlocked(name, import_)

if module is None:
message = ('import of {} halted; '
'None in sys.modules'.format(name))
raise ModuleNotFoundError(message, name=name)

_lock_unlock_module(name)
return module


Expand Down
2 changes: 1 addition & 1 deletion third_party/python/Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1613,7 +1613,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
Py_DECREF(value);
if (initializing == -1)
PyErr_Clear();
if (initializing > 0) {
if (0 && initializing > 0) {
value = _PyObject_CallMethodIdObjArgs(interp->importlib,
&PyId__lock_unlock_module, abs_name,
NULL);
Expand Down

0 comments on commit e043046

Please sign in to comment.