1 # Test the runpy module
7 from test
.test_support
import verbose
, run_unittest
, forget
8 from runpy
import _run_code
, _run_module_code
, run_module
10 # Note: This module can't safely test _run_module_as_main as it
11 # runs its tests in the current process, which would mess with the
12 # real __main__ module (usually test.regrtest)
13 # See test_cmd_line_script for a test that executes that code path
15 # Set up the test code and expected results
17 class RunModuleCodeTest(unittest
.TestCase
):
19 expected_result
= ["Top level assignment", "Lower level reference"]
21 "# Check basic code execution\n"
22 "result = ['Top level assignment']\n"
24 " result.append('Lower level reference')\n"
26 "# Check the sys module\n"
28 "run_argv0 = sys.argv[0]\n"
29 "run_name_in_sys_modules = __name__ in sys.modules\n"
30 "if run_name_in_sys_modules:\n"
31 " module_in_sys_modules = globals() is sys.modules[__name__].__dict__\n"
32 "# Check nested operation\n"
34 "nested = runpy._run_module_code('x=1\\n', mod_name='<run>')\n"
37 def test_run_code(self
):
38 saved_argv0
= sys
.argv
[0]
39 d
= _run_code(self
.test_source
, {})
40 self
.failUnless(d
["result"] == self
.expected_result
)
41 self
.failUnless(d
["__name__"] is None)
42 self
.failUnless(d
["__file__"] is None)
43 self
.failUnless(d
["__loader__"] is None)
44 self
.failUnless(d
["__package__"] is None)
45 self
.failUnless(d
["run_argv0"] is saved_argv0
)
46 self
.failUnless("run_name" not in d
)
47 self
.failUnless(sys
.argv
[0] is saved_argv0
)
49 def test_run_module_code(self
):
52 file = "Some other nonsense"
53 loader
= "Now you're just being silly"
54 package
= '' # Treat as a top level module
55 d1
= dict(initial
=initial
)
56 saved_argv0
= sys
.argv
[0]
57 d2
= _run_module_code(self
.test_source
,
63 self
.failUnless("result" not in d1
)
64 self
.failUnless(d2
["initial"] is initial
)
65 self
.failUnless(d2
["result"] == self
.expected_result
)
66 self
.failUnless(d2
["nested"]["x"] == 1)
67 self
.failUnless(d2
["__name__"] is name
)
68 self
.failUnless(d2
["run_name_in_sys_modules"])
69 self
.failUnless(d2
["module_in_sys_modules"])
70 self
.failUnless(d2
["__file__"] is file)
71 self
.failUnless(d2
["run_argv0"] is file)
72 self
.failUnless(d2
["__loader__"] is loader
)
73 self
.failUnless(d2
["__package__"] is package
)
74 self
.failUnless(sys
.argv
[0] is saved_argv0
)
75 self
.failUnless(name
not in sys
.modules
)
78 class RunModuleTest(unittest
.TestCase
):
80 def expect_import_error(self
, mod_name
):
86 self
.fail("Expected import error for " + mod_name
)
88 def test_invalid_names(self
):
90 self
.expect_import_error("sys")
91 # Non-existent modules
92 self
.expect_import_error("sys.imp.eric")
93 self
.expect_import_error("os.path.half")
94 self
.expect_import_error("a.bee")
95 self
.expect_import_error(".howard")
96 self
.expect_import_error("..eaten")
98 self
.expect_import_error("logging")
100 def test_library_module(self
):
103 def _add_pkg_dir(self
, pkg_dir
):
105 pkg_fname
= os
.path
.join(pkg_dir
, "__init__"+os
.extsep
+"py")
106 pkg_file
= open(pkg_fname
, "w")
110 def _make_pkg(self
, source
, depth
):
111 pkg_name
= "__runpy_pkg__"
112 test_fname
= "runpy_test"+os
.extsep
+"py"
113 pkg_dir
= sub_dir
= tempfile
.mkdtemp()
114 if verbose
: print " Package tree in:", sub_dir
115 sys
.path
.insert(0, pkg_dir
)
116 if verbose
: print " Updated sys.path:", sys
.path
[0]
117 for i
in range(depth
):
118 sub_dir
= os
.path
.join(sub_dir
, pkg_name
)
119 pkg_fname
= self
._add
_pkg
_dir
(sub_dir
)
120 if verbose
: print " Next level in:", sub_dir
121 if verbose
: print " Created:", pkg_fname
122 mod_fname
= os
.path
.join(sub_dir
, test_fname
)
123 mod_file
= open(mod_fname
, "w")
124 mod_file
.write(source
)
126 if verbose
: print " Created:", mod_fname
127 mod_name
= (pkg_name
+".")*depth
+ "runpy_test"
128 return pkg_dir
, mod_fname
, mod_name
130 def _del_pkg(self
, top
, depth
, mod_name
):
131 for entry
in list(sys
.modules
):
132 if entry
.startswith("__runpy_pkg__"):
133 del sys
.modules
[entry
]
134 if verbose
: print " Removed sys.modules entries"
136 if verbose
: print " Removed sys.path entry"
137 for root
, dirs
, files
in os
.walk(top
, topdown
=False):
140 os
.remove(os
.path
.join(root
, name
))
142 if verbose
: print ex
# Persist with cleaning up
144 fullname
= os
.path
.join(root
, name
)
148 if verbose
: print ex
# Persist with cleaning up
151 if verbose
: print " Removed package tree"
153 if verbose
: print ex
# Persist with cleaning up
155 def _check_module(self
, depth
):
156 pkg_dir
, mod_fname
, mod_name
= (
157 self
._make
_pkg
("x=1\n", depth
))
160 if verbose
: print "Running from source:", mod_name
161 d1
= run_module(mod_name
) # Read from source
162 self
.failUnless("x" in d1
)
163 self
.failUnless(d1
["x"] == 1)
164 del d1
# Ensure __loader__ entry doesn't keep file open
167 if verbose
: print "Running from compiled:", mod_name
168 d2
= run_module(mod_name
) # Read from bytecode
169 self
.failUnless("x" in d2
)
170 self
.failUnless(d2
["x"] == 1)
171 del d2
# Ensure __loader__ entry doesn't keep file open
173 self
._del
_pkg
(pkg_dir
, depth
, mod_name
)
174 if verbose
: print "Module executed successfully"
176 def _add_relative_modules(self
, base_dir
, source
, depth
):
178 raise ValueError("Relative module test needs depth > 1")
179 pkg_name
= "__runpy_pkg__"
180 module_dir
= base_dir
181 for i
in range(depth
):
182 parent_dir
= module_dir
183 module_dir
= os
.path
.join(module_dir
, pkg_name
)
185 sibling_fname
= os
.path
.join(module_dir
, "sibling"+os
.extsep
+"py")
186 sibling_file
= open(sibling_fname
, "w")
188 if verbose
: print " Added sibling module:", sibling_fname
190 uncle_dir
= os
.path
.join(parent_dir
, "uncle")
191 self
._add
_pkg
_dir
(uncle_dir
)
192 if verbose
: print " Added uncle package:", uncle_dir
193 cousin_dir
= os
.path
.join(uncle_dir
, "cousin")
194 self
._add
_pkg
_dir
(cousin_dir
)
195 if verbose
: print " Added cousin package:", cousin_dir
196 nephew_fname
= os
.path
.join(cousin_dir
, "nephew"+os
.extsep
+"py")
197 nephew_file
= open(nephew_fname
, "w")
199 if verbose
: print " Added nephew module:", nephew_fname
201 def _check_relative_imports(self
, depth
, run_name
=None):
203 from __future__ import absolute_import
204 from . import sibling
205 from ..uncle.cousin import nephew
207 pkg_dir
, mod_fname
, mod_name
= (
208 self
._make
_pkg
(contents
, depth
))
210 self
._add
_relative
_modules
(pkg_dir
, contents
, depth
)
211 pkg_name
= mod_name
.rpartition('.')[0]
212 if verbose
: print "Running from source:", mod_name
213 d1
= run_module(mod_name
, run_name
=run_name
) # Read from source
214 self
.failUnless("__package__" in d1
)
215 self
.failUnless(d1
["__package__"] == pkg_name
)
216 self
.failUnless("sibling" in d1
)
217 self
.failUnless("nephew" in d1
)
218 del d1
# Ensure __loader__ entry doesn't keep file open
221 if verbose
: print "Running from compiled:", mod_name
222 d2
= run_module(mod_name
, run_name
=run_name
) # Read from bytecode
223 self
.failUnless("__package__" in d2
)
224 self
.failUnless(d2
["__package__"] == pkg_name
)
225 self
.failUnless("sibling" in d2
)
226 self
.failUnless("nephew" in d2
)
227 del d2
# Ensure __loader__ entry doesn't keep file open
229 self
._del
_pkg
(pkg_dir
, depth
, mod_name
)
230 if verbose
: print "Module executed successfully"
232 def test_run_module(self
):
233 for depth
in range(4):
234 if verbose
: print "Testing package depth:", depth
235 self
._check
_module
(depth
)
237 def test_explicit_relative_import(self
):
238 for depth
in range(2, 5):
239 if verbose
: print "Testing relative imports at depth:", depth
240 self
._check
_relative
_imports
(depth
)
242 def test_main_relative_import(self
):
243 for depth
in range(2, 5):
244 if verbose
: print "Testing main relative imports at depth:", depth
245 self
._check
_relative
_imports
(depth
, "__main__")
249 run_unittest(RunModuleCodeTest
)
250 run_unittest(RunModuleTest
)
252 if __name__
== "__main__":