logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

runpy.py (12238B)


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