Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_cmd_line.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | a0f43f4481e0 |
| children | 4ebe1ede981e |
| 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 |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
5 import test.test_support, unittest |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
6 import sys |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
54239
diff
changeset
|
7 from test.script_helper import spawn_python, kill_python, python_exit_code |
|
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
|
8 |
|
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
|
9 |
|
35673
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
10 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
|
11 def start_python(self, *args): |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
54239
diff
changeset
|
12 p = spawn_python(*args) |
|
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
54239
diff
changeset
|
13 return kill_python(p) |
|
35673
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
14 |
|
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
|
15 def exit_code(self, *args): |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
54239
diff
changeset
|
16 return python_exit_code(*args) |
|
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
|
17 |
|
35673
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
18 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
|
19 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
|
20 self.assertNotEqual(self.exit_code('< .'), 0) |
|
35673
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
21 |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
22 def verify_valid_flag(self, cmd_line): |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
23 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
|
24 self.assertTrue(data == '' or data.endswith('\n')) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
25 self.assertNotIn('Traceback', data) |
|
35673
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
26 |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
27 def test_optimize(self): |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
28 self.verify_valid_flag('-O') |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
29 self.verify_valid_flag('-OO') |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
30 |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
31 def test_q(self): |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
32 self.verify_valid_flag('-Qold') |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
33 self.verify_valid_flag('-Qnew') |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
34 self.verify_valid_flag('-Qwarn') |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
35 self.verify_valid_flag('-Qwarnall') |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
36 |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
37 def test_site_flag(self): |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
38 self.verify_valid_flag('-S') |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
39 |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
40 def test_usage(self): |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
41 self.assertIn('usage', self.start_python('-h')) |
|
35673
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
42 |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
43 def test_version(self): |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
44 version = 'Python %d.%d' % sys.version_info[:2] |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
45 self.assertTrue(self.start_python('-V').startswith(version)) |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
46 |
|
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
|
47 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
|
48 # 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
|
49 # 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
|
50 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
|
51 # 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
|
52 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
|
53 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
|
54 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
|
55 # 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
|
56 # 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
|
57 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
|
58 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
|
59 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
|
60 # 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
|
61 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
|
62 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
|
63 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
|
64 |
|
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
|
65 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
|
66 # -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
|
67 # 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
|
68 # namespace has been populated appropriately |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
54239
diff
changeset
|
69 p = spawn_python('-i', '-m', 'timeit', '-n', '1') |
|
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
|
70 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
|
71 p.stdin.write('exit()\n') |
|
57255
40881db31689
Issue #6816: expose the zipfile and directory execution mechanism to Python code via the runpy module. Also consolidated some script execution functionality in the test harness into a helper module and removed some implementation details from the runpy module documentation.
Nick Coghlan <ncoghlan@gmail.com>
parents:
54239
diff
changeset
|
72 data = kill_python(p) |
|
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
|
73 self.assertTrue(data.startswith('1 loop')) |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
74 self.assertIn('__main__.Timer', data) |
|
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
|
75 |
|
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
|
76 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
|
77 # 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
|
78 # 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
|
79 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
|
80 # 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
|
81 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
|
82 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
|
83 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
|
84 # 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
|
85 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
|
86 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
|
87 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
|
88 |
|
75100
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
89 def test_hash_randomization(self): |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
90 # Verify that -R enables hash randomization: |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
91 self.verify_valid_flag('-R') |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
92 hashes = [] |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
93 for i in range(2): |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
94 code = 'print(hash("spam"))' |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
95 data = self.start_python('-R', '-c', code) |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
96 hashes.append(data) |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
97 self.assertNotEqual(hashes[0], hashes[1]) |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
98 |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
99 # Verify that sys.flags contains hash_randomization |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
100 code = 'import sys; print sys.flags' |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
101 data = self.start_python('-R', '-c', code) |
|
6b7704fe1be1
- Issue #13703: oCERT-2011-003: add -R command-line option and PYTHONHASHSEED
Barry Warsaw <barry@python.org>
parents:
43895
diff
changeset
|
102 self.assertTrue('hash_randomization=1' in data) |
|
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
|
103 |
|
35673
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
104 def test_main(): |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
105 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
|
106 test.test_support.reap_children() |
|
35673
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
107 |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
108 if __name__ == "__main__": |
|
612a12178e47
SF bug #887946, segfault if redirecting directory
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
109 test_main() |
