annotate Lib/test/test_runpy.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 17891566a478
children c70746a0291f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
1 # Test the runpy module
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
2 import unittest
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
3 import os
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
4 import os.path
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
5 import sys
57313
3d2b06aa7b36 Correctly escape arbitrary error message text in the runpy unit tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 57289
diff changeset
6 import re
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
7 import tempfile
42729
2480b864be03 Make test_runpy re-entrant.
Brett Cannon <bcannon@gmail.com>
parents: 42357
diff changeset
8 from test.test_support import verbose, run_unittest, forget
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: 57132
diff changeset
9 from test.script_helper import (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: 57132
diff changeset
10 make_pkg, make_zip_script, make_zip_pkg)
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
11
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: 57132
diff changeset
12
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: 57132
diff changeset
13 from runpy import _run_code, _run_module_code, run_module, run_path
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
14 # Note: This module can't safely test _run_module_as_main as it
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
15 # runs its tests in the current process, which would mess with the
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
16 # real __main__ module (usually test.regrtest)
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
17 # See test_cmd_line_script for a test that executes that code path
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
18
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
19 # Set up the test code and expected results
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
20
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
21 class RunModuleCodeTest(unittest.TestCase):
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: 57132
diff changeset
22 """Unit tests for runpy._run_code and runpy._run_module_code"""
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
23
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
24 expected_result = ["Top level assignment", "Lower level reference"]
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
25 test_source = (
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
26 "# Check basic code execution\n"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
27 "result = ['Top level assignment']\n"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
28 "def f():\n"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
29 " result.append('Lower level reference')\n"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
30 "f()\n"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
31 "# Check the sys module\n"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
32 "import sys\n"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
33 "run_argv0 = sys.argv[0]\n"
42356
d94d5b416a94 Tweak runpy test to do a better job of confirming that sys has been manipulated correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 42349
diff changeset
34 "run_name_in_sys_modules = __name__ in sys.modules\n"
d94d5b416a94 Tweak runpy test to do a better job of confirming that sys has been manipulated correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 42349
diff changeset
35 "if run_name_in_sys_modules:\n"
d94d5b416a94 Tweak runpy test to do a better job of confirming that sys has been manipulated correctly
Nick Coghlan <ncoghlan@gmail.com>
parents: 42349
diff changeset
36 " module_in_sys_modules = globals() is sys.modules[__name__].__dict__\n"
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
37 "# Check nested operation\n"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
38 "import runpy\n"
42825
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 42819
diff changeset
39 "nested = runpy._run_module_code('x=1\\n', mod_name='<run>')\n"
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
40 )
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
41
42825
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 42819
diff changeset
42 def test_run_code(self):
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 42819
diff changeset
43 saved_argv0 = sys.argv[0]
543f2ba97fab Fix bug 1764407 - the -i switch now does the right thing when using the -m switch
Nick Coghlan <ncoghlan@gmail.com>
parents: 42819
diff changeset
44 d = _run_code(self.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: 57132
diff changeset
45 self.assertEqual(d["result"], self.expected_result)
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: 57132
diff changeset
46 self.assertIs(d["__name__"], None)
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: 57132
diff changeset
47 self.assertIs(d["__file__"], None)
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: 57132
diff changeset
48 self.assertIs(d["__loader__"], None)
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: 57132
diff changeset
49 self.assertIs(d["__package__"], None)
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: 57132
diff changeset
50 self.assertIs(d["run_argv0"], saved_argv0)
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: 57132
diff changeset
51 self.assertNotIn("run_name", d)
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: 57132
diff changeset
52 self.assertIs(sys.argv[0], saved_argv0)
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
53
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
54 def test_run_module_code(self):
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
55 initial = object()
39022
34cb9a6ce12d Revert the __module_name__ changes made in rev 47142. We'll revisit this in Python 2.6
Nick Coghlan <ncoghlan@gmail.com>
parents: 38951
diff changeset
56 name = "<Nonsense>"
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
57 file = "Some other nonsense"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
58 loader = "Now you're just being silly"
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
59 package = '' # Treat as a top level module
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
60 d1 = dict(initial=initial)
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
61 saved_argv0 = sys.argv[0]
42819
1b420e6e00eb Revert misguided attempt at fixing incompatibility between -m and -i switches (better fix coming soon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 42729
diff changeset
62 d2 = _run_module_code(self.test_source,
1b420e6e00eb Revert misguided attempt at fixing incompatibility between -m and -i switches (better fix coming soon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 42729
diff changeset
63 d1,
1b420e6e00eb Revert misguided attempt at fixing incompatibility between -m and -i switches (better fix coming soon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 42729
diff changeset
64 name,
1b420e6e00eb Revert misguided attempt at fixing incompatibility between -m and -i switches (better fix coming soon)
Nick Coghlan <ncoghlan@gmail.com>
parents: 42729
diff changeset
65 file,
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
66 loader,
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
67 package)
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: 57132
diff changeset
68 self.assertNotIn("result", d1)
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: 57132
diff changeset
69 self.assertIs(d2["initial"], initial)
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: 57132
diff changeset
70 self.assertEqual(d2["result"], self.expected_result)
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: 57132
diff changeset
71 self.assertEqual(d2["nested"]["x"], 1)
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: 57132
diff changeset
72 self.assertIs(d2["__name__"], name)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44116
diff changeset
73 self.assertTrue(d2["run_name_in_sys_modules"])
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44116
diff changeset
74 self.assertTrue(d2["module_in_sys_modules"])
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: 57132
diff changeset
75 self.assertIs(d2["__file__"], file)
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: 57132
diff changeset
76 self.assertIs(d2["run_argv0"], file)
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: 57132
diff changeset
77 self.assertIs(d2["__loader__"], loader)
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: 57132
diff changeset
78 self.assertIs(d2["__package__"], package)
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: 57132
diff changeset
79 self.assertIs(sys.argv[0], saved_argv0)
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: 57132
diff changeset
80 self.assertNotIn(name, sys.modules)
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
81
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
82
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
83 class RunModuleTest(unittest.TestCase):
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: 57132
diff changeset
84 """Unit tests for runpy.run_module"""
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
85
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
86 def expect_import_error(self, mod_name):
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
87 try:
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
88 run_module(mod_name)
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
89 except ImportError:
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
90 pass
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
91 else:
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
92 self.fail("Expected import error for " + mod_name)
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
93
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
94 def test_invalid_names(self):
42349
3fe643d62ecc Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents: 42337
diff changeset
95 # Builtin module
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
96 self.expect_import_error("sys")
42349
3fe643d62ecc Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents: 42337
diff changeset
97 # Non-existent modules
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
98 self.expect_import_error("sys.imp.eric")
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
99 self.expect_import_error("os.path.half")
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
100 self.expect_import_error("a.bee")
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
101 self.expect_import_error(".howard")
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
102 self.expect_import_error("..eaten")
57132
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
103 # Package without __main__.py
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
104 self.expect_import_error("multiprocessing")
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
105
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
106 def test_library_module(self):
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
107 run_module("runpy")
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
108
42337
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
109 def _add_pkg_dir(self, pkg_dir):
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
110 os.mkdir(pkg_dir)
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
111 pkg_fname = os.path.join(pkg_dir, "__init__"+os.extsep+"py")
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
112 pkg_file = open(pkg_fname, "w")
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
113 pkg_file.close()
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
114 return pkg_fname
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
115
57132
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
116 def _make_pkg(self, source, depth, mod_base="runpy_test"):
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
117 pkg_name = "__runpy_pkg__"
57132
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
118 test_fname = mod_base+os.extsep+"py"
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
119 pkg_dir = sub_dir = tempfile.mkdtemp()
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
120 if verbose: print " Package tree in:", sub_dir
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
121 sys.path.insert(0, pkg_dir)
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
122 if verbose: print " Updated sys.path:", sys.path[0]
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
123 for i in range(depth):
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
124 sub_dir = os.path.join(sub_dir, pkg_name)
42337
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
125 pkg_fname = self._add_pkg_dir(sub_dir)
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
126 if verbose: print " Next level in:", sub_dir
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
127 if verbose: print " Created:", pkg_fname
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
128 mod_fname = os.path.join(sub_dir, test_fname)
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
129 mod_file = open(mod_fname, "w")
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
130 mod_file.write(source)
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
131 mod_file.close()
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
132 if verbose: print " Created:", mod_fname
57132
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
133 mod_name = (pkg_name+".")*depth + mod_base
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
134 return pkg_dir, mod_fname, mod_name
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
135
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
136 def _del_pkg(self, top, depth, mod_name):
42349
3fe643d62ecc Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents: 42337
diff changeset
137 for entry in list(sys.modules):
3fe643d62ecc Correctly cleanup sys.modules after executing runpy relative import
Nick Coghlan <ncoghlan@gmail.com>
parents: 42337
diff changeset
138 if entry.startswith("__runpy_pkg__"):
37000
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
139 del sys.modules[entry]
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
140 if verbose: print " Removed sys.modules entries"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
141 del sys.path[0]
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
142 if verbose: print " Removed sys.path entry"
36998
451da3a52913 Make test_runpy close all references to test modules before trying to delete the underlying files
Nick Coghlan <ncoghlan@gmail.com>
parents: 36995
diff changeset
143 for root, dirs, files in os.walk(top, topdown=False):
451da3a52913 Make test_runpy close all references to test modules before trying to delete the underlying files
Nick Coghlan <ncoghlan@gmail.com>
parents: 36995
diff changeset
144 for name in files:
37000
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
145 try:
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
146 os.remove(os.path.join(root, name))
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
147 except OSError, ex:
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
148 if verbose: print ex # Persist with cleaning up
36998
451da3a52913 Make test_runpy close all references to test modules before trying to delete the underlying files
Nick Coghlan <ncoghlan@gmail.com>
parents: 36995
diff changeset
149 for name in dirs:
37000
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
150 fullname = os.path.join(root, name)
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
151 try:
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
152 os.rmdir(fullname)
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
153 except OSError, ex:
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
154 if verbose: print ex # Persist with cleaning up
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
155 try:
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
156 os.rmdir(top)
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
157 if verbose: print " Removed package tree"
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
158 except OSError, ex:
86f13301c3db Don't let cleanup errors mask real errors in the runpy tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 36998
diff changeset
159 if verbose: print ex # Persist with cleaning up
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
160
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
161 def _check_module(self, depth):
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
162 pkg_dir, mod_fname, mod_name = (
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
163 self._make_pkg("x=1\n", depth))
42729
2480b864be03 Make test_runpy re-entrant.
Brett Cannon <bcannon@gmail.com>
parents: 42357
diff changeset
164 forget(mod_name)
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
165 try:
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
166 if verbose: print "Running from source:", mod_name
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
167 d1 = run_module(mod_name) # Read from source
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57313
diff changeset
168 self.assertIn("x", d1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44116
diff changeset
169 self.assertTrue(d1["x"] == 1)
36998
451da3a52913 Make test_runpy close all references to test modules before trying to delete the underlying files
Nick Coghlan <ncoghlan@gmail.com>
parents: 36995
diff changeset
170 del d1 # Ensure __loader__ entry doesn't keep file open
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
171 __import__(mod_name)
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
172 os.remove(mod_fname)
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
173 if verbose: print "Running from compiled:", mod_name
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
174 d2 = run_module(mod_name) # Read from bytecode
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57313
diff changeset
175 self.assertIn("x", d2)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44116
diff changeset
176 self.assertTrue(d2["x"] == 1)
36998
451da3a52913 Make test_runpy close all references to test modules before trying to delete the underlying files
Nick Coghlan <ncoghlan@gmail.com>
parents: 36995
diff changeset
177 del d2 # Ensure __loader__ entry doesn't keep file open
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
178 finally:
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
179 self._del_pkg(pkg_dir, depth, mod_name)
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
180 if verbose: print "Module executed successfully"
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
181
57132
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
182 def _check_package(self, depth):
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
183 pkg_dir, mod_fname, mod_name = (
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
184 self._make_pkg("x=1\n", depth, "__main__"))
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
185 pkg_name, _, _ = mod_name.rpartition(".")
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
186 forget(mod_name)
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
187 try:
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
188 if verbose: print "Running from source:", pkg_name
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
189 d1 = run_module(pkg_name) # Read from source
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57313
diff changeset
190 self.assertIn("x", d1)
57132
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
191 self.assertTrue(d1["x"] == 1)
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
192 del d1 # Ensure __loader__ entry doesn't keep file open
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
193 __import__(mod_name)
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
194 os.remove(mod_fname)
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
195 if verbose: print "Running from compiled:", pkg_name
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
196 d2 = run_module(pkg_name) # Read from bytecode
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57313
diff changeset
197 self.assertIn("x", d2)
57132
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
198 self.assertTrue(d2["x"] == 1)
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
199 del d2 # Ensure __loader__ entry doesn't keep file open
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
200 finally:
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
201 self._del_pkg(pkg_dir, depth, pkg_name)
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
202 if verbose: print "Package executed successfully"
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
203
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
204 def _add_relative_modules(self, base_dir, source, depth):
42337
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
205 if depth <= 1:
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
206 raise ValueError("Relative module test needs depth > 1")
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
207 pkg_name = "__runpy_pkg__"
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
208 module_dir = base_dir
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
209 for i in range(depth):
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
210 parent_dir = module_dir
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
211 module_dir = os.path.join(module_dir, pkg_name)
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
212 # Add sibling module
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
213 sibling_fname = os.path.join(module_dir, "sibling"+os.extsep+"py")
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
214 sibling_file = open(sibling_fname, "w")
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
215 sibling_file.close()
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
216 if verbose: print " Added sibling module:", sibling_fname
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
217 # Add nephew module
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
218 uncle_dir = os.path.join(parent_dir, "uncle")
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
219 self._add_pkg_dir(uncle_dir)
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
220 if verbose: print " Added uncle package:", uncle_dir
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
221 cousin_dir = os.path.join(uncle_dir, "cousin")
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
222 self._add_pkg_dir(cousin_dir)
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
223 if verbose: print " Added cousin package:", cousin_dir
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
224 nephew_fname = os.path.join(cousin_dir, "nephew"+os.extsep+"py")
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
225 nephew_file = open(nephew_fname, "w")
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
226 nephew_file.close()
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
227 if verbose: print " Added nephew module:", nephew_fname
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
228
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
229 def _check_relative_imports(self, depth, run_name=None):
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
230 contents = r"""\
42337
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
231 from __future__ import absolute_import
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
232 from . import sibling
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
233 from ..uncle.cousin import nephew
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
234 """
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
235 pkg_dir, mod_fname, mod_name = (
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
236 self._make_pkg(contents, depth))
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
237 try:
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
238 self._add_relative_modules(pkg_dir, contents, depth)
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
239 pkg_name = mod_name.rpartition('.')[0]
42337
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
240 if verbose: print "Running from source:", mod_name
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
241 d1 = run_module(mod_name, run_name=run_name) # Read from source
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57313
diff changeset
242 self.assertIn("__package__", d1)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44116
diff changeset
243 self.assertTrue(d1["__package__"] == pkg_name)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57313
diff changeset
244 self.assertIn("sibling", d1)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57313
diff changeset
245 self.assertIn("nephew", d1)
42337
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
246 del d1 # Ensure __loader__ entry doesn't keep file open
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
247 __import__(mod_name)
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
248 os.remove(mod_fname)
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
249 if verbose: print "Running from compiled:", mod_name
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
250 d2 = run_module(mod_name, run_name=run_name) # Read from bytecode
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57313
diff changeset
251 self.assertIn("__package__", d2)
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 44116
diff changeset
252 self.assertTrue(d2["__package__"] == pkg_name)
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57313
diff changeset
253 self.assertIn("sibling", d2)
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 57313
diff changeset
254 self.assertIn("nephew", d2)
42337
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
255 del d2 # Ensure __loader__ entry doesn't keep file open
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
256 finally:
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
257 self._del_pkg(pkg_dir, depth, mod_name)
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
258 if verbose: print "Module executed successfully"
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
259
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
260 def test_run_module(self):
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
261 for depth in range(4):
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
262 if verbose: print "Testing package depth:", depth
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
263 self._check_module(depth)
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
264
57132
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
265 def test_run_package(self):
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
266 for depth in range(1, 4):
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
267 if verbose: print "Testing package depth:", depth
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
268 self._check_package(depth)
818b0b287aa4 Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package
Nick Coghlan <ncoghlan@gmail.com>
parents: 55002
diff changeset
269
42337
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
270 def test_explicit_relative_import(self):
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
271 for depth in range(2, 5):
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
272 if verbose: print "Testing relative imports at depth:", depth
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
273 self._check_relative_imports(depth)
38d1beb650ba Add explicit relative import tests for runpy.run_module
Nick Coghlan <ncoghlan@gmail.com>
parents: 39022
diff changeset
274
44116
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
275 def test_main_relative_import(self):
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
276 for depth in range(2, 5):
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
277 if verbose: print "Testing main relative imports at depth:", depth
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
278 self._check_relative_imports(depth, "__main__")
803edf9b1dd9 Implement PEP 366
Nick Coghlan <ncoghlan@gmail.com>
parents: 42825
diff changeset
279
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
280
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: 57132
diff changeset
281 class RunPathTest(unittest.TestCase):
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: 57132
diff changeset
282 """Unit tests for runpy.run_path"""
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: 57132
diff changeset
283 # Based on corresponding tests in test_cmd_line_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: 57132
diff changeset
284
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: 57132
diff changeset
285 test_source = """\
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: 57132
diff changeset
286 # Script may be run with optimisation enabled, so don't rely on assert
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: 57132
diff changeset
287 # statements being executed
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: 57132
diff changeset
288 def assertEqual(lhs, rhs):
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: 57132
diff changeset
289 if lhs != rhs:
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: 57132
diff changeset
290 raise AssertionError('%r != %r' % (lhs, rhs))
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: 57132
diff changeset
291 def assertIs(lhs, rhs):
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: 57132
diff changeset
292 if lhs is not rhs:
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: 57132
diff changeset
293 raise AssertionError('%r is not %r' % (lhs, rhs))
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: 57132
diff changeset
294 # Check basic code execution
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: 57132
diff changeset
295 result = ['Top level assignment']
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: 57132
diff changeset
296 def f():
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: 57132
diff changeset
297 result.append('Lower level reference')
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: 57132
diff changeset
298 f()
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: 57132
diff changeset
299 assertEqual(result, ['Top level assignment', 'Lower level reference'])
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: 57132
diff changeset
300 # Check the sys module
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: 57132
diff changeset
301 import sys
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: 57132
diff changeset
302 assertIs(globals(), sys.modules[__name__].__dict__)
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: 57132
diff changeset
303 argv0 = sys.argv[0]
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: 57132
diff changeset
304 """
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: 57132
diff changeset
305
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: 57132
diff changeset
306 def _make_test_script(self, script_dir, script_basename, source=None):
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: 57132
diff changeset
307 if source is None:
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: 57132
diff changeset
308 source = self.test_source
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: 57132
diff changeset
309 return make_script(script_dir, script_basename, source)
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: 57132
diff changeset
310
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: 57132
diff changeset
311 def _check_script(self, script_name, expected_name, expected_file,
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: 57132
diff changeset
312 expected_argv0, expected_package):
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: 57132
diff changeset
313 result = run_path(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: 57132
diff changeset
314 self.assertEqual(result["__name__"], expected_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: 57132
diff changeset
315 self.assertEqual(result["__file__"], expected_file)
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: 57132
diff changeset
316 self.assertIn("argv0", result)
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: 57132
diff changeset
317 self.assertEqual(result["argv0"], expected_argv0)
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: 57132
diff changeset
318 self.assertEqual(result["__package__"], expected_package)
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: 57132
diff changeset
319
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: 57132
diff changeset
320 def _check_import_error(self, script_name, msg):
57313
3d2b06aa7b36 Correctly escape arbitrary error message text in the runpy unit tests
Nick Coghlan <ncoghlan@gmail.com>
parents: 57289
diff changeset
321 msg = re.escape(msg)
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: 57132
diff changeset
322 self.assertRaisesRegexp(ImportError, msg, run_path, 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: 57132
diff changeset
323
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: 57132
diff changeset
324 def test_basic_script(self):
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: 57132
diff changeset
325 with temp_dir() as script_dir:
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: 57132
diff changeset
326 mod_name = '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: 57132
diff changeset
327 script_name = self._make_test_script(script_dir, mod_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: 57132
diff changeset
328 self._check_script(script_name, "<run_path>", 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: 57132
diff changeset
329 script_name, None)
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: 57132
diff changeset
330
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: 57132
diff changeset
331 def test_script_compiled(self):
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: 57132
diff changeset
332 with temp_dir() as script_dir:
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: 57132
diff changeset
333 mod_name = '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: 57132
diff changeset
334 script_name = self._make_test_script(script_dir, mod_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: 57132
diff changeset
335 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: 57132
diff changeset
336 os.remove(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: 57132
diff changeset
337 self._check_script(compiled_name, "<run_path>", compiled_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: 57132
diff changeset
338 compiled_name, None)
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: 57132
diff changeset
339
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: 57132
diff changeset
340 def test_directory(self):
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: 57132
diff changeset
341 with temp_dir() as script_dir:
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: 57132
diff changeset
342 mod_name = '__main__'
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: 57132
diff changeset
343 script_name = self._make_test_script(script_dir, mod_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: 57132
diff changeset
344 self._check_script(script_dir, "<run_path>", 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: 57132
diff changeset
345 script_dir, '')
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: 57132
diff changeset
346
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: 57132
diff changeset
347 def test_directory_compiled(self):
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: 57132
diff changeset
348 with temp_dir() as script_dir:
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: 57132
diff changeset
349 mod_name = '__main__'
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: 57132
diff changeset
350 script_name = self._make_test_script(script_dir, mod_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: 57132
diff changeset
351 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: 57132
diff changeset
352 os.remove(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: 57132
diff changeset
353 self._check_script(script_dir, "<run_path>", compiled_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: 57132
diff changeset
354 script_dir, '')
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: 57132
diff changeset
355
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: 57132
diff changeset
356 def test_directory_error(self):
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: 57132
diff changeset
357 with temp_dir() as script_dir:
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: 57132
diff changeset
358 mod_name = 'not_main'
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: 57132
diff changeset
359 script_name = self._make_test_script(script_dir, mod_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: 57132
diff changeset
360 msg = "can't find '__main__' module in %r" % script_dir
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: 57132
diff changeset
361 self._check_import_error(script_dir, msg)
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: 57132
diff changeset
362
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: 57132
diff changeset
363 def test_zipfile(self):
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: 57132
diff changeset
364 with temp_dir() as script_dir:
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: 57132
diff changeset
365 mod_name = '__main__'
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: 57132
diff changeset
366 script_name = self._make_test_script(script_dir, mod_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: 57132
diff changeset
367 zip_name, fname = 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: 57132
diff changeset
368 self._check_script(zip_name, "<run_path>", fname, zip_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: 57132
diff changeset
369
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: 57132
diff changeset
370 def test_zipfile_compiled(self):
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: 57132
diff changeset
371 with temp_dir() as script_dir:
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: 57132
diff changeset
372 mod_name = '__main__'
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: 57132
diff changeset
373 script_name = self._make_test_script(script_dir, mod_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: 57132
diff changeset
374 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: 57132
diff changeset
375 zip_name, fname = make_zip_script(script_dir, 'test_zip', compiled_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: 57132
diff changeset
376 self._check_script(zip_name, "<run_path>", fname, zip_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: 57132
diff changeset
377
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: 57132
diff changeset
378 def test_zipfile_error(self):
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: 57132
diff changeset
379 with temp_dir() as script_dir:
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: 57132
diff changeset
380 mod_name = 'not_main'
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: 57132
diff changeset
381 script_name = self._make_test_script(script_dir, mod_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: 57132
diff changeset
382 zip_name, fname = 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: 57132
diff changeset
383 msg = "can't find '__main__' module in %r" % zip_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: 57132
diff changeset
384 self._check_import_error(zip_name, msg)
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: 57132
diff changeset
385
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: 57132
diff changeset
386 def test_main_recursion_error(self):
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: 57132
diff changeset
387 with temp_dir() as script_dir, temp_dir() as dummy_dir:
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: 57132
diff changeset
388 mod_name = '__main__'
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: 57132
diff changeset
389 source = ("import runpy\n"
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: 57132
diff changeset
390 "runpy.run_path(%r)\n") % dummy_dir
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: 57132
diff changeset
391 script_name = self._make_test_script(script_dir, mod_name, source)
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: 57132
diff changeset
392 zip_name, fname = 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: 57132
diff changeset
393 msg = "recursion depth exceeded"
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: 57132
diff changeset
394 self.assertRaisesRegexp(RuntimeError, msg, run_path, zip_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: 57132
diff changeset
395
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: 57132
diff changeset
396
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: 57132
diff changeset
397
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
398 def test_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: 57132
diff changeset
399 run_unittest(RunModuleCodeTest, RunModuleTest, RunPathTest)
36995
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
400
f914630f419c Implement PEP 338 which has been marked as accepted by GvR
Nick Coghlan <ncoghlan@gmail.com>
parents:
diff changeset
401 if __name__ == "__main__":
37003
4f44412e9f6c Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 37001
diff changeset
402 test_main()