ยปCore Development>Code coverage>Lib/test/test_runpy.py

Python code coverage for Lib/test/test_runpy.py

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