1 | n/a | # Test the runpy module |
---|
2 | n/a | import unittest |
---|
3 | n/a | import os |
---|
4 | n/a | import os.path |
---|
5 | n/a | import sys |
---|
6 | n/a | import re |
---|
7 | n/a | import tempfile |
---|
8 | n/a | import importlib, importlib.machinery, importlib.util |
---|
9 | n/a | import py_compile |
---|
10 | n/a | import warnings |
---|
11 | n/a | from test.support import ( |
---|
12 | n/a | forget, make_legacy_pyc, unload, verbose, no_tracing, |
---|
13 | n/a | create_empty_file, temp_dir) |
---|
14 | n/a | from test.support.script_helper import ( |
---|
15 | n/a | make_pkg, make_script, make_zip_pkg, make_zip_script) |
---|
16 | n/a | |
---|
17 | n/a | |
---|
18 | n/a | import runpy |
---|
19 | n/a | from runpy import _run_code, _run_module_code, run_module, run_path |
---|
20 | n/a | # Note: This module can't safely test _run_module_as_main as it |
---|
21 | n/a | # runs its tests in the current process, which would mess with the |
---|
22 | n/a | # real __main__ module (usually test.regrtest) |
---|
23 | n/a | # See test_cmd_line_script for a test that executes that code path |
---|
24 | n/a | |
---|
25 | n/a | |
---|
26 | n/a | # Set up the test code and expected results |
---|
27 | n/a | example_source = """\ |
---|
28 | n/a | # Check basic code execution |
---|
29 | n/a | result = ['Top level assignment'] |
---|
30 | n/a | def f(): |
---|
31 | n/a | result.append('Lower level reference') |
---|
32 | n/a | f() |
---|
33 | n/a | del f |
---|
34 | n/a | # Check the sys module |
---|
35 | n/a | import sys |
---|
36 | n/a | run_argv0 = sys.argv[0] |
---|
37 | n/a | run_name_in_sys_modules = __name__ in sys.modules |
---|
38 | n/a | module_in_sys_modules = (run_name_in_sys_modules and |
---|
39 | n/a | globals() is sys.modules[__name__].__dict__) |
---|
40 | n/a | # Check nested operation |
---|
41 | n/a | import runpy |
---|
42 | n/a | nested = runpy._run_module_code('x=1\\n', mod_name='<run>') |
---|
43 | n/a | """ |
---|
44 | n/a | |
---|
45 | n/a | implicit_namespace = { |
---|
46 | n/a | "__name__": None, |
---|
47 | n/a | "__file__": None, |
---|
48 | n/a | "__cached__": None, |
---|
49 | n/a | "__package__": None, |
---|
50 | n/a | "__doc__": None, |
---|
51 | n/a | "__spec__": None |
---|
52 | n/a | } |
---|
53 | n/a | example_namespace = { |
---|
54 | n/a | "sys": sys, |
---|
55 | n/a | "runpy": runpy, |
---|
56 | n/a | "result": ["Top level assignment", "Lower level reference"], |
---|
57 | n/a | "run_argv0": sys.argv[0], |
---|
58 | n/a | "run_name_in_sys_modules": False, |
---|
59 | n/a | "module_in_sys_modules": False, |
---|
60 | n/a | "nested": dict(implicit_namespace, |
---|
61 | n/a | x=1, __name__="<run>", __loader__=None), |
---|
62 | n/a | } |
---|
63 | n/a | example_namespace.update(implicit_namespace) |
---|
64 | n/a | |
---|
65 | n/a | class CodeExecutionMixin: |
---|
66 | n/a | # Issue #15230 (run_path not handling run_name correctly) highlighted a |
---|
67 | n/a | # problem with the way arguments were being passed from higher level APIs |
---|
68 | n/a | # down to lower level code. This mixin makes it easier to ensure full |
---|
69 | n/a | # testing occurs at those upper layers as well, not just at the utility |
---|
70 | n/a | # layer |
---|
71 | n/a | |
---|
72 | n/a | # Figuring out the loader details in advance is hard to do, so we skip |
---|
73 | n/a | # checking the full details of loader and loader_state |
---|
74 | n/a | CHECKED_SPEC_ATTRIBUTES = ["name", "parent", "origin", "cached", |
---|
75 | n/a | "has_location", "submodule_search_locations"] |
---|
76 | n/a | |
---|
77 | n/a | def assertNamespaceMatches(self, result_ns, expected_ns): |
---|
78 | n/a | """Check two namespaces match. |
---|
79 | n/a | |
---|
80 | n/a | Ignores any unspecified interpreter created names |
---|
81 | n/a | """ |
---|
82 | n/a | # Avoid side effects |
---|
83 | n/a | result_ns = result_ns.copy() |
---|
84 | n/a | expected_ns = expected_ns.copy() |
---|
85 | n/a | # Impls are permitted to add extra names, so filter them out |
---|
86 | n/a | for k in list(result_ns): |
---|
87 | n/a | if k.startswith("__") and k.endswith("__"): |
---|
88 | n/a | if k not in expected_ns: |
---|
89 | n/a | result_ns.pop(k) |
---|
90 | n/a | if k not in expected_ns["nested"]: |
---|
91 | n/a | result_ns["nested"].pop(k) |
---|
92 | n/a | # Spec equality includes the loader, so we take the spec out of the |
---|
93 | n/a | # result namespace and check that separately |
---|
94 | n/a | result_spec = result_ns.pop("__spec__") |
---|
95 | n/a | expected_spec = expected_ns.pop("__spec__") |
---|
96 | n/a | if expected_spec is None: |
---|
97 | n/a | self.assertIsNone(result_spec) |
---|
98 | n/a | else: |
---|
99 | n/a | # If an expected loader is set, we just check we got the right |
---|
100 | n/a | # type, rather than checking for full equality |
---|
101 | n/a | if expected_spec.loader is not None: |
---|
102 | n/a | self.assertEqual(type(result_spec.loader), |
---|
103 | n/a | type(expected_spec.loader)) |
---|
104 | n/a | for attr in self.CHECKED_SPEC_ATTRIBUTES: |
---|
105 | n/a | k = "__spec__." + attr |
---|
106 | n/a | actual = (k, getattr(result_spec, attr)) |
---|
107 | n/a | expected = (k, getattr(expected_spec, attr)) |
---|
108 | n/a | self.assertEqual(actual, expected) |
---|
109 | n/a | # For the rest, we still don't use direct dict comparison on the |
---|
110 | n/a | # namespace, as the diffs are too hard to debug if anything breaks |
---|
111 | n/a | self.assertEqual(set(result_ns), set(expected_ns)) |
---|
112 | n/a | for k in result_ns: |
---|
113 | n/a | actual = (k, result_ns[k]) |
---|
114 | n/a | expected = (k, expected_ns[k]) |
---|
115 | n/a | self.assertEqual(actual, expected) |
---|
116 | n/a | |
---|
117 | n/a | def check_code_execution(self, create_namespace, expected_namespace): |
---|
118 | n/a | """Check that an interface runs the example code correctly |
---|
119 | n/a | |
---|
120 | n/a | First argument is a callable accepting the initial globals and |
---|
121 | n/a | using them to create the actual namespace |
---|
122 | n/a | Second argument is the expected result |
---|
123 | n/a | """ |
---|
124 | n/a | sentinel = object() |
---|
125 | n/a | expected_ns = expected_namespace.copy() |
---|
126 | n/a | run_name = expected_ns["__name__"] |
---|
127 | n/a | saved_argv0 = sys.argv[0] |
---|
128 | n/a | saved_mod = sys.modules.get(run_name, sentinel) |
---|
129 | n/a | # Check without initial globals |
---|
130 | n/a | result_ns = create_namespace(None) |
---|
131 | n/a | self.assertNamespaceMatches(result_ns, expected_ns) |
---|
132 | n/a | self.assertIs(sys.argv[0], saved_argv0) |
---|
133 | n/a | self.assertIs(sys.modules.get(run_name, sentinel), saved_mod) |
---|
134 | n/a | # And then with initial globals |
---|
135 | n/a | initial_ns = {"sentinel": sentinel} |
---|
136 | n/a | expected_ns["sentinel"] = sentinel |
---|
137 | n/a | result_ns = create_namespace(initial_ns) |
---|
138 | n/a | self.assertIsNot(result_ns, initial_ns) |
---|
139 | n/a | self.assertNamespaceMatches(result_ns, expected_ns) |
---|
140 | n/a | self.assertIs(sys.argv[0], saved_argv0) |
---|
141 | n/a | self.assertIs(sys.modules.get(run_name, sentinel), saved_mod) |
---|
142 | n/a | |
---|
143 | n/a | |
---|
144 | n/a | class ExecutionLayerTestCase(unittest.TestCase, CodeExecutionMixin): |
---|
145 | n/a | """Unit tests for runpy._run_code and runpy._run_module_code""" |
---|
146 | n/a | |
---|
147 | n/a | def test_run_code(self): |
---|
148 | n/a | expected_ns = example_namespace.copy() |
---|
149 | n/a | expected_ns.update({ |
---|
150 | n/a | "__loader__": None, |
---|
151 | n/a | }) |
---|
152 | n/a | def create_ns(init_globals): |
---|
153 | n/a | return _run_code(example_source, {}, init_globals) |
---|
154 | n/a | self.check_code_execution(create_ns, expected_ns) |
---|
155 | n/a | |
---|
156 | n/a | def test_run_module_code(self): |
---|
157 | n/a | mod_name = "<Nonsense>" |
---|
158 | n/a | mod_fname = "Some other nonsense" |
---|
159 | n/a | mod_loader = "Now you're just being silly" |
---|
160 | n/a | mod_package = '' # Treat as a top level module |
---|
161 | n/a | mod_spec = importlib.machinery.ModuleSpec(mod_name, |
---|
162 | n/a | origin=mod_fname, |
---|
163 | n/a | loader=mod_loader) |
---|
164 | n/a | expected_ns = example_namespace.copy() |
---|
165 | n/a | expected_ns.update({ |
---|
166 | n/a | "__name__": mod_name, |
---|
167 | n/a | "__file__": mod_fname, |
---|
168 | n/a | "__loader__": mod_loader, |
---|
169 | n/a | "__package__": mod_package, |
---|
170 | n/a | "__spec__": mod_spec, |
---|
171 | n/a | "run_argv0": mod_fname, |
---|
172 | n/a | "run_name_in_sys_modules": True, |
---|
173 | n/a | "module_in_sys_modules": True, |
---|
174 | n/a | }) |
---|
175 | n/a | def create_ns(init_globals): |
---|
176 | n/a | return _run_module_code(example_source, |
---|
177 | n/a | init_globals, |
---|
178 | n/a | mod_name, |
---|
179 | n/a | mod_spec) |
---|
180 | n/a | self.check_code_execution(create_ns, expected_ns) |
---|
181 | n/a | |
---|
182 | n/a | # TODO: Use self.addCleanup to get rid of a lot of try-finally blocks |
---|
183 | n/a | class RunModuleTestCase(unittest.TestCase, CodeExecutionMixin): |
---|
184 | n/a | """Unit tests for runpy.run_module""" |
---|
185 | n/a | |
---|
186 | n/a | def expect_import_error(self, mod_name): |
---|
187 | n/a | try: |
---|
188 | n/a | run_module(mod_name) |
---|
189 | n/a | except ImportError: |
---|
190 | n/a | pass |
---|
191 | n/a | else: |
---|
192 | n/a | self.fail("Expected import error for " + mod_name) |
---|
193 | n/a | |
---|
194 | n/a | def test_invalid_names(self): |
---|
195 | n/a | # Builtin module |
---|
196 | n/a | self.expect_import_error("sys") |
---|
197 | n/a | # Non-existent modules |
---|
198 | n/a | self.expect_import_error("sys.imp.eric") |
---|
199 | n/a | self.expect_import_error("os.path.half") |
---|
200 | n/a | self.expect_import_error("a.bee") |
---|
201 | n/a | # Relative names not allowed |
---|
202 | n/a | self.expect_import_error(".howard") |
---|
203 | n/a | self.expect_import_error("..eaten") |
---|
204 | n/a | self.expect_import_error(".test_runpy") |
---|
205 | n/a | self.expect_import_error(".unittest") |
---|
206 | n/a | # Package without __main__.py |
---|
207 | n/a | self.expect_import_error("multiprocessing") |
---|
208 | n/a | |
---|
209 | n/a | def test_library_module(self): |
---|
210 | n/a | self.assertEqual(run_module("runpy")["__name__"], "runpy") |
---|
211 | n/a | |
---|
212 | n/a | def _add_pkg_dir(self, pkg_dir, namespace=False): |
---|
213 | n/a | os.mkdir(pkg_dir) |
---|
214 | n/a | if namespace: |
---|
215 | n/a | return None |
---|
216 | n/a | pkg_fname = os.path.join(pkg_dir, "__init__.py") |
---|
217 | n/a | create_empty_file(pkg_fname) |
---|
218 | n/a | return pkg_fname |
---|
219 | n/a | |
---|
220 | n/a | def _make_pkg(self, source, depth, mod_base="runpy_test", |
---|
221 | n/a | *, namespace=False, parent_namespaces=False): |
---|
222 | n/a | # Enforce a couple of internal sanity checks on test cases |
---|
223 | n/a | if (namespace or parent_namespaces) and not depth: |
---|
224 | n/a | raise RuntimeError("Can't mark top level module as a " |
---|
225 | n/a | "namespace package") |
---|
226 | n/a | pkg_name = "__runpy_pkg__" |
---|
227 | n/a | test_fname = mod_base+os.extsep+"py" |
---|
228 | n/a | pkg_dir = sub_dir = os.path.realpath(tempfile.mkdtemp()) |
---|
229 | n/a | if verbose > 1: print(" Package tree in:", sub_dir) |
---|
230 | n/a | sys.path.insert(0, pkg_dir) |
---|
231 | n/a | if verbose > 1: print(" Updated sys.path:", sys.path[0]) |
---|
232 | n/a | if depth: |
---|
233 | n/a | namespace_flags = [parent_namespaces] * depth |
---|
234 | n/a | namespace_flags[-1] = namespace |
---|
235 | n/a | for namespace_flag in namespace_flags: |
---|
236 | n/a | sub_dir = os.path.join(sub_dir, pkg_name) |
---|
237 | n/a | pkg_fname = self._add_pkg_dir(sub_dir, namespace_flag) |
---|
238 | n/a | if verbose > 1: print(" Next level in:", sub_dir) |
---|
239 | n/a | if verbose > 1: print(" Created:", pkg_fname) |
---|
240 | n/a | mod_fname = os.path.join(sub_dir, test_fname) |
---|
241 | n/a | mod_file = open(mod_fname, "w") |
---|
242 | n/a | mod_file.write(source) |
---|
243 | n/a | mod_file.close() |
---|
244 | n/a | if verbose > 1: print(" Created:", mod_fname) |
---|
245 | n/a | mod_name = (pkg_name+".")*depth + mod_base |
---|
246 | n/a | mod_spec = importlib.util.spec_from_file_location(mod_name, |
---|
247 | n/a | mod_fname) |
---|
248 | n/a | return pkg_dir, mod_fname, mod_name, mod_spec |
---|
249 | n/a | |
---|
250 | n/a | def _del_pkg(self, top): |
---|
251 | n/a | for entry in list(sys.modules): |
---|
252 | n/a | if entry.startswith("__runpy_pkg__"): |
---|
253 | n/a | del sys.modules[entry] |
---|
254 | n/a | if verbose > 1: print(" Removed sys.modules entries") |
---|
255 | n/a | del sys.path[0] |
---|
256 | n/a | if verbose > 1: print(" Removed sys.path entry") |
---|
257 | n/a | for root, dirs, files in os.walk(top, topdown=False): |
---|
258 | n/a | for name in files: |
---|
259 | n/a | try: |
---|
260 | n/a | os.remove(os.path.join(root, name)) |
---|
261 | n/a | except OSError as ex: |
---|
262 | n/a | if verbose > 1: print(ex) # Persist with cleaning up |
---|
263 | n/a | for name in dirs: |
---|
264 | n/a | fullname = os.path.join(root, name) |
---|
265 | n/a | try: |
---|
266 | n/a | os.rmdir(fullname) |
---|
267 | n/a | except OSError as ex: |
---|
268 | n/a | if verbose > 1: print(ex) # Persist with cleaning up |
---|
269 | n/a | try: |
---|
270 | n/a | os.rmdir(top) |
---|
271 | n/a | if verbose > 1: print(" Removed package tree") |
---|
272 | n/a | except OSError as ex: |
---|
273 | n/a | if verbose > 1: print(ex) # Persist with cleaning up |
---|
274 | n/a | |
---|
275 | n/a | def _fix_ns_for_legacy_pyc(self, ns, alter_sys): |
---|
276 | n/a | char_to_add = "c" |
---|
277 | n/a | ns["__file__"] += char_to_add |
---|
278 | n/a | ns["__cached__"] = ns["__file__"] |
---|
279 | n/a | spec = ns["__spec__"] |
---|
280 | n/a | new_spec = importlib.util.spec_from_file_location(spec.name, |
---|
281 | n/a | ns["__file__"]) |
---|
282 | n/a | ns["__spec__"] = new_spec |
---|
283 | n/a | if alter_sys: |
---|
284 | n/a | ns["run_argv0"] += char_to_add |
---|
285 | n/a | |
---|
286 | n/a | |
---|
287 | n/a | def _check_module(self, depth, alter_sys=False, |
---|
288 | n/a | *, namespace=False, parent_namespaces=False): |
---|
289 | n/a | pkg_dir, mod_fname, mod_name, mod_spec = ( |
---|
290 | n/a | self._make_pkg(example_source, depth, |
---|
291 | n/a | namespace=namespace, |
---|
292 | n/a | parent_namespaces=parent_namespaces)) |
---|
293 | n/a | forget(mod_name) |
---|
294 | n/a | expected_ns = example_namespace.copy() |
---|
295 | n/a | expected_ns.update({ |
---|
296 | n/a | "__name__": mod_name, |
---|
297 | n/a | "__file__": mod_fname, |
---|
298 | n/a | "__cached__": mod_spec.cached, |
---|
299 | n/a | "__package__": mod_name.rpartition(".")[0], |
---|
300 | n/a | "__spec__": mod_spec, |
---|
301 | n/a | }) |
---|
302 | n/a | if alter_sys: |
---|
303 | n/a | expected_ns.update({ |
---|
304 | n/a | "run_argv0": mod_fname, |
---|
305 | n/a | "run_name_in_sys_modules": True, |
---|
306 | n/a | "module_in_sys_modules": True, |
---|
307 | n/a | }) |
---|
308 | n/a | def create_ns(init_globals): |
---|
309 | n/a | return run_module(mod_name, init_globals, alter_sys=alter_sys) |
---|
310 | n/a | try: |
---|
311 | n/a | if verbose > 1: print("Running from source:", mod_name) |
---|
312 | n/a | self.check_code_execution(create_ns, expected_ns) |
---|
313 | n/a | importlib.invalidate_caches() |
---|
314 | n/a | __import__(mod_name) |
---|
315 | n/a | os.remove(mod_fname) |
---|
316 | n/a | if not sys.dont_write_bytecode: |
---|
317 | n/a | make_legacy_pyc(mod_fname) |
---|
318 | n/a | unload(mod_name) # In case loader caches paths |
---|
319 | n/a | importlib.invalidate_caches() |
---|
320 | n/a | if verbose > 1: print("Running from compiled:", mod_name) |
---|
321 | n/a | self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) |
---|
322 | n/a | self.check_code_execution(create_ns, expected_ns) |
---|
323 | n/a | finally: |
---|
324 | n/a | self._del_pkg(pkg_dir) |
---|
325 | n/a | if verbose > 1: print("Module executed successfully") |
---|
326 | n/a | |
---|
327 | n/a | def _check_package(self, depth, alter_sys=False, |
---|
328 | n/a | *, namespace=False, parent_namespaces=False): |
---|
329 | n/a | pkg_dir, mod_fname, mod_name, mod_spec = ( |
---|
330 | n/a | self._make_pkg(example_source, depth, "__main__", |
---|
331 | n/a | namespace=namespace, |
---|
332 | n/a | parent_namespaces=parent_namespaces)) |
---|
333 | n/a | pkg_name = mod_name.rpartition(".")[0] |
---|
334 | n/a | forget(mod_name) |
---|
335 | n/a | expected_ns = example_namespace.copy() |
---|
336 | n/a | expected_ns.update({ |
---|
337 | n/a | "__name__": mod_name, |
---|
338 | n/a | "__file__": mod_fname, |
---|
339 | n/a | "__cached__": importlib.util.cache_from_source(mod_fname), |
---|
340 | n/a | "__package__": pkg_name, |
---|
341 | n/a | "__spec__": mod_spec, |
---|
342 | n/a | }) |
---|
343 | n/a | if alter_sys: |
---|
344 | n/a | expected_ns.update({ |
---|
345 | n/a | "run_argv0": mod_fname, |
---|
346 | n/a | "run_name_in_sys_modules": True, |
---|
347 | n/a | "module_in_sys_modules": True, |
---|
348 | n/a | }) |
---|
349 | n/a | def create_ns(init_globals): |
---|
350 | n/a | return run_module(pkg_name, init_globals, alter_sys=alter_sys) |
---|
351 | n/a | try: |
---|
352 | n/a | if verbose > 1: print("Running from source:", pkg_name) |
---|
353 | n/a | self.check_code_execution(create_ns, expected_ns) |
---|
354 | n/a | importlib.invalidate_caches() |
---|
355 | n/a | __import__(mod_name) |
---|
356 | n/a | os.remove(mod_fname) |
---|
357 | n/a | if not sys.dont_write_bytecode: |
---|
358 | n/a | make_legacy_pyc(mod_fname) |
---|
359 | n/a | unload(mod_name) # In case loader caches paths |
---|
360 | n/a | if verbose > 1: print("Running from compiled:", pkg_name) |
---|
361 | n/a | importlib.invalidate_caches() |
---|
362 | n/a | self._fix_ns_for_legacy_pyc(expected_ns, alter_sys) |
---|
363 | n/a | self.check_code_execution(create_ns, expected_ns) |
---|
364 | n/a | finally: |
---|
365 | n/a | self._del_pkg(pkg_dir) |
---|
366 | n/a | if verbose > 1: print("Package executed successfully") |
---|
367 | n/a | |
---|
368 | n/a | def _add_relative_modules(self, base_dir, source, depth): |
---|
369 | n/a | if depth <= 1: |
---|
370 | n/a | raise ValueError("Relative module test needs depth > 1") |
---|
371 | n/a | pkg_name = "__runpy_pkg__" |
---|
372 | n/a | module_dir = base_dir |
---|
373 | n/a | for i in range(depth): |
---|
374 | n/a | parent_dir = module_dir |
---|
375 | n/a | module_dir = os.path.join(module_dir, pkg_name) |
---|
376 | n/a | # Add sibling module |
---|
377 | n/a | sibling_fname = os.path.join(module_dir, "sibling.py") |
---|
378 | n/a | create_empty_file(sibling_fname) |
---|
379 | n/a | if verbose > 1: print(" Added sibling module:", sibling_fname) |
---|
380 | n/a | # Add nephew module |
---|
381 | n/a | uncle_dir = os.path.join(parent_dir, "uncle") |
---|
382 | n/a | self._add_pkg_dir(uncle_dir) |
---|
383 | n/a | if verbose > 1: print(" Added uncle package:", uncle_dir) |
---|
384 | n/a | cousin_dir = os.path.join(uncle_dir, "cousin") |
---|
385 | n/a | self._add_pkg_dir(cousin_dir) |
---|
386 | n/a | if verbose > 1: print(" Added cousin package:", cousin_dir) |
---|
387 | n/a | nephew_fname = os.path.join(cousin_dir, "nephew.py") |
---|
388 | n/a | create_empty_file(nephew_fname) |
---|
389 | n/a | if verbose > 1: print(" Added nephew module:", nephew_fname) |
---|
390 | n/a | |
---|
391 | n/a | def _check_relative_imports(self, depth, run_name=None): |
---|
392 | n/a | contents = r"""\ |
---|
393 | n/a | from __future__ import absolute_import |
---|
394 | n/a | from . import sibling |
---|
395 | n/a | from ..uncle.cousin import nephew |
---|
396 | n/a | """ |
---|
397 | n/a | pkg_dir, mod_fname, mod_name, mod_spec = ( |
---|
398 | n/a | self._make_pkg(contents, depth)) |
---|
399 | n/a | if run_name is None: |
---|
400 | n/a | expected_name = mod_name |
---|
401 | n/a | else: |
---|
402 | n/a | expected_name = run_name |
---|
403 | n/a | try: |
---|
404 | n/a | self._add_relative_modules(pkg_dir, contents, depth) |
---|
405 | n/a | pkg_name = mod_name.rpartition('.')[0] |
---|
406 | n/a | if verbose > 1: print("Running from source:", mod_name) |
---|
407 | n/a | d1 = run_module(mod_name, run_name=run_name) # Read from source |
---|
408 | n/a | self.assertEqual(d1["__name__"], expected_name) |
---|
409 | n/a | self.assertEqual(d1["__package__"], pkg_name) |
---|
410 | n/a | self.assertIn("sibling", d1) |
---|
411 | n/a | self.assertIn("nephew", d1) |
---|
412 | n/a | del d1 # Ensure __loader__ entry doesn't keep file open |
---|
413 | n/a | importlib.invalidate_caches() |
---|
414 | n/a | __import__(mod_name) |
---|
415 | n/a | os.remove(mod_fname) |
---|
416 | n/a | if not sys.dont_write_bytecode: |
---|
417 | n/a | make_legacy_pyc(mod_fname) |
---|
418 | n/a | unload(mod_name) # In case the loader caches paths |
---|
419 | n/a | if verbose > 1: print("Running from compiled:", mod_name) |
---|
420 | n/a | importlib.invalidate_caches() |
---|
421 | n/a | d2 = run_module(mod_name, run_name=run_name) # Read from bytecode |
---|
422 | n/a | self.assertEqual(d2["__name__"], expected_name) |
---|
423 | n/a | self.assertEqual(d2["__package__"], pkg_name) |
---|
424 | n/a | self.assertIn("sibling", d2) |
---|
425 | n/a | self.assertIn("nephew", d2) |
---|
426 | n/a | del d2 # Ensure __loader__ entry doesn't keep file open |
---|
427 | n/a | finally: |
---|
428 | n/a | self._del_pkg(pkg_dir) |
---|
429 | n/a | if verbose > 1: print("Module executed successfully") |
---|
430 | n/a | |
---|
431 | n/a | def test_run_module(self): |
---|
432 | n/a | for depth in range(4): |
---|
433 | n/a | if verbose > 1: print("Testing package depth:", depth) |
---|
434 | n/a | self._check_module(depth) |
---|
435 | n/a | |
---|
436 | n/a | def test_run_module_in_namespace_package(self): |
---|
437 | n/a | for depth in range(1, 4): |
---|
438 | n/a | if verbose > 1: print("Testing package depth:", depth) |
---|
439 | n/a | self._check_module(depth, namespace=True, parent_namespaces=True) |
---|
440 | n/a | |
---|
441 | n/a | def test_run_package(self): |
---|
442 | n/a | for depth in range(1, 4): |
---|
443 | n/a | if verbose > 1: print("Testing package depth:", depth) |
---|
444 | n/a | self._check_package(depth) |
---|
445 | n/a | |
---|
446 | n/a | def test_run_package_init_exceptions(self): |
---|
447 | n/a | # These were previously wrapped in an ImportError; see Issue 14285 |
---|
448 | n/a | result = self._make_pkg("", 1, "__main__") |
---|
449 | n/a | pkg_dir, _, mod_name, _ = result |
---|
450 | n/a | mod_name = mod_name.replace(".__main__", "") |
---|
451 | n/a | self.addCleanup(self._del_pkg, pkg_dir) |
---|
452 | n/a | init = os.path.join(pkg_dir, "__runpy_pkg__", "__init__.py") |
---|
453 | n/a | |
---|
454 | n/a | exceptions = (ImportError, AttributeError, TypeError, ValueError) |
---|
455 | n/a | for exception in exceptions: |
---|
456 | n/a | name = exception.__name__ |
---|
457 | n/a | with self.subTest(name): |
---|
458 | n/a | source = "raise {0}('{0} in __init__.py.')".format(name) |
---|
459 | n/a | with open(init, "wt", encoding="ascii") as mod_file: |
---|
460 | n/a | mod_file.write(source) |
---|
461 | n/a | try: |
---|
462 | n/a | run_module(mod_name) |
---|
463 | n/a | except exception as err: |
---|
464 | n/a | self.assertNotIn("finding spec", format(err)) |
---|
465 | n/a | else: |
---|
466 | n/a | self.fail("Nothing raised; expected {}".format(name)) |
---|
467 | n/a | try: |
---|
468 | n/a | run_module(mod_name + ".submodule") |
---|
469 | n/a | except exception as err: |
---|
470 | n/a | self.assertNotIn("finding spec", format(err)) |
---|
471 | n/a | else: |
---|
472 | n/a | self.fail("Nothing raised; expected {}".format(name)) |
---|
473 | n/a | |
---|
474 | n/a | def test_submodule_imported_warning(self): |
---|
475 | n/a | pkg_dir, _, mod_name, _ = self._make_pkg("", 1) |
---|
476 | n/a | try: |
---|
477 | n/a | __import__(mod_name) |
---|
478 | n/a | with self.assertWarnsRegex(RuntimeWarning, |
---|
479 | n/a | r"found in sys\.modules"): |
---|
480 | n/a | run_module(mod_name) |
---|
481 | n/a | finally: |
---|
482 | n/a | self._del_pkg(pkg_dir) |
---|
483 | n/a | |
---|
484 | n/a | def test_package_imported_no_warning(self): |
---|
485 | n/a | pkg_dir, _, mod_name, _ = self._make_pkg("", 1, "__main__") |
---|
486 | n/a | self.addCleanup(self._del_pkg, pkg_dir) |
---|
487 | n/a | package = mod_name.replace(".__main__", "") |
---|
488 | n/a | # No warning should occur if we only imported the parent package |
---|
489 | n/a | __import__(package) |
---|
490 | n/a | self.assertIn(package, sys.modules) |
---|
491 | n/a | with warnings.catch_warnings(): |
---|
492 | n/a | warnings.simplefilter("error", RuntimeWarning) |
---|
493 | n/a | run_module(package) |
---|
494 | n/a | # But the warning should occur if we imported the __main__ submodule |
---|
495 | n/a | __import__(mod_name) |
---|
496 | n/a | with self.assertWarnsRegex(RuntimeWarning, r"found in sys\.modules"): |
---|
497 | n/a | run_module(package) |
---|
498 | n/a | |
---|
499 | n/a | def test_run_package_in_namespace_package(self): |
---|
500 | n/a | for depth in range(1, 4): |
---|
501 | n/a | if verbose > 1: print("Testing package depth:", depth) |
---|
502 | n/a | self._check_package(depth, parent_namespaces=True) |
---|
503 | n/a | |
---|
504 | n/a | def test_run_namespace_package(self): |
---|
505 | n/a | for depth in range(1, 4): |
---|
506 | n/a | if verbose > 1: print("Testing package depth:", depth) |
---|
507 | n/a | self._check_package(depth, namespace=True) |
---|
508 | n/a | |
---|
509 | n/a | def test_run_namespace_package_in_namespace_package(self): |
---|
510 | n/a | for depth in range(1, 4): |
---|
511 | n/a | if verbose > 1: print("Testing package depth:", depth) |
---|
512 | n/a | self._check_package(depth, namespace=True, parent_namespaces=True) |
---|
513 | n/a | |
---|
514 | n/a | def test_run_module_alter_sys(self): |
---|
515 | n/a | for depth in range(4): |
---|
516 | n/a | if verbose > 1: print("Testing package depth:", depth) |
---|
517 | n/a | self._check_module(depth, alter_sys=True) |
---|
518 | n/a | |
---|
519 | n/a | def test_run_package_alter_sys(self): |
---|
520 | n/a | for depth in range(1, 4): |
---|
521 | n/a | if verbose > 1: print("Testing package depth:", depth) |
---|
522 | n/a | self._check_package(depth, alter_sys=True) |
---|
523 | n/a | |
---|
524 | n/a | def test_explicit_relative_import(self): |
---|
525 | n/a | for depth in range(2, 5): |
---|
526 | n/a | if verbose > 1: print("Testing relative imports at depth:", depth) |
---|
527 | n/a | self._check_relative_imports(depth) |
---|
528 | n/a | |
---|
529 | n/a | def test_main_relative_import(self): |
---|
530 | n/a | for depth in range(2, 5): |
---|
531 | n/a | if verbose > 1: print("Testing main relative imports at depth:", depth) |
---|
532 | n/a | self._check_relative_imports(depth, "__main__") |
---|
533 | n/a | |
---|
534 | n/a | def test_run_name(self): |
---|
535 | n/a | depth = 1 |
---|
536 | n/a | run_name = "And now for something completely different" |
---|
537 | n/a | pkg_dir, mod_fname, mod_name, mod_spec = ( |
---|
538 | n/a | self._make_pkg(example_source, depth)) |
---|
539 | n/a | forget(mod_name) |
---|
540 | n/a | expected_ns = example_namespace.copy() |
---|
541 | n/a | expected_ns.update({ |
---|
542 | n/a | "__name__": run_name, |
---|
543 | n/a | "__file__": mod_fname, |
---|
544 | n/a | "__cached__": importlib.util.cache_from_source(mod_fname), |
---|
545 | n/a | "__package__": mod_name.rpartition(".")[0], |
---|
546 | n/a | "__spec__": mod_spec, |
---|
547 | n/a | }) |
---|
548 | n/a | def create_ns(init_globals): |
---|
549 | n/a | return run_module(mod_name, init_globals, run_name) |
---|
550 | n/a | try: |
---|
551 | n/a | self.check_code_execution(create_ns, expected_ns) |
---|
552 | n/a | finally: |
---|
553 | n/a | self._del_pkg(pkg_dir) |
---|
554 | n/a | |
---|
555 | n/a | def test_pkgutil_walk_packages(self): |
---|
556 | n/a | # This is a dodgy hack to use the test_runpy infrastructure to test |
---|
557 | n/a | # issue #15343. Issue #15348 declares this is indeed a dodgy hack ;) |
---|
558 | n/a | import pkgutil |
---|
559 | n/a | max_depth = 4 |
---|
560 | n/a | base_name = "__runpy_pkg__" |
---|
561 | n/a | package_suffixes = ["uncle", "uncle.cousin"] |
---|
562 | n/a | module_suffixes = ["uncle.cousin.nephew", base_name + ".sibling"] |
---|
563 | n/a | expected_packages = set() |
---|
564 | n/a | expected_modules = set() |
---|
565 | n/a | for depth in range(1, max_depth): |
---|
566 | n/a | pkg_name = ".".join([base_name] * depth) |
---|
567 | n/a | expected_packages.add(pkg_name) |
---|
568 | n/a | for name in package_suffixes: |
---|
569 | n/a | expected_packages.add(pkg_name + "." + name) |
---|
570 | n/a | for name in module_suffixes: |
---|
571 | n/a | expected_modules.add(pkg_name + "." + name) |
---|
572 | n/a | pkg_name = ".".join([base_name] * max_depth) |
---|
573 | n/a | expected_packages.add(pkg_name) |
---|
574 | n/a | expected_modules.add(pkg_name + ".runpy_test") |
---|
575 | n/a | pkg_dir, mod_fname, mod_name, mod_spec = ( |
---|
576 | n/a | self._make_pkg("", max_depth)) |
---|
577 | n/a | self.addCleanup(self._del_pkg, pkg_dir) |
---|
578 | n/a | for depth in range(2, max_depth+1): |
---|
579 | n/a | self._add_relative_modules(pkg_dir, "", depth) |
---|
580 | n/a | for moduleinfo in pkgutil.walk_packages([pkg_dir]): |
---|
581 | n/a | self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo) |
---|
582 | n/a | self.assertIsInstance(moduleinfo.module_finder, |
---|
583 | n/a | importlib.machinery.FileFinder) |
---|
584 | n/a | if moduleinfo.ispkg: |
---|
585 | n/a | expected_packages.remove(moduleinfo.name) |
---|
586 | n/a | else: |
---|
587 | n/a | expected_modules.remove(moduleinfo.name) |
---|
588 | n/a | self.assertEqual(len(expected_packages), 0, expected_packages) |
---|
589 | n/a | self.assertEqual(len(expected_modules), 0, expected_modules) |
---|
590 | n/a | |
---|
591 | n/a | class RunPathTestCase(unittest.TestCase, CodeExecutionMixin): |
---|
592 | n/a | """Unit tests for runpy.run_path""" |
---|
593 | n/a | |
---|
594 | n/a | def _make_test_script(self, script_dir, script_basename, |
---|
595 | n/a | source=None, omit_suffix=False): |
---|
596 | n/a | if source is None: |
---|
597 | n/a | source = example_source |
---|
598 | n/a | return make_script(script_dir, script_basename, |
---|
599 | n/a | source, omit_suffix) |
---|
600 | n/a | |
---|
601 | n/a | def _check_script(self, script_name, expected_name, expected_file, |
---|
602 | n/a | expected_argv0, mod_name=None, |
---|
603 | n/a | expect_spec=True, check_loader=True): |
---|
604 | n/a | # First check is without run_name |
---|
605 | n/a | def create_ns(init_globals): |
---|
606 | n/a | return run_path(script_name, init_globals) |
---|
607 | n/a | expected_ns = example_namespace.copy() |
---|
608 | n/a | if mod_name is None: |
---|
609 | n/a | spec_name = expected_name |
---|
610 | n/a | else: |
---|
611 | n/a | spec_name = mod_name |
---|
612 | n/a | if expect_spec: |
---|
613 | n/a | mod_spec = importlib.util.spec_from_file_location(spec_name, |
---|
614 | n/a | expected_file) |
---|
615 | n/a | mod_cached = mod_spec.cached |
---|
616 | n/a | if not check_loader: |
---|
617 | n/a | mod_spec.loader = None |
---|
618 | n/a | else: |
---|
619 | n/a | mod_spec = mod_cached = None |
---|
620 | n/a | |
---|
621 | n/a | expected_ns.update({ |
---|
622 | n/a | "__name__": expected_name, |
---|
623 | n/a | "__file__": expected_file, |
---|
624 | n/a | "__cached__": mod_cached, |
---|
625 | n/a | "__package__": "", |
---|
626 | n/a | "__spec__": mod_spec, |
---|
627 | n/a | "run_argv0": expected_argv0, |
---|
628 | n/a | "run_name_in_sys_modules": True, |
---|
629 | n/a | "module_in_sys_modules": True, |
---|
630 | n/a | }) |
---|
631 | n/a | self.check_code_execution(create_ns, expected_ns) |
---|
632 | n/a | # Second check makes sure run_name works in all cases |
---|
633 | n/a | run_name = "prove.issue15230.is.fixed" |
---|
634 | n/a | def create_ns(init_globals): |
---|
635 | n/a | return run_path(script_name, init_globals, run_name) |
---|
636 | n/a | if expect_spec and mod_name is None: |
---|
637 | n/a | mod_spec = importlib.util.spec_from_file_location(run_name, |
---|
638 | n/a | expected_file) |
---|
639 | n/a | if not check_loader: |
---|
640 | n/a | mod_spec.loader = None |
---|
641 | n/a | expected_ns["__spec__"] = mod_spec |
---|
642 | n/a | expected_ns["__name__"] = run_name |
---|
643 | n/a | expected_ns["__package__"] = run_name.rpartition(".")[0] |
---|
644 | n/a | self.check_code_execution(create_ns, expected_ns) |
---|
645 | n/a | |
---|
646 | n/a | def _check_import_error(self, script_name, msg): |
---|
647 | n/a | msg = re.escape(msg) |
---|
648 | n/a | self.assertRaisesRegex(ImportError, msg, run_path, script_name) |
---|
649 | n/a | |
---|
650 | n/a | def test_basic_script(self): |
---|
651 | n/a | with temp_dir() as script_dir: |
---|
652 | n/a | mod_name = 'script' |
---|
653 | n/a | script_name = self._make_test_script(script_dir, mod_name) |
---|
654 | n/a | self._check_script(script_name, "<run_path>", script_name, |
---|
655 | n/a | script_name, expect_spec=False) |
---|
656 | n/a | |
---|
657 | n/a | def test_basic_script_no_suffix(self): |
---|
658 | n/a | with temp_dir() as script_dir: |
---|
659 | n/a | mod_name = 'script' |
---|
660 | n/a | script_name = self._make_test_script(script_dir, mod_name, |
---|
661 | n/a | omit_suffix=True) |
---|
662 | n/a | self._check_script(script_name, "<run_path>", script_name, |
---|
663 | n/a | script_name, expect_spec=False) |
---|
664 | n/a | |
---|
665 | n/a | def test_script_compiled(self): |
---|
666 | n/a | with temp_dir() as script_dir: |
---|
667 | n/a | mod_name = 'script' |
---|
668 | n/a | script_name = self._make_test_script(script_dir, mod_name) |
---|
669 | n/a | compiled_name = py_compile.compile(script_name, doraise=True) |
---|
670 | n/a | os.remove(script_name) |
---|
671 | n/a | self._check_script(compiled_name, "<run_path>", compiled_name, |
---|
672 | n/a | compiled_name, expect_spec=False) |
---|
673 | n/a | |
---|
674 | n/a | def test_directory(self): |
---|
675 | n/a | with temp_dir() as script_dir: |
---|
676 | n/a | mod_name = '__main__' |
---|
677 | n/a | script_name = self._make_test_script(script_dir, mod_name) |
---|
678 | n/a | self._check_script(script_dir, "<run_path>", script_name, |
---|
679 | n/a | script_dir, mod_name=mod_name) |
---|
680 | n/a | |
---|
681 | n/a | def test_directory_compiled(self): |
---|
682 | n/a | with temp_dir() as script_dir: |
---|
683 | n/a | mod_name = '__main__' |
---|
684 | n/a | script_name = self._make_test_script(script_dir, mod_name) |
---|
685 | n/a | compiled_name = py_compile.compile(script_name, doraise=True) |
---|
686 | n/a | os.remove(script_name) |
---|
687 | n/a | if not sys.dont_write_bytecode: |
---|
688 | n/a | legacy_pyc = make_legacy_pyc(script_name) |
---|
689 | n/a | self._check_script(script_dir, "<run_path>", legacy_pyc, |
---|
690 | n/a | script_dir, mod_name=mod_name) |
---|
691 | n/a | |
---|
692 | n/a | def test_directory_error(self): |
---|
693 | n/a | with temp_dir() as script_dir: |
---|
694 | n/a | mod_name = 'not_main' |
---|
695 | n/a | script_name = self._make_test_script(script_dir, mod_name) |
---|
696 | n/a | msg = "can't find '__main__' module in %r" % script_dir |
---|
697 | n/a | self._check_import_error(script_dir, msg) |
---|
698 | n/a | |
---|
699 | n/a | def test_zipfile(self): |
---|
700 | n/a | with temp_dir() as script_dir: |
---|
701 | n/a | mod_name = '__main__' |
---|
702 | n/a | script_name = self._make_test_script(script_dir, mod_name) |
---|
703 | n/a | zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) |
---|
704 | n/a | self._check_script(zip_name, "<run_path>", fname, zip_name, |
---|
705 | n/a | mod_name=mod_name, check_loader=False) |
---|
706 | n/a | |
---|
707 | n/a | def test_zipfile_compiled(self): |
---|
708 | n/a | with temp_dir() as script_dir: |
---|
709 | n/a | mod_name = '__main__' |
---|
710 | n/a | script_name = self._make_test_script(script_dir, mod_name) |
---|
711 | n/a | compiled_name = py_compile.compile(script_name, doraise=True) |
---|
712 | n/a | zip_name, fname = make_zip_script(script_dir, 'test_zip', |
---|
713 | n/a | compiled_name) |
---|
714 | n/a | self._check_script(zip_name, "<run_path>", fname, zip_name, |
---|
715 | n/a | mod_name=mod_name, check_loader=False) |
---|
716 | n/a | |
---|
717 | n/a | def test_zipfile_error(self): |
---|
718 | n/a | with temp_dir() as script_dir: |
---|
719 | n/a | mod_name = 'not_main' |
---|
720 | n/a | script_name = self._make_test_script(script_dir, mod_name) |
---|
721 | n/a | zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) |
---|
722 | n/a | msg = "can't find '__main__' module in %r" % zip_name |
---|
723 | n/a | self._check_import_error(zip_name, msg) |
---|
724 | n/a | |
---|
725 | n/a | @no_tracing |
---|
726 | n/a | def test_main_recursion_error(self): |
---|
727 | n/a | with temp_dir() as script_dir, temp_dir() as dummy_dir: |
---|
728 | n/a | mod_name = '__main__' |
---|
729 | n/a | source = ("import runpy\n" |
---|
730 | n/a | "runpy.run_path(%r)\n") % dummy_dir |
---|
731 | n/a | script_name = self._make_test_script(script_dir, mod_name, source) |
---|
732 | n/a | zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name) |
---|
733 | n/a | msg = "recursion depth exceeded" |
---|
734 | n/a | self.assertRaisesRegex(RecursionError, msg, run_path, zip_name) |
---|
735 | n/a | |
---|
736 | n/a | def test_encoding(self): |
---|
737 | n/a | with temp_dir() as script_dir: |
---|
738 | n/a | filename = os.path.join(script_dir, 'script.py') |
---|
739 | n/a | with open(filename, 'w', encoding='latin1') as f: |
---|
740 | n/a | f.write(""" |
---|
741 | n/a | #coding:latin1 |
---|
742 | n/a | s = "non-ASCII: h\xe9" |
---|
743 | n/a | """) |
---|
744 | n/a | result = run_path(filename) |
---|
745 | n/a | self.assertEqual(result['s'], "non-ASCII: h\xe9") |
---|
746 | n/a | |
---|
747 | n/a | |
---|
748 | n/a | if __name__ == "__main__": |
---|
749 | n/a | unittest.main() |
---|