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