summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_cmd_line_script.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_cmd_line_script.py')
-rw-r--r--Lib/test/test_cmd_line_script.py71
1 files changed, 62 insertions, 9 deletions
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index fda3e62..77cb95c 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -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