diff options
author | Martin Panter <vadmium+py@gmail.com> | 2015-12-03 01:23:10 +0000 |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2015-12-03 01:23:10 +0000 |
commit | 657257edb61c7d69a9e73352fdad8f243e1494ab (patch) | |
tree | a7a3a16c452b07b7d5120442a0c8cf05fc66b499 /Lib/test/test_runpy.py | |
parent | 6648bf5661b79f5b40385b21570dff6f146c5eb5 (diff) | |
download | cpython-git-657257edb61c7d69a9e73352fdad8f243e1494ab.tar.gz |
Issue #14285: Do not catch __init__.py exceptions in runpy
Initialize package before calling find_spec() for __main__, so that we do not
incorrectly handle exceptions from __init__.py. When runpy is used from the
Python CLI, use an internal exception rather than ImportError, to avoid
catching unexpected exceptions.
Also remove exception message rewriting in _run_module_as_main(), because it
seems to be redundant with the _get_main_module_details() function.
Diffstat (limited to 'Lib/test/test_runpy.py')
-rw-r--r-- | Lib/test/test_runpy.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_runpy.py b/Lib/test/test_runpy.py index 4bae949d21..01f6abd8c5 100644 --- a/Lib/test/test_runpy.py +++ b/Lib/test/test_runpy.py @@ -439,6 +439,28 @@ from ..uncle.cousin import nephew if verbose > 1: print("Testing package depth:", depth) self._check_package(depth) + def test_run_package_init_exceptions(self): + # These were previously wrapped in an ImportError; see Issue 14285 + result = self._make_pkg("", 1, "__main__") + pkg_dir, _, mod_name, _ = result + mod_name = mod_name.replace(".__main__", "") + self.addCleanup(self._del_pkg, pkg_dir, 1, mod_name) + init = os.path.join(pkg_dir, "__runpy_pkg__", "__init__.py") + + exceptions = (ImportError, AttributeError, TypeError, ValueError) + for exception in exceptions: + name = exception.__name__ + with self.subTest(name): + source = "raise {0}('{0} in __init__.py.')".format(name) + with open(init, "wt", encoding="ascii") as mod_file: + mod_file.write(source) + try: + run_module(mod_name) + except exception as err: + self.assertNotIn("finding spec", format(err)) + else: + self.fail("Nothing raised; expected {}".format(name)) + def test_run_package_in_namespace_package(self): for depth in range(1, 4): if verbose > 1: print("Testing package depth:", depth) |