Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_cmd_line_script.py @ 65035:87f4b32905df 2.7
Cosmetic fix to use print function.
| author | Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp> |
|---|---|
| date | Fri, 01 Oct 2010 10:48:47 +0000 |
| parents | 70e115c34f97 |
| children | 55b3de6d701e |
| 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 |
|
43910
54b973f0f55b
Make this work stand-alone, too.
Guido van Rossum <guido@python.org>
parents:
43907
diff
changeset
|
6 import test.test_support |
|
58817
70e115c34f97
Remove unused imports in test modules.
Georg Brandl <georg@python.org>
parents:
58460
diff
changeset
|
7 from test.script_helper import (run_python, |
|
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 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
|
9 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
|
10 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
11 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
|
12 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
13 |
| 44116 | 14 test_source = """\ |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
15 # 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
|
16 # statements being executed |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
17 def assertEqual(lhs, rhs): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
18 if lhs != rhs: |
| 44116 | 19 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
|
20 def assertIdentical(lhs, rhs): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
21 if lhs is not rhs: |
| 44116 | 22 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
|
23 # Check basic code execution |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
24 result = ['Top level assignment'] |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
25 def f(): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
26 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
|
27 f() |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
28 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
|
29 # 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
|
30 assertEqual(__name__, '__main__') |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
31 print '__file__==%r' % __file__ |
| 44116 | 32 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
|
33 # Check the sys module |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
34 import sys |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
35 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
|
36 print 'sys.argv[0]==%r' % sys.argv[0] |
| 44116 | 37 """ |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
38 |
| 44116 | 39 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
|
40 return make_script(script_dir, script_basename, source) |
| 44116 | 41 |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
42 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
|
43 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
|
44 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
|
45 source, depth) |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
46 |
| 44116 | 47 # There's no easy way to pass the script directory in to get |
| 48 # -m to work (avoiding that is the whole point of making | |
| 49 # directories and zipfiles executable!) | |
| 50 # So we fake it for testing purposes with a custom launch script | |
| 51 launch_source = """\ | |
| 52 import sys, os.path, runpy | |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
53 sys.path.insert(0, %s) |
| 44116 | 54 runpy._run_module_as_main(%r) |
| 55 """ | |
| 56 | |
|
50162
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
57 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
|
58 if path is None: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
59 path = "os.path.dirname(__file__)" |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
60 else: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
61 path = repr(path) |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
62 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
|
63 return make_script(script_dir, script_basename, source) |
| 44116 | 64 |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
65 class CmdLineTest(unittest.TestCase): |
| 44116 | 66 def _check_script(self, script_name, expected_file, |
| 67 expected_argv0, expected_package, | |
| 68 *cmd_line_switches): | |
| 69 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
|
70 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
|
71 if verbose: |
| 44116 | 72 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
|
73 print data |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
74 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
|
75 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
|
76 printed_argv0 = 'sys.argv[0]==%r' % expected_argv0 |
| 44116 | 77 printed_package = '__package__==%r' % expected_package |
| 78 if verbose: | |
| 79 print 'Expected output:' | |
| 80 print printed_file | |
| 81 print printed_package | |
| 82 print printed_argv0 | |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
83 self.assertIn(printed_file, data) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
84 self.assertIn(printed_package, data) |
|
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
85 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
|
86 |
|
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
|
87 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
|
88 *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
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 print 'Expected output: %r' % expected_msg |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
57255
diff
changeset
|
95 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
|
96 |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
97 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
|
98 with temp_dir() as script_dir: |
| 44116 | 99 script_name = _make_test_script(script_dir, 'script') |
| 100 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
|
101 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
102 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
|
103 with temp_dir() as script_dir: |
| 44116 | 104 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
|
105 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
|
106 os.remove(script_name) |
| 44116 | 107 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
|
108 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
109 def test_directory(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
110 with temp_dir() as script_dir: |
| 44116 | 111 script_name = _make_test_script(script_dir, '__main__') |
| 112 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
|
113 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
114 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
|
115 with temp_dir() as script_dir: |
| 44116 | 116 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
|
117 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
|
118 os.remove(script_name) |
| 44116 | 119 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
|
120 |
|
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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
126 def test_zipfile(self): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
127 with temp_dir() as script_dir: |
| 44116 | 128 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
|
129 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
|
130 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
|
131 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
132 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
|
133 with temp_dir() as script_dir: |
| 44116 | 134 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
|
135 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
|
136 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
|
137 self._check_script(zip_name, run_name, zip_name, '') |
| 44116 | 138 |
|
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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 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
|
145 |
| 44116 | 146 def test_module_in_package(self): |
| 147 with temp_dir() as script_dir: | |
| 148 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
|
149 make_pkg(pkg_dir) |
| 44116 | 150 script_name = _make_test_script(pkg_dir, 'script') |
| 151 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
|
152 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
|
153 |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
154 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
|
155 with temp_dir() as script_dir: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
160 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
|
161 with temp_dir() as script_dir: |
|
4264cf00a75c
Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents:
46613
diff
changeset
|
162 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
|
163 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
|
164 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
|
165 |
|
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
|
166 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
|
167 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
|
168 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
|
169 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
|
170 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
|
171 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
|
172 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
|
173 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
|
174 |
|
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 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 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
|
182 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
|
183 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
|
184 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
|
185 |
|
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 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 "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
|
192 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
|
193 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
|
194 |
|
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 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
|
196 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
|
197 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
|
198 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
|
199 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
|
200 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
|
201 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
|
202 "'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
|
203 "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
|
204 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
|
205 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
|
206 |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
207 |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
208 def test_main(): |
|
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
209 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
|
210 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
|
211 |
| 44116 | 212 if __name__ == '__main__': |
|
43895
4cec0c6392c1
Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff
changeset
|
213 test_main() |
