annotate Lib/test/test_cmd_line.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 1b62bffd2ef2
children 40881db31689
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents: 43331
diff changeset
1 # Tests invocation of the interpreter with various command line arguments
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents: 43331
diff changeset
2 # All tests are executed with environment variables ignored
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents: 43331
diff changeset
3 # See test_cmd_line_script.py for testing of script execution
35673
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
4
54239
1b62bffd2ef2 don't use subprocess.call with PIPEs as the child can fill the pipe buf and
Philip Jenvey <pjenvey@underboss.org>
parents: 43895
diff changeset
5 import os
35673
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
6 import test.test_support, unittest
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
7 import sys
35949
f8056328f01a SF patch #1364545: test_cmd_line.py relied on english error messages when
Walter Dörwald <walter@livinglogic.de>
parents: 35810
diff changeset
8 import subprocess
35673
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
9
42825
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
10 def _spawn_python(*args):
43331
bfe18b907d87 Always use the -E flag when spawning subprocesses in test_cmd_line (Issue 1056)
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
11 cmd_line = [sys.executable, '-E']
42825
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
12 cmd_line.extend(args)
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
13 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
14 stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
15
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
16 def _kill_python(p):
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
17 p.stdin.close()
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
18 data = p.stdout.read()
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
19 p.stdout.close()
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
20 # try to cleanup the child so we don't appear to leak when running
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
21 # with regrtest -R. This should be a no-op on Windows.
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
22 subprocess._cleanup()
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
23 return data
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
24
35673
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
25 class CmdLineTest(unittest.TestCase):
42825
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
26 def start_python(self, *args):
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
27 p = _spawn_python(*args)
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
28 return _kill_python(p)
35673
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
29
37958
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
30 def exit_code(self, *args):
43331
bfe18b907d87 Always use the -E flag when spawning subprocesses in test_cmd_line (Issue 1056)
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
31 cmd_line = [sys.executable, '-E']
37958
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
32 cmd_line.extend(args)
54239
1b62bffd2ef2 don't use subprocess.call with PIPEs as the child can fill the pipe buf and
Philip Jenvey <pjenvey@underboss.org>
parents: 43895
diff changeset
33 with open(os.devnull, 'w') as devnull:
1b62bffd2ef2 don't use subprocess.call with PIPEs as the child can fill the pipe buf and
Philip Jenvey <pjenvey@underboss.org>
parents: 43895
diff changeset
34 return subprocess.call(cmd_line, stdout=devnull,
1b62bffd2ef2 don't use subprocess.call with PIPEs as the child can fill the pipe buf and
Philip Jenvey <pjenvey@underboss.org>
parents: 43895
diff changeset
35 stderr=subprocess.STDOUT)
35949
f8056328f01a SF patch #1364545: test_cmd_line.py relied on english error messages when
Walter Dörwald <walter@livinglogic.de>
parents: 35810
diff changeset
36
35673
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
37 def test_directories(self):
36910
91a9e2e31e93 Try to be a bit more consistent on all platforms:
Neal Norwitz <nnorwitz@gmail.com>
parents: 36016
diff changeset
38 self.assertNotEqual(self.exit_code('.'), 0)
91a9e2e31e93 Try to be a bit more consistent on all platforms:
Neal Norwitz <nnorwitz@gmail.com>
parents: 36016
diff changeset
39 self.assertNotEqual(self.exit_code('< .'), 0)
35673
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
40
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
41 def verify_valid_flag(self, cmd_line):
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
42 data = self.start_python(cmd_line)
35705
4e398837373c Fix unit test failure -- the output received from Python can be empty,
Guido van Rossum <guido@python.org>
parents: 35673
diff changeset
43 self.assertTrue(data == '' or data.endswith('\n'))
35673
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
44 self.assertTrue('Traceback' not in data)
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
45
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
46 def test_optimize(self):
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
47 self.verify_valid_flag('-O')
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
48 self.verify_valid_flag('-OO')
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
49
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
50 def test_q(self):
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
51 self.verify_valid_flag('-Qold')
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
52 self.verify_valid_flag('-Qnew')
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
53 self.verify_valid_flag('-Qwarn')
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
54 self.verify_valid_flag('-Qwarnall')
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
55
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
56 def test_site_flag(self):
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
57 self.verify_valid_flag('-S')
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
58
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
59 def test_usage(self):
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
60 self.assertTrue('usage' in self.start_python('-h'))
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
61
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
62 def test_version(self):
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
63 version = 'Python %d.%d' % sys.version_info[:2]
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
64 self.assertTrue(self.start_python('-V').startswith(version))
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
65
37958
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
66 def test_run_module(self):
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
67 # Test expected operation of the '-m' switch
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
68 # Switch needs an argument
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
69 self.assertNotEqual(self.exit_code('-m'), 0)
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
70 # Check we get an error for a nonexistent module
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
71 self.assertNotEqual(
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
72 self.exit_code('-m', 'fnord43520xyz'),
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
73 0)
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
74 # Check the runpy module also gives an error for
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
75 # a nonexistent module
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
76 self.assertNotEqual(
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
77 self.exit_code('-m', 'runpy', 'fnord43520xyz'),
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
78 0)
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
79 # All good if module is located and run successfully
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
80 self.assertEqual(
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
81 self.exit_code('-m', 'timeit', '-n', '1'),
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
82 0)
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
83
42825
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
84 def test_run_module_bug1764407(self):
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
85 # -m and -i need to play well together
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
86 # Runs the timeit module and checks the __main__
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
87 # namespace has been populated appropriately
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
88 p = _spawn_python('-i', '-m', 'timeit', '-n', '1')
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
89 p.stdin.write('Timer\n')
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
90 p.stdin.write('exit()\n')
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
91 data = _kill_python(p)
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
92 self.assertTrue(data.startswith('1 loop'))
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
93 self.assertTrue('__main__.Timer' in data)
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 41690
diff changeset
94
37958
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
95 def test_run_code(self):
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
96 # Test expected operation of the '-c' switch
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
97 # Switch needs an argument
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
98 self.assertNotEqual(self.exit_code('-c'), 0)
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
99 # Check we get an error for an uncaught exception
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
100 self.assertNotEqual(
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
101 self.exit_code('-c', 'raise Exception'),
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
102 0)
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
103 # All good if execution is successful
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
104 self.assertEqual(
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
105 self.exit_code('-c', 'pass'),
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
106 0)
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
107
c78d306aaccf More reliable version of new command line tests that just checks the exit codes
Nick Coghlan <ncoghlan@gmail.com>
parents: 37957
diff changeset
108
35673
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
109 def test_main():
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
110 test.test_support.run_unittest(CmdLineTest)
38961
0a83dc3529d1 Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents: 37958
diff changeset
111 test.test_support.reap_children()
35673
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
112
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
113 if __name__ == "__main__":
612a12178e47 SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
114 test_main()