Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 1 | # tests command line execution of scripts |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 2 | |
Antoine Pitrou | 9845c7e | 2014-05-11 13:42:17 +0200 | [diff] [blame] | 3 | import contextlib |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 4 | import importlib |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 5 | import importlib.machinery |
| 6 | import zipimport |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 7 | import unittest |
Georg Brandl | f5247e3 | 2010-10-14 08:08:56 +0000 | [diff] [blame] | 8 | import sys |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 9 | import os |
| 10 | import os.path |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 11 | import py_compile |
Antoine Pitrou | 9845c7e | 2014-05-11 13:42:17 +0200 | [diff] [blame] | 12 | import subprocess |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 13 | |
Nick Coghlan | 1d5ccdb | 2012-05-21 23:03:30 +1000 | [diff] [blame] | 14 | import textwrap |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 15 | from test import support |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 16 | from test.support.script_helper import ( |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 17 | make_pkg, make_script, make_zip_pkg, make_zip_script, |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 18 | assert_python_ok, assert_python_failure, spawn_python, kill_python) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 19 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 20 | verbose = support.verbose |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 21 | |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 22 | example_args = ['test1', 'test2', 'test3'] |
| 23 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 24 | test_source = """\ |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 25 | # Script may be run with optimisation enabled, so don't rely on assert |
| 26 | # statements being executed |
| 27 | def assertEqual(lhs, rhs): |
| 28 | if lhs != rhs: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 29 | raise AssertionError('%r != %r' % (lhs, rhs)) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 30 | def assertIdentical(lhs, rhs): |
| 31 | if lhs is not rhs: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 32 | raise AssertionError('%r is not %r' % (lhs, rhs)) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 33 | # Check basic code execution |
| 34 | result = ['Top level assignment'] |
| 35 | def f(): |
| 36 | result.append('Lower level reference') |
| 37 | f() |
| 38 | assertEqual(result, ['Top level assignment', 'Lower level reference']) |
| 39 | # Check population of magic variables |
| 40 | assertEqual(__name__, '__main__') |
Nick Coghlan | 3f94cbf | 2012-07-15 19:10:39 +1000 | [diff] [blame] | 41 | from importlib.machinery import BuiltinImporter |
| 42 | _loader = __loader__ if __loader__ is BuiltinImporter else type(__loader__) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 43 | print('__loader__==%a' % _loader) |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 44 | print('__file__==%a' % __file__) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 45 | print('__cached__==%a' % __cached__) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 46 | print('__package__==%r' % __package__) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 47 | # Check PEP 451 details |
| 48 | import os.path |
| 49 | if __package__ is not None: |
| 50 | print('__main__ was located through the import system') |
| 51 | assertIdentical(__spec__.loader, __loader__) |
| 52 | expected_spec_name = os.path.splitext(os.path.basename(__file__))[0] |
| 53 | if __package__: |
| 54 | expected_spec_name = __package__ + "." + expected_spec_name |
| 55 | assertEqual(__spec__.name, expected_spec_name) |
| 56 | assertEqual(__spec__.parent, __package__) |
| 57 | assertIdentical(__spec__.submodule_search_locations, None) |
| 58 | assertEqual(__spec__.origin, __file__) |
| 59 | if __spec__.cached is not None: |
| 60 | assertEqual(__spec__.cached, __cached__) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 61 | # Check the sys module |
| 62 | import sys |
| 63 | assertIdentical(globals(), sys.modules[__name__].__dict__) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 64 | if __spec__ is not None: |
| 65 | # XXX: We're not currently making __main__ available under its real name |
| 66 | pass # assertIdentical(globals(), sys.modules[__spec__.name].__dict__) |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 67 | from test import test_cmd_line_script |
| 68 | example_args_list = test_cmd_line_script.example_args |
| 69 | assertEqual(sys.argv[1:], example_args_list) |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 70 | print('sys.argv[0]==%a' % sys.argv[0]) |
| 71 | print('sys.path[0]==%a' % sys.path[0]) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 72 | # Check the working directory |
| 73 | import os |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 74 | print('cwd==%a' % os.getcwd()) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 75 | """ |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 76 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 77 | def _make_test_script(script_dir, script_basename, source=test_source): |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 78 | to_return = make_script(script_dir, script_basename, source) |
| 79 | importlib.invalidate_caches() |
| 80 | return to_return |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 81 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 82 | def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
| 83 | source=test_source, depth=1): |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 84 | to_return = make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
| 85 | source, depth) |
| 86 | importlib.invalidate_caches() |
| 87 | return to_return |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 88 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 89 | # There's no easy way to pass the script directory in to get |
| 90 | # -m to work (avoiding that is the whole point of making |
| 91 | # directories and zipfiles executable!) |
| 92 | # So we fake it for testing purposes with a custom launch script |
| 93 | launch_source = """\ |
| 94 | import sys, os.path, runpy |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 95 | sys.path.insert(0, %s) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 96 | runpy._run_module_as_main(%r) |
| 97 | """ |
| 98 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 99 | def _make_launch_script(script_dir, script_basename, module_name, path=None): |
| 100 | if path is None: |
| 101 | path = "os.path.dirname(__file__)" |
| 102 | else: |
| 103 | path = repr(path) |
| 104 | source = launch_source % (path, module_name) |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 105 | to_return = make_script(script_dir, script_basename, source) |
| 106 | importlib.invalidate_caches() |
| 107 | return to_return |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 108 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 109 | class CmdLineTest(unittest.TestCase): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 110 | def _check_output(self, script_name, exit_code, data, |
| 111 | expected_file, expected_argv0, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 112 | expected_path0, expected_package, |
| 113 | expected_loader): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 114 | if verbose > 1: |
Guido van Rossum | 87c0f1d | 2007-11-19 18:03:44 +0000 | [diff] [blame] | 115 | print("Output from test script %r:" % script_name) |
Serhiy Storchaka | b0749ca | 2015-03-25 01:33:19 +0200 | [diff] [blame] | 116 | print(repr(data)) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 117 | self.assertEqual(exit_code, 0) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 118 | printed_loader = '__loader__==%a' % expected_loader |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 119 | printed_file = '__file__==%a' % expected_file |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 120 | printed_package = '__package__==%r' % expected_package |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 121 | printed_argv0 = 'sys.argv[0]==%a' % expected_argv0 |
| 122 | printed_path0 = 'sys.path[0]==%a' % expected_path0 |
| 123 | printed_cwd = 'cwd==%a' % os.getcwd() |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 124 | if verbose > 1: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 125 | print('Expected output:') |
| 126 | print(printed_file) |
| 127 | print(printed_package) |
| 128 | print(printed_argv0) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 129 | print(printed_cwd) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 130 | self.assertIn(printed_loader.encode('utf-8'), data) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 131 | self.assertIn(printed_file.encode('utf-8'), data) |
| 132 | self.assertIn(printed_package.encode('utf-8'), data) |
| 133 | self.assertIn(printed_argv0.encode('utf-8'), data) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 134 | self.assertIn(printed_path0.encode('utf-8'), data) |
| 135 | self.assertIn(printed_cwd.encode('utf-8'), data) |
| 136 | |
| 137 | def _check_script(self, script_name, expected_file, |
| 138 | expected_argv0, expected_path0, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 139 | expected_package, expected_loader, |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 140 | *cmd_line_switches): |
Georg Brandl | f5247e3 | 2010-10-14 08:08:56 +0000 | [diff] [blame] | 141 | if not __debug__: |
| 142 | cmd_line_switches += ('-' + 'O' * sys.flags.optimize,) |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 143 | run_args = cmd_line_switches + (script_name,) + tuple(example_args) |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 144 | rc, out, err = assert_python_ok(*run_args, __isolated=False) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 145 | self._check_output(script_name, rc, out + err, expected_file, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 146 | expected_argv0, expected_path0, |
| 147 | expected_package, expected_loader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 148 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 149 | def _check_import_error(self, script_name, expected_msg, |
| 150 | *cmd_line_switches): |
| 151 | run_args = cmd_line_switches + (script_name,) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 152 | rc, out, err = assert_python_failure(*run_args) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 153 | if verbose > 1: |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 154 | print('Output from test script %r:' % script_name) |
Serhiy Storchaka | b0749ca | 2015-03-25 01:33:19 +0200 | [diff] [blame] | 155 | print(repr(err)) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 156 | print('Expected output: %r' % expected_msg) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 157 | self.assertIn(expected_msg.encode('utf-8'), err) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 158 | |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 159 | def test_dash_c_loader(self): |
| 160 | rc, out, err = assert_python_ok("-c", "print(__loader__)") |
| 161 | expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8") |
| 162 | self.assertIn(expected, out) |
| 163 | |
| 164 | def test_stdin_loader(self): |
Nick Coghlan | 37b3b90 | 2012-07-15 18:24:42 +1000 | [diff] [blame] | 165 | # Unfortunately, there's no way to automatically test the fully |
| 166 | # interactive REPL, since that code path only gets executed when |
Nick Coghlan | 8e6e7d3 | 2012-07-15 23:13:18 +1000 | [diff] [blame] | 167 | # stdin is an interactive tty. |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 168 | p = spawn_python() |
| 169 | try: |
| 170 | p.stdin.write(b"print(__loader__)\n") |
| 171 | p.stdin.flush() |
| 172 | finally: |
| 173 | out = kill_python(p) |
| 174 | expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8") |
| 175 | self.assertIn(expected, out) |
| 176 | |
Antoine Pitrou | 9845c7e | 2014-05-11 13:42:17 +0200 | [diff] [blame] | 177 | @contextlib.contextmanager |
| 178 | def interactive_python(self, separate_stderr=False): |
| 179 | if separate_stderr: |
| 180 | p = spawn_python('-i', bufsize=1, stderr=subprocess.PIPE) |
| 181 | stderr = p.stderr |
| 182 | else: |
| 183 | p = spawn_python('-i', bufsize=1, stderr=subprocess.STDOUT) |
| 184 | stderr = p.stdout |
| 185 | try: |
| 186 | # Drain stderr until prompt |
| 187 | while True: |
| 188 | data = stderr.read(4) |
| 189 | if data == b">>> ": |
| 190 | break |
| 191 | stderr.readline() |
| 192 | yield p |
| 193 | finally: |
| 194 | kill_python(p) |
| 195 | stderr.close() |
| 196 | |
| 197 | def check_repl_stdout_flush(self, separate_stderr=False): |
| 198 | with self.interactive_python(separate_stderr) as p: |
| 199 | p.stdin.write(b"print('foo')\n") |
| 200 | p.stdin.flush() |
| 201 | self.assertEqual(b'foo', p.stdout.readline().strip()) |
| 202 | |
| 203 | def check_repl_stderr_flush(self, separate_stderr=False): |
| 204 | with self.interactive_python(separate_stderr) as p: |
| 205 | p.stdin.write(b"1/0\n") |
| 206 | p.stdin.flush() |
| 207 | stderr = p.stderr if separate_stderr else p.stdout |
| 208 | self.assertIn(b'Traceback ', stderr.readline()) |
| 209 | self.assertIn(b'File "<stdin>"', stderr.readline()) |
| 210 | self.assertIn(b'ZeroDivisionError', stderr.readline()) |
| 211 | |
| 212 | def test_repl_stdout_flush(self): |
| 213 | self.check_repl_stdout_flush() |
| 214 | |
| 215 | def test_repl_stdout_flush_separate_stderr(self): |
| 216 | self.check_repl_stdout_flush(True) |
| 217 | |
| 218 | def test_repl_stderr_flush(self): |
| 219 | self.check_repl_stderr_flush() |
| 220 | |
| 221 | def test_repl_stderr_flush_separate_stderr(self): |
| 222 | self.check_repl_stderr_flush(True) |
| 223 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 224 | def test_basic_script(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 225 | with support.temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 226 | script_name = _make_test_script(script_dir, 'script') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 227 | self._check_script(script_name, script_name, script_name, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 228 | script_dir, None, |
| 229 | importlib.machinery.SourceFileLoader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 230 | |
| 231 | def test_script_compiled(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 232 | with support.temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 233 | script_name = _make_test_script(script_dir, 'script') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 234 | py_compile.compile(script_name, doraise=True) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 235 | os.remove(script_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 236 | pyc_file = support.make_legacy_pyc(script_name) |
| 237 | self._check_script(pyc_file, pyc_file, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 238 | pyc_file, script_dir, None, |
| 239 | importlib.machinery.SourcelessFileLoader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 240 | |
| 241 | def test_directory(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 242 | with support.temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 243 | script_name = _make_test_script(script_dir, '__main__') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 244 | self._check_script(script_dir, script_name, script_dir, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 245 | script_dir, '', |
| 246 | importlib.machinery.SourceFileLoader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 247 | |
| 248 | def test_directory_compiled(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 249 | with support.temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 250 | script_name = _make_test_script(script_dir, '__main__') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 251 | py_compile.compile(script_name, doraise=True) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 252 | os.remove(script_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 253 | pyc_file = support.make_legacy_pyc(script_name) |
| 254 | self._check_script(script_dir, pyc_file, script_dir, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 255 | script_dir, '', |
| 256 | importlib.machinery.SourcelessFileLoader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 257 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 258 | def test_directory_error(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 259 | with support.temp_dir() as script_dir: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 260 | msg = "can't find '__main__' module in %r" % script_dir |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 261 | self._check_import_error(script_dir, msg) |
| 262 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 263 | def test_zipfile(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 264 | with support.temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 265 | script_name = _make_test_script(script_dir, '__main__') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 266 | zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 267 | self._check_script(zip_name, run_name, zip_name, zip_name, '', |
| 268 | zipimport.zipimporter) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 269 | |
| 270 | def test_zipfile_compiled(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 271 | with support.temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 272 | script_name = _make_test_script(script_dir, '__main__') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 273 | compiled_name = py_compile.compile(script_name, doraise=True) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 274 | zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 275 | self._check_script(zip_name, run_name, zip_name, zip_name, '', |
| 276 | zipimport.zipimporter) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 277 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 278 | def test_zipfile_error(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 279 | with support.temp_dir() as script_dir: |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 280 | script_name = _make_test_script(script_dir, 'not_main') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 281 | zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) |
| 282 | msg = "can't find '__main__' module in %r" % zip_name |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 283 | self._check_import_error(zip_name, msg) |
| 284 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 285 | def test_module_in_package(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 286 | with support.temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 287 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 288 | make_pkg(pkg_dir) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 289 | script_name = _make_test_script(pkg_dir, 'script') |
| 290 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script') |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 291 | self._check_script(launch_name, script_name, script_name, |
| 292 | script_dir, 'test_pkg', |
| 293 | importlib.machinery.SourceFileLoader) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 294 | |
| 295 | def test_module_in_package_in_zipfile(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 296 | with support.temp_dir() as script_dir: |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 297 | zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script') |
| 298 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 299 | self._check_script(launch_name, run_name, run_name, |
| 300 | zip_name, 'test_pkg', zipimport.zipimporter) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 301 | |
| 302 | def test_module_in_subpackage_in_zipfile(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 303 | with support.temp_dir() as script_dir: |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 304 | zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2) |
| 305 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 306 | self._check_script(launch_name, run_name, run_name, |
| 307 | zip_name, 'test_pkg.test_pkg', |
| 308 | zipimport.zipimporter) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 309 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 310 | def test_package(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 311 | with support.temp_dir() as script_dir: |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 312 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 313 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 314 | script_name = _make_test_script(pkg_dir, '__main__') |
| 315 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
| 316 | self._check_script(launch_name, script_name, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 317 | script_name, script_dir, 'test_pkg', |
| 318 | importlib.machinery.SourceFileLoader) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 319 | |
| 320 | def test_package_compiled(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 321 | with support.temp_dir() as script_dir: |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 322 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 323 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 324 | script_name = _make_test_script(pkg_dir, '__main__') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 325 | compiled_name = py_compile.compile(script_name, doraise=True) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 326 | os.remove(script_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 327 | pyc_file = support.make_legacy_pyc(script_name) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 328 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 329 | self._check_script(launch_name, pyc_file, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 330 | pyc_file, script_dir, 'test_pkg', |
| 331 | importlib.machinery.SourcelessFileLoader) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 332 | |
| 333 | def test_package_error(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 334 | with support.temp_dir() as script_dir: |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 335 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 336 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 337 | msg = ("'test_pkg' is a package and cannot " |
| 338 | "be directly executed") |
| 339 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
| 340 | self._check_import_error(launch_name, msg) |
| 341 | |
| 342 | def test_package_recursion(self): |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 343 | with support.temp_dir() as script_dir: |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 344 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 345 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 346 | main_dir = os.path.join(pkg_dir, '__main__') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 347 | make_pkg(main_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 348 | msg = ("Cannot use package as __main__ module; " |
| 349 | "'test_pkg' is a package and cannot " |
| 350 | "be directly executed") |
| 351 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
| 352 | self._check_import_error(launch_name, msg) |
| 353 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 354 | def test_issue8202(self): |
| 355 | # Make sure package __init__ modules see "-m" in sys.argv0 while |
| 356 | # searching for the module to execute |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 357 | with support.temp_dir() as script_dir: |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 358 | with support.change_cwd(path=script_dir): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 359 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
| 360 | make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])") |
| 361 | script_name = _make_test_script(pkg_dir, 'script') |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 362 | rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args, __isolated=False) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 363 | if verbose > 1: |
Serhiy Storchaka | b0749ca | 2015-03-25 01:33:19 +0200 | [diff] [blame] | 364 | print(repr(out)) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 365 | expected = "init_argv0==%r" % '-m' |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 366 | self.assertIn(expected.encode('utf-8'), out) |
| 367 | self._check_output(script_name, rc, out, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 368 | script_name, script_name, '', 'test_pkg', |
| 369 | importlib.machinery.SourceFileLoader) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 370 | |
| 371 | def test_issue8202_dash_c_file_ignored(self): |
| 372 | # Make sure a "-c" file in the current directory |
| 373 | # does not alter the value of sys.path[0] |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 374 | with support.temp_dir() as script_dir: |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 375 | with support.change_cwd(path=script_dir): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 376 | with open("-c", "w") as f: |
| 377 | f.write("data") |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 378 | rc, out, err = assert_python_ok('-c', |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 379 | 'import sys; print("sys.path[0]==%r" % sys.path[0])', |
| 380 | __isolated=False) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 381 | if verbose > 1: |
Serhiy Storchaka | b0749ca | 2015-03-25 01:33:19 +0200 | [diff] [blame] | 382 | print(repr(out)) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 383 | expected = "sys.path[0]==%r" % '' |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 384 | self.assertIn(expected.encode('utf-8'), out) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 385 | |
| 386 | def test_issue8202_dash_m_file_ignored(self): |
| 387 | # Make sure a "-m" file in the current directory |
| 388 | # does not alter the value of sys.path[0] |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 389 | with support.temp_dir() as script_dir: |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 390 | script_name = _make_test_script(script_dir, 'other') |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 391 | with support.change_cwd(path=script_dir): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 392 | with open("-m", "w") as f: |
| 393 | f.write("data") |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 394 | rc, out, err = assert_python_ok('-m', 'other', *example_args, |
| 395 | __isolated=False) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 396 | self._check_output(script_name, rc, out, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 397 | script_name, script_name, '', '', |
| 398 | importlib.machinery.SourceFileLoader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 399 | |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 400 | @contextlib.contextmanager |
| 401 | def setup_test_pkg(self, *args): |
| 402 | with support.temp_dir() as script_dir, \ |
| 403 | support.change_cwd(path=script_dir): |
| 404 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
| 405 | make_pkg(pkg_dir, *args) |
| 406 | yield pkg_dir |
| 407 | |
| 408 | def check_dash_m_failure(self, *args): |
| 409 | rc, out, err = assert_python_failure('-m', *args, __isolated=False) |
| 410 | if verbose > 1: |
| 411 | print(repr(out)) |
| 412 | self.assertEqual(rc, 1) |
| 413 | return err |
| 414 | |
Senthil Kumaran | f01a337 | 2012-07-04 19:28:16 -0700 | [diff] [blame] | 415 | def test_dash_m_error_code_is_one(self): |
| 416 | # If a module is invoked with the -m command line flag |
| 417 | # and results in an error that the return code to the |
| 418 | # shell is '1' |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 419 | with self.setup_test_pkg() as pkg_dir: |
| 420 | script_name = _make_test_script(pkg_dir, 'other', |
| 421 | "if __name__ == '__main__': raise ValueError") |
| 422 | err = self.check_dash_m_failure('test_pkg.other', *example_args) |
| 423 | self.assertIn(b'ValueError', err) |
| 424 | |
| 425 | def test_dash_m_errors(self): |
| 426 | # Exercise error reporting for various invalid package executions |
| 427 | tests = ( |
| 428 | ('builtins', br'No code object available'), |
Martin Panter | 9c8aa9b | 2016-08-21 04:07:58 +0000 | [diff] [blame] | 429 | ('builtins.x', br'Error while finding module specification.*' |
| 430 | br'AttributeError'), |
| 431 | ('builtins.x.y', br'Error while finding module specification.*' |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 432 | br'ImportError.*No module named.*not a package'), |
| 433 | ('os.path', br'loader.*cannot handle'), |
| 434 | ('importlib', br'No module named.*' |
| 435 | br'is a package and cannot be directly executed'), |
| 436 | ('importlib.nonexistant', br'No module named'), |
Martin Panter | 7dda421 | 2015-12-10 06:47:06 +0000 | [diff] [blame] | 437 | ('.unittest', br'Relative module names not supported'), |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 438 | ) |
| 439 | for name, regex in tests: |
| 440 | with self.subTest(name): |
| 441 | rc, _, err = assert_python_failure('-m', name) |
Senthil Kumaran | f01a337 | 2012-07-04 19:28:16 -0700 | [diff] [blame] | 442 | self.assertEqual(rc, 1) |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 443 | self.assertRegex(err, regex) |
| 444 | self.assertNotIn(b'Traceback', err) |
| 445 | |
Martin Panter | dda5843 | 2015-12-12 06:58:55 +0000 | [diff] [blame] | 446 | def test_dash_m_bad_pyc(self): |
| 447 | with support.temp_dir() as script_dir, \ |
| 448 | support.change_cwd(path=script_dir): |
| 449 | os.mkdir('test_pkg') |
| 450 | # Create invalid *.pyc as empty file |
| 451 | with open('test_pkg/__init__.pyc', 'wb'): |
| 452 | pass |
| 453 | err = self.check_dash_m_failure('test_pkg') |
Martin Panter | 9c8aa9b | 2016-08-21 04:07:58 +0000 | [diff] [blame] | 454 | self.assertRegex(err, |
| 455 | br'Error while finding module specification.*' |
Martin Panter | dda5843 | 2015-12-12 06:58:55 +0000 | [diff] [blame] | 456 | br'ImportError.*bad magic number') |
| 457 | self.assertNotIn(b'is a package', err) |
| 458 | self.assertNotIn(b'Traceback', err) |
| 459 | |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 460 | def test_dash_m_init_traceback(self): |
| 461 | # These were wrapped in an ImportError and tracebacks were |
| 462 | # suppressed; see Issue 14285 |
| 463 | exceptions = (ImportError, AttributeError, TypeError, ValueError) |
| 464 | for exception in exceptions: |
| 465 | exception = exception.__name__ |
| 466 | init = "raise {0}('Exception in __init__.py')".format(exception) |
| 467 | with self.subTest(exception), \ |
| 468 | self.setup_test_pkg(init) as pkg_dir: |
| 469 | err = self.check_dash_m_failure('test_pkg') |
| 470 | self.assertIn(exception.encode('ascii'), err) |
| 471 | self.assertIn(b'Exception in __init__.py', err) |
| 472 | self.assertIn(b'Traceback', err) |
| 473 | |
| 474 | def test_dash_m_main_traceback(self): |
| 475 | # Ensure that an ImportError's traceback is reported |
| 476 | with self.setup_test_pkg() as pkg_dir: |
| 477 | main = "raise ImportError('Exception in __main__ module')" |
| 478 | _make_test_script(pkg_dir, '__main__', main) |
| 479 | err = self.check_dash_m_failure('test_pkg') |
| 480 | self.assertIn(b'ImportError', err) |
| 481 | self.assertIn(b'Exception in __main__ module', err) |
| 482 | self.assertIn(b'Traceback', err) |
Senthil Kumaran | f01a337 | 2012-07-04 19:28:16 -0700 | [diff] [blame] | 483 | |
Nick Coghlan | 1d5ccdb | 2012-05-21 23:03:30 +1000 | [diff] [blame] | 484 | def test_pep_409_verbiage(self): |
| 485 | # Make sure PEP 409 syntax properly suppresses |
| 486 | # the context of an exception |
| 487 | script = textwrap.dedent("""\ |
| 488 | try: |
| 489 | raise ValueError |
| 490 | except: |
| 491 | raise NameError from None |
| 492 | """) |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 493 | with support.temp_dir() as script_dir: |
Nick Coghlan | 1d5ccdb | 2012-05-21 23:03:30 +1000 | [diff] [blame] | 494 | script_name = _make_test_script(script_dir, 'script', script) |
| 495 | exitcode, stdout, stderr = assert_python_failure(script_name) |
| 496 | text = stderr.decode('ascii').split('\n') |
| 497 | self.assertEqual(len(text), 4) |
| 498 | self.assertTrue(text[0].startswith('Traceback')) |
| 499 | self.assertTrue(text[1].startswith(' File ')) |
| 500 | self.assertTrue(text[3].startswith('NameError')) |
| 501 | |
Victor Stinner | e667e98 | 2012-11-12 01:23:15 +0100 | [diff] [blame] | 502 | def test_non_ascii(self): |
| 503 | # Mac OS X denies the creation of a file with an invalid UTF-8 name. |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 504 | # Windows allows creating a name with an arbitrary bytes name, but |
Victor Stinner | e667e98 | 2012-11-12 01:23:15 +0100 | [diff] [blame] | 505 | # Python cannot a undecodable bytes argument to a subprocess. |
Victor Stinner | 0af71aa | 2013-01-03 01:50:30 +0100 | [diff] [blame] | 506 | if (support.TESTFN_UNDECODABLE |
| 507 | and sys.platform not in ('win32', 'darwin')): |
| 508 | name = os.fsdecode(support.TESTFN_UNDECODABLE) |
| 509 | elif support.TESTFN_NONASCII: |
Victor Stinner | e667e98 | 2012-11-12 01:23:15 +0100 | [diff] [blame] | 510 | name = support.TESTFN_NONASCII |
| 511 | else: |
| 512 | self.skipTest("need support.TESTFN_NONASCII") |
| 513 | |
Andrew Svetlov | 90c0eb2 | 2012-11-01 14:51:14 +0200 | [diff] [blame] | 514 | # Issue #16218 |
Victor Stinner | e667e98 | 2012-11-12 01:23:15 +0100 | [diff] [blame] | 515 | source = 'print(ascii(__file__))\n' |
| 516 | script_name = _make_test_script(os.curdir, name, source) |
| 517 | self.addCleanup(support.unlink, script_name) |
| 518 | rc, stdout, stderr = assert_python_ok(script_name) |
| 519 | self.assertEqual( |
| 520 | ascii(script_name), |
| 521 | stdout.rstrip().decode('ascii'), |
| 522 | 'stdout=%r stderr=%r' % (stdout, stderr)) |
| 523 | self.assertEqual(0, rc) |
| 524 | |
Nick Coghlan | d979e43 | 2014-02-09 10:43:21 +1000 | [diff] [blame] | 525 | def test_issue20500_exit_with_exception_value(self): |
| 526 | script = textwrap.dedent("""\ |
| 527 | import sys |
| 528 | error = None |
| 529 | try: |
| 530 | raise ValueError('some text') |
| 531 | except ValueError as err: |
| 532 | error = err |
| 533 | |
| 534 | if error: |
| 535 | sys.exit(error) |
| 536 | """) |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 537 | with support.temp_dir() as script_dir: |
Nick Coghlan | d979e43 | 2014-02-09 10:43:21 +1000 | [diff] [blame] | 538 | script_name = _make_test_script(script_dir, 'script', script) |
| 539 | exitcode, stdout, stderr = assert_python_failure(script_name) |
| 540 | text = stderr.decode('ascii') |
| 541 | self.assertEqual(text, "some text") |
| 542 | |
Andrew Svetlov | 90c0eb2 | 2012-11-01 14:51:14 +0200 | [diff] [blame] | 543 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 544 | def test_main(): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 545 | support.run_unittest(CmdLineTest) |
| 546 | support.reap_children() |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 547 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 548 | if __name__ == '__main__': |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 549 | test_main() |