Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1 | # Tests invocation of the interpreter with various command line arguments |
Nick Coghlan | d26c18a | 2010-08-17 13:06:11 +0000 | [diff] [blame] | 2 | # Most tests are executed with environment variables ignored |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 3 | # See test_cmd_line_script.py for testing of script execution |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 4 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | import test.support, unittest |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 6 | import os |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 7 | import sys |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 8 | from test.script_helper import spawn_python, kill_python, assert_python_ok, assert_python_failure |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 9 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 10 | # spawn_python normally enforces use of -E to avoid environmental effects |
| 11 | # but one test checks PYTHONPATH behaviour explicitly |
| 12 | # XXX (ncoghlan): Give script_helper.spawn_python an option to switch |
| 13 | # off the -E flag that is normally inserted automatically |
| 14 | import subprocess |
| 15 | def _spawn_python_with_env(*args): |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 16 | cmd_line = [sys.executable] |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 17 | cmd_line.extend(args) |
| 18 | return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| 19 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 20 | |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 21 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 22 | # XXX (ncoghlan): Move to script_helper and make consistent with run_python |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 23 | def _kill_python_and_exit_code(p): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 24 | data = kill_python(p) |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 25 | returncode = p.wait() |
| 26 | return data, returncode |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 27 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 28 | class CmdLineTest(unittest.TestCase): |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 29 | def test_directories(self): |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 30 | assert_python_failure('.') |
| 31 | assert_python_failure('< .') |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 32 | |
| 33 | def verify_valid_flag(self, cmd_line): |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 34 | rc, out, err = assert_python_ok(*cmd_line) |
| 35 | self.assertTrue(out == b'' or out.endswith(b'\n')) |
| 36 | self.assertNotIn(b'Traceback', out) |
| 37 | self.assertNotIn(b'Traceback', err) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 38 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 39 | def test_optimize(self): |
| 40 | self.verify_valid_flag('-O') |
| 41 | self.verify_valid_flag('-OO') |
| 42 | |
| 43 | def test_q(self): |
| 44 | self.verify_valid_flag('-Qold') |
| 45 | self.verify_valid_flag('-Qnew') |
| 46 | self.verify_valid_flag('-Qwarn') |
| 47 | self.verify_valid_flag('-Qwarnall') |
| 48 | |
| 49 | def test_site_flag(self): |
| 50 | self.verify_valid_flag('-S') |
| 51 | |
| 52 | def test_usage(self): |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 53 | rc, out, err = assert_python_ok('-h') |
| 54 | self.assertIn(b'usage', out) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 55 | |
| 56 | def test_version(self): |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 57 | version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii") |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 58 | rc, out, err = assert_python_ok('-V') |
| 59 | self.assertTrue(err.startswith(version)) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 60 | |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 61 | def test_verbose(self): |
| 62 | # -v causes imports to write to stderr. If the write to |
| 63 | # stderr itself causes an import to happen (for the output |
| 64 | # codec), a recursion loop can occur. |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 65 | rc, out, err = assert_python_ok('-v') |
| 66 | self.assertNotIn(b'stack overflow', err) |
| 67 | rc, out, err = assert_python_ok('-vv') |
| 68 | self.assertNotIn(b'stack overflow', err) |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 69 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 70 | def test_run_module(self): |
| 71 | # Test expected operation of the '-m' switch |
| 72 | # Switch needs an argument |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 73 | assert_python_failure('-m') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 74 | # Check we get an error for a nonexistent module |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 75 | assert_python_failure('-m', 'fnord43520xyz') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 76 | # Check the runpy module also gives an error for |
| 77 | # a nonexistent module |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 78 | assert_python_failure('-m', 'runpy', 'fnord43520xyz'), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 79 | # All good if module is located and run successfully |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 80 | assert_python_ok('-m', 'timeit', '-n', '1'), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 81 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 82 | def test_run_module_bug1764407(self): |
| 83 | # -m and -i need to play well together |
| 84 | # Runs the timeit module and checks the __main__ |
| 85 | # namespace has been populated appropriately |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 86 | p = spawn_python('-i', '-m', 'timeit', '-n', '1') |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 87 | p.stdin.write(b'Timer\n') |
| 88 | p.stdin.write(b'exit()\n') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 89 | data = kill_python(p) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 90 | self.assertTrue(data.find(b'1 loop') != -1) |
| 91 | self.assertTrue(data.find(b'__main__.Timer') != -1) |
| 92 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 93 | def test_run_code(self): |
| 94 | # Test expected operation of the '-c' switch |
| 95 | # Switch needs an argument |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 96 | assert_python_failure('-c') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 97 | # Check we get an error for an uncaught exception |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 98 | assert_python_failure('-c', 'raise Exception') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 99 | # All good if execution is successful |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 100 | assert_python_ok('-c', 'pass') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 101 | |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 102 | # Test handling of non-ascii data |
Amaury Forgeot d'Arc | e255764 | 2008-11-12 00:59:11 +0000 | [diff] [blame] | 103 | if sys.getfilesystemencoding() != 'ascii': |
Antoine Pitrou | d793109 | 2010-10-08 18:46:09 +0000 | [diff] [blame] | 104 | if test.support.verbose: |
| 105 | import locale |
| 106 | print('locale encoding = %s, filesystem encoding = %s' |
| 107 | % (locale.getpreferredencoding(), sys.getfilesystemencoding())) |
Amaury Forgeot d'Arc | e255764 | 2008-11-12 00:59:11 +0000 | [diff] [blame] | 108 | command = "assert(ord('\xe9') == 0xe9)" |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 109 | assert_python_ok('-c', command) |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 110 | |
Victor Stinner | f6211ed | 2010-10-20 21:52:33 +0000 | [diff] [blame^] | 111 | # On Windows, pass bytes to subprocess doesn't test how Python decodes the |
| 112 | # command line, but how subprocess does decode bytes to unicode. Python |
| 113 | # doesn't decode the command line because Windows provides directly the |
| 114 | # arguments as unicode (using wmain() instead of main()). |
| 115 | @unittest.skipIf(sys.platform == 'win32', |
| 116 | 'Windows has a native unicode API') |
| 117 | def test_undecodable_code(self): |
| 118 | undecodable = b"\xff" |
| 119 | env = os.environ.copy() |
| 120 | # Use C locale to get ascii for the locale encoding |
| 121 | env['LC_ALL'] = 'C' |
| 122 | code = ( |
| 123 | b'import locale; ' |
| 124 | b'print(ascii("' + undecodable + b'"), ' |
| 125 | b'locale.getpreferredencoding())') |
| 126 | p = subprocess.Popen( |
| 127 | [sys.executable, "-c", code], |
| 128 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 129 | env=env) |
| 130 | stdout, stderr = p.communicate() |
| 131 | if p.returncode == 1: |
| 132 | # _Py_char2wchar() decoded b'\xff' as '\udcff' (b'\xff' is not |
| 133 | # decodable from ASCII) and run_command() failed on |
| 134 | # PyUnicode_AsUTF8String(). This is the expected behaviour on |
| 135 | # Linux. |
| 136 | pattern = b"Unable to decode the command from the command line:" |
| 137 | elif p.returncode == 0: |
| 138 | # _Py_char2wchar() decoded b'\xff' as '\xff' even if the locale is |
| 139 | # C and the locale encoding is ASCII. It occurs on FreeBSD, Solaris |
| 140 | # and Mac OS X. |
| 141 | pattern = b"'\\xff' " |
| 142 | # The output is followed by the encoding name, an alias to ASCII. |
| 143 | # Examples: "US-ASCII" or "646" (ISO 646, on Solaris). |
| 144 | else: |
| 145 | raise AssertionError("Unknown exit code: %s, output=%a" % (p.returncode, stdout)) |
| 146 | if not stdout.startswith(pattern): |
| 147 | raise AssertionError("%a doesn't start with %a" % (stdout, pattern)) |
| 148 | |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 149 | def test_unbuffered_output(self): |
| 150 | # Test expected operation of the '-u' switch |
| 151 | for stream in ('stdout', 'stderr'): |
| 152 | # Binary is unbuffered |
| 153 | code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)" |
| 154 | % stream) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 155 | rc, out, err = assert_python_ok('-u', '-c', code) |
| 156 | data = err if stream == 'stderr' else out |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 157 | self.assertEqual(data, b'x', "binary %s not unbuffered" % stream) |
| 158 | # Text is line-buffered |
| 159 | code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)" |
| 160 | % stream) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 161 | rc, out, err = assert_python_ok('-u', '-c', code) |
| 162 | data = err if stream == 'stderr' else out |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 163 | self.assertEqual(data.strip(), b'x', |
| 164 | "text %s not line-buffered" % stream) |
| 165 | |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 166 | def test_unbuffered_input(self): |
| 167 | # sys.stdin still works with '-u' |
| 168 | code = ("import sys; sys.stdout.write(sys.stdin.read(1))") |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 169 | p = spawn_python('-u', '-c', code) |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 170 | p.stdin.write(b'x') |
| 171 | p.stdin.flush() |
| 172 | data, rc = _kill_python_and_exit_code(p) |
| 173 | self.assertEqual(rc, 0) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 174 | self.assertTrue(data.startswith(b'x'), data) |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 175 | |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 176 | def test_large_PYTHONPATH(self): |
| 177 | with test.support.EnvironmentVarGuard() as env: |
| 178 | path1 = "ABCDE" * 100 |
| 179 | path2 = "FGHIJ" * 100 |
| 180 | env['PYTHONPATH'] = path1 + os.pathsep + path2 |
Victor Stinner | 76cf687 | 2010-04-16 15:10:27 +0000 | [diff] [blame] | 181 | |
| 182 | code = """ |
| 183 | import sys |
| 184 | path = ":".join(sys.path) |
| 185 | path = path.encode("ascii", "backslashreplace") |
| 186 | sys.stdout.buffer.write(path)""" |
| 187 | code = code.strip().splitlines() |
| 188 | code = '; '.join(code) |
| 189 | p = _spawn_python_with_env('-S', '-c', code) |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 190 | stdout, _ = p.communicate() |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 191 | self.assertIn(path1.encode('ascii'), stdout) |
| 192 | self.assertIn(path2.encode('ascii'), stdout) |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 193 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 194 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 195 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 196 | test.support.run_unittest(CmdLineTest) |
| 197 | test.support.reap_children() |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 198 | |
| 199 | if __name__ == "__main__": |
| 200 | test_main() |