blob: fc0e3a728a649d6c0ddedc6fd141ddf8659c0a15 [file] [log] [blame]
Christian Heimes9cd17752007-11-18 19:35:23 +00001# 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 Norwitz11bd1192005-10-03 00:54:56 +00004
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005import test.support, unittest
Antoine Pitrou87695762008-08-14 22:44:29 +00006import os
Neal Norwitz11bd1192005-10-03 00:54:56 +00007import sys
Walter Dörwald9356fb92005-11-25 15:22:10 +00008import subprocess
Neal Norwitz11bd1192005-10-03 00:54:56 +00009
Thomas Woutersed03b412007-08-28 21:37:11 +000010def _spawn_python(*args):
Antoine Pitrou87695762008-08-14 22:44:29 +000011 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 Woutersed03b412007-08-28 21:37:11 +000015 cmd_line.extend(args)
16 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
17 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
18
19def _kill_python(p):
Trent Nelson39e307e2008-03-19 06:45:48 +000020 return _kill_python_and_exit_code(p)[0]
21
22def _kill_python_and_exit_code(p):
Thomas Woutersed03b412007-08-28 21:37:11 +000023 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 Nelson39e307e2008-03-19 06:45:48 +000029 returncode = p.wait()
30 return data, returncode
Thomas Woutersed03b412007-08-28 21:37:11 +000031
Neal Norwitz11bd1192005-10-03 00:54:56 +000032class CmdLineTest(unittest.TestCase):
Thomas Woutersed03b412007-08-28 21:37:11 +000033 def start_python(self, *args):
Trent Nelson39e307e2008-03-19 06:45:48 +000034 return self.start_python_and_exit_code(*args)[0]
35
36 def start_python_and_exit_code(self, *args):
Thomas Woutersed03b412007-08-28 21:37:11 +000037 p = _spawn_python(*args)
Trent Nelson39e307e2008-03-19 06:45:48 +000038 return _kill_python_and_exit_code(p)
Neal Norwitz11bd1192005-10-03 00:54:56 +000039
Thomas Wouters477c8d52006-05-27 19:21:47 +000040 def exit_code(self, *args):
Thomas Wouters1b7f8912007-09-19 03:06:30 +000041 cmd_line = [sys.executable, '-E']
Thomas Wouters477c8d52006-05-27 19:21:47 +000042 cmd_line.extend(args)
43 return subprocess.call(cmd_line, stdout=subprocess.PIPE,
44 stderr=subprocess.PIPE)
Walter Dörwald9356fb92005-11-25 15:22:10 +000045
Neal Norwitz11bd1192005-10-03 00:54:56 +000046 def test_directories(self):
Neal Norwitz72c2c062006-03-09 05:58:11 +000047 self.assertNotEqual(self.exit_code('.'), 0)
48 self.assertNotEqual(self.exit_code('< .'), 0)
Neal Norwitz11bd1192005-10-03 00:54:56 +000049
50 def verify_valid_flag(self, cmd_line):
51 data = self.start_python(cmd_line)
Guido van Rossuma1c42a92007-08-29 03:47:36 +000052 self.assertTrue(data == b'' or data.endswith(b'\n'))
Guido van Rossumf074b642007-07-11 06:56:16 +000053 self.assertTrue(b'Traceback' not in data)
Neal Norwitz11bd1192005-10-03 00:54:56 +000054
Neal Norwitz11bd1192005-10-03 00:54:56 +000055 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 Pitrou87695762008-08-14 22:44:29 +000066 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 Norwitz11bd1192005-10-03 00:54:56 +000076 self.verify_valid_flag('-S')
77
78 def test_usage(self):
Guido van Rossumf074b642007-07-11 06:56:16 +000079 self.assertTrue(b'usage' in self.start_python('-h'))
Neal Norwitz11bd1192005-10-03 00:54:56 +000080
81 def test_version(self):
Guido van Rossuma1c42a92007-08-29 03:47:36 +000082 version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
Neal Norwitz11bd1192005-10-03 00:54:56 +000083 self.assertTrue(self.start_python('-V').startswith(version))
84
Trent Nelson39e307e2008-03-19 06:45:48 +000085 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 Wouters477c8d52006-05-27 19:21:47 +000096 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 Woutersed03b412007-08-28 21:37:11 +0000114 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 Rossuma1c42a92007-08-29 03:47:36 +0000119 p.stdin.write(b'Timer\n')
120 p.stdin.write(b'exit()\n')
Thomas Woutersed03b412007-08-28 21:37:11 +0000121 data = _kill_python(p)
122 self.assertTrue(data.find(b'1 loop') != -1)
123 self.assertTrue(data.find(b'__main__.Timer') != -1)
124
Thomas Wouters477c8d52006-05-27 19:21:47 +0000125 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'Arc9a5499b2008-11-11 23:04:59 +0000138 # 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 Wouters477c8d52006-05-27 19:21:47 +0000144
Neal Norwitz11bd1192005-10-03 00:54:56 +0000145def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000146 test.support.run_unittest(CmdLineTest)
147 test.support.reap_children()
Neal Norwitz11bd1192005-10-03 00:54:56 +0000148
149if __name__ == "__main__":
150 test_main()