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 | |
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 |
Walter Dörwald | 9356fb9 | 2005-11-25 15:22:10 +0000 | [diff] [blame] | 8 | import subprocess |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 9 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 10 | def _spawn_python(*args): |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 11 | cmd_line = [sys.executable] |
| 12 | # When testing -S, we need PYTHONPATH to work (see test_site_flag()) |
| 13 | if '-S' not in args: |
| 14 | cmd_line.append('-E') |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 15 | cmd_line.extend(args) |
| 16 | return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| 17 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 18 | |
| 19 | def _kill_python(p): |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 20 | return _kill_python_and_exit_code(p)[0] |
| 21 | |
| 22 | def _kill_python_and_exit_code(p): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 23 | p.stdin.close() |
| 24 | data = p.stdout.read() |
| 25 | p.stdout.close() |
| 26 | # try to cleanup the child so we don't appear to leak when running |
| 27 | # with regrtest -R. This should be a no-op on Windows. |
| 28 | subprocess._cleanup() |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 29 | returncode = p.wait() |
| 30 | return data, returncode |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 31 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 32 | class CmdLineTest(unittest.TestCase): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 33 | def start_python(self, *args): |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 34 | return self.start_python_and_exit_code(*args)[0] |
| 35 | |
| 36 | def start_python_and_exit_code(self, *args): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 37 | p = _spawn_python(*args) |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 38 | return _kill_python_and_exit_code(p) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 39 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 40 | def exit_code(self, *args): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 41 | cmd_line = [sys.executable, '-E'] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 42 | cmd_line.extend(args) |
| 43 | return subprocess.call(cmd_line, stdout=subprocess.PIPE, |
| 44 | stderr=subprocess.PIPE) |
Walter Dörwald | 9356fb9 | 2005-11-25 15:22:10 +0000 | [diff] [blame] | 45 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 46 | def test_directories(self): |
Neal Norwitz | 72c2c06 | 2006-03-09 05:58:11 +0000 | [diff] [blame] | 47 | self.assertNotEqual(self.exit_code('.'), 0) |
| 48 | self.assertNotEqual(self.exit_code('< .'), 0) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 49 | |
| 50 | def verify_valid_flag(self, cmd_line): |
| 51 | data = self.start_python(cmd_line) |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 52 | self.assertTrue(data == b'' or data.endswith(b'\n')) |
Guido van Rossum | f074b64 | 2007-07-11 06:56:16 +0000 | [diff] [blame] | 53 | self.assertTrue(b'Traceback' not in data) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 54 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 55 | def test_optimize(self): |
| 56 | self.verify_valid_flag('-O') |
| 57 | self.verify_valid_flag('-OO') |
| 58 | |
| 59 | def test_q(self): |
| 60 | self.verify_valid_flag('-Qold') |
| 61 | self.verify_valid_flag('-Qnew') |
| 62 | self.verify_valid_flag('-Qwarn') |
| 63 | self.verify_valid_flag('-Qwarnall') |
| 64 | |
| 65 | def test_site_flag(self): |
Antoine Pitrou | 8769576 | 2008-08-14 22:44:29 +0000 | [diff] [blame] | 66 | if os.name == 'posix': |
| 67 | # Workaround bug #586680 by adding the extension dir to PYTHONPATH |
| 68 | from distutils.util import get_platform |
| 69 | s = "./build/lib.%s-%.3s" % (get_platform(), sys.version) |
| 70 | if hasattr(sys, 'gettotalrefcount'): |
| 71 | s += '-pydebug' |
| 72 | p = os.environ.get('PYTHONPATH', '') |
| 73 | if p: |
| 74 | p += ':' |
| 75 | os.environ['PYTHONPATH'] = p + s |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 76 | self.verify_valid_flag('-S') |
| 77 | |
| 78 | def test_usage(self): |
Guido van Rossum | f074b64 | 2007-07-11 06:56:16 +0000 | [diff] [blame] | 79 | self.assertTrue(b'usage' in self.start_python('-h')) |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 80 | |
| 81 | def test_version(self): |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 82 | version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii") |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 83 | self.assertTrue(self.start_python('-V').startswith(version)) |
| 84 | |
Trent Nelson | 39e307e | 2008-03-19 06:45:48 +0000 | [diff] [blame] | 85 | def test_verbose(self): |
| 86 | # -v causes imports to write to stderr. If the write to |
| 87 | # stderr itself causes an import to happen (for the output |
| 88 | # codec), a recursion loop can occur. |
| 89 | data, rc = self.start_python_and_exit_code('-v') |
| 90 | self.assertEqual(rc, 0) |
| 91 | self.assertTrue(b'stack overflow' not in data) |
| 92 | data, rc = self.start_python_and_exit_code('-vv') |
| 93 | self.assertEqual(rc, 0) |
| 94 | self.assertTrue(b'stack overflow' not in data) |
| 95 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 96 | def test_run_module(self): |
| 97 | # Test expected operation of the '-m' switch |
| 98 | # Switch needs an argument |
| 99 | self.assertNotEqual(self.exit_code('-m'), 0) |
| 100 | # Check we get an error for a nonexistent module |
| 101 | self.assertNotEqual( |
| 102 | self.exit_code('-m', 'fnord43520xyz'), |
| 103 | 0) |
| 104 | # Check the runpy module also gives an error for |
| 105 | # a nonexistent module |
| 106 | self.assertNotEqual( |
| 107 | self.exit_code('-m', 'runpy', 'fnord43520xyz'), |
| 108 | 0) |
| 109 | # All good if module is located and run successfully |
| 110 | self.assertEqual( |
| 111 | self.exit_code('-m', 'timeit', '-n', '1'), |
| 112 | 0) |
| 113 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 114 | def test_run_module_bug1764407(self): |
| 115 | # -m and -i need to play well together |
| 116 | # Runs the timeit module and checks the __main__ |
| 117 | # namespace has been populated appropriately |
| 118 | p = _spawn_python('-i', '-m', 'timeit', '-n', '1') |
Guido van Rossum | a1c42a9 | 2007-08-29 03:47:36 +0000 | [diff] [blame] | 119 | p.stdin.write(b'Timer\n') |
| 120 | p.stdin.write(b'exit()\n') |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 121 | data = _kill_python(p) |
| 122 | self.assertTrue(data.find(b'1 loop') != -1) |
| 123 | self.assertTrue(data.find(b'__main__.Timer') != -1) |
| 124 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 125 | def test_run_code(self): |
| 126 | # Test expected operation of the '-c' switch |
| 127 | # Switch needs an argument |
| 128 | self.assertNotEqual(self.exit_code('-c'), 0) |
| 129 | # Check we get an error for an uncaught exception |
| 130 | self.assertNotEqual( |
| 131 | self.exit_code('-c', 'raise Exception'), |
| 132 | 0) |
| 133 | # All good if execution is successful |
| 134 | self.assertEqual( |
| 135 | self.exit_code('-c', 'pass'), |
| 136 | 0) |
| 137 | |
Amaury Forgeot d'Arc | 9a5499b | 2008-11-11 23:04:59 +0000 | [diff] [blame^] | 138 | # Test handling of non-ascii data |
| 139 | command = "assert(ord('\xe9') == 0xe9)" |
| 140 | self.assertEqual( |
| 141 | self.exit_code('-c', command), |
| 142 | 0) |
| 143 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 144 | |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 145 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 146 | test.support.run_unittest(CmdLineTest) |
| 147 | test.support.reap_children() |
Neal Norwitz | 11bd119 | 2005-10-03 00:54:56 +0000 | [diff] [blame] | 148 | |
| 149 | if __name__ == "__main__": |
| 150 | test_main() |