Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1 | # Tests invocation of the interpreter with various command line arguments |
| 2 | # All tests are executed with environment variables ignored |
| 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 | |
Philip Jenvey | ab7481a | 2009-05-22 05:46:35 +0000 | [diff] [blame] | 5 | import os |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 6 | import test.support, unittest |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 7 | import os |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 8 | import sys |
Walter Dörwald | 9356fb9 | 2005-11-25 15:22:10 +0000 | [diff] [blame] | 9 | import subprocess |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 11 | def _spawn_python(*args): |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 12 | cmd_line = [sys.executable] |
| 13 | # When testing -S, we need PYTHONPATH to work (see test_site_flag()) |
| 14 | if '-S' not in args: |
| 15 | cmd_line.append('-E') |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 16 | cmd_line.extend(args) |
| 17 | return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| 18 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 19 | |
| 20 | def _kill_python(p): |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 21 | return _kill_python_and_exit_code(p)[0] |
| 22 | |
| 23 | def _kill_python_and_exit_code(p): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 24 | p.stdin.close() |
| 25 | data = p.stdout.read() |
| 26 | p.stdout.close() |
| 27 | # try to cleanup the child so we don't appear to leak when running |
| 28 | # with regrtest -R. This should be a no-op on Windows. |
| 29 | subprocess._cleanup() |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 30 | returncode = p.wait() |
| 31 | return data, returncode |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 32 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 33 | class CmdLineTest(unittest.TestCase): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 34 | def start_python(self, *args): |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 35 | return self.start_python_and_exit_code(*args)[0] |
| 36 | |
| 37 | def start_python_and_exit_code(self, *args): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 38 | p = _spawn_python(*args) |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 39 | return _kill_python_and_exit_code(p) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 40 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 41 | def exit_code(self, *args): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 42 | cmd_line = [sys.executable, '-E'] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 43 | cmd_line.extend(args) |
Philip Jenvey | ab7481a | 2009-05-22 05:46:35 +0000 | [diff] [blame] | 44 | with open(os.devnull, 'w') as devnull: |
| 45 | return subprocess.call(cmd_line, stdout=devnull, |
| 46 | stderr=subprocess.STDOUT) |
Walter Dörwald | 9356fb9 | 2005-11-25 15:22:10 +0000 | [diff] [blame] | 47 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 48 | def test_directories(self): |
Neal Norwitz | 72c2c06 | 2006-03-09 05:58:11 +0000 | [diff] [blame] | 49 | self.assertNotEqual(self.exit_code('.'), 0) |
| 50 | self.assertNotEqual(self.exit_code('< .'), 0) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 51 | |
| 52 | def verify_valid_flag(self, cmd_line): |
| 53 | data = self.start_python(cmd_line) |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 54 | self.assertTrue(data == b'' or data.endswith(b'\n')) |
Guido van Rossum | f074b64 | 2007-07-11 06:56:16 +0000 | [diff] [blame] | 55 | self.assertTrue(b'Traceback' not in data) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 56 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 57 | def test_optimize(self): |
| 58 | self.verify_valid_flag('-O') |
| 59 | self.verify_valid_flag('-OO') |
| 60 | |
| 61 | def test_q(self): |
| 62 | self.verify_valid_flag('-Qold') |
| 63 | self.verify_valid_flag('-Qnew') |
| 64 | self.verify_valid_flag('-Qwarn') |
| 65 | self.verify_valid_flag('-Qwarnall') |
| 66 | |
| 67 | def test_site_flag(self): |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 68 | if os.name == 'posix': |
| 69 | # Workaround bug #586680 by adding the extension dir to PYTHONPATH |
| 70 | from distutils.util import get_platform |
| 71 | s = "./build/lib.%s-%.3s" % (get_platform(), sys.version) |
| 72 | if hasattr(sys, 'gettotalrefcount'): |
| 73 | s += '-pydebug' |
| 74 | p = os.environ.get('PYTHONPATH', '') |
| 75 | if p: |
| 76 | p += ':' |
| 77 | os.environ['PYTHONPATH'] = p + s |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 78 | self.verify_valid_flag('-S') |
| 79 | |
| 80 | def test_usage(self): |
Guido van Rossum | f074b64 | 2007-07-11 06:56:16 +0000 | [diff] [blame] | 81 | self.assertTrue(b'usage' in self.start_python('-h')) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 82 | |
| 83 | def test_version(self): |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 84 | version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii") |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 85 | self.assertTrue(self.start_python('-V').startswith(version)) |
| 86 | |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 87 | def test_verbose(self): |
| 88 | # -v causes imports to write to stderr. If the write to |
| 89 | # stderr itself causes an import to happen (for the output |
| 90 | # codec), a recursion loop can occur. |
| 91 | data, rc = self.start_python_and_exit_code('-v') |
| 92 | self.assertEqual(rc, 0) |
| 93 | self.assertTrue(b'stack overflow' not in data) |
| 94 | data, rc = self.start_python_and_exit_code('-vv') |
| 95 | self.assertEqual(rc, 0) |
| 96 | self.assertTrue(b'stack overflow' not in data) |
| 97 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 98 | def test_run_module(self): |
| 99 | # Test expected operation of the '-m' switch |
| 100 | # Switch needs an argument |
| 101 | self.assertNotEqual(self.exit_code('-m'), 0) |
| 102 | # Check we get an error for a nonexistent module |
| 103 | self.assertNotEqual( |
| 104 | self.exit_code('-m', 'fnord43520xyz'), |
| 105 | 0) |
| 106 | # Check the runpy module also gives an error for |
| 107 | # a nonexistent module |
| 108 | self.assertNotEqual( |
| 109 | self.exit_code('-m', 'runpy', 'fnord43520xyz'), |
| 110 | 0) |
| 111 | # All good if module is located and run successfully |
| 112 | self.assertEqual( |
| 113 | self.exit_code('-m', 'timeit', '-n', '1'), |
| 114 | 0) |
| 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 |
| 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') |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 123 | data = _kill_python(p) |
| 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 |
| 130 | self.assertNotEqual(self.exit_code('-c'), 0) |
| 131 | # Check we get an error for an uncaught exception |
| 132 | self.assertNotEqual( |
| 133 | self.exit_code('-c', 'raise Exception'), |
| 134 | 0) |
| 135 | # All good if execution is successful |
| 136 | self.assertEqual( |
| 137 | self.exit_code('-c', 'pass'), |
| 138 | 0) |
| 139 | |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 140 | # Test handling of non-ascii data |
Amaury Forgeot d'Arc | e255764 | 2008-11-12 00:59:11 +0000 | [diff] [blame] | 141 | if sys.getfilesystemencoding() != 'ascii': |
| 142 | command = "assert(ord('\xe9') == 0xe9)" |
| 143 | self.assertEqual( |
| 144 | self.exit_code('-c', command), |
| 145 | 0) |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame] | 146 | |
Antoine Pitrou | 0560843 | 2009-01-09 18:53:14 +0000 | [diff] [blame] | 147 | def test_unbuffered_output(self): |
| 148 | # Test expected operation of the '-u' switch |
| 149 | for stream in ('stdout', 'stderr'): |
| 150 | # Binary is unbuffered |
| 151 | code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)" |
| 152 | % stream) |
| 153 | data, rc = self.start_python_and_exit_code('-u', '-c', code) |
| 154 | self.assertEqual(rc, 0) |
| 155 | self.assertEqual(data, b'x', "binary %s not unbuffered" % stream) |
| 156 | # Text is line-buffered |
| 157 | code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)" |
| 158 | % stream) |
| 159 | data, rc = self.start_python_and_exit_code('-u', '-c', code) |
| 160 | self.assertEqual(rc, 0) |
| 161 | self.assertEqual(data.strip(), b'x', |
| 162 | "text %s not line-buffered" % stream) |
| 163 | |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 164 | def test_unbuffered_input(self): |
| 165 | # sys.stdin still works with '-u' |
| 166 | code = ("import sys; sys.stdout.write(sys.stdin.read(1))") |
| 167 | p = _spawn_python('-u', '-c', code) |
| 168 | p.stdin.write(b'x') |
| 169 | p.stdin.flush() |
| 170 | data, rc = _kill_python_and_exit_code(p) |
| 171 | self.assertEqual(rc, 0) |
Antoine Pitrou | 39df610 | 2009-01-26 22:00:21 +0000 | [diff] [blame] | 172 | self.assert_(data.startswith(b'x'), data) |
Antoine Pitrou | 27fe9fc | 2009-01-26 21:48:00 +0000 | [diff] [blame] | 173 | |
Amaury Forgeot d'Arc | 66f8c43 | 2009-06-09 21:30:01 +0000 | [diff] [blame^] | 174 | def test_large_PYTHONPATH(self): |
| 175 | with test.support.EnvironmentVarGuard() as env: |
| 176 | path1 = "ABCDE" * 100 |
| 177 | path2 = "FGHIJ" * 100 |
| 178 | env['PYTHONPATH'] = path1 + os.pathsep + path2 |
| 179 | p = _spawn_python('-S', '-c', 'import sys; print(sys.path)') |
| 180 | stdout, _ = p.communicate() |
| 181 | self.assert_(path1.encode('ascii') in stdout) |
| 182 | self.assert_(path2.encode('ascii') in stdout) |
| 183 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 184 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 185 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 186 | test.support.run_unittest(CmdLineTest) |
| 187 | test.support.reap_children() |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 188 | |
| 189 | if __name__ == "__main__": |
| 190 | test_main() |