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 |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 8 | import subprocess |
Victor Stinner | 02bfdb3 | 2011-02-23 12:10:23 +0000 | [diff] [blame] | 9 | import tempfile |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 10 | from test.script_helper import spawn_python, kill_python, assert_python_ok, assert_python_failure |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 11 | |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 12 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 13 | # XXX (ncoghlan): Move to script_helper and make consistent with run_python |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 14 | def _kill_python_and_exit_code(p): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 15 | data = kill_python(p) |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 16 | returncode = p.wait() |
| 17 | return data, returncode |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 18 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 19 | class CmdLineTest(unittest.TestCase): |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 20 | def test_directories(self): |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 21 | assert_python_failure('.') |
| 22 | assert_python_failure('< .') |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 23 | |
| 24 | def verify_valid_flag(self, cmd_line): |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 25 | rc, out, err = assert_python_ok(*cmd_line) |
| 26 | self.assertTrue(out == b'' or out.endswith(b'\n')) |
| 27 | self.assertNotIn(b'Traceback', out) |
| 28 | self.assertNotIn(b'Traceback', err) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 29 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 30 | def test_optimize(self): |
| 31 | self.verify_valid_flag('-O') |
| 32 | self.verify_valid_flag('-OO') |
| 33 | |
| 34 | def test_q(self): |
| 35 | self.verify_valid_flag('-Qold') |
| 36 | self.verify_valid_flag('-Qnew') |
| 37 | self.verify_valid_flag('-Qwarn') |
| 38 | self.verify_valid_flag('-Qwarnall') |
| 39 | |
| 40 | def test_site_flag(self): |
| 41 | self.verify_valid_flag('-S') |
| 42 | |
| 43 | def test_usage(self): |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 44 | rc, out, err = assert_python_ok('-h') |
| 45 | self.assertIn(b'usage', out) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 46 | |
| 47 | def test_version(self): |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 48 | version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii") |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 49 | rc, out, err = assert_python_ok('-V') |
| 50 | self.assertTrue(err.startswith(version)) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 51 | |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 52 | def test_verbose(self): |
| 53 | # -v causes imports to write to stderr. If the write to |
| 54 | # stderr itself causes an import to happen (for the output |
| 55 | # codec), a recursion loop can occur. |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 56 | rc, out, err = assert_python_ok('-v') |
| 57 | self.assertNotIn(b'stack overflow', err) |
| 58 | rc, out, err = assert_python_ok('-vv') |
| 59 | self.assertNotIn(b'stack overflow', err) |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 60 | |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 61 | def test_xoptions(self): |
| 62 | rc, out, err = assert_python_ok('-c', 'import sys; print(sys._xoptions)') |
| 63 | opts = eval(out.splitlines()[0]) |
| 64 | self.assertEqual(opts, {}) |
| 65 | rc, out, err = assert_python_ok( |
| 66 | '-Xa', '-Xb=c,d=e', '-c', 'import sys; print(sys._xoptions)') |
| 67 | opts = eval(out.splitlines()[0]) |
| 68 | self.assertEqual(opts, {'a': True, 'b': 'c,d=e'}) |
| 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 | |
Victor Stinner | 073f759 | 2010-10-20 21:56:55 +0000 | [diff] [blame] | 102 | @unittest.skipIf(sys.getfilesystemencoding() == 'ascii', |
| 103 | 'need a filesystem encoding different than ASCII') |
| 104 | def test_non_ascii(self): |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 105 | # Test handling of non-ascii data |
Victor Stinner | 073f759 | 2010-10-20 21:56:55 +0000 | [diff] [blame] | 106 | if test.support.verbose: |
| 107 | import locale |
| 108 | print('locale encoding = %s, filesystem encoding = %s' |
| 109 | % (locale.getpreferredencoding(), sys.getfilesystemencoding())) |
| 110 | command = "assert(ord('\xe9') == 0xe9)" |
| 111 | assert_python_ok('-c', command) |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 112 | |
Victor Stinner | f6211ed | 2010-10-20 21:52:33 +0000 | [diff] [blame] | 113 | # On Windows, pass bytes to subprocess doesn't test how Python decodes the |
| 114 | # command line, but how subprocess does decode bytes to unicode. Python |
| 115 | # doesn't decode the command line because Windows provides directly the |
| 116 | # arguments as unicode (using wmain() instead of main()). |
| 117 | @unittest.skipIf(sys.platform == 'win32', |
| 118 | 'Windows has a native unicode API') |
| 119 | def test_undecodable_code(self): |
| 120 | undecodable = b"\xff" |
| 121 | env = os.environ.copy() |
| 122 | # Use C locale to get ascii for the locale encoding |
| 123 | env['LC_ALL'] = 'C' |
| 124 | code = ( |
| 125 | b'import locale; ' |
| 126 | b'print(ascii("' + undecodable + b'"), ' |
| 127 | b'locale.getpreferredencoding())') |
| 128 | p = subprocess.Popen( |
| 129 | [sys.executable, "-c", code], |
| 130 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 131 | env=env) |
| 132 | stdout, stderr = p.communicate() |
| 133 | if p.returncode == 1: |
| 134 | # _Py_char2wchar() decoded b'\xff' as '\udcff' (b'\xff' is not |
| 135 | # decodable from ASCII) and run_command() failed on |
| 136 | # PyUnicode_AsUTF8String(). This is the expected behaviour on |
| 137 | # Linux. |
| 138 | pattern = b"Unable to decode the command from the command line:" |
| 139 | elif p.returncode == 0: |
| 140 | # _Py_char2wchar() decoded b'\xff' as '\xff' even if the locale is |
| 141 | # C and the locale encoding is ASCII. It occurs on FreeBSD, Solaris |
| 142 | # and Mac OS X. |
| 143 | pattern = b"'\\xff' " |
| 144 | # The output is followed by the encoding name, an alias to ASCII. |
| 145 | # Examples: "US-ASCII" or "646" (ISO 646, on Solaris). |
| 146 | else: |
| 147 | raise AssertionError("Unknown exit code: %s, output=%a" % (p.returncode, stdout)) |
| 148 | if not stdout.startswith(pattern): |
| 149 | raise AssertionError("%a doesn't start with %a" % (stdout, pattern)) |
| 150 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 151 | @unittest.skipUnless(sys.platform == 'darwin', 'test specific to Mac OS X') |
| 152 | def test_osx_utf8(self): |
| 153 | def check_output(text): |
| 154 | decoded = text.decode('utf8', 'surrogateescape') |
| 155 | expected = ascii(decoded).encode('ascii') + b'\n' |
| 156 | |
| 157 | env = os.environ.copy() |
| 158 | # C locale gives ASCII locale encoding, but Python uses UTF-8 |
| 159 | # to parse the command line arguments on Mac OS X |
| 160 | env['LC_ALL'] = 'C' |
| 161 | |
| 162 | p = subprocess.Popen( |
| 163 | (sys.executable, "-c", "import sys; print(ascii(sys.argv[1]))", text), |
| 164 | stdout=subprocess.PIPE, |
| 165 | env=env) |
| 166 | stdout, stderr = p.communicate() |
| 167 | self.assertEqual(stdout, expected) |
| 168 | self.assertEqual(p.returncode, 0) |
| 169 | |
| 170 | # test valid utf-8 |
| 171 | text = 'e:\xe9, euro:\u20ac, non-bmp:\U0010ffff'.encode('utf-8') |
| 172 | check_output(text) |
| 173 | |
| 174 | # test invalid utf-8 |
| 175 | text = ( |
| 176 | b'\xff' # invalid byte |
| 177 | b'\xc3\xa9' # valid utf-8 character |
| 178 | b'\xc3\xff' # invalid byte sequence |
| 179 | b'\xed\xa0\x80' # lone surrogate character (invalid) |
| 180 | ) |
| 181 | check_output(text) |
| 182 | |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 183 | def test_unbuffered_output(self): |
| 184 | # Test expected operation of the '-u' switch |
| 185 | for stream in ('stdout', 'stderr'): |
| 186 | # Binary is unbuffered |
| 187 | code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)" |
| 188 | % stream) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 189 | rc, out, err = assert_python_ok('-u', '-c', code) |
| 190 | data = err if stream == 'stderr' else out |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 191 | self.assertEqual(data, b'x', "binary %s not unbuffered" % stream) |
| 192 | # Text is line-buffered |
| 193 | code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)" |
| 194 | % stream) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 195 | rc, out, err = assert_python_ok('-u', '-c', code) |
| 196 | data = err if stream == 'stderr' else out |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 197 | self.assertEqual(data.strip(), b'x', |
| 198 | "text %s not line-buffered" % stream) |
| 199 | |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 200 | def test_unbuffered_input(self): |
| 201 | # sys.stdin still works with '-u' |
| 202 | code = ("import sys; sys.stdout.write(sys.stdin.read(1))") |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 203 | p = spawn_python('-u', '-c', code) |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 204 | p.stdin.write(b'x') |
| 205 | p.stdin.flush() |
| 206 | data, rc = _kill_python_and_exit_code(p) |
| 207 | self.assertEqual(rc, 0) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 208 | self.assertTrue(data.startswith(b'x'), data) |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 209 | |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 210 | def test_large_PYTHONPATH(self): |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 211 | path1 = "ABCDE" * 100 |
| 212 | path2 = "FGHIJ" * 100 |
| 213 | path = path1 + os.pathsep + path2 |
Victor Stinner | 76cf687 | 2010-04-16 15:10:27 +0000 | [diff] [blame] | 214 | |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 215 | code = """if 1: |
| 216 | import sys |
| 217 | path = ":".join(sys.path) |
| 218 | path = path.encode("ascii", "backslashreplace") |
| 219 | sys.stdout.buffer.write(path)""" |
| 220 | rc, out, err = assert_python_ok('-S', '-c', code, |
| 221 | PYTHONPATH=path) |
| 222 | self.assertIn(path1.encode('ascii'), out) |
| 223 | self.assertIn(path2.encode('ascii'), out) |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 224 | |
Victor Stinner | 13d49ee | 2010-12-04 17:24:33 +0000 | [diff] [blame] | 225 | def test_displayhook_unencodable(self): |
| 226 | for encoding in ('ascii', 'latin1', 'utf8'): |
| 227 | env = os.environ.copy() |
| 228 | env['PYTHONIOENCODING'] = encoding |
| 229 | p = subprocess.Popen( |
| 230 | [sys.executable, '-i'], |
| 231 | stdin=subprocess.PIPE, |
| 232 | stdout=subprocess.PIPE, |
| 233 | stderr=subprocess.STDOUT, |
| 234 | env=env) |
| 235 | # non-ascii, surrogate, non-BMP printable, non-BMP unprintable |
| 236 | text = "a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF" |
| 237 | p.stdin.write(ascii(text).encode('ascii') + b"\n") |
| 238 | p.stdin.write(b'exit()\n') |
| 239 | data = kill_python(p) |
| 240 | escaped = repr(text).encode(encoding, 'backslashreplace') |
| 241 | self.assertIn(escaped, data) |
| 242 | |
Victor Stinner | 02bfdb3 | 2011-02-23 12:10:23 +0000 | [diff] [blame] | 243 | def check_input(self, code, expected): |
| 244 | with tempfile.NamedTemporaryFile("wb+") as stdin: |
| 245 | sep = os.linesep.encode('ASCII') |
| 246 | stdin.write(sep.join((b'abc', b'def'))) |
| 247 | stdin.flush() |
| 248 | stdin.seek(0) |
| 249 | with subprocess.Popen( |
| 250 | (sys.executable, "-c", code), |
| 251 | stdin=stdin, stdout=subprocess.PIPE) as proc: |
| 252 | stdout, stderr = proc.communicate() |
| 253 | self.assertEqual(stdout.rstrip(), expected) |
| 254 | |
| 255 | def test_stdin_readline(self): |
| 256 | # Issue #11272: check that sys.stdin.readline() replaces '\r\n' by '\n' |
| 257 | # on Windows (sys.stdin is opened in binary mode) |
| 258 | self.check_input( |
| 259 | "import sys; print(repr(sys.stdin.readline()))", |
| 260 | b"'abc\\n'") |
| 261 | |
| 262 | def test_builtin_input(self): |
| 263 | # Issue #11272: check that input() strips newlines ('\n' or '\r\n') |
| 264 | self.check_input( |
| 265 | "print(repr(input()))", |
| 266 | b"'abc'") |
| 267 | |
R David Murray | e697e37 | 2011-06-24 13:26:31 -0400 | [diff] [blame^] | 268 | def test_unmached_quote(self): |
| 269 | # Issue #10206: python program starting with unmatched quote |
| 270 | # spewed spaces to stdout |
| 271 | rc, out, err = assert_python_failure('-c', "'") |
| 272 | self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError') |
| 273 | self.assertEqual(b'', out) |
| 274 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 275 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 276 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 277 | test.support.run_unittest(CmdLineTest) |
| 278 | test.support.reap_children() |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 279 | |
| 280 | if __name__ == "__main__": |
| 281 | test_main() |