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