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 | |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 3 | import importlib |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 4 | import importlib.machinery |
| 5 | import zipimport |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 6 | import unittest |
Georg Brandl | f5247e3 | 2010-10-14 08:08:56 +0000 | [diff] [blame] | 7 | import sys |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 8 | import os |
| 9 | import os.path |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 10 | import py_compile |
| 11 | |
Nick Coghlan | 1d5ccdb | 2012-05-21 23:03:30 +1000 | [diff] [blame] | 12 | import textwrap |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 13 | from test import support |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 14 | from test.script_helper import ( |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 15 | make_pkg, make_script, make_zip_pkg, make_zip_script, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 16 | assert_python_ok, assert_python_failure, temp_dir, |
| 17 | spawn_python, kill_python) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 18 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 19 | verbose = support.verbose |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 20 | |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 21 | example_args = ['test1', 'test2', 'test3'] |
| 22 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 23 | test_source = """\ |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 24 | # Script may be run with optimisation enabled, so don't rely on assert |
| 25 | # statements being executed |
| 26 | def assertEqual(lhs, rhs): |
| 27 | if lhs != rhs: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 28 | raise AssertionError('%r != %r' % (lhs, rhs)) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 29 | def assertIdentical(lhs, rhs): |
| 30 | if lhs is not rhs: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 31 | raise AssertionError('%r is not %r' % (lhs, rhs)) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 32 | # Check basic code execution |
| 33 | result = ['Top level assignment'] |
| 34 | def f(): |
| 35 | result.append('Lower level reference') |
| 36 | f() |
| 37 | assertEqual(result, ['Top level assignment', 'Lower level reference']) |
| 38 | # Check population of magic variables |
| 39 | assertEqual(__name__, '__main__') |
Nick Coghlan | 3f94cbf | 2012-07-15 19:10:39 +1000 | [diff] [blame] | 40 | from importlib.machinery import BuiltinImporter |
| 41 | _loader = __loader__ if __loader__ is BuiltinImporter else type(__loader__) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 42 | print('__loader__==%a' % _loader) |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 43 | print('__file__==%a' % __file__) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 44 | assertEqual(__cached__, None) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 45 | print('__package__==%r' % __package__) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 46 | # Check the sys module |
| 47 | import sys |
| 48 | assertIdentical(globals(), sys.modules[__name__].__dict__) |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 49 | from test import test_cmd_line_script |
| 50 | example_args_list = test_cmd_line_script.example_args |
| 51 | assertEqual(sys.argv[1:], example_args_list) |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 52 | print('sys.argv[0]==%a' % sys.argv[0]) |
| 53 | print('sys.path[0]==%a' % sys.path[0]) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 54 | # Check the working directory |
| 55 | import os |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 56 | print('cwd==%a' % os.getcwd()) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 57 | """ |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 58 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 59 | def _make_test_script(script_dir, script_basename, source=test_source): |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 60 | to_return = make_script(script_dir, script_basename, source) |
| 61 | importlib.invalidate_caches() |
| 62 | return to_return |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 63 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 64 | def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
| 65 | source=test_source, depth=1): |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 66 | to_return = make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
| 67 | source, depth) |
| 68 | importlib.invalidate_caches() |
| 69 | return to_return |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 70 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 71 | # There's no easy way to pass the script directory in to get |
| 72 | # -m to work (avoiding that is the whole point of making |
| 73 | # directories and zipfiles executable!) |
| 74 | # So we fake it for testing purposes with a custom launch script |
| 75 | launch_source = """\ |
| 76 | import sys, os.path, runpy |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 77 | sys.path.insert(0, %s) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 78 | runpy._run_module_as_main(%r) |
| 79 | """ |
| 80 | |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 81 | def _make_launch_script(script_dir, script_basename, module_name, path=None): |
| 82 | if path is None: |
| 83 | path = "os.path.dirname(__file__)" |
| 84 | else: |
| 85 | path = repr(path) |
| 86 | source = launch_source % (path, module_name) |
Brett Cannon | e0d88a1 | 2012-04-25 20:54:04 -0400 | [diff] [blame] | 87 | to_return = make_script(script_dir, script_basename, source) |
| 88 | importlib.invalidate_caches() |
| 89 | return to_return |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 90 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 91 | class CmdLineTest(unittest.TestCase): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 92 | def _check_output(self, script_name, exit_code, data, |
| 93 | expected_file, expected_argv0, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 94 | expected_path0, expected_package, |
| 95 | expected_loader): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 96 | if verbose > 1: |
Guido van Rossum | 87c0f1d | 2007-11-19 18:03:44 +0000 | [diff] [blame] | 97 | print("Output from test script %r:" % script_name) |
| 98 | print(data) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 99 | self.assertEqual(exit_code, 0) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 100 | printed_loader = '__loader__==%a' % expected_loader |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 101 | printed_file = '__file__==%a' % expected_file |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 102 | printed_package = '__package__==%r' % expected_package |
Victor Stinner | e3874ed | 2010-10-17 01:41:09 +0000 | [diff] [blame] | 103 | printed_argv0 = 'sys.argv[0]==%a' % expected_argv0 |
| 104 | printed_path0 = 'sys.path[0]==%a' % expected_path0 |
| 105 | printed_cwd = 'cwd==%a' % os.getcwd() |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 106 | if verbose > 1: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 107 | print('Expected output:') |
| 108 | print(printed_file) |
| 109 | print(printed_package) |
| 110 | print(printed_argv0) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 111 | print(printed_cwd) |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 112 | self.assertIn(printed_loader.encode('utf-8'), data) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 113 | self.assertIn(printed_file.encode('utf-8'), data) |
| 114 | self.assertIn(printed_package.encode('utf-8'), data) |
| 115 | self.assertIn(printed_argv0.encode('utf-8'), data) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 116 | self.assertIn(printed_path0.encode('utf-8'), data) |
| 117 | self.assertIn(printed_cwd.encode('utf-8'), data) |
| 118 | |
| 119 | def _check_script(self, script_name, expected_file, |
| 120 | expected_argv0, expected_path0, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 121 | expected_package, expected_loader, |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 122 | *cmd_line_switches): |
Georg Brandl | f5247e3 | 2010-10-14 08:08:56 +0000 | [diff] [blame] | 123 | if not __debug__: |
| 124 | cmd_line_switches += ('-' + 'O' * sys.flags.optimize,) |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 125 | run_args = cmd_line_switches + (script_name,) + tuple(example_args) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 126 | rc, out, err = assert_python_ok(*run_args) |
| 127 | self._check_output(script_name, rc, out + err, expected_file, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 128 | expected_argv0, expected_path0, |
| 129 | expected_package, expected_loader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 130 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 131 | def _check_import_error(self, script_name, expected_msg, |
| 132 | *cmd_line_switches): |
| 133 | run_args = cmd_line_switches + (script_name,) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 134 | rc, out, err = assert_python_failure(*run_args) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 135 | if verbose > 1: |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 136 | print('Output from test script %r:' % script_name) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 137 | print(err) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 138 | print('Expected output: %r' % expected_msg) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 139 | self.assertIn(expected_msg.encode('utf-8'), err) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 140 | |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 141 | def test_dash_c_loader(self): |
| 142 | rc, out, err = assert_python_ok("-c", "print(__loader__)") |
| 143 | expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8") |
| 144 | self.assertIn(expected, out) |
| 145 | |
| 146 | def test_stdin_loader(self): |
Nick Coghlan | 37b3b90 | 2012-07-15 18:24:42 +1000 | [diff] [blame] | 147 | # Unfortunately, there's no way to automatically test the fully |
| 148 | # interactive REPL, since that code path only gets executed when |
Nick Coghlan | 8e6e7d3 | 2012-07-15 23:13:18 +1000 | [diff] [blame] | 149 | # stdin is an interactive tty. |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 150 | p = spawn_python() |
| 151 | try: |
| 152 | p.stdin.write(b"print(__loader__)\n") |
| 153 | p.stdin.flush() |
| 154 | finally: |
| 155 | out = kill_python(p) |
| 156 | expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8") |
| 157 | self.assertIn(expected, out) |
| 158 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 159 | def test_basic_script(self): |
| 160 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 161 | script_name = _make_test_script(script_dir, 'script') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 162 | self._check_script(script_name, script_name, script_name, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 163 | script_dir, None, |
| 164 | importlib.machinery.SourceFileLoader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 165 | |
| 166 | def test_script_compiled(self): |
| 167 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 168 | script_name = _make_test_script(script_dir, 'script') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 169 | py_compile.compile(script_name, doraise=True) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 170 | os.remove(script_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 171 | pyc_file = support.make_legacy_pyc(script_name) |
| 172 | self._check_script(pyc_file, pyc_file, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 173 | pyc_file, script_dir, None, |
| 174 | importlib.machinery.SourcelessFileLoader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 175 | |
| 176 | def test_directory(self): |
| 177 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 178 | script_name = _make_test_script(script_dir, '__main__') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 179 | self._check_script(script_dir, script_name, script_dir, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 180 | script_dir, '', |
| 181 | importlib.machinery.SourceFileLoader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 182 | |
| 183 | def test_directory_compiled(self): |
| 184 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 185 | script_name = _make_test_script(script_dir, '__main__') |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 186 | py_compile.compile(script_name, doraise=True) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 187 | os.remove(script_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 188 | pyc_file = support.make_legacy_pyc(script_name) |
| 189 | self._check_script(script_dir, pyc_file, script_dir, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 190 | script_dir, '', |
| 191 | importlib.machinery.SourcelessFileLoader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 192 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 193 | def test_directory_error(self): |
| 194 | with temp_dir() as script_dir: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 195 | msg = "can't find '__main__' module in %r" % script_dir |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 196 | self._check_import_error(script_dir, msg) |
| 197 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 198 | def test_zipfile(self): |
| 199 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 200 | script_name = _make_test_script(script_dir, '__main__') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 201 | 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] | 202 | self._check_script(zip_name, run_name, zip_name, zip_name, '', |
| 203 | zipimport.zipimporter) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 204 | |
| 205 | def test_zipfile_compiled(self): |
| 206 | with temp_dir() as script_dir: |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 207 | script_name = _make_test_script(script_dir, '__main__') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 208 | compiled_name = py_compile.compile(script_name, doraise=True) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 209 | 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] | 210 | self._check_script(zip_name, run_name, zip_name, zip_name, '', |
| 211 | zipimport.zipimporter) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 212 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 213 | def test_zipfile_error(self): |
| 214 | with temp_dir() as script_dir: |
| 215 | script_name = _make_test_script(script_dir, 'not_main') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 216 | zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) |
| 217 | msg = "can't find '__main__' module in %r" % zip_name |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 218 | self._check_import_error(zip_name, msg) |
| 219 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 220 | def test_module_in_package(self): |
| 221 | with temp_dir() as script_dir: |
| 222 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 223 | make_pkg(pkg_dir) |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 224 | script_name = _make_test_script(pkg_dir, 'script') |
| 225 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script') |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 226 | self._check_script(launch_name, script_name, script_name, |
| 227 | script_dir, 'test_pkg', |
| 228 | importlib.machinery.SourceFileLoader) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 229 | |
| 230 | def test_module_in_package_in_zipfile(self): |
| 231 | with temp_dir() as script_dir: |
| 232 | zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script') |
| 233 | 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] | 234 | self._check_script(launch_name, run_name, run_name, |
| 235 | zip_name, 'test_pkg', zipimport.zipimporter) |
Nick Coghlan | f088e5e | 2008-12-14 11:50:48 +0000 | [diff] [blame] | 236 | |
| 237 | def test_module_in_subpackage_in_zipfile(self): |
| 238 | with temp_dir() as script_dir: |
| 239 | zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2) |
| 240 | 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] | 241 | self._check_script(launch_name, run_name, run_name, |
| 242 | zip_name, 'test_pkg.test_pkg', |
| 243 | zipimport.zipimporter) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 244 | |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 245 | def test_package(self): |
| 246 | with temp_dir() as script_dir: |
| 247 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 248 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 249 | script_name = _make_test_script(pkg_dir, '__main__') |
| 250 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
| 251 | self._check_script(launch_name, script_name, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 252 | script_name, script_dir, 'test_pkg', |
| 253 | importlib.machinery.SourceFileLoader) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 254 | |
| 255 | def test_package_compiled(self): |
| 256 | with temp_dir() as script_dir: |
| 257 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 258 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 259 | script_name = _make_test_script(pkg_dir, '__main__') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 260 | compiled_name = py_compile.compile(script_name, doraise=True) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 261 | os.remove(script_name) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 262 | pyc_file = support.make_legacy_pyc(script_name) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 263 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 264 | self._check_script(launch_name, pyc_file, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 265 | pyc_file, script_dir, 'test_pkg', |
| 266 | importlib.machinery.SourcelessFileLoader) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 267 | |
| 268 | def test_package_error(self): |
| 269 | with temp_dir() as script_dir: |
| 270 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 271 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 272 | msg = ("'test_pkg' is a package and cannot " |
| 273 | "be directly executed") |
| 274 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
| 275 | self._check_import_error(launch_name, msg) |
| 276 | |
| 277 | def test_package_recursion(self): |
| 278 | with temp_dir() as script_dir: |
| 279 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 280 | make_pkg(pkg_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 281 | main_dir = os.path.join(pkg_dir, '__main__') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 282 | make_pkg(main_dir) |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 283 | msg = ("Cannot use package as __main__ module; " |
| 284 | "'test_pkg' is a package and cannot " |
| 285 | "be directly executed") |
| 286 | launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
| 287 | self._check_import_error(launch_name, msg) |
| 288 | |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 289 | def test_issue8202(self): |
| 290 | # Make sure package __init__ modules see "-m" in sys.argv0 while |
| 291 | # searching for the module to execute |
| 292 | with temp_dir() as script_dir: |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 293 | with support.change_cwd(path=script_dir): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 294 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
| 295 | make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])") |
| 296 | script_name = _make_test_script(pkg_dir, 'script') |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 297 | rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 298 | if verbose > 1: |
Nick Coghlan | 10ac77d | 2012-04-19 22:19:36 +1000 | [diff] [blame] | 299 | print(out) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 300 | expected = "init_argv0==%r" % '-m' |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 301 | self.assertIn(expected.encode('utf-8'), out) |
| 302 | self._check_output(script_name, rc, out, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 303 | script_name, script_name, '', 'test_pkg', |
| 304 | importlib.machinery.SourceFileLoader) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 305 | |
| 306 | def test_issue8202_dash_c_file_ignored(self): |
| 307 | # Make sure a "-c" file in the current directory |
| 308 | # does not alter the value of sys.path[0] |
| 309 | with temp_dir() as script_dir: |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 310 | with support.change_cwd(path=script_dir): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 311 | with open("-c", "w") as f: |
| 312 | f.write("data") |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 313 | rc, out, err = assert_python_ok('-c', |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 314 | 'import sys; print("sys.path[0]==%r" % sys.path[0])') |
| 315 | if verbose > 1: |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 316 | print(out) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 317 | expected = "sys.path[0]==%r" % '' |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 318 | self.assertIn(expected.encode('utf-8'), out) |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 319 | |
| 320 | def test_issue8202_dash_m_file_ignored(self): |
| 321 | # Make sure a "-m" file in the current directory |
| 322 | # does not alter the value of sys.path[0] |
| 323 | with temp_dir() as script_dir: |
| 324 | script_name = _make_test_script(script_dir, 'other') |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 325 | with support.change_cwd(path=script_dir): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 326 | with open("-m", "w") as f: |
| 327 | f.write("data") |
Nick Coghlan | 37fc401 | 2012-04-22 17:11:33 +1000 | [diff] [blame] | 328 | rc, out, err = assert_python_ok('-m', 'other', *example_args) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 329 | self._check_output(script_name, rc, out, |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 330 | script_name, script_name, '', '', |
| 331 | importlib.machinery.SourceFileLoader) |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 332 | |
Senthil Kumaran | f01a337 | 2012-07-04 19:28:16 -0700 | [diff] [blame] | 333 | def test_dash_m_error_code_is_one(self): |
| 334 | # If a module is invoked with the -m command line flag |
| 335 | # and results in an error that the return code to the |
| 336 | # shell is '1' |
| 337 | with temp_dir() as script_dir: |
Nick Coghlan | 5517596 | 2013-07-28 22:11:50 +1000 | [diff] [blame] | 338 | with support.change_cwd(path=script_dir): |
Senthil Kumaran | f01a337 | 2012-07-04 19:28:16 -0700 | [diff] [blame] | 339 | pkg_dir = os.path.join(script_dir, 'test_pkg') |
| 340 | make_pkg(pkg_dir) |
| 341 | script_name = _make_test_script(pkg_dir, 'other', |
| 342 | "if __name__ == '__main__': raise ValueError") |
| 343 | rc, out, err = assert_python_failure('-m', 'test_pkg.other', *example_args) |
| 344 | if verbose > 1: |
| 345 | print(out) |
| 346 | self.assertEqual(rc, 1) |
| 347 | |
Nick Coghlan | 1d5ccdb | 2012-05-21 23:03:30 +1000 | [diff] [blame] | 348 | def test_pep_409_verbiage(self): |
| 349 | # Make sure PEP 409 syntax properly suppresses |
| 350 | # the context of an exception |
| 351 | script = textwrap.dedent("""\ |
| 352 | try: |
| 353 | raise ValueError |
| 354 | except: |
| 355 | raise NameError from None |
| 356 | """) |
| 357 | with temp_dir() as script_dir: |
| 358 | script_name = _make_test_script(script_dir, 'script', script) |
| 359 | exitcode, stdout, stderr = assert_python_failure(script_name) |
| 360 | text = stderr.decode('ascii').split('\n') |
| 361 | self.assertEqual(len(text), 4) |
| 362 | self.assertTrue(text[0].startswith('Traceback')) |
| 363 | self.assertTrue(text[1].startswith(' File ')) |
| 364 | self.assertTrue(text[3].startswith('NameError')) |
| 365 | |
Victor Stinner | e667e98 | 2012-11-12 01:23:15 +0100 | [diff] [blame] | 366 | def test_non_ascii(self): |
| 367 | # Mac OS X denies the creation of a file with an invalid UTF-8 name. |
| 368 | # Windows allows to create a name with an arbitrary bytes name, but |
| 369 | # Python cannot a undecodable bytes argument to a subprocess. |
Victor Stinner | 0af71aa | 2013-01-03 01:50:30 +0100 | [diff] [blame] | 370 | if (support.TESTFN_UNDECODABLE |
| 371 | and sys.platform not in ('win32', 'darwin')): |
| 372 | name = os.fsdecode(support.TESTFN_UNDECODABLE) |
| 373 | elif support.TESTFN_NONASCII: |
Victor Stinner | e667e98 | 2012-11-12 01:23:15 +0100 | [diff] [blame] | 374 | name = support.TESTFN_NONASCII |
| 375 | else: |
| 376 | self.skipTest("need support.TESTFN_NONASCII") |
| 377 | |
Andrew Svetlov | 90c0eb2 | 2012-11-01 14:51:14 +0200 | [diff] [blame] | 378 | # Issue #16218 |
Victor Stinner | e667e98 | 2012-11-12 01:23:15 +0100 | [diff] [blame] | 379 | source = 'print(ascii(__file__))\n' |
| 380 | script_name = _make_test_script(os.curdir, name, source) |
| 381 | self.addCleanup(support.unlink, script_name) |
| 382 | rc, stdout, stderr = assert_python_ok(script_name) |
| 383 | self.assertEqual( |
| 384 | ascii(script_name), |
| 385 | stdout.rstrip().decode('ascii'), |
| 386 | 'stdout=%r stderr=%r' % (stdout, stderr)) |
| 387 | self.assertEqual(0, rc) |
| 388 | |
Andrew Svetlov | 90c0eb2 | 2012-11-01 14:51:14 +0200 | [diff] [blame] | 389 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 390 | def test_main(): |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 391 | support.run_unittest(CmdLineTest) |
| 392 | support.reap_children() |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 393 | |
Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 394 | if __name__ == '__main__': |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 395 | test_main() |