diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2013-11-01 04:22:15 (GMT) |
---|---|---|
committer | Eric Snow <ericsnowcurrently@gmail.com> | 2013-11-01 04:22:15 (GMT) |
commit | cdf601281f5909b4b7c6e1f10f3a80d3c216cdc3 (patch) | |
tree | 2bea84bd69350b853084377ab70bd17254bd4010 /Lib/importlib | |
parent | dcdd05b0b4c23da0eb39d3b29d40f8865b6b73ee (diff) | |
download | cpython-cdf601281f5909b4b7c6e1f10f3a80d3c216cdc3.zip cpython-cdf601281f5909b4b7c6e1f10f3a80d3c216cdc3.tar.gz cpython-cdf601281f5909b4b7c6e1f10f3a80d3c216cdc3.tar.bz2 |
Issue #19413: Restore pre-3.3 reload() semantics of re-finding modules.
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/__init__.py | 8 | ||||
-rw-r--r-- | Lib/importlib/_bootstrap.py | 8 |
2 files changed, 12 insertions, 4 deletions
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py index 69ca9ce..e56e9c3 100644 --- a/Lib/importlib/__init__.py +++ b/Lib/importlib/__init__.py @@ -107,7 +107,7 @@ def reload(module): if not module or not isinstance(module, types.ModuleType): raise TypeError("reload() argument must be module") name = module.__name__ - if name not in sys.modules: + if sys.modules.get(name) is not module: msg = "module {} not in sys.modules" raise ImportError(msg.format(name), name=name) if name in _RELOADING: @@ -118,7 +118,11 @@ def reload(module): if parent_name and parent_name not in sys.modules: msg = "parent {!r} not in sys.modules" raise ImportError(msg.format(parent_name), name=parent_name) - module.__loader__.load_module(name) + loader = _bootstrap._find_module(name, None) + if loader is None: + raise ImportError(_bootstrap._ERR_MSG.format(name), name=name) + module.__loader__ = loader + loader.load_module(name) # The module may have replaced itself in sys.modules! return sys.modules[module.__name__] finally: diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index ec10532..a06a02f 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -1510,15 +1510,19 @@ def _find_module(name, path): """Find a module's loader.""" if not sys.meta_path: _warnings.warn('sys.meta_path is empty', ImportWarning) + is_reload = name in sys.modules for finder in sys.meta_path: with _ImportLockContext(): loader = finder.find_module(name, path) if loader is not None: # The parent import may have already imported this module. - if name not in sys.modules: + if is_reload or name not in sys.modules: return loader else: - return sys.modules[name].__loader__ + try: + return sys.modules[name].__loader__ + except AttributeError: + return loader else: return None |