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 | c0f1a1a | 2011-02-23 12:07:37 +0000 | [diff] [blame] | 9 | import tempfile |
Ezio Melotti | 7c66319 | 2012-11-18 13:55:52 +0200 | [diff] [blame] | 10 | from test.script_helper import (spawn_python, kill_python, assert_python_ok, |
| 11 | assert_python_failure) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 12 | |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 13 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 14 | # XXX (ncoghlan): Move to script_helper and make consistent with run_python |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 15 | def _kill_python_and_exit_code(p): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 16 | data = kill_python(p) |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 17 | returncode = p.wait() |
| 18 | return data, returncode |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 19 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 20 | class CmdLineTest(unittest.TestCase): |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 21 | def test_directories(self): |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 22 | assert_python_failure('.') |
| 23 | assert_python_failure('< .') |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 24 | |
| 25 | def verify_valid_flag(self, cmd_line): |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 26 | rc, out, err = assert_python_ok(*cmd_line) |
| 27 | self.assertTrue(out == b'' or out.endswith(b'\n')) |
| 28 | self.assertNotIn(b'Traceback', out) |
| 29 | self.assertNotIn(b'Traceback', err) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 30 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 31 | def test_optimize(self): |
| 32 | self.verify_valid_flag('-O') |
| 33 | self.verify_valid_flag('-OO') |
| 34 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 35 | def test_site_flag(self): |
| 36 | self.verify_valid_flag('-S') |
| 37 | |
| 38 | def test_usage(self): |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 39 | rc, out, err = assert_python_ok('-h') |
| 40 | self.assertIn(b'usage', out) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 41 | |
| 42 | def test_version(self): |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 43 | version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii") |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 44 | rc, out, err = assert_python_ok('-V') |
| 45 | self.assertTrue(err.startswith(version)) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 46 | |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 47 | def test_verbose(self): |
| 48 | # -v causes imports to write to stderr. If the write to |
| 49 | # stderr itself causes an import to happen (for the output |
| 50 | # codec), a recursion loop can occur. |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 51 | rc, out, err = assert_python_ok('-v') |
| 52 | self.assertNotIn(b'stack overflow', err) |
| 53 | rc, out, err = assert_python_ok('-vv') |
| 54 | self.assertNotIn(b'stack overflow', err) |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 55 | |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 56 | def test_xoptions(self): |
| 57 | rc, out, err = assert_python_ok('-c', 'import sys; print(sys._xoptions)') |
| 58 | opts = eval(out.splitlines()[0]) |
| 59 | self.assertEqual(opts, {}) |
| 60 | rc, out, err = assert_python_ok( |
| 61 | '-Xa', '-Xb=c,d=e', '-c', 'import sys; print(sys._xoptions)') |
| 62 | opts = eval(out.splitlines()[0]) |
| 63 | self.assertEqual(opts, {'a': True, 'b': 'c,d=e'}) |
| 64 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 65 | def test_run_module(self): |
| 66 | # Test expected operation of the '-m' switch |
| 67 | # Switch needs an argument |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 68 | assert_python_failure('-m') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 69 | # Check we get an error for a nonexistent module |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 70 | assert_python_failure('-m', 'fnord43520xyz') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 71 | # Check the runpy module also gives an error for |
| 72 | # a nonexistent module |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 73 | assert_python_failure('-m', 'runpy', 'fnord43520xyz'), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 74 | # All good if module is located and run successfully |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 75 | assert_python_ok('-m', 'timeit', '-n', '1'), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 76 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 77 | def test_run_module_bug1764407(self): |
| 78 | # -m and -i need to play well together |
| 79 | # Runs the timeit module and checks the __main__ |
| 80 | # namespace has been populated appropriately |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 81 | p = spawn_python('-i', '-m', 'timeit', '-n', '1') |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 82 | p.stdin.write(b'Timer\n') |
| 83 | p.stdin.write(b'exit()\n') |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 84 | data = kill_python(p) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 85 | self.assertTrue(data.find(b'1 loop') != -1) |
| 86 | self.assertTrue(data.find(b'__main__.Timer') != -1) |
| 87 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 88 | def test_run_code(self): |
| 89 | # Test expected operation of the '-c' switch |
| 90 | # Switch needs an argument |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 91 | assert_python_failure('-c') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 92 | # Check we get an error for an uncaught exception |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 93 | assert_python_failure('-c', 'raise Exception') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 94 | # All good if execution is successful |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 95 | assert_python_ok('-c', 'pass') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 96 | |
Victor Stinner | e667e98 | 2012-11-12 01:23:15 +0100 | [diff] [blame] | 97 | @unittest.skipUnless(test.support.FS_NONASCII, 'need support.FS_NONASCII') |
Victor Stinner | 073f759 | 2010-10-20 21:56:55 +0000 | [diff] [blame] | 98 | def test_non_ascii(self): |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 99 | # Test handling of non-ascii data |
Victor Stinner | e667e98 | 2012-11-12 01:23:15 +0100 | [diff] [blame] | 100 | command = ("assert(ord(%r) == %s)" |
| 101 | % (test.support.FS_NONASCII, ord(test.support.FS_NONASCII))) |
Victor Stinner | 073f759 | 2010-10-20 21:56:55 +0000 | [diff] [blame] | 102 | assert_python_ok('-c', command) |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 103 | |
Victor Stinner | f6211ed | 2010-10-20 21:52:33 +0000 | [diff] [blame] | 104 | # On Windows, pass bytes to subprocess doesn't test how Python decodes the |
| 105 | # command line, but how subprocess does decode bytes to unicode. Python |
| 106 | # doesn't decode the command line because Windows provides directly the |
| 107 | # arguments as unicode (using wmain() instead of main()). |
| 108 | @unittest.skipIf(sys.platform == 'win32', |
| 109 | 'Windows has a native unicode API') |
| 110 | def test_undecodable_code(self): |
| 111 | undecodable = b"\xff" |
| 112 | env = os.environ.copy() |
| 113 | # Use C locale to get ascii for the locale encoding |
| 114 | env['LC_ALL'] = 'C' |
| 115 | code = ( |
| 116 | b'import locale; ' |
| 117 | b'print(ascii("' + undecodable + b'"), ' |
| 118 | b'locale.getpreferredencoding())') |
| 119 | p = subprocess.Popen( |
| 120 | [sys.executable, "-c", code], |
| 121 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
| 122 | env=env) |
| 123 | stdout, stderr = p.communicate() |
| 124 | if p.returncode == 1: |
| 125 | # _Py_char2wchar() decoded b'\xff' as '\udcff' (b'\xff' is not |
| 126 | # decodable from ASCII) and run_command() failed on |
| 127 | # PyUnicode_AsUTF8String(). This is the expected behaviour on |
| 128 | # Linux. |
| 129 | pattern = b"Unable to decode the command from the command line:" |
| 130 | elif p.returncode == 0: |
| 131 | # _Py_char2wchar() decoded b'\xff' as '\xff' even if the locale is |
| 132 | # C and the locale encoding is ASCII. It occurs on FreeBSD, Solaris |
| 133 | # and Mac OS X. |
| 134 | pattern = b"'\\xff' " |
| 135 | # The output is followed by the encoding name, an alias to ASCII. |
| 136 | # Examples: "US-ASCII" or "646" (ISO 646, on Solaris). |
| 137 | else: |
| 138 | raise AssertionError("Unknown exit code: %s, output=%a" % (p.returncode, stdout)) |
| 139 | if not stdout.startswith(pattern): |
| 140 | raise AssertionError("%a doesn't start with %a" % (stdout, pattern)) |
| 141 | |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 142 | @unittest.skipUnless(sys.platform == 'darwin', 'test specific to Mac OS X') |
| 143 | def test_osx_utf8(self): |
| 144 | def check_output(text): |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 145 | decoded = text.decode('utf-8', 'surrogateescape') |
Victor Stinner | f933e1a | 2010-10-20 22:58:25 +0000 | [diff] [blame] | 146 | expected = ascii(decoded).encode('ascii') + b'\n' |
| 147 | |
| 148 | env = os.environ.copy() |
| 149 | # C locale gives ASCII locale encoding, but Python uses UTF-8 |
| 150 | # to parse the command line arguments on Mac OS X |
| 151 | env['LC_ALL'] = 'C' |
| 152 | |
| 153 | p = subprocess.Popen( |
| 154 | (sys.executable, "-c", "import sys; print(ascii(sys.argv[1]))", text), |
| 155 | stdout=subprocess.PIPE, |
| 156 | env=env) |
| 157 | stdout, stderr = p.communicate() |
| 158 | self.assertEqual(stdout, expected) |
| 159 | self.assertEqual(p.returncode, 0) |
| 160 | |
| 161 | # test valid utf-8 |
| 162 | text = 'e:\xe9, euro:\u20ac, non-bmp:\U0010ffff'.encode('utf-8') |
| 163 | check_output(text) |
| 164 | |
| 165 | # test invalid utf-8 |
| 166 | text = ( |
| 167 | b'\xff' # invalid byte |
| 168 | b'\xc3\xa9' # valid utf-8 character |
| 169 | b'\xc3\xff' # invalid byte sequence |
| 170 | b'\xed\xa0\x80' # lone surrogate character (invalid) |
| 171 | ) |
| 172 | check_output(text) |
| 173 | |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 174 | def test_unbuffered_output(self): |
| 175 | # Test expected operation of the '-u' switch |
| 176 | for stream in ('stdout', 'stderr'): |
| 177 | # Binary is unbuffered |
| 178 | code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)" |
| 179 | % stream) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 180 | rc, out, err = assert_python_ok('-u', '-c', code) |
| 181 | data = err if stream == 'stderr' else out |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 182 | self.assertEqual(data, b'x', "binary %s not unbuffered" % stream) |
| 183 | # Text is line-buffered |
| 184 | code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)" |
| 185 | % stream) |
Antoine Pitrou | f51d8d3 | 2010-10-08 18:05:42 +0000 | [diff] [blame] | 186 | rc, out, err = assert_python_ok('-u', '-c', code) |
| 187 | data = err if stream == 'stderr' else out |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 188 | self.assertEqual(data.strip(), b'x', |
| 189 | "text %s not line-buffered" % stream) |
| 190 | |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 191 | def test_unbuffered_input(self): |
| 192 | # sys.stdin still works with '-u' |
| 193 | code = ("import sys; sys.stdout.write(sys.stdin.read(1))") |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 194 | p = spawn_python('-u', '-c', code) |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 195 | p.stdin.write(b'x') |
| 196 | p.stdin.flush() |
| 197 | data, rc = _kill_python_and_exit_code(p) |
| 198 | self.assertEqual(rc, 0) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 199 | self.assertTrue(data.startswith(b'x'), data) |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 200 | |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 201 | def test_large_PYTHONPATH(self): |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 202 | path1 = "ABCDE" * 100 |
| 203 | path2 = "FGHIJ" * 100 |
| 204 | path = path1 + os.pathsep + path2 |
Victor Stinner | 76cf687 | 2010-04-16 15:10:27 +0000 | [diff] [blame] | 205 | |
Antoine Pitrou | 9bc3568 | 2010-11-09 21:33:55 +0000 | [diff] [blame] | 206 | code = """if 1: |
| 207 | import sys |
| 208 | path = ":".join(sys.path) |
| 209 | path = path.encode("ascii", "backslashreplace") |
| 210 | sys.stdout.buffer.write(path)""" |
| 211 | rc, out, err = assert_python_ok('-S', '-c', code, |
| 212 | PYTHONPATH=path) |
| 213 | self.assertIn(path1.encode('ascii'), out) |
| 214 | self.assertIn(path2.encode('ascii'), out) |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame] | 215 | |
Victor Stinner | 13d49ee | 2010-12-04 17:24:33 +0000 | [diff] [blame] | 216 | def test_displayhook_unencodable(self): |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 217 | for encoding in ('ascii', 'latin-1', 'utf-8'): |
Victor Stinner | 13d49ee | 2010-12-04 17:24:33 +0000 | [diff] [blame] | 218 | env = os.environ.copy() |
| 219 | env['PYTHONIOENCODING'] = encoding |
| 220 | p = subprocess.Popen( |
| 221 | [sys.executable, '-i'], |
| 222 | stdin=subprocess.PIPE, |
| 223 | stdout=subprocess.PIPE, |
| 224 | stderr=subprocess.STDOUT, |
| 225 | env=env) |
| 226 | # non-ascii, surrogate, non-BMP printable, non-BMP unprintable |
| 227 | text = "a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF" |
| 228 | p.stdin.write(ascii(text).encode('ascii') + b"\n") |
| 229 | p.stdin.write(b'exit()\n') |
| 230 | data = kill_python(p) |
| 231 | escaped = repr(text).encode(encoding, 'backslashreplace') |
| 232 | self.assertIn(escaped, data) |
| 233 | |
Victor Stinner | c0f1a1a | 2011-02-23 12:07:37 +0000 | [diff] [blame] | 234 | def check_input(self, code, expected): |
| 235 | with tempfile.NamedTemporaryFile("wb+") as stdin: |
| 236 | sep = os.linesep.encode('ASCII') |
| 237 | stdin.write(sep.join((b'abc', b'def'))) |
| 238 | stdin.flush() |
| 239 | stdin.seek(0) |
| 240 | with subprocess.Popen( |
| 241 | (sys.executable, "-c", code), |
| 242 | stdin=stdin, stdout=subprocess.PIPE) as proc: |
| 243 | stdout, stderr = proc.communicate() |
| 244 | self.assertEqual(stdout.rstrip(), expected) |
| 245 | |
| 246 | def test_stdin_readline(self): |
| 247 | # Issue #11272: check that sys.stdin.readline() replaces '\r\n' by '\n' |
| 248 | # on Windows (sys.stdin is opened in binary mode) |
| 249 | self.check_input( |
| 250 | "import sys; print(repr(sys.stdin.readline()))", |
| 251 | b"'abc\\n'") |
| 252 | |
| 253 | def test_builtin_input(self): |
| 254 | # Issue #11272: check that input() strips newlines ('\n' or '\r\n') |
| 255 | self.check_input( |
| 256 | "print(repr(input()))", |
| 257 | b"'abc'") |
| 258 | |
Victor Stinner | 7b3f0fa | 2012-08-04 01:28:00 +0200 | [diff] [blame] | 259 | def test_output_newline(self): |
| 260 | # Issue 13119 Newline for print() should be \r\n on Windows. |
| 261 | code = """if 1: |
| 262 | import sys |
| 263 | print(1) |
| 264 | print(2) |
| 265 | print(3, file=sys.stderr) |
| 266 | print(4, file=sys.stderr)""" |
| 267 | rc, out, err = assert_python_ok('-c', code) |
| 268 | |
| 269 | if sys.platform == 'win32': |
| 270 | self.assertEqual(b'1\r\n2\r\n', out) |
| 271 | self.assertEqual(b'3\r\n4', err) |
| 272 | else: |
| 273 | self.assertEqual(b'1\n2\n', out) |
| 274 | self.assertEqual(b'3\n4', err) |
| 275 | |
R David Murray | e697e37 | 2011-06-24 13:26:31 -0400 | [diff] [blame] | 276 | def test_unmached_quote(self): |
| 277 | # Issue #10206: python program starting with unmatched quote |
| 278 | # spewed spaces to stdout |
| 279 | rc, out, err = assert_python_failure('-c', "'") |
| 280 | self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError') |
| 281 | self.assertEqual(b'', out) |
| 282 | |
Antoine Pitrou | d7c8fbf | 2011-11-26 21:59:36 +0100 | [diff] [blame] | 283 | def test_stdout_flush_at_shutdown(self): |
| 284 | # Issue #5319: if stdout.flush() fails at shutdown, an error should |
| 285 | # be printed out. |
| 286 | code = """if 1: |
| 287 | import os, sys |
| 288 | sys.stdout.write('x') |
| 289 | os.close(sys.stdout.fileno())""" |
| 290 | rc, out, err = assert_python_ok('-c', code) |
| 291 | self.assertEqual(b'', out) |
| 292 | self.assertRegex(err.decode('ascii', 'ignore'), |
Antoine Pitrou | 5604ef3 | 2011-11-26 22:02:29 +0100 | [diff] [blame] | 293 | 'Exception OSError: .* ignored') |
Antoine Pitrou | d7c8fbf | 2011-11-26 21:59:36 +0100 | [diff] [blame] | 294 | |
| 295 | def test_closed_stdout(self): |
| 296 | # Issue #13444: if stdout has been explicitly closed, we should |
| 297 | # not attempt to flush it at shutdown. |
| 298 | code = "import sys; sys.stdout.close()" |
| 299 | rc, out, err = assert_python_ok('-c', code) |
| 300 | self.assertEqual(b'', err) |
| 301 | |
Antoine Pitrou | 11942a5 | 2011-11-28 19:08:36 +0100 | [diff] [blame] | 302 | # Issue #7111: Python should work without standard streams |
| 303 | |
| 304 | @unittest.skipIf(os.name != 'posix', "test needs POSIX semantics") |
| 305 | def _test_no_stdio(self, streams): |
| 306 | code = """if 1: |
| 307 | import os, sys |
| 308 | for i, s in enumerate({streams}): |
| 309 | if getattr(sys, s) is not None: |
| 310 | os._exit(i + 1) |
| 311 | os._exit(42)""".format(streams=streams) |
| 312 | def preexec(): |
| 313 | if 'stdin' in streams: |
| 314 | os.close(0) |
| 315 | if 'stdout' in streams: |
| 316 | os.close(1) |
| 317 | if 'stderr' in streams: |
| 318 | os.close(2) |
| 319 | p = subprocess.Popen( |
| 320 | [sys.executable, "-E", "-c", code], |
| 321 | stdin=subprocess.PIPE, |
| 322 | stdout=subprocess.PIPE, |
| 323 | stderr=subprocess.PIPE, |
| 324 | preexec_fn=preexec) |
| 325 | out, err = p.communicate() |
| 326 | self.assertEqual(test.support.strip_python_stderr(err), b'') |
| 327 | self.assertEqual(p.returncode, 42) |
| 328 | |
| 329 | def test_no_stdin(self): |
| 330 | self._test_no_stdio(['stdin']) |
| 331 | |
| 332 | def test_no_stdout(self): |
| 333 | self._test_no_stdio(['stdout']) |
| 334 | |
| 335 | def test_no_stderr(self): |
| 336 | self._test_no_stdio(['stderr']) |
| 337 | |
| 338 | def test_no_std_streams(self): |
| 339 | self._test_no_stdio(['stdin', 'stdout', 'stderr']) |
| 340 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 341 | def test_hash_randomization(self): |
| 342 | # Verify that -R enables hash randomization: |
| 343 | self.verify_valid_flag('-R') |
| 344 | hashes = [] |
| 345 | for i in range(2): |
| 346 | code = 'print(hash("spam"))' |
Benjamin Peterson | c9f54cf | 2012-02-21 16:08:05 -0500 | [diff] [blame] | 347 | rc, out, err = assert_python_ok('-c', code) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 348 | self.assertEqual(rc, 0) |
Georg Brandl | 09a7c72 | 2012-02-20 21:31:46 +0100 | [diff] [blame] | 349 | hashes.append(out) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 350 | self.assertNotEqual(hashes[0], hashes[1]) |
| 351 | |
| 352 | # Verify that sys.flags contains hash_randomization |
| 353 | code = 'import sys; print("random is", sys.flags.hash_randomization)' |
Benjamin Peterson | c9f54cf | 2012-02-21 16:08:05 -0500 | [diff] [blame] | 354 | rc, out, err = assert_python_ok('-c', code) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 355 | self.assertEqual(rc, 0) |
Georg Brandl | 09a7c72 | 2012-02-20 21:31:46 +0100 | [diff] [blame] | 356 | self.assertIn(b'random is 1', out) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 357 | |
Hynek Schlawack | 5c6b3e2 | 2012-11-07 09:02:24 +0100 | [diff] [blame] | 358 | def test_del___main__(self): |
| 359 | # Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a |
| 360 | # borrowed reference to the dict of __main__ module and later modify |
| 361 | # the dict whereas the module was destroyed |
| 362 | filename = test.support.TESTFN |
| 363 | self.addCleanup(test.support.unlink, filename) |
| 364 | with open(filename, "w") as script: |
| 365 | print("import sys", file=script) |
| 366 | print("del sys.modules['__main__']", file=script) |
| 367 | assert_python_ok(filename) |
| 368 | |
Ezio Melotti | 7c66319 | 2012-11-18 13:55:52 +0200 | [diff] [blame] | 369 | def test_unknown_options(self): |
Ezio Melotti | a0dd22e | 2012-11-23 18:48:32 +0200 | [diff] [blame] | 370 | rc, out, err = assert_python_failure('-E', '-z') |
| 371 | self.assertIn(b'Unknown option: -z', err) |
Ezio Melotti | 7c66319 | 2012-11-18 13:55:52 +0200 | [diff] [blame] | 372 | self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1) |
| 373 | self.assertEqual(b'', out) |
Ezio Melotti | a0dd22e | 2012-11-23 18:48:32 +0200 | [diff] [blame] | 374 | # Add "without='-E'" to prevent _assert_python to append -E |
| 375 | # to env_vars and change the output of stderr |
| 376 | rc, out, err = assert_python_failure('-z', without='-E') |
| 377 | self.assertIn(b'Unknown option: -z', err) |
| 378 | self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1) |
| 379 | self.assertEqual(b'', out) |
| 380 | rc, out, err = assert_python_failure('-a', '-z', without='-E') |
| 381 | self.assertIn(b'Unknown option: -a', err) |
| 382 | # only the first unknown option is reported |
| 383 | self.assertNotIn(b'Unknown option: -z', err) |
| 384 | self.assertEqual(err.splitlines().count(b'Unknown option: -a'), 1) |
| 385 | self.assertEqual(b'', out) |
| 386 | |
Ezio Melotti | 7c66319 | 2012-11-18 13:55:52 +0200 | [diff] [blame] | 387 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 388 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 389 | test.support.run_unittest(CmdLineTest) |
| 390 | test.support.reap_children() |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 391 | |
| 392 | if __name__ == "__main__": |
| 393 | test_main() |