/Lib/test/test_cmd_line.py
Python | 147 lines | 135 code | 7 blank | 5 comment | 0 complexity | cf0ee90c92784f245156e8fd4175018f MD5 | raw file
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 4 5import test.test_support, unittest 6import sys 7import subprocess 8try: 9 import _llvm 10except ImportError: 11 WITH_LLVM = False 12else: 13 WITH_LLVM = True 14 del _llvm 15 16def _spawn_python(*args): 17 cmd_line = [sys.executable, '-E'] 18 cmd_line.extend(args) 19 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, 20 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 21 22def _exhaust_python(p): 23 p.stdin.close() 24 data = p.stdout.read() 25 p.stdout.close() 26 p.wait() 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() 30 return data, p.returncode 31 32class CmdLineTest(unittest.TestCase): 33 def start_python(self, *args): 34 p = _spawn_python(*args) 35 return _exhaust_python(p) 36 37 def exit_code(self, *args): 38 cmd_line = [sys.executable, '-E'] 39 cmd_line.extend(args) 40 return subprocess.call(cmd_line, stdout=subprocess.PIPE, 41 stderr=subprocess.PIPE) 42 43 def test_directories(self): 44 self.assertNotEqual(self.exit_code('.'), 0) 45 self.assertNotEqual(self.exit_code('< .'), 0) 46 47 def verify_valid_flag(self, *flags): 48 cmd_line = flags + ('-c', 'import sys; sys.exit()') 49 data, returncode = self.start_python(*cmd_line) 50 self.assertTrue(data == '' or data.endswith('\n')) 51 self.assertTrue('Traceback' not in data) 52 self.assertEqual(returncode, 0) 53 54 def verify_invalid_flag(self, *flags): 55 cmd_line = flags + ('-c', 'import sys; sys.exit()') 56 data, returncode = self.start_python(*cmd_line) 57 self.assertTrue('usage:' in data) 58 self.assertEqual(returncode, 2) 59 60 def test_optimize(self): 61 # Ordered by how much optimization results: O0, O, O1, OO, O2. 62 # -OO is supported for backwards compatibility. 63 self.verify_valid_flag('-O0') # Oh zero. 64 self.verify_valid_flag('-O') # Same as -O1 65 self.verify_valid_flag('-O1') 66 self.verify_valid_flag('-OO') # Oh oh. Same as -O2 67 self.verify_valid_flag('-O2') 68 self.verify_invalid_flag('-O3') # Used to be valid, no more. 69 self.verify_invalid_flag('-O128') 70 71 def test_q(self): 72 self.verify_valid_flag('-Qold') 73 self.verify_valid_flag('-Qnew') 74 self.verify_valid_flag('-Qwarn') 75 self.verify_valid_flag('-Qwarnall') 76 77 if WITH_LLVM: 78 def test_jit_flag(self): 79 self.verify_valid_flag('-Xjit=never') 80 self.verify_valid_flag('-Xjit=whenhot') 81 self.verify_valid_flag('-Xjit=always') 82 self.verify_invalid_flag('-Xjit', 'always') 83 84 def test_site_flag(self): 85 self.verify_valid_flag('-S') 86 87 def test_usage(self): 88 data, returncode = self.start_python('-h') 89 self.assertTrue('usage' in data) 90 self.assertEqual(returncode, 0) 91 92 def test_version(self): 93 version = 'Python %d.%d' % sys.version_info[:2] 94 data, returncode = self.start_python('-V') 95 self.assertTrue(data.startswith(version), data) 96 self.assertEqual(returncode, 0) 97 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 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') 121 p.stdin.write('Timer\n') 122 p.stdin.write('exit()\n') 123 data, returncode = _exhaust_python(p) 124 self.assertTrue(data.startswith('1 loop')) 125 self.assertTrue('__main__.Timer' in data) 126 self.assertEqual(returncode, 0) 127 128 def test_run_code(self): 129 # Test expected operation of the '-c' switch 130 # Switch needs an argument 131 self.assertNotEqual(self.exit_code('-c'), 0) 132 # Check we get an error for an uncaught exception 133 self.assertNotEqual( 134 self.exit_code('-c', 'raise Exception'), 135 0) 136 # All good if execution is successful 137 self.assertEqual( 138 self.exit_code('-c', 'pass'), 139 0) 140 141 142def test_main(): 143 test.test_support.run_unittest(CmdLineTest) 144 test.test_support.reap_children() 145 146if __name__ == "__main__": 147 test_main()