annotate Lib/test/test_cmd_line_script.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 55b3de6d701e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
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,
77948
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
9 assert_python_failure, make_pkg,
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
10 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
77948
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
15 example_args = ['test1', 'test2', 'test3']
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
16
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
17 test_source = """\
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
18 # 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
19 # statements being executed
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
20 def assertEqual(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 != rhs:
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
22 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
23 def assertIdentical(lhs, rhs):
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
24 if lhs is not rhs:
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
25 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
26 # Check basic code execution
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
27 result = ['Top level assignment']
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
28 def f():
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
29 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
30 f()
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
31 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
32 # 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
33 assertEqual(__name__, '__main__')
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
34 print '__file__==%r' % __file__
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
35 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
36 # Check the sys module
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
37 import sys
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
38 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
39 print 'sys.argv[0]==%r' % sys.argv[0]
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
40 """
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
41
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
42 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
43 return make_script(script_dir, script_basename, source)
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
44
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
45 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
46 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
47 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
48 source, depth)
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
49
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
50 # There's no easy way to pass the script directory in to get
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
51 # -m to work (avoiding that is the whole point of making
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
52 # directories and zipfiles executable!)
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
53 # So we fake it for testing purposes with a custom launch script
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
54 launch_source = """\
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
55 import sys, os.path, runpy
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
56 sys.path.insert(0, %s)
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
57 runpy._run_module_as_main(%r)
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
58 """
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
59
50162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
60 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
61 if path is None:
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
62 path = "os.path.dirname(__file__)"
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
63 else:
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
64 path = repr(path)
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
65 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
66 return make_script(script_dir, script_basename, source)
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
67
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
68 class CmdLineTest(unittest.TestCase):
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
69 def _check_script(self, script_name, expected_file,
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
70 expected_argv0, expected_package,
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
71 *cmd_line_switches):
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
72 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
73 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
74 if verbose:
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
75 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
76 print data
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
77 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
78 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
79 printed_argv0 = 'sys.argv[0]==%r' % expected_argv0
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
80 printed_package = '__package__==%r' % expected_package
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
81 if verbose:
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
82 print 'Expected output:'
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
83 print printed_file
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
84 print printed_package
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
85 print printed_argv0
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57255
diff changeset
86 self.assertIn(printed_file, data)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57255
diff changeset
87 self.assertIn(printed_package, data)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57255
diff changeset
88 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
89
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
90 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
91 *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
92 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
93 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
94 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
95 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
96 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
97 print 'Expected output: %r' % expected_msg
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57255
diff changeset
98 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
99
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
100 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
101 with temp_dir() as script_dir:
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
102 script_name = _make_test_script(script_dir, 'script')
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
103 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
104
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
105 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
106 with temp_dir() as script_dir:
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
107 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
108 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
109 os.remove(script_name)
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
110 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
111
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
112 def test_directory(self):
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
113 with temp_dir() as script_dir:
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
114 script_name = _make_test_script(script_dir, '__main__')
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
115 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
116
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
117 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
118 with temp_dir() as script_dir:
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
119 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
120 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
121 os.remove(script_name)
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
122 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
123
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 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
125 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
126 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
127 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
128
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
129 def test_zipfile(self):
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
130 with temp_dir() as script_dir:
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
131 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
132 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
133 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
134
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
135 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
136 with temp_dir() as script_dir:
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
137 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
138 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
139 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
140 self._check_script(zip_name, run_name, zip_name, '')
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
141
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
142 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
143 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
144 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
145 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
146 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
147 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
148
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
149 def test_module_in_package(self):
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
150 with temp_dir() as script_dir:
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
151 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
152 make_pkg(pkg_dir)
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
153 script_name = _make_test_script(pkg_dir, 'script')
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
154 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
155 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
156
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
157 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
158 with temp_dir() as script_dir:
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
159 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
160 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
161 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
162
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
163 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
164 with temp_dir() as script_dir:
4264cf00a75c Merged revisions 67750-67751 via svnmerge from
Nick Coghlan <ncoghlan@gmail.com>
parents: 46613
diff changeset
165 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
166 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
167 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
168
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
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176 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
177
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 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
179 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
180 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
181 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
182 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
183 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
184 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
185 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
186 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
187 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
188
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 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
190 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
191 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
192 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
193 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
194 "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
195 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
196 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
197
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 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
199 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
200 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
201 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
202 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
203 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
204 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
205 "'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
206 "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
207 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
208 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
209
77948
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
210 def test_dash_m_error_code_is_one(self):
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
211 # If a module is invoked with the -m command line flag
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
212 # and results in an error that the return code to the
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
213 # shell is '1'
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
214 with temp_dir() as script_dir:
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
215 pkg_dir = os.path.join(script_dir, 'test_pkg')
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
216 make_pkg(pkg_dir)
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
217 script_name = _make_test_script(pkg_dir, 'other', "if __name__ == '__main__': raise ValueError")
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
218 rc, out, err = assert_python_failure('-m', 'test_pkg.other', *example_args)
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
219 if verbose > 1:
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
220 print(out)
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
221 self.assertEqual(rc, 1)
55b3de6d701e Fix closes issue # 15033 - Return the proper exitcode for failure when modules are invoked using -m switch. Patch contributed by Jeff Knupp
Senthil Kumaran <senthil@uthcode.com>
parents: 58817
diff changeset
222
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
223
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
224 def test_main():
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
225 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
226 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
227
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 43911
diff changeset
228 if __name__ == '__main__':
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
229 test_main()