Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_cmd_line_script.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 17891566a478 |
| children | 70e115c34f97 |
| rev | line source |
|---|---|
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
1 # Tests command line execution of scripts |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
2 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
3 import unittest |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
4 import os |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
5 import os.path |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
6 import sys |
|
43910
54b973f0f55b
Make this work stand-alone, too.
Guido van Rossum <guido@python.org>
parents:
43907
diff
changeset
|
7 import test.test_support |
|
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:
55002
diff
changeset
|
8 from test.script_helper import (spawn_python, kill_python, run_python, |
|
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:
55002
diff
changeset
|
9 temp_dir, make_script, compile_script, |
|
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:
55002
diff
changeset
|
10 make_pkg, make_zip_script, make_zip_pkg) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
11 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
12 verbose = test.test_support.verbose |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
13 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
14 |
| 44116 | 15 test_source = """\ |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
16 # Script may be run with optimisation enabled, so don't rely on assert |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
17 # statements being executed |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
18 def assertEqual(lhs, rhs): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
19 if lhs != rhs: |
| 44116 | 20 raise AssertionError('%r != %r' % (lhs, rhs)) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
21 def assertIdentical(lhs, rhs): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
22 if lhs is not rhs: |
| 44116 | 23 raise AssertionError('%r is not %r' % (lhs, rhs)) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
24 # Check basic code execution |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
25 result = ['Top level assignment'] |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
26 def f(): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
27 result.append('Lower level reference') |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
28 f() |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
29 assertEqual(result, ['Top level assignment', 'Lower level reference']) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
30 # Check population of magic variables |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
31 assertEqual(__name__, '__main__') |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
32 print '__file__==%r' % __file__ |
| 44116 | 33 print '__package__==%r' % __package__ |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
34 # Check the sys module |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
35 import sys |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
36 assertIdentical(globals(), sys.modules[__name__].__dict__) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
37 print 'sys.argv[0]==%r' % sys.argv[0] |
| 44116 | 38 """ |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
39 |
| 44116 | 40 def _make_test_script(script_dir, script_basename, source=test_source): |
|
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:
55002
diff
changeset
|
41 return make_script(script_dir, script_basename, source) |
| 44116 | 42 |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
43 def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
44 source=test_source, depth=1): |
|
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:
55002
diff
changeset
|
45 return make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, |
|
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:
55002
diff
changeset
|
46 source, depth) |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
47 |
| 44116 | 48 # There's no easy way to pass the script directory in to get |
| 49 # -m to work (avoiding that is the whole point of making | |
| 50 # directories and zipfiles executable!) | |
| 51 # So we fake it for testing purposes with a custom launch script | |
| 52 launch_source = """\ | |
| 53 import sys, os.path, runpy | |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
54 sys.path.insert(0, %s) |
| 44116 | 55 runpy._run_module_as_main(%r) |
| 56 """ | |
| 57 | |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
58 def _make_launch_script(script_dir, script_basename, module_name, path=None): |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
59 if path is None: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
60 path = "os.path.dirname(__file__)" |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
61 else: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
62 path = repr(path) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
63 source = launch_source % (path, module_name) |
|
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:
55002
diff
changeset
|
64 return make_script(script_dir, script_basename, source) |
| 44116 | 65 |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
66 class CmdLineTest(unittest.TestCase): |
| 44116 | 67 def _check_script(self, script_name, expected_file, |
| 68 expected_argv0, expected_package, | |
| 69 *cmd_line_switches): | |
| 70 run_args = cmd_line_switches + (script_name,) | |
|
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:
55002
diff
changeset
|
71 exit_code, data = run_python(*run_args) |
|
43907
780c728406e6
Enable some test_cmd_line_script debugging output to investigate failure on Mac OSX buildbot
Nick Coghlan <ncoghlan@gmail.com>
parents:
43895
diff
changeset
|
72 if verbose: |
| 44116 | 73 print 'Output from test script %r:' % script_name |
|
43907
780c728406e6
Enable some test_cmd_line_script debugging output to investigate failure on Mac OSX buildbot
Nick Coghlan <ncoghlan@gmail.com>
parents:
43895
diff
changeset
|
74 print data |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
75 self.assertEqual(exit_code, 0) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
76 printed_file = '__file__==%r' % expected_file |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
77 printed_argv0 = 'sys.argv[0]==%r' % expected_argv0 |
| 44116 | 78 printed_package = '__package__==%r' % expected_package |
| 79 if verbose: | |
| 80 print 'Expected output:' | |
| 81 print printed_file | |
| 82 print printed_package | |
| 83 print printed_argv0 | |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
84 self.assertIn(printed_file, data) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
85 self.assertIn(printed_package, data) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
86 self.assertIn(printed_argv0, data) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
87 |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
88 def _check_import_error(self, script_name, expected_msg, |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
89 *cmd_line_switches): |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
90 run_args = cmd_line_switches + (script_name,) |
|
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:
55002
diff
changeset
|
91 exit_code, data = run_python(*run_args) |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
92 if verbose: |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
93 print 'Output from test script %r:' % script_name |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
94 print data |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
95 print 'Expected output: %r' % expected_msg |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
96 self.assertIn(expected_msg, data) |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
97 |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
98 def test_basic_script(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
99 with temp_dir() as script_dir: |
| 44116 | 100 script_name = _make_test_script(script_dir, 'script') |
| 101 self._check_script(script_name, script_name, script_name, None) | |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
102 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
103 def test_script_compiled(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
104 with temp_dir() as script_dir: |
| 44116 | 105 script_name = _make_test_script(script_dir, 'script') |
|
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:
55002
diff
changeset
|
106 compiled_name = compile_script(script_name) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
107 os.remove(script_name) |
| 44116 | 108 self._check_script(compiled_name, compiled_name, compiled_name, None) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
109 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
110 def test_directory(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
111 with temp_dir() as script_dir: |
| 44116 | 112 script_name = _make_test_script(script_dir, '__main__') |
| 113 self._check_script(script_dir, script_name, script_dir, '') | |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
114 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
115 def test_directory_compiled(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
116 with temp_dir() as script_dir: |
| 44116 | 117 script_name = _make_test_script(script_dir, '__main__') |
|
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:
55002
diff
changeset
|
118 compiled_name = compile_script(script_name) |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
119 os.remove(script_name) |
| 44116 | 120 self._check_script(script_dir, compiled_name, script_dir, '') |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
121 |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
122 def test_directory_error(self): |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
123 with temp_dir() as script_dir: |
|
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:
55002
diff
changeset
|
124 msg = "can't find '__main__' module in %r" % script_dir |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
125 self._check_import_error(script_dir, msg) |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
126 |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
127 def test_zipfile(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
128 with temp_dir() as script_dir: |
| 44116 | 129 script_name = _make_test_script(script_dir, '__main__') |
|
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:
55002
diff
changeset
|
130 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
131 self._check_script(zip_name, run_name, zip_name, '') |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
132 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
133 def test_zipfile_compiled(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
134 with temp_dir() as script_dir: |
| 44116 | 135 script_name = _make_test_script(script_dir, '__main__') |
|
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:
55002
diff
changeset
|
136 compiled_name = compile_script(script_name) |
|
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:
55002
diff
changeset
|
137 zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name) |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
138 self._check_script(zip_name, run_name, zip_name, '') |
| 44116 | 139 |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
140 def test_zipfile_error(self): |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
141 with temp_dir() as script_dir: |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
142 script_name = _make_test_script(script_dir, 'not_main') |
|
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:
55002
diff
changeset
|
143 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name) |
|
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:
55002
diff
changeset
|
144 msg = "can't find '__main__' module in %r" % zip_name |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
145 self._check_import_error(zip_name, msg) |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
146 |
| 44116 | 147 def test_module_in_package(self): |
| 148 with temp_dir() as script_dir: | |
| 149 pkg_dir = os.path.join(script_dir, 'test_pkg') | |
|
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:
55002
diff
changeset
|
150 make_pkg(pkg_dir) |
| 44116 | 151 script_name = _make_test_script(pkg_dir, 'script') |
| 152 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script') | |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
153 self._check_script(launch_name, script_name, script_name, 'test_pkg') |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
154 |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
155 def test_module_in_package_in_zipfile(self): |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
156 with temp_dir() as script_dir: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
157 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script') |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
158 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
159 self._check_script(launch_name, run_name, run_name, 'test_pkg') |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
160 |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
161 def test_module_in_subpackage_in_zipfile(self): |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
162 with temp_dir() as script_dir: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
163 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
164 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
165 self._check_script(launch_name, run_name, run_name, 'test_pkg.test_pkg') |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
166 |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
167 def test_package(self): |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
168 with temp_dir() as script_dir: |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
169 pkg_dir = os.path.join(script_dir, 'test_pkg') |
|
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:
55002
diff
changeset
|
170 make_pkg(pkg_dir) |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
171 script_name = _make_test_script(pkg_dir, '__main__') |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
172 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
173 self._check_script(launch_name, script_name, |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
174 script_name, 'test_pkg') |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
175 |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
176 def test_package_compiled(self): |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
177 with temp_dir() as script_dir: |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
178 pkg_dir = os.path.join(script_dir, 'test_pkg') |
|
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:
55002
diff
changeset
|
179 make_pkg(pkg_dir) |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
180 script_name = _make_test_script(pkg_dir, '__main__') |
|
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:
55002
diff
changeset
|
181 compiled_name = compile_script(script_name) |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
182 os.remove(script_name) |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
183 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
184 self._check_script(launch_name, compiled_name, |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
185 compiled_name, 'test_pkg') |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
186 |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
187 def test_package_error(self): |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
188 with temp_dir() as script_dir: |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
189 pkg_dir = os.path.join(script_dir, 'test_pkg') |
|
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:
55002
diff
changeset
|
190 make_pkg(pkg_dir) |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
191 msg = ("'test_pkg' is a package and cannot " |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
192 "be directly executed") |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
193 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
194 self._check_import_error(launch_name, msg) |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
195 |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
196 def test_package_recursion(self): |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
197 with temp_dir() as script_dir: |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
198 pkg_dir = os.path.join(script_dir, 'test_pkg') |
|
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:
55002
diff
changeset
|
199 make_pkg(pkg_dir) |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
200 main_dir = os.path.join(pkg_dir, '__main__') |
|
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:
55002
diff
changeset
|
201 make_pkg(main_dir) |
|
51450
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
202 msg = ("Cannot use package as __main__ module; " |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
203 "'test_pkg' is a package and cannot " |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
204 "be directly executed") |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
205 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg') |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
206 self._check_import_error(launch_name, msg) |
|
5cce99aa1f58
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
Nick Coghlan <ncoghlan@gmail.com>
parents:
50162
diff
changeset
|
207 |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
208 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
209 def test_main(): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
210 test.test_support.run_unittest(CmdLineTest) |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
211 test.test_support.reap_children() |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
212 |
| 44116 | 213 if __name__ == '__main__': |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
214 test_main() |
