--- /dev/null
+"""runpy.py - locating and running Python code using the module namespace\r
+\r
+Provides support for locating and running Python scripts using the Python\r
+module namespace instead of the native filesystem.\r
+\r
+This allows Python code to play nicely with non-filesystem based PEP 302\r
+importers when locating support scripts as well as when importing modules.\r
+"""\r
+# Written by Nick Coghlan <ncoghlan at gmail.com>\r
+# to implement PEP 338 (Executing Modules as Scripts)\r
+\r
+import sys\r
+import imp\r
+from pkgutil import read_code\r
+try:\r
+ from imp import get_loader\r
+except ImportError:\r
+ from pkgutil import get_loader\r
+\r
+__all__ = [\r
+ "run_module", "run_path",\r
+]\r
+\r
+class _TempModule(object):\r
+ """Temporarily replace a module in sys.modules with an empty namespace"""\r
+ def __init__(self, mod_name):\r
+ self.mod_name = mod_name\r
+ self.module = imp.new_module(mod_name)\r
+ self._saved_module = []\r
+\r
+ def __enter__(self):\r
+ mod_name = self.mod_name\r
+ try:\r
+ self._saved_module.append(sys.modules[mod_name])\r
+ except KeyError:\r
+ pass\r
+ sys.modules[mod_name] = self.module\r
+ return self\r
+\r
+ def __exit__(self, *args):\r
+ if self._saved_module:\r
+ sys.modules[self.mod_name] = self._saved_module[0]\r
+ else:\r
+ del sys.modules[self.mod_name]\r
+ self._saved_module = []\r
+\r
+class _ModifiedArgv0(object):\r
+ def __init__(self, value):\r
+ self.value = value\r
+ self._saved_value = self._sentinel = object()\r
+\r
+ def __enter__(self):\r
+ if self._saved_value is not self._sentinel:\r
+ raise RuntimeError("Already preserving saved value")\r
+ self._saved_value = sys.argv[0]\r
+ sys.argv[0] = self.value\r
+\r
+ def __exit__(self, *args):\r
+ self.value = self._sentinel\r
+ sys.argv[0] = self._saved_value\r
+\r
+def _run_code(code, run_globals, init_globals=None,\r
+ mod_name=None, mod_fname=None,\r
+ mod_loader=None, pkg_name=None):\r
+ """Helper to run code in nominated namespace"""\r
+ if init_globals is not None:\r
+ run_globals.update(init_globals)\r
+ run_globals.update(__name__ = mod_name,\r
+ __file__ = mod_fname,\r
+ __loader__ = mod_loader,\r
+ __package__ = pkg_name)\r
+ exec code in run_globals\r
+ return run_globals\r
+\r
+def _run_module_code(code, init_globals=None,\r
+ mod_name=None, mod_fname=None,\r
+ mod_loader=None, pkg_name=None):\r
+ """Helper to run code in new namespace with sys modified"""\r
+ with _TempModule(mod_name) as temp_module, _ModifiedArgv0(mod_fname):\r
+ mod_globals = temp_module.module.__dict__\r
+ _run_code(code, mod_globals, init_globals,\r
+ mod_name, mod_fname, mod_loader, pkg_name)\r
+ # Copy the globals of the temporary module, as they\r
+ # may be cleared when the temporary module goes away\r
+ return mod_globals.copy()\r
+\r
+\r
+# This helper is needed due to a missing component in the PEP 302\r
+# loader protocol (specifically, "get_filename" is non-standard)\r
+# Since we can't introduce new features in maintenance releases,\r
+# support was added to zipimporter under the name '_get_filename'\r
+def _get_filename(loader, mod_name):\r
+ for attr in ("get_filename", "_get_filename"):\r
+ meth = getattr(loader, attr, None)\r
+ if meth is not None:\r
+ return meth(mod_name)\r
+ return None\r
+\r
+# Helper to get the loader, code and filename for a module\r
+def _get_module_details(mod_name):\r
+ loader = get_loader(mod_name)\r
+ if loader is None:\r
+ raise ImportError("No module named %s" % mod_name)\r
+ if loader.is_package(mod_name):\r
+ if mod_name == "__main__" or mod_name.endswith(".__main__"):\r
+ raise ImportError("Cannot use package as __main__ module")\r
+ try:\r
+ pkg_main_name = mod_name + ".__main__"\r
+ return _get_module_details(pkg_main_name)\r
+ except ImportError, e:\r
+ raise ImportError(("%s; %r is a package and cannot " +\r
+ "be directly executed") %(e, mod_name))\r
+ code = loader.get_code(mod_name)\r
+ if code is None:\r
+ raise ImportError("No code object available for %s" % mod_name)\r
+ filename = _get_filename(loader, mod_name)\r
+ return mod_name, loader, code, filename\r
+\r
+\r
+def _get_main_module_details():\r
+ # Helper that gives a nicer error message when attempting to\r
+ # execute a zipfile or directory by invoking __main__.py\r
+ main_name = "__main__"\r
+ try:\r
+ return _get_module_details(main_name)\r
+ except ImportError as exc:\r
+ if main_name in str(exc):\r
+ raise ImportError("can't find %r module in %r" %\r
+ (main_name, sys.path[0]))\r
+ raise\r
+\r
+# This function is the actual implementation of the -m switch and direct\r
+# execution of zipfiles and directories and is deliberately kept private.\r
+# This avoids a repeat of the situation where run_module() no longer met the\r
+# needs of mainmodule.c, but couldn't be changed because it was public\r
+def _run_module_as_main(mod_name, alter_argv=True):\r
+ """Runs the designated module in the __main__ namespace\r
+\r
+ Note that the executed module will have full access to the\r
+ __main__ namespace. If this is not desirable, the run_module()\r
+ function should be used to run the module code in a fresh namespace.\r
+\r
+ At the very least, these variables in __main__ will be overwritten:\r
+ __name__\r
+ __file__\r
+ __loader__\r
+ __package__\r
+ """\r
+ try:\r
+ if alter_argv or mod_name != "__main__": # i.e. -m switch\r
+ mod_name, loader, code, fname = _get_module_details(mod_name)\r
+ else: # i.e. directory or zipfile execution\r
+ mod_name, loader, code, fname = _get_main_module_details()\r
+ except ImportError as exc:\r
+ msg = "%s: %s" % (sys.executable, str(exc))\r
+ sys.exit(msg)\r
+ pkg_name = mod_name.rpartition('.')[0]\r
+ main_globals = sys.modules["__main__"].__dict__\r
+ if alter_argv:\r
+ sys.argv[0] = fname\r
+ return _run_code(code, main_globals, None,\r
+ "__main__", fname, loader, pkg_name)\r
+\r
+def run_module(mod_name, init_globals=None,\r
+ run_name=None, alter_sys=False):\r
+ """Execute a module's code without importing it\r
+\r
+ Returns the resulting top level namespace dictionary\r
+ """\r
+ mod_name, loader, code, fname = _get_module_details(mod_name)\r
+ if run_name is None:\r
+ run_name = mod_name\r
+ pkg_name = mod_name.rpartition('.')[0]\r
+ if alter_sys:\r
+ return _run_module_code(code, init_globals, run_name,\r
+ fname, loader, pkg_name)\r
+ else:\r
+ # Leave the sys module alone\r
+ return _run_code(code, {}, init_globals, run_name,\r
+ fname, loader, pkg_name)\r
+\r
+\r
+# XXX (ncoghlan): Perhaps expose the C API function\r
+# as imp.get_importer instead of reimplementing it in Python?\r
+def _get_importer(path_name):\r
+ """Python version of PyImport_GetImporter C API function"""\r
+ cache = sys.path_importer_cache\r
+ try:\r
+ importer = cache[path_name]\r
+ except KeyError:\r
+ # Not yet cached. Flag as using the\r
+ # standard machinery until we finish\r
+ # checking the hooks\r
+ cache[path_name] = None\r
+ for hook in sys.path_hooks:\r
+ try:\r
+ importer = hook(path_name)\r
+ break\r
+ except ImportError:\r
+ pass\r
+ else:\r
+ # The following check looks a bit odd. The trick is that\r
+ # NullImporter raises ImportError if the supplied path is a\r
+ # *valid* directory entry (and hence able to be handled\r
+ # by the standard import machinery)\r
+ try:\r
+ importer = imp.NullImporter(path_name)\r
+ except ImportError:\r
+ return None\r
+ cache[path_name] = importer\r
+ return importer\r
+\r
+def _get_code_from_file(fname):\r
+ # Check for a compiled file first\r
+ with open(fname, "rb") as f:\r
+ code = read_code(f)\r
+ if code is None:\r
+ # That didn't work, so try it as normal source code\r
+ with open(fname, "rU") as f:\r
+ code = compile(f.read(), fname, 'exec')\r
+ return code\r
+\r
+def run_path(path_name, init_globals=None, run_name=None):\r
+ """Execute code located at the specified filesystem location\r
+\r
+ Returns the resulting top level namespace dictionary\r
+\r
+ The file path may refer directly to a Python script (i.e.\r
+ one that could be directly executed with execfile) or else\r
+ it may refer to a zipfile or directory containing a top\r
+ level __main__.py script.\r
+ """\r
+ if run_name is None:\r
+ run_name = "<run_path>"\r
+ importer = _get_importer(path_name)\r
+ if isinstance(importer, imp.NullImporter):\r
+ # Not a valid sys.path entry, so run the code directly\r
+ # execfile() doesn't help as we want to allow compiled files\r
+ code = _get_code_from_file(path_name)\r
+ return _run_module_code(code, init_globals, run_name, path_name)\r
+ else:\r
+ # Importer is defined for path, so add it to\r
+ # the start of sys.path\r
+ sys.path.insert(0, path_name)\r
+ try:\r
+ # Here's where things are a little different from the run_module\r
+ # case. There, we only had to replace the module in sys while the\r
+ # code was running and doing so was somewhat optional. Here, we\r
+ # have no choice and we have to remove it even while we read the\r
+ # code. If we don't do this, a __loader__ attribute in the\r
+ # existing __main__ module may prevent location of the new module.\r
+ main_name = "__main__"\r
+ saved_main = sys.modules[main_name]\r
+ del sys.modules[main_name]\r
+ try:\r
+ mod_name, loader, code, fname = _get_main_module_details()\r
+ finally:\r
+ sys.modules[main_name] = saved_main\r
+ pkg_name = ""\r
+ with _TempModule(run_name) as temp_module, \\r
+ _ModifiedArgv0(path_name):\r
+ mod_globals = temp_module.module.__dict__\r
+ return _run_code(code, mod_globals, init_globals,\r
+ run_name, fname, loader, pkg_name).copy()\r
+ finally:\r
+ try:\r
+ sys.path.remove(path_name)\r
+ except ValueError:\r
+ pass\r
+\r
+\r
+if __name__ == "__main__":\r
+ # Run the module specified as the next command line argument\r
+ if len(sys.argv) < 2:\r
+ print >> sys.stderr, "No module specified for execution"\r
+ else:\r
+ del sys.argv[0] # Make the requested module sys.argv[0]\r
+ _run_module_as_main(sys.argv[0])\r