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