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.
This commit is contained in:
Martin Panter 2015-12-03 01:23:10 +00:00
parent 6648bf5661
commit 657257edb6
4 changed files with 110 additions and 32 deletions

View file

@ -397,20 +397,73 @@ class CmdLineTest(unittest.TestCase):
script_name, script_name, '', '',
importlib.machinery.SourceFileLoader)
@contextlib.contextmanager
def setup_test_pkg(self, *args):
with support.temp_dir() as script_dir, \
support.change_cwd(path=script_dir):
pkg_dir = os.path.join(script_dir, 'test_pkg')
make_pkg(pkg_dir, *args)
yield pkg_dir
def check_dash_m_failure(self, *args):
rc, out, err = assert_python_failure('-m', *args, __isolated=False)
if verbose > 1:
print(repr(out))
self.assertEqual(rc, 1)
return err
def test_dash_m_error_code_is_one(self):
# If a module is invoked with the -m command line flag
# and results in an error that the return code to the
# shell is '1'
with support.temp_dir() as script_dir:
with support.change_cwd(path=script_dir):
pkg_dir = os.path.join(script_dir, 'test_pkg')
make_pkg(pkg_dir)
script_name = _make_test_script(pkg_dir, 'other',
"if __name__ == '__main__': raise ValueError")
rc, out, err = assert_python_failure('-m', 'test_pkg.other', *example_args)
if verbose > 1:
print(repr(out))
with self.setup_test_pkg() as pkg_dir:
script_name = _make_test_script(pkg_dir, 'other',
"if __name__ == '__main__': raise ValueError")
err = self.check_dash_m_failure('test_pkg.other', *example_args)
self.assertIn(b'ValueError', err)
def test_dash_m_errors(self):
# Exercise error reporting for various invalid package executions
tests = (
('builtins', br'No code object available'),
('builtins.x', br'Error while finding spec.*AttributeError'),
('builtins.x.y', br'Error while finding spec.*'
br'ImportError.*No module named.*not a package'),
('os.path', br'loader.*cannot handle'),
('importlib', br'No module named.*'
br'is a package and cannot be directly executed'),
('importlib.nonexistant', br'No module named'),
)
for name, regex in tests:
with self.subTest(name):
rc, _, err = assert_python_failure('-m', name)
self.assertEqual(rc, 1)
self.assertRegex(err, regex)
self.assertNotIn(b'Traceback', err)
def test_dash_m_init_traceback(self):
# These were wrapped in an ImportError and tracebacks were
# suppressed; see Issue 14285
exceptions = (ImportError, AttributeError, TypeError, ValueError)
for exception in exceptions:
exception = exception.__name__
init = "raise {0}('Exception in __init__.py')".format(exception)
with self.subTest(exception), \
self.setup_test_pkg(init) as pkg_dir:
err = self.check_dash_m_failure('test_pkg')
self.assertIn(exception.encode('ascii'), err)
self.assertIn(b'Exception in __init__.py', err)
self.assertIn(b'Traceback', err)
def test_dash_m_main_traceback(self):
# Ensure that an ImportError's traceback is reported
with self.setup_test_pkg() as pkg_dir:
main = "raise ImportError('Exception in __main__ module')"
_make_test_script(pkg_dir, '__main__', main)
err = self.check_dash_m_failure('test_pkg')
self.assertIn(b'ImportError', err)
self.assertIn(b'Exception in __main__ module', err)
self.assertIn(b'Traceback', err)
def test_pep_409_verbiage(self):
# Make sure PEP 409 syntax properly suppresses