blob: ad4d077a45fa4775b8994c0fcc5a9878e37da0eb [file] [log] [blame]
Nick Coghlane2ebb2d2006-03-15 11:00:26 +00001"""runpy.py - locating and running Python code using the module namespace
2
3Provides support for locating and running Python scripts using the Python
4module namespace instead of the native filesystem.
5
6This allows Python code to play nicely with non-filesystem based PEP 302
7importers when locating support scripts as well as when importing modules.
8"""
9# Written by Nick Coghlan <ncoghlan at gmail.com>
10# to implement PEP 338 (Executing Modules as Scripts)
11
12import sys
13import imp
Nick Coghlan49868cb2009-11-15 07:30:34 +000014from pkgutil import read_code
Phillip J. Ebyab1d2452006-04-17 20:17:25 +000015try:
16 from imp import get_loader
17except ImportError:
18 from pkgutil import get_loader
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000019
20__all__ = [
Nick Coghlan49868cb2009-11-15 07:30:34 +000021 "run_module", "run_path",
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000022]
23
Nick Coghlan49868cb2009-11-15 07:30:34 +000024class _TempModule(object):
25 """Temporarily replace a module in sys.modules with an empty namespace"""
26 def __init__(self, mod_name):
27 self.mod_name = mod_name
28 self.module = imp.new_module(mod_name)
29 self._saved_module = []
30
31 def __enter__(self):
32 mod_name = self.mod_name
33 try:
34 self._saved_module.append(sys.modules[mod_name])
35 except KeyError:
36 pass
37 sys.modules[mod_name] = self.module
38 return self
39
40 def __exit__(self, *args):
41 if self._saved_module:
42 sys.modules[self.mod_name] = self._saved_module[0]
43 else:
44 del sys.modules[self.mod_name]
45 self._saved_module = []
46
47class _ModifiedArgv0(object):
48 def __init__(self, value):
49 self.value = value
50 self._saved_value = self._sentinel = object()
51
52 def __enter__(self):
53 if self._saved_value is not self._sentinel:
54 raise RuntimeError("Already preserving saved value")
55 self._saved_value = sys.argv[0]
56 sys.argv[0] = self.value
57
58 def __exit__(self, *args):
59 self.value = self._sentinel
60 sys.argv[0] = self._saved_value
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000061
Nick Coghlan1a42ece2007-08-25 10:50:41 +000062def _run_code(code, run_globals, init_globals=None,
63 mod_name=None, mod_fname=None,
Nick Coghlanef01d822007-12-03 12:55:17 +000064 mod_loader=None, pkg_name=None):
Nick Coghlane471b9b2009-11-07 08:15:01 +000065 """Helper to run code in nominated namespace"""
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000066 if init_globals is not None:
67 run_globals.update(init_globals)
Nick Coghlan56829d52006-07-06 12:53:04 +000068 run_globals.update(__name__ = mod_name,
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000069 __file__ = mod_fname,
Nick Coghlanef01d822007-12-03 12:55:17 +000070 __loader__ = mod_loader,
71 __package__ = pkg_name)
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000072 exec code in run_globals
73 return run_globals
74
Nick Coghlan56829d52006-07-06 12:53:04 +000075def _run_module_code(code, init_globals=None,
Nick Coghlan3af0e782007-08-25 04:32:07 +000076 mod_name=None, mod_fname=None,
Nick Coghlanef01d822007-12-03 12:55:17 +000077 mod_loader=None, pkg_name=None):
Nick Coghlane471b9b2009-11-07 08:15:01 +000078 """Helper to run code in new namespace with sys modified"""
Nick Coghlan49868cb2009-11-15 07:30:34 +000079 with _TempModule(mod_name) as temp_module, _ModifiedArgv0(mod_fname):
80 mod_globals = temp_module.module.__dict__
Nick Coghlan1a42ece2007-08-25 10:50:41 +000081 _run_code(code, mod_globals, init_globals,
Nick Coghlan49868cb2009-11-15 07:30:34 +000082 mod_name, mod_fname, mod_loader, pkg_name)
Nick Coghlan1a42ece2007-08-25 10:50:41 +000083 # Copy the globals of the temporary module, as they
84 # may be cleared when the temporary module goes away
85 return mod_globals.copy()
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000086
87
Phillip J. Ebyab1d2452006-04-17 20:17:25 +000088# This helper is needed due to a missing component in the PEP 302
89# loader protocol (specifically, "get_filename" is non-standard)
Nick Coghlana2053472008-12-14 10:54:50 +000090# Since we can't introduce new features in maintenance releases,
91# support was added to zipimporter under the name '_get_filename'
Phillip J. Ebyab1d2452006-04-17 20:17:25 +000092def _get_filename(loader, mod_name):
Nick Coghlana2053472008-12-14 10:54:50 +000093 for attr in ("get_filename", "_get_filename"):
94 meth = getattr(loader, attr, None)
95 if meth is not None:
96 return meth(mod_name)
97 return None
Phillip J. Ebyab1d2452006-04-17 20:17:25 +000098
Nick Coghlan1a42ece2007-08-25 10:50:41 +000099# Helper to get the loader, code and filename for a module
Martin Panter7e59ce82015-12-03 01:23:10 +0000100def _get_module_details(mod_name, error=ImportError):
101 try:
102 loader = get_loader(mod_name)
103 if loader is None:
104 raise error("No module named %s" % mod_name)
105 ispkg = loader.is_package(mod_name)
106 except ImportError as e:
107 raise error(format(e))
108 if ispkg:
Nick Coghland39600e2009-02-08 01:26:34 +0000109 if mod_name == "__main__" or mod_name.endswith(".__main__"):
Martin Panter7e59ce82015-12-03 01:23:10 +0000110 raise error("Cannot use package as __main__ module")
111 __import__(mod_name) # Do not catch exceptions initializing package
Nick Coghland39600e2009-02-08 01:26:34 +0000112 try:
113 pkg_main_name = mod_name + ".__main__"
114 return _get_module_details(pkg_main_name)
115 except ImportError, e:
Martin Panter7e59ce82015-12-03 01:23:10 +0000116 raise error(("%s; %r is a package and cannot " +
Nick Coghland39600e2009-02-08 01:26:34 +0000117 "be directly executed") %(e, mod_name))
Martin Panter7e59ce82015-12-03 01:23:10 +0000118 try:
119 code = loader.get_code(mod_name)
120 except ImportError as e:
121 raise error(format(e))
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000122 if code is None:
Martin Panter7e59ce82015-12-03 01:23:10 +0000123 raise error("No code object available for %s" % mod_name)
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000124 filename = _get_filename(loader, mod_name)
Nick Coghland39600e2009-02-08 01:26:34 +0000125 return mod_name, loader, code, filename
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000126
127
Martin Panter7e59ce82015-12-03 01:23:10 +0000128def _get_main_module_details(error=ImportError):
Nick Coghlan49868cb2009-11-15 07:30:34 +0000129 # Helper that gives a nicer error message when attempting to
130 # execute a zipfile or directory by invoking __main__.py
131 main_name = "__main__"
132 try:
133 return _get_module_details(main_name)
134 except ImportError as exc:
135 if main_name in str(exc):
Martin Panter7e59ce82015-12-03 01:23:10 +0000136 raise error("can't find %r module in %r" %
Nick Coghlan49868cb2009-11-15 07:30:34 +0000137 (main_name, sys.path[0]))
138 raise
139
Martin Panter7e59ce82015-12-03 01:23:10 +0000140class _Error(Exception):
141 """Error that _run_module_as_main() should report without a traceback"""
142
Nick Coghlan49868cb2009-11-15 07:30:34 +0000143# This function is the actual implementation of the -m switch and direct
144# execution of zipfiles and directories and is deliberately kept private.
145# This avoids a repeat of the situation where run_module() no longer met the
146# needs of mainmodule.c, but couldn't be changed because it was public
Nick Coghlane471b9b2009-11-07 08:15:01 +0000147def _run_module_as_main(mod_name, alter_argv=True):
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000148 """Runs the designated module in the __main__ namespace
149
Nick Coghlane471b9b2009-11-07 08:15:01 +0000150 Note that the executed module will have full access to the
151 __main__ namespace. If this is not desirable, the run_module()
R. David Murray97138112009-12-20 16:24:46 +0000152 function should be used to run the module code in a fresh namespace.
Nick Coghlane471b9b2009-11-07 08:15:01 +0000153
154 At the very least, these variables in __main__ will be overwritten:
155 __name__
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000156 __file__
157 __loader__
Nick Coghlane471b9b2009-11-07 08:15:01 +0000158 __package__
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000159 """
Nick Coghlana14a4e82008-02-22 10:54:06 +0000160 try:
Nick Coghlan49868cb2009-11-15 07:30:34 +0000161 if alter_argv or mod_name != "__main__": # i.e. -m switch
Martin Panter7e59ce82015-12-03 01:23:10 +0000162 mod_name, loader, code, fname = _get_module_details(
163 mod_name, _Error)
Nick Coghlan49868cb2009-11-15 07:30:34 +0000164 else: # i.e. directory or zipfile execution
Martin Panter7e59ce82015-12-03 01:23:10 +0000165 mod_name, loader, code, fname = _get_main_module_details(_Error)
166 except _Error as exc:
167 msg = "%s: %s" % (sys.executable, exc)
Nick Coghlana14a4e82008-02-22 10:54:06 +0000168 sys.exit(msg)
Nick Coghlanef01d822007-12-03 12:55:17 +0000169 pkg_name = mod_name.rpartition('.')[0]
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000170 main_globals = sys.modules["__main__"].__dict__
Nick Coghlane471b9b2009-11-07 08:15:01 +0000171 if alter_argv:
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000172 sys.argv[0] = fname
173 return _run_code(code, main_globals, None,
Nick Coghlanef01d822007-12-03 12:55:17 +0000174 "__main__", fname, loader, pkg_name)
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000175
176def run_module(mod_name, init_globals=None,
177 run_name=None, alter_sys=False):
178 """Execute a module's code without importing it
179
180 Returns the resulting top level namespace dictionary
181 """
Nick Coghland39600e2009-02-08 01:26:34 +0000182 mod_name, loader, code, fname = _get_module_details(mod_name)
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000183 if run_name is None:
184 run_name = mod_name
Nick Coghlanef01d822007-12-03 12:55:17 +0000185 pkg_name = mod_name.rpartition('.')[0]
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000186 if alter_sys:
187 return _run_module_code(code, init_globals, run_name,
Nick Coghlanef01d822007-12-03 12:55:17 +0000188 fname, loader, pkg_name)
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000189 else:
190 # Leave the sys module alone
Nick Coghlanef01d822007-12-03 12:55:17 +0000191 return _run_code(code, {}, init_globals, run_name,
192 fname, loader, pkg_name)
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000193
194
Nick Coghlan49868cb2009-11-15 07:30:34 +0000195# XXX (ncoghlan): Perhaps expose the C API function
196# as imp.get_importer instead of reimplementing it in Python?
197def _get_importer(path_name):
198 """Python version of PyImport_GetImporter C API function"""
199 cache = sys.path_importer_cache
200 try:
201 importer = cache[path_name]
202 except KeyError:
203 # Not yet cached. Flag as using the
204 # standard machinery until we finish
205 # checking the hooks
206 cache[path_name] = None
207 for hook in sys.path_hooks:
208 try:
209 importer = hook(path_name)
210 break
211 except ImportError:
212 pass
213 else:
214 # The following check looks a bit odd. The trick is that
Andrew Svetlov4bb142b2012-12-18 21:27:37 +0200215 # NullImporter raises ImportError if the supplied path is a
Nick Coghlan49868cb2009-11-15 07:30:34 +0000216 # *valid* directory entry (and hence able to be handled
217 # by the standard import machinery)
218 try:
219 importer = imp.NullImporter(path_name)
220 except ImportError:
221 return None
222 cache[path_name] = importer
223 return importer
224
225def _get_code_from_file(fname):
226 # Check for a compiled file first
227 with open(fname, "rb") as f:
228 code = read_code(f)
229 if code is None:
230 # That didn't work, so try it as normal source code
231 with open(fname, "rU") as f:
232 code = compile(f.read(), fname, 'exec')
233 return code
234
235def run_path(path_name, init_globals=None, run_name=None):
236 """Execute code located at the specified filesystem location
237
238 Returns the resulting top level namespace dictionary
239
240 The file path may refer directly to a Python script (i.e.
241 one that could be directly executed with execfile) or else
242 it may refer to a zipfile or directory containing a top
243 level __main__.py script.
244 """
245 if run_name is None:
246 run_name = "<run_path>"
247 importer = _get_importer(path_name)
248 if isinstance(importer, imp.NullImporter):
249 # Not a valid sys.path entry, so run the code directly
250 # execfile() doesn't help as we want to allow compiled files
251 code = _get_code_from_file(path_name)
252 return _run_module_code(code, init_globals, run_name, path_name)
253 else:
254 # Importer is defined for path, so add it to
255 # the start of sys.path
256 sys.path.insert(0, path_name)
257 try:
258 # Here's where things are a little different from the run_module
259 # case. There, we only had to replace the module in sys while the
260 # code was running and doing so was somewhat optional. Here, we
261 # have no choice and we have to remove it even while we read the
262 # code. If we don't do this, a __loader__ attribute in the
263 # existing __main__ module may prevent location of the new module.
264 main_name = "__main__"
265 saved_main = sys.modules[main_name]
266 del sys.modules[main_name]
267 try:
268 mod_name, loader, code, fname = _get_main_module_details()
269 finally:
270 sys.modules[main_name] = saved_main
271 pkg_name = ""
272 with _TempModule(run_name) as temp_module, \
273 _ModifiedArgv0(path_name):
274 mod_globals = temp_module.module.__dict__
275 return _run_code(code, mod_globals, init_globals,
Benjamin Peterson25def7d2010-10-13 01:10:16 +0000276 run_name, fname, loader, pkg_name).copy()
Nick Coghlan49868cb2009-11-15 07:30:34 +0000277 finally:
278 try:
279 sys.path.remove(path_name)
280 except ValueError:
281 pass
282
283
Nick Coghlane2ebb2d2006-03-15 11:00:26 +0000284if __name__ == "__main__":
285 # Run the module specified as the next command line argument
286 if len(sys.argv) < 2:
287 print >> sys.stderr, "No module specified for execution"
288 else:
289 del sys.argv[0] # Make the requested module sys.argv[0]
Nick Coghlan1a42ece2007-08-25 10:50:41 +0000290 _run_module_as_main(sys.argv[0])