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