ยปCore Development>Code coverage>Lib/runpy.py

Python code coverage for Lib/runpy.py

#countcontent
1n/a"""runpy.py - locating and running Python code using the module namespace
2n/a
3n/aProvides support for locating and running Python scripts using the Python
4n/amodule namespace instead of the native filesystem.
5n/a
6n/aThis allows Python code to play nicely with non-filesystem based PEP 302
7n/aimporters when locating support scripts as well as when importing modules.
8n/a"""
9n/a# Written by Nick Coghlan <ncoghlan at gmail.com>
10n/a# to implement PEP 338 (Executing Modules as Scripts)
11n/a
12n/a
13n/aimport sys
14n/aimport importlib.machinery # importlib first so we can test #15386 via -m
15n/aimport importlib.util
16n/aimport types
17n/afrom pkgutil import read_code, get_importer
18n/a
19n/a__all__ = [
20n/a "run_module", "run_path",
21n/a]
22n/a
23n/aclass _TempModule(object):
24n/a """Temporarily replace a module in sys.modules with an empty namespace"""
25n/a def __init__(self, mod_name):
26n/a self.mod_name = mod_name
27n/a self.module = types.ModuleType(mod_name)
28n/a self._saved_module = []
29n/a
30n/a def __enter__(self):
31n/a mod_name = self.mod_name
32n/a try:
33n/a self._saved_module.append(sys.modules[mod_name])
34n/a except KeyError:
35n/a pass
36n/a sys.modules[mod_name] = self.module
37n/a return self
38n/a
39n/a def __exit__(self, *args):
40n/a if self._saved_module:
41n/a sys.modules[self.mod_name] = self._saved_module[0]
42n/a else:
43n/a del sys.modules[self.mod_name]
44n/a self._saved_module = []
45n/a
46n/aclass _ModifiedArgv0(object):
47n/a def __init__(self, value):
48n/a self.value = value
49n/a self._saved_value = self._sentinel = object()
50n/a
51n/a def __enter__(self):
52n/a if self._saved_value is not self._sentinel:
53n/a raise RuntimeError("Already preserving saved value")
54n/a self._saved_value = sys.argv[0]
55n/a sys.argv[0] = self.value
56n/a
57n/a def __exit__(self, *args):
58n/a self.value = self._sentinel
59n/a sys.argv[0] = self._saved_value
60n/a
61n/a# TODO: Replace these helpers with importlib._bootstrap_external functions.
62n/adef _run_code(code, run_globals, init_globals=None,
63n/a mod_name=None, mod_spec=None,
64n/a pkg_name=None, script_name=None):
65n/a """Helper to run code in nominated namespace"""
66n/a if init_globals is not None:
67n/a run_globals.update(init_globals)
68n/a if mod_spec is None:
69n/a loader = None
70n/a fname = script_name
71n/a cached = None
72n/a else:
73n/a loader = mod_spec.loader
74n/a fname = mod_spec.origin
75n/a cached = mod_spec.cached
76n/a if pkg_name is None:
77n/a pkg_name = mod_spec.parent
78n/a run_globals.update(__name__ = mod_name,
79n/a __file__ = fname,
80n/a __cached__ = cached,
81n/a __doc__ = None,
82n/a __loader__ = loader,
83n/a __package__ = pkg_name,
84n/a __spec__ = mod_spec)
85n/a exec(code, run_globals)
86n/a return run_globals
87n/a
88n/adef _run_module_code(code, init_globals=None,
89n/a mod_name=None, mod_spec=None,
90n/a pkg_name=None, script_name=None):
91n/a """Helper to run code in new namespace with sys modified"""
92n/a fname = script_name if mod_spec is None else mod_spec.origin
93n/a with _TempModule(mod_name) as temp_module, _ModifiedArgv0(fname):
94n/a mod_globals = temp_module.module.__dict__
95n/a _run_code(code, mod_globals, init_globals,
96n/a mod_name, mod_spec, pkg_name, script_name)
97n/a # Copy the globals of the temporary module, as they
98n/a # may be cleared when the temporary module goes away
99n/a return mod_globals.copy()
100n/a
101n/a# Helper to get the full name, spec and code for a module
102n/adef _get_module_details(mod_name, error=ImportError):
103n/a if mod_name.startswith("."):
104n/a raise error("Relative module names not supported")
105n/a pkg_name, _, _ = mod_name.rpartition(".")
106n/a if pkg_name:
107n/a # Try importing the parent to avoid catching initialization errors
108n/a try:
109n/a __import__(pkg_name)
110n/a except ImportError as e:
111n/a # If the parent or higher ancestor package is missing, let the
112n/a # error be raised by find_spec() below and then be caught. But do
113n/a # not allow other errors to be caught.
114n/a if e.name is None or (e.name != pkg_name and
115n/a not pkg_name.startswith(e.name + ".")):
116n/a raise
117n/a # Warn if the module has already been imported under its normal name
118n/a existing = sys.modules.get(mod_name)
119n/a if existing is not None and not hasattr(existing, "__path__"):
120n/a from warnings import warn
121n/a msg = "{mod_name!r} found in sys.modules after import of " \
122n/a "package {pkg_name!r}, but prior to execution of " \
123n/a "{mod_name!r}; this may result in unpredictable " \
124n/a "behaviour".format(mod_name=mod_name, pkg_name=pkg_name)
125n/a warn(RuntimeWarning(msg))
126n/a
127n/a try:
128n/a spec = importlib.util.find_spec(mod_name)
129n/a except (ImportError, AttributeError, TypeError, ValueError) as ex:
130n/a # This hack fixes an impedance mismatch between pkgutil and
131n/a # importlib, where the latter raises other errors for cases where
132n/a # pkgutil previously raised ImportError
133n/a msg = "Error while finding module specification for {!r} ({}: {})"
134n/a raise error(msg.format(mod_name, type(ex).__name__, ex)) from ex
135n/a if spec is None:
136n/a raise error("No module named %s" % mod_name)
137n/a if spec.submodule_search_locations is not None:
138n/a if mod_name == "__main__" or mod_name.endswith(".__main__"):
139n/a raise error("Cannot use package as __main__ module")
140n/a try:
141n/a pkg_main_name = mod_name + ".__main__"
142n/a return _get_module_details(pkg_main_name, error)
143n/a except error as e:
144n/a if mod_name not in sys.modules:
145n/a raise # No module loaded; being a package is irrelevant
146n/a raise error(("%s; %r is a package and cannot " +
147n/a "be directly executed") %(e, mod_name))
148n/a loader = spec.loader
149n/a if loader is None:
150n/a raise error("%r is a namespace package and cannot be executed"
151n/a % mod_name)
152n/a try:
153n/a code = loader.get_code(mod_name)
154n/a except ImportError as e:
155n/a raise error(format(e)) from e
156n/a if code is None:
157n/a raise error("No code object available for %s" % mod_name)
158n/a return mod_name, spec, code
159n/a
160n/aclass _Error(Exception):
161n/a """Error that _run_module_as_main() should report without a traceback"""
162n/a
163n/a# XXX ncoghlan: Should this be documented and made public?
164n/a# (Current thoughts: don't repeat the mistake that lead to its
165n/a# creation when run_module() no longer met the needs of
166n/a# mainmodule.c, but couldn't be changed because it was public)
167n/adef _run_module_as_main(mod_name, alter_argv=True):
168n/a """Runs the designated module in the __main__ namespace
169n/a
170n/a Note that the executed module will have full access to the
171n/a __main__ namespace. If this is not desirable, the run_module()
172n/a function should be used to run the module code in a fresh namespace.
173n/a
174n/a At the very least, these variables in __main__ will be overwritten:
175n/a __name__
176n/a __file__
177n/a __cached__
178n/a __loader__
179n/a __package__
180n/a """
181n/a try:
182n/a if alter_argv or mod_name != "__main__": # i.e. -m switch
183n/a mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
184n/a else: # i.e. directory or zipfile execution
185n/a mod_name, mod_spec, code = _get_main_module_details(_Error)
186n/a except _Error as exc:
187n/a msg = "%s: %s" % (sys.executable, exc)
188n/a sys.exit(msg)
189n/a main_globals = sys.modules["__main__"].__dict__
190n/a if alter_argv:
191n/a sys.argv[0] = mod_spec.origin
192n/a return _run_code(code, main_globals, None,
193n/a "__main__", mod_spec)
194n/a
195n/adef run_module(mod_name, init_globals=None,
196n/a run_name=None, alter_sys=False):
197n/a """Execute a module's code without importing it
198n/a
199n/a Returns the resulting top level namespace dictionary
200n/a """
201n/a mod_name, mod_spec, code = _get_module_details(mod_name)
202n/a if run_name is None:
203n/a run_name = mod_name
204n/a if alter_sys:
205n/a return _run_module_code(code, init_globals, run_name, mod_spec)
206n/a else:
207n/a # Leave the sys module alone
208n/a return _run_code(code, {}, init_globals, run_name, mod_spec)
209n/a
210n/adef _get_main_module_details(error=ImportError):
211n/a # Helper that gives a nicer error message when attempting to
212n/a # execute a zipfile or directory by invoking __main__.py
213n/a # Also moves the standard __main__ out of the way so that the
214n/a # preexisting __loader__ entry doesn't cause issues
215n/a main_name = "__main__"
216n/a saved_main = sys.modules[main_name]
217n/a del sys.modules[main_name]
218n/a try:
219n/a return _get_module_details(main_name)
220n/a except ImportError as exc:
221n/a if main_name in str(exc):
222n/a raise error("can't find %r module in %r" %
223n/a (main_name, sys.path[0])) from exc
224n/a raise
225n/a finally:
226n/a sys.modules[main_name] = saved_main
227n/a
228n/a
229n/adef _get_code_from_file(run_name, fname):
230n/a # Check for a compiled file first
231n/a with open(fname, "rb") as f:
232n/a code = read_code(f)
233n/a if code is None:
234n/a # That didn't work, so try it as normal source code
235n/a with open(fname, "rb") as f:
236n/a code = compile(f.read(), fname, 'exec')
237n/a return code, fname
238n/a
239n/adef run_path(path_name, init_globals=None, run_name=None):
240n/a """Execute code located at the specified filesystem location
241n/a
242n/a Returns the resulting top level namespace dictionary
243n/a
244n/a The file path may refer directly to a Python script (i.e.
245n/a one that could be directly executed with execfile) or else
246n/a it may refer to a zipfile or directory containing a top
247n/a level __main__.py script.
248n/a """
249n/a if run_name is None:
250n/a run_name = "<run_path>"
251n/a pkg_name = run_name.rpartition(".")[0]
252n/a importer = get_importer(path_name)
253n/a # Trying to avoid importing imp so as to not consume the deprecation warning.
254n/a is_NullImporter = False
255n/a if type(importer).__module__ == 'imp':
256n/a if type(importer).__name__ == 'NullImporter':
257n/a is_NullImporter = True
258n/a if isinstance(importer, type(None)) or is_NullImporter:
259n/a # Not a valid sys.path entry, so run the code directly
260n/a # execfile() doesn't help as we want to allow compiled files
261n/a code, fname = _get_code_from_file(run_name, path_name)
262n/a return _run_module_code(code, init_globals, run_name,
263n/a pkg_name=pkg_name, script_name=fname)
264n/a else:
265n/a # Finder is defined for path, so add it to
266n/a # the start of sys.path
267n/a sys.path.insert(0, path_name)
268n/a try:
269n/a # Here's where things are a little different from the run_module
270n/a # case. There, we only had to replace the module in sys while the
271n/a # code was running and doing so was somewhat optional. Here, we
272n/a # have no choice and we have to remove it even while we read the
273n/a # code. If we don't do this, a __loader__ attribute in the
274n/a # existing __main__ module may prevent location of the new module.
275n/a mod_name, mod_spec, code = _get_main_module_details()
276n/a with _TempModule(run_name) as temp_module, \
277n/a _ModifiedArgv0(path_name):
278n/a mod_globals = temp_module.module.__dict__
279n/a return _run_code(code, mod_globals, init_globals,
280n/a run_name, mod_spec, pkg_name).copy()
281n/a finally:
282n/a try:
283n/a sys.path.remove(path_name)
284n/a except ValueError:
285n/a pass
286n/a
287n/a
288n/aif __name__ == "__main__":
289n/a # Run the module specified as the next command line argument
290n/a if len(sys.argv) < 2:
291n/a print("No module specified for execution", file=sys.stderr)
292n/a else:
293n/a del sys.argv[0] # Make the requested module sys.argv[0]
294n/a _run_module_as_main(sys.argv[0])