summaryrefslogtreecommitdiff
path: root/Lib/importlib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-07-06 08:09:03 +0300
committerGitHub <noreply@github.com>2017-07-06 08:09:03 +0300
commitb4baacee1adc06edbe30ac7574d17a8cd168e2e0 (patch)
treea01eb344f832a876239e6a7dd33806f5945206c2 /Lib/importlib
parent1ccbad9c95cf5782a1329eeaecba6e3eb0c37cb8 (diff)
downloadcpython-git-b4baacee1adc06edbe30ac7574d17a8cd168e2e0.tar.gz
bpo-30814: Fixed a race condition when import a submodule from a package. (#2580)
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/_bootstrap.py28
1 files changed, 14 insertions, 14 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 891bd062bb..f7c89caca8 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -956,9 +956,19 @@ def _find_and_load_unlocked(name, import_):
def _find_and_load(name, import_):
- """Find and load the module, and release the import lock."""
- with _ModuleLockManager(name):
- return _find_and_load_unlocked(name, import_)
+ """Find and load the module."""
+ _imp.acquire_lock()
+ if name not in sys.modules:
+ with _ModuleLockManager(name):
+ return _find_and_load_unlocked(name, import_)
+ module = sys.modules[name]
+ if module is None:
+ _imp.release_lock()
+ message = ('import of {} halted; '
+ 'None in sys.modules'.format(name))
+ raise ModuleNotFoundError(message, name=name)
+ _lock_unlock_module(name)
+ return module
def _gcd_import(name, package=None, level=0):
@@ -973,17 +983,7 @@ def _gcd_import(name, package=None, level=0):
_sanity_check(name, package, level)
if level > 0:
name = _resolve_name(name, package, level)
- _imp.acquire_lock()
- if name not in sys.modules:
- return _find_and_load(name, _gcd_import)
- module = sys.modules[name]
- if module is None:
- _imp.release_lock()
- message = ('import of {} halted; '
- 'None in sys.modules'.format(name))
- raise ModuleNotFoundError(message, name=name)
- _lock_unlock_module(name)
- return module
+ return _find_and_load(name, _gcd_import)
def _handle_fromlist(module, fromlist, import_):