diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-08-21 04:07:58 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-08-21 04:07:58 (GMT) |
commit | 9c8aa9bffe755fe6126dc72dfd037c6b20e65906 (patch) | |
tree | 27f0bb6088bde14cd358eef389833b0c3d0fe56f /Lib/runpy.py | |
parent | 9f957705d66bb7501d5590520778183217c301c9 (diff) | |
download | cpython-9c8aa9bffe755fe6126dc72dfd037c6b20e65906.zip cpython-9c8aa9bffe755fe6126dc72dfd037c6b20e65906.tar.gz cpython-9c8aa9bffe755fe6126dc72dfd037c6b20e65906.tar.bz2 |
Issue #27487: Warn if submodule already imported before runpy execution
Also try to clarify the find_spec() error message.
Diffstat (limited to 'Lib/runpy.py')
-rw-r--r-- | Lib/runpy.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/Lib/runpy.py b/Lib/runpy.py index af6205d..e290c21 100644 --- a/Lib/runpy.py +++ b/Lib/runpy.py @@ -114,6 +114,15 @@ def _get_module_details(mod_name, error=ImportError): if e.name is None or (e.name != pkg_name and not pkg_name.startswith(e.name + ".")): raise + # Warn if the module has already been imported under its normal name + existing = sys.modules.get(mod_name) + if existing is not None and not hasattr(existing, "__path__"): + from warnings import warn + msg = "{mod_name!r} found in sys.modules after import of " \ + "package {pkg_name!r}, but prior to execution of " \ + "{mod_name!r}; this may result in unpredictable " \ + "behaviour".format(mod_name=mod_name, pkg_name=pkg_name) + warn(RuntimeWarning(msg)) try: spec = importlib.util.find_spec(mod_name) @@ -121,7 +130,7 @@ def _get_module_details(mod_name, error=ImportError): # This hack fixes an impedance mismatch between pkgutil and # importlib, where the latter raises other errors for cases where # pkgutil previously raised ImportError - msg = "Error while finding spec for {!r} ({}: {})" + msg = "Error while finding module specification for {!r} ({}: {})" raise error(msg.format(mod_name, type(ex).__name__, ex)) from ex if spec is None: raise error("No module named %s" % mod_name) |