| 1 | n/a | # Tests invocation of the interpreter with various command line arguments |
|---|
| 2 | n/a | # Most tests are executed with environment variables ignored |
|---|
| 3 | n/a | # See test_cmd_line_script.py for testing of script execution |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | import test.support, unittest |
|---|
| 6 | n/a | import os |
|---|
| 7 | n/a | import sys |
|---|
| 8 | n/a | import subprocess |
|---|
| 9 | n/a | import tempfile |
|---|
| 10 | n/a | from test.support import script_helper, is_android |
|---|
| 11 | n/a | from test.support.script_helper import (spawn_python, kill_python, assert_python_ok, |
|---|
| 12 | n/a | assert_python_failure) |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | # XXX (ncoghlan): Move to script_helper and make consistent with run_python |
|---|
| 16 | n/a | def _kill_python_and_exit_code(p): |
|---|
| 17 | n/a | data = kill_python(p) |
|---|
| 18 | n/a | returncode = p.wait() |
|---|
| 19 | n/a | return data, returncode |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | class CmdLineTest(unittest.TestCase): |
|---|
| 22 | n/a | def test_directories(self): |
|---|
| 23 | n/a | assert_python_failure('.') |
|---|
| 24 | n/a | assert_python_failure('< .') |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | def verify_valid_flag(self, cmd_line): |
|---|
| 27 | n/a | rc, out, err = assert_python_ok(*cmd_line) |
|---|
| 28 | n/a | self.assertTrue(out == b'' or out.endswith(b'\n')) |
|---|
| 29 | n/a | self.assertNotIn(b'Traceback', out) |
|---|
| 30 | n/a | self.assertNotIn(b'Traceback', err) |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | def test_optimize(self): |
|---|
| 33 | n/a | self.verify_valid_flag('-O') |
|---|
| 34 | n/a | self.verify_valid_flag('-OO') |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | def test_site_flag(self): |
|---|
| 37 | n/a | self.verify_valid_flag('-S') |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | def test_usage(self): |
|---|
| 40 | n/a | rc, out, err = assert_python_ok('-h') |
|---|
| 41 | n/a | self.assertIn(b'usage', out) |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | def test_version(self): |
|---|
| 44 | n/a | version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii") |
|---|
| 45 | n/a | for switch in '-V', '--version', '-VV': |
|---|
| 46 | n/a | rc, out, err = assert_python_ok(switch) |
|---|
| 47 | n/a | self.assertFalse(err.startswith(version)) |
|---|
| 48 | n/a | self.assertTrue(out.startswith(version)) |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | def test_verbose(self): |
|---|
| 51 | n/a | # -v causes imports to write to stderr. If the write to |
|---|
| 52 | n/a | # stderr itself causes an import to happen (for the output |
|---|
| 53 | n/a | # codec), a recursion loop can occur. |
|---|
| 54 | n/a | rc, out, err = assert_python_ok('-v') |
|---|
| 55 | n/a | self.assertNotIn(b'stack overflow', err) |
|---|
| 56 | n/a | rc, out, err = assert_python_ok('-vv') |
|---|
| 57 | n/a | self.assertNotIn(b'stack overflow', err) |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | def test_xoptions(self): |
|---|
| 60 | n/a | def get_xoptions(*args): |
|---|
| 61 | n/a | # use subprocess module directly because test.support.script_helper adds |
|---|
| 62 | n/a | # "-X faulthandler" to the command line |
|---|
| 63 | n/a | args = (sys.executable, '-E') + args |
|---|
| 64 | n/a | args += ('-c', 'import sys; print(sys._xoptions)') |
|---|
| 65 | n/a | out = subprocess.check_output(args) |
|---|
| 66 | n/a | opts = eval(out.splitlines()[0]) |
|---|
| 67 | n/a | return opts |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | opts = get_xoptions() |
|---|
| 70 | n/a | self.assertEqual(opts, {}) |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | opts = get_xoptions('-Xa', '-Xb=c,d=e') |
|---|
| 73 | n/a | self.assertEqual(opts, {'a': True, 'b': 'c,d=e'}) |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | def test_showrefcount(self): |
|---|
| 76 | n/a | def run_python(*args): |
|---|
| 77 | n/a | # this is similar to assert_python_ok but doesn't strip |
|---|
| 78 | n/a | # the refcount from stderr. It can be replaced once |
|---|
| 79 | n/a | # assert_python_ok stops doing that. |
|---|
| 80 | n/a | cmd = [sys.executable] |
|---|
| 81 | n/a | cmd.extend(args) |
|---|
| 82 | n/a | PIPE = subprocess.PIPE |
|---|
| 83 | n/a | p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE) |
|---|
| 84 | n/a | out, err = p.communicate() |
|---|
| 85 | n/a | p.stdout.close() |
|---|
| 86 | n/a | p.stderr.close() |
|---|
| 87 | n/a | rc = p.returncode |
|---|
| 88 | n/a | self.assertEqual(rc, 0) |
|---|
| 89 | n/a | return rc, out, err |
|---|
| 90 | n/a | code = 'import sys; print(sys._xoptions)' |
|---|
| 91 | n/a | # normally the refcount is hidden |
|---|
| 92 | n/a | rc, out, err = run_python('-c', code) |
|---|
| 93 | n/a | self.assertEqual(out.rstrip(), b'{}') |
|---|
| 94 | n/a | self.assertEqual(err, b'') |
|---|
| 95 | n/a | # "-X showrefcount" shows the refcount, but only in debug builds |
|---|
| 96 | n/a | rc, out, err = run_python('-X', 'showrefcount', '-c', code) |
|---|
| 97 | n/a | self.assertEqual(out.rstrip(), b"{'showrefcount': True}") |
|---|
| 98 | n/a | if hasattr(sys, 'gettotalrefcount'): # debug build |
|---|
| 99 | n/a | self.assertRegex(err, br'^\[\d+ refs, \d+ blocks\]') |
|---|
| 100 | n/a | else: |
|---|
| 101 | n/a | self.assertEqual(err, b'') |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | def test_run_module(self): |
|---|
| 104 | n/a | # Test expected operation of the '-m' switch |
|---|
| 105 | n/a | # Switch needs an argument |
|---|
| 106 | n/a | assert_python_failure('-m') |
|---|
| 107 | n/a | # Check we get an error for a nonexistent module |
|---|
| 108 | n/a | assert_python_failure('-m', 'fnord43520xyz') |
|---|
| 109 | n/a | # Check the runpy module also gives an error for |
|---|
| 110 | n/a | # a nonexistent module |
|---|
| 111 | n/a | assert_python_failure('-m', 'runpy', 'fnord43520xyz') |
|---|
| 112 | n/a | # All good if module is located and run successfully |
|---|
| 113 | n/a | assert_python_ok('-m', 'timeit', '-n', '1') |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | def test_run_module_bug1764407(self): |
|---|
| 116 | n/a | # -m and -i need to play well together |
|---|
| 117 | n/a | # Runs the timeit module and checks the __main__ |
|---|
| 118 | n/a | # namespace has been populated appropriately |
|---|
| 119 | n/a | p = spawn_python('-i', '-m', 'timeit', '-n', '1') |
|---|
| 120 | n/a | p.stdin.write(b'Timer\n') |
|---|
| 121 | n/a | p.stdin.write(b'exit()\n') |
|---|
| 122 | n/a | data = kill_python(p) |
|---|
| 123 | n/a | self.assertTrue(data.find(b'1 loop') != -1) |
|---|
| 124 | n/a | self.assertTrue(data.find(b'__main__.Timer') != -1) |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | def test_run_code(self): |
|---|
| 127 | n/a | # Test expected operation of the '-c' switch |
|---|
| 128 | n/a | # Switch needs an argument |
|---|
| 129 | n/a | assert_python_failure('-c') |
|---|
| 130 | n/a | # Check we get an error for an uncaught exception |
|---|
| 131 | n/a | assert_python_failure('-c', 'raise Exception') |
|---|
| 132 | n/a | # All good if execution is successful |
|---|
| 133 | n/a | assert_python_ok('-c', 'pass') |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | @unittest.skipUnless(test.support.FS_NONASCII, 'need support.FS_NONASCII') |
|---|
| 136 | n/a | def test_non_ascii(self): |
|---|
| 137 | n/a | # Test handling of non-ascii data |
|---|
| 138 | n/a | command = ("assert(ord(%r) == %s)" |
|---|
| 139 | n/a | % (test.support.FS_NONASCII, ord(test.support.FS_NONASCII))) |
|---|
| 140 | n/a | assert_python_ok('-c', command) |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | # On Windows, pass bytes to subprocess doesn't test how Python decodes the |
|---|
| 143 | n/a | # command line, but how subprocess does decode bytes to unicode. Python |
|---|
| 144 | n/a | # doesn't decode the command line because Windows provides directly the |
|---|
| 145 | n/a | # arguments as unicode (using wmain() instead of main()). |
|---|
| 146 | n/a | @unittest.skipIf(sys.platform == 'win32', |
|---|
| 147 | n/a | 'Windows has a native unicode API') |
|---|
| 148 | n/a | def test_undecodable_code(self): |
|---|
| 149 | n/a | undecodable = b"\xff" |
|---|
| 150 | n/a | env = os.environ.copy() |
|---|
| 151 | n/a | # Use C locale to get ascii for the locale encoding |
|---|
| 152 | n/a | env['LC_ALL'] = 'C' |
|---|
| 153 | n/a | code = ( |
|---|
| 154 | n/a | b'import locale; ' |
|---|
| 155 | n/a | b'print(ascii("' + undecodable + b'"), ' |
|---|
| 156 | n/a | b'locale.getpreferredencoding())') |
|---|
| 157 | n/a | p = subprocess.Popen( |
|---|
| 158 | n/a | [sys.executable, "-c", code], |
|---|
| 159 | n/a | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, |
|---|
| 160 | n/a | env=env) |
|---|
| 161 | n/a | stdout, stderr = p.communicate() |
|---|
| 162 | n/a | if p.returncode == 1: |
|---|
| 163 | n/a | # _Py_char2wchar() decoded b'\xff' as '\udcff' (b'\xff' is not |
|---|
| 164 | n/a | # decodable from ASCII) and run_command() failed on |
|---|
| 165 | n/a | # PyUnicode_AsUTF8String(). This is the expected behaviour on |
|---|
| 166 | n/a | # Linux. |
|---|
| 167 | n/a | pattern = b"Unable to decode the command from the command line:" |
|---|
| 168 | n/a | elif p.returncode == 0: |
|---|
| 169 | n/a | # _Py_char2wchar() decoded b'\xff' as '\xff' even if the locale is |
|---|
| 170 | n/a | # C and the locale encoding is ASCII. It occurs on FreeBSD, Solaris |
|---|
| 171 | n/a | # and Mac OS X. |
|---|
| 172 | n/a | pattern = b"'\\xff' " |
|---|
| 173 | n/a | # The output is followed by the encoding name, an alias to ASCII. |
|---|
| 174 | n/a | # Examples: "US-ASCII" or "646" (ISO 646, on Solaris). |
|---|
| 175 | n/a | else: |
|---|
| 176 | n/a | raise AssertionError("Unknown exit code: %s, output=%a" % (p.returncode, stdout)) |
|---|
| 177 | n/a | if not stdout.startswith(pattern): |
|---|
| 178 | n/a | raise AssertionError("%a doesn't start with %a" % (stdout, pattern)) |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | @unittest.skipUnless((sys.platform == 'darwin' or |
|---|
| 181 | n/a | is_android), 'test specific to Mac OS X and Android') |
|---|
| 182 | n/a | def test_osx_android_utf8(self): |
|---|
| 183 | n/a | def check_output(text): |
|---|
| 184 | n/a | decoded = text.decode('utf-8', 'surrogateescape') |
|---|
| 185 | n/a | expected = ascii(decoded).encode('ascii') + b'\n' |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | env = os.environ.copy() |
|---|
| 188 | n/a | # C locale gives ASCII locale encoding, but Python uses UTF-8 |
|---|
| 189 | n/a | # to parse the command line arguments on Mac OS X and Android. |
|---|
| 190 | n/a | env['LC_ALL'] = 'C' |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | p = subprocess.Popen( |
|---|
| 193 | n/a | (sys.executable, "-c", "import sys; print(ascii(sys.argv[1]))", text), |
|---|
| 194 | n/a | stdout=subprocess.PIPE, |
|---|
| 195 | n/a | env=env) |
|---|
| 196 | n/a | stdout, stderr = p.communicate() |
|---|
| 197 | n/a | self.assertEqual(stdout, expected) |
|---|
| 198 | n/a | self.assertEqual(p.returncode, 0) |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | # test valid utf-8 |
|---|
| 201 | n/a | text = 'e:\xe9, euro:\u20ac, non-bmp:\U0010ffff'.encode('utf-8') |
|---|
| 202 | n/a | check_output(text) |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | # test invalid utf-8 |
|---|
| 205 | n/a | text = ( |
|---|
| 206 | n/a | b'\xff' # invalid byte |
|---|
| 207 | n/a | b'\xc3\xa9' # valid utf-8 character |
|---|
| 208 | n/a | b'\xc3\xff' # invalid byte sequence |
|---|
| 209 | n/a | b'\xed\xa0\x80' # lone surrogate character (invalid) |
|---|
| 210 | n/a | ) |
|---|
| 211 | n/a | check_output(text) |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | def test_unbuffered_output(self): |
|---|
| 214 | n/a | # Test expected operation of the '-u' switch |
|---|
| 215 | n/a | for stream in ('stdout', 'stderr'): |
|---|
| 216 | n/a | # Binary is unbuffered |
|---|
| 217 | n/a | code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)" |
|---|
| 218 | n/a | % stream) |
|---|
| 219 | n/a | rc, out, err = assert_python_ok('-u', '-c', code) |
|---|
| 220 | n/a | data = err if stream == 'stderr' else out |
|---|
| 221 | n/a | self.assertEqual(data, b'x', "binary %s not unbuffered" % stream) |
|---|
| 222 | n/a | # Text is line-buffered |
|---|
| 223 | n/a | code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)" |
|---|
| 224 | n/a | % stream) |
|---|
| 225 | n/a | rc, out, err = assert_python_ok('-u', '-c', code) |
|---|
| 226 | n/a | data = err if stream == 'stderr' else out |
|---|
| 227 | n/a | self.assertEqual(data.strip(), b'x', |
|---|
| 228 | n/a | "text %s not line-buffered" % stream) |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | def test_unbuffered_input(self): |
|---|
| 231 | n/a | # sys.stdin still works with '-u' |
|---|
| 232 | n/a | code = ("import sys; sys.stdout.write(sys.stdin.read(1))") |
|---|
| 233 | n/a | p = spawn_python('-u', '-c', code) |
|---|
| 234 | n/a | p.stdin.write(b'x') |
|---|
| 235 | n/a | p.stdin.flush() |
|---|
| 236 | n/a | data, rc = _kill_python_and_exit_code(p) |
|---|
| 237 | n/a | self.assertEqual(rc, 0) |
|---|
| 238 | n/a | self.assertTrue(data.startswith(b'x'), data) |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | def test_large_PYTHONPATH(self): |
|---|
| 241 | n/a | path1 = "ABCDE" * 100 |
|---|
| 242 | n/a | path2 = "FGHIJ" * 100 |
|---|
| 243 | n/a | path = path1 + os.pathsep + path2 |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | code = """if 1: |
|---|
| 246 | n/a | import sys |
|---|
| 247 | n/a | path = ":".join(sys.path) |
|---|
| 248 | n/a | path = path.encode("ascii", "backslashreplace") |
|---|
| 249 | n/a | sys.stdout.buffer.write(path)""" |
|---|
| 250 | n/a | rc, out, err = assert_python_ok('-S', '-c', code, |
|---|
| 251 | n/a | PYTHONPATH=path) |
|---|
| 252 | n/a | self.assertIn(path1.encode('ascii'), out) |
|---|
| 253 | n/a | self.assertIn(path2.encode('ascii'), out) |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | def test_empty_PYTHONPATH_issue16309(self): |
|---|
| 256 | n/a | # On Posix, it is documented that setting PATH to the |
|---|
| 257 | n/a | # empty string is equivalent to not setting PATH at all, |
|---|
| 258 | n/a | # which is an exception to the rule that in a string like |
|---|
| 259 | n/a | # "/bin::/usr/bin" the empty string in the middle gets |
|---|
| 260 | n/a | # interpreted as '.' |
|---|
| 261 | n/a | code = """if 1: |
|---|
| 262 | n/a | import sys |
|---|
| 263 | n/a | path = ":".join(sys.path) |
|---|
| 264 | n/a | path = path.encode("ascii", "backslashreplace") |
|---|
| 265 | n/a | sys.stdout.buffer.write(path)""" |
|---|
| 266 | n/a | rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="") |
|---|
| 267 | n/a | rc2, out2, err2 = assert_python_ok('-c', code, __isolated=False) |
|---|
| 268 | n/a | # regarding to Posix specification, outputs should be equal |
|---|
| 269 | n/a | # for empty and unset PYTHONPATH |
|---|
| 270 | n/a | self.assertEqual(out1, out2) |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | def test_displayhook_unencodable(self): |
|---|
| 273 | n/a | for encoding in ('ascii', 'latin-1', 'utf-8'): |
|---|
| 274 | n/a | # We are testing a PYTHON environment variable here, so we can't |
|---|
| 275 | n/a | # use -E, -I, or script_helper (which uses them). So instead we do |
|---|
| 276 | n/a | # poor-man's isolation by deleting the PYTHON vars from env. |
|---|
| 277 | n/a | env = {key:value for (key,value) in os.environ.copy().items() |
|---|
| 278 | n/a | if not key.startswith('PYTHON')} |
|---|
| 279 | n/a | env['PYTHONIOENCODING'] = encoding |
|---|
| 280 | n/a | p = subprocess.Popen( |
|---|
| 281 | n/a | [sys.executable, '-i'], |
|---|
| 282 | n/a | stdin=subprocess.PIPE, |
|---|
| 283 | n/a | stdout=subprocess.PIPE, |
|---|
| 284 | n/a | stderr=subprocess.STDOUT, |
|---|
| 285 | n/a | env=env) |
|---|
| 286 | n/a | # non-ascii, surrogate, non-BMP printable, non-BMP unprintable |
|---|
| 287 | n/a | text = "a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF" |
|---|
| 288 | n/a | p.stdin.write(ascii(text).encode('ascii') + b"\n") |
|---|
| 289 | n/a | p.stdin.write(b'exit()\n') |
|---|
| 290 | n/a | data = kill_python(p) |
|---|
| 291 | n/a | escaped = repr(text).encode(encoding, 'backslashreplace') |
|---|
| 292 | n/a | self.assertIn(escaped, data) |
|---|
| 293 | n/a | |
|---|
| 294 | n/a | def check_input(self, code, expected): |
|---|
| 295 | n/a | with tempfile.NamedTemporaryFile("wb+") as stdin: |
|---|
| 296 | n/a | sep = os.linesep.encode('ASCII') |
|---|
| 297 | n/a | stdin.write(sep.join((b'abc', b'def'))) |
|---|
| 298 | n/a | stdin.flush() |
|---|
| 299 | n/a | stdin.seek(0) |
|---|
| 300 | n/a | with subprocess.Popen( |
|---|
| 301 | n/a | (sys.executable, "-c", code), |
|---|
| 302 | n/a | stdin=stdin, stdout=subprocess.PIPE) as proc: |
|---|
| 303 | n/a | stdout, stderr = proc.communicate() |
|---|
| 304 | n/a | self.assertEqual(stdout.rstrip(), expected) |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | def test_stdin_readline(self): |
|---|
| 307 | n/a | # Issue #11272: check that sys.stdin.readline() replaces '\r\n' by '\n' |
|---|
| 308 | n/a | # on Windows (sys.stdin is opened in binary mode) |
|---|
| 309 | n/a | self.check_input( |
|---|
| 310 | n/a | "import sys; print(repr(sys.stdin.readline()))", |
|---|
| 311 | n/a | b"'abc\\n'") |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | def test_builtin_input(self): |
|---|
| 314 | n/a | # Issue #11272: check that input() strips newlines ('\n' or '\r\n') |
|---|
| 315 | n/a | self.check_input( |
|---|
| 316 | n/a | "print(repr(input()))", |
|---|
| 317 | n/a | b"'abc'") |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | def test_output_newline(self): |
|---|
| 320 | n/a | # Issue 13119 Newline for print() should be \r\n on Windows. |
|---|
| 321 | n/a | code = """if 1: |
|---|
| 322 | n/a | import sys |
|---|
| 323 | n/a | print(1) |
|---|
| 324 | n/a | print(2) |
|---|
| 325 | n/a | print(3, file=sys.stderr) |
|---|
| 326 | n/a | print(4, file=sys.stderr)""" |
|---|
| 327 | n/a | rc, out, err = assert_python_ok('-c', code) |
|---|
| 328 | n/a | |
|---|
| 329 | n/a | if sys.platform == 'win32': |
|---|
| 330 | n/a | self.assertEqual(b'1\r\n2\r\n', out) |
|---|
| 331 | n/a | self.assertEqual(b'3\r\n4', err) |
|---|
| 332 | n/a | else: |
|---|
| 333 | n/a | self.assertEqual(b'1\n2\n', out) |
|---|
| 334 | n/a | self.assertEqual(b'3\n4', err) |
|---|
| 335 | n/a | |
|---|
| 336 | n/a | def test_unmached_quote(self): |
|---|
| 337 | n/a | # Issue #10206: python program starting with unmatched quote |
|---|
| 338 | n/a | # spewed spaces to stdout |
|---|
| 339 | n/a | rc, out, err = assert_python_failure('-c', "'") |
|---|
| 340 | n/a | self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError') |
|---|
| 341 | n/a | self.assertEqual(b'', out) |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | def test_stdout_flush_at_shutdown(self): |
|---|
| 344 | n/a | # Issue #5319: if stdout.flush() fails at shutdown, an error should |
|---|
| 345 | n/a | # be printed out. |
|---|
| 346 | n/a | code = """if 1: |
|---|
| 347 | n/a | import os, sys, test.support |
|---|
| 348 | n/a | test.support.SuppressCrashReport().__enter__() |
|---|
| 349 | n/a | sys.stdout.write('x') |
|---|
| 350 | n/a | os.close(sys.stdout.fileno())""" |
|---|
| 351 | n/a | rc, out, err = assert_python_failure('-c', code) |
|---|
| 352 | n/a | self.assertEqual(b'', out) |
|---|
| 353 | n/a | self.assertEqual(120, rc) |
|---|
| 354 | n/a | self.assertRegex(err.decode('ascii', 'ignore'), |
|---|
| 355 | n/a | 'Exception ignored in.*\nOSError: .*') |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | def test_closed_stdout(self): |
|---|
| 358 | n/a | # Issue #13444: if stdout has been explicitly closed, we should |
|---|
| 359 | n/a | # not attempt to flush it at shutdown. |
|---|
| 360 | n/a | code = "import sys; sys.stdout.close()" |
|---|
| 361 | n/a | rc, out, err = assert_python_ok('-c', code) |
|---|
| 362 | n/a | self.assertEqual(b'', err) |
|---|
| 363 | n/a | |
|---|
| 364 | n/a | # Issue #7111: Python should work without standard streams |
|---|
| 365 | n/a | |
|---|
| 366 | n/a | @unittest.skipIf(os.name != 'posix', "test needs POSIX semantics") |
|---|
| 367 | n/a | def _test_no_stdio(self, streams): |
|---|
| 368 | n/a | code = """if 1: |
|---|
| 369 | n/a | import os, sys |
|---|
| 370 | n/a | for i, s in enumerate({streams}): |
|---|
| 371 | n/a | if getattr(sys, s) is not None: |
|---|
| 372 | n/a | os._exit(i + 1) |
|---|
| 373 | n/a | os._exit(42)""".format(streams=streams) |
|---|
| 374 | n/a | def preexec(): |
|---|
| 375 | n/a | if 'stdin' in streams: |
|---|
| 376 | n/a | os.close(0) |
|---|
| 377 | n/a | if 'stdout' in streams: |
|---|
| 378 | n/a | os.close(1) |
|---|
| 379 | n/a | if 'stderr' in streams: |
|---|
| 380 | n/a | os.close(2) |
|---|
| 381 | n/a | p = subprocess.Popen( |
|---|
| 382 | n/a | [sys.executable, "-E", "-c", code], |
|---|
| 383 | n/a | stdin=subprocess.PIPE, |
|---|
| 384 | n/a | stdout=subprocess.PIPE, |
|---|
| 385 | n/a | stderr=subprocess.PIPE, |
|---|
| 386 | n/a | preexec_fn=preexec) |
|---|
| 387 | n/a | out, err = p.communicate() |
|---|
| 388 | n/a | self.assertEqual(test.support.strip_python_stderr(err), b'') |
|---|
| 389 | n/a | self.assertEqual(p.returncode, 42) |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | def test_no_stdin(self): |
|---|
| 392 | n/a | self._test_no_stdio(['stdin']) |
|---|
| 393 | n/a | |
|---|
| 394 | n/a | def test_no_stdout(self): |
|---|
| 395 | n/a | self._test_no_stdio(['stdout']) |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | def test_no_stderr(self): |
|---|
| 398 | n/a | self._test_no_stdio(['stderr']) |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | def test_no_std_streams(self): |
|---|
| 401 | n/a | self._test_no_stdio(['stdin', 'stdout', 'stderr']) |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | def test_hash_randomization(self): |
|---|
| 404 | n/a | # Verify that -R enables hash randomization: |
|---|
| 405 | n/a | self.verify_valid_flag('-R') |
|---|
| 406 | n/a | hashes = [] |
|---|
| 407 | n/a | if os.environ.get('PYTHONHASHSEED', 'random') != 'random': |
|---|
| 408 | n/a | env = dict(os.environ) # copy |
|---|
| 409 | n/a | # We need to test that it is enabled by default without |
|---|
| 410 | n/a | # the environment variable enabling it for us. |
|---|
| 411 | n/a | del env['PYTHONHASHSEED'] |
|---|
| 412 | n/a | env['__cleanenv'] = '1' # consumed by assert_python_ok() |
|---|
| 413 | n/a | else: |
|---|
| 414 | n/a | env = {} |
|---|
| 415 | n/a | for i in range(3): |
|---|
| 416 | n/a | code = 'print(hash("spam"))' |
|---|
| 417 | n/a | rc, out, err = assert_python_ok('-c', code, **env) |
|---|
| 418 | n/a | self.assertEqual(rc, 0) |
|---|
| 419 | n/a | hashes.append(out) |
|---|
| 420 | n/a | hashes = sorted(set(hashes)) # uniq |
|---|
| 421 | n/a | # Rare chance of failure due to 3 random seeds honestly being equal. |
|---|
| 422 | n/a | self.assertGreater(len(hashes), 1, |
|---|
| 423 | n/a | msg='3 runs produced an identical random hash ' |
|---|
| 424 | n/a | ' for "spam": {}'.format(hashes)) |
|---|
| 425 | n/a | |
|---|
| 426 | n/a | # Verify that sys.flags contains hash_randomization |
|---|
| 427 | n/a | code = 'import sys; print("random is", sys.flags.hash_randomization)' |
|---|
| 428 | n/a | rc, out, err = assert_python_ok('-c', code) |
|---|
| 429 | n/a | self.assertEqual(rc, 0) |
|---|
| 430 | n/a | self.assertIn(b'random is 1', out) |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | def test_del___main__(self): |
|---|
| 433 | n/a | # Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a |
|---|
| 434 | n/a | # borrowed reference to the dict of __main__ module and later modify |
|---|
| 435 | n/a | # the dict whereas the module was destroyed |
|---|
| 436 | n/a | filename = test.support.TESTFN |
|---|
| 437 | n/a | self.addCleanup(test.support.unlink, filename) |
|---|
| 438 | n/a | with open(filename, "w") as script: |
|---|
| 439 | n/a | print("import sys", file=script) |
|---|
| 440 | n/a | print("del sys.modules['__main__']", file=script) |
|---|
| 441 | n/a | assert_python_ok(filename) |
|---|
| 442 | n/a | |
|---|
| 443 | n/a | def test_unknown_options(self): |
|---|
| 444 | n/a | rc, out, err = assert_python_failure('-E', '-z') |
|---|
| 445 | n/a | self.assertIn(b'Unknown option: -z', err) |
|---|
| 446 | n/a | self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1) |
|---|
| 447 | n/a | self.assertEqual(b'', out) |
|---|
| 448 | n/a | # Add "without='-E'" to prevent _assert_python to append -E |
|---|
| 449 | n/a | # to env_vars and change the output of stderr |
|---|
| 450 | n/a | rc, out, err = assert_python_failure('-z', without='-E') |
|---|
| 451 | n/a | self.assertIn(b'Unknown option: -z', err) |
|---|
| 452 | n/a | self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1) |
|---|
| 453 | n/a | self.assertEqual(b'', out) |
|---|
| 454 | n/a | rc, out, err = assert_python_failure('-a', '-z', without='-E') |
|---|
| 455 | n/a | self.assertIn(b'Unknown option: -a', err) |
|---|
| 456 | n/a | # only the first unknown option is reported |
|---|
| 457 | n/a | self.assertNotIn(b'Unknown option: -z', err) |
|---|
| 458 | n/a | self.assertEqual(err.splitlines().count(b'Unknown option: -a'), 1) |
|---|
| 459 | n/a | self.assertEqual(b'', out) |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | @unittest.skipIf(script_helper.interpreter_requires_environment(), |
|---|
| 462 | n/a | 'Cannot run -I tests when PYTHON env vars are required.') |
|---|
| 463 | n/a | def test_isolatedmode(self): |
|---|
| 464 | n/a | self.verify_valid_flag('-I') |
|---|
| 465 | n/a | self.verify_valid_flag('-IEs') |
|---|
| 466 | n/a | rc, out, err = assert_python_ok('-I', '-c', |
|---|
| 467 | n/a | 'from sys import flags as f; ' |
|---|
| 468 | n/a | 'print(f.no_user_site, f.ignore_environment, f.isolated)', |
|---|
| 469 | n/a | # dummyvar to prevent extraneous -E |
|---|
| 470 | n/a | dummyvar="") |
|---|
| 471 | n/a | self.assertEqual(out.strip(), b'1 1 1') |
|---|
| 472 | n/a | with test.support.temp_cwd() as tmpdir: |
|---|
| 473 | n/a | fake = os.path.join(tmpdir, "uuid.py") |
|---|
| 474 | n/a | main = os.path.join(tmpdir, "main.py") |
|---|
| 475 | n/a | with open(fake, "w") as f: |
|---|
| 476 | n/a | f.write("raise RuntimeError('isolated mode test')\n") |
|---|
| 477 | n/a | with open(main, "w") as f: |
|---|
| 478 | n/a | f.write("import uuid\n") |
|---|
| 479 | n/a | f.write("print('ok')\n") |
|---|
| 480 | n/a | self.assertRaises(subprocess.CalledProcessError, |
|---|
| 481 | n/a | subprocess.check_output, |
|---|
| 482 | n/a | [sys.executable, main], cwd=tmpdir, |
|---|
| 483 | n/a | stderr=subprocess.DEVNULL) |
|---|
| 484 | n/a | out = subprocess.check_output([sys.executable, "-I", main], |
|---|
| 485 | n/a | cwd=tmpdir) |
|---|
| 486 | n/a | self.assertEqual(out.strip(), b"ok") |
|---|
| 487 | n/a | |
|---|
| 488 | n/a | def test_main(): |
|---|
| 489 | n/a | test.support.run_unittest(CmdLineTest) |
|---|
| 490 | n/a | test.support.reap_children() |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | if __name__ == "__main__": |
|---|
| 493 | n/a | test_main() |
|---|