blob: 7cb4668a01cf5e347c8850ae79ce42d4d99cc9df [file] [log] [blame]
Thomas Woutersa9773292006-04-21 09:43:23 +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 Coghlan260bd3e2009-11-16 06:49:25 +000014from pkgutil import read_code
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015try:
16 from imp import get_loader
17except ImportError:
18 from pkgutil import get_loader
Thomas Woutersa9773292006-04-21 09:43:23 +000019
20__all__ = [
Nick Coghlan260bd3e2009-11-16 06:49:25 +000021 "run_module", "run_path",
Thomas Woutersa9773292006-04-21 09:43:23 +000022]
23
Nick Coghlan260bd3e2009-11-16 06:49:25 +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
Thomas Woutersa9773292006-04-21 09:43:23 +000061
Thomas Woutersed03b412007-08-28 21:37:11 +000062def _run_code(code, run_globals, init_globals=None,
63 mod_name=None, mod_fname=None,
Christian Heimescbf3b5c2007-12-03 21:02:03 +000064 mod_loader=None, pkg_name=None):
Benjamin Petersonf6489f92009-11-25 17:46:26 +000065 """Helper to run code in nominated namespace"""
Thomas Woutersa9773292006-04-21 09:43:23 +000066 if init_globals is not None:
67 run_globals.update(init_globals)
68 run_globals.update(__name__ = mod_name,
69 __file__ = mod_fname,
Barry Warsaw28a691b2010-04-17 00:19:56 +000070 __cached__ = None,
Nick Coghlan761bb112012-07-14 23:59:22 +100071 __doc__ = None,
Christian Heimescbf3b5c2007-12-03 21:02:03 +000072 __loader__ = mod_loader,
73 __package__ = pkg_name)
Georg Brandl7cae87c2006-09-06 06:51:57 +000074 exec(code, run_globals)
Thomas Woutersa9773292006-04-21 09:43:23 +000075 return run_globals
76
77def _run_module_code(code, init_globals=None,
Thomas Woutersed03b412007-08-28 21:37:11 +000078 mod_name=None, mod_fname=None,
Christian Heimescbf3b5c2007-12-03 21:02:03 +000079 mod_loader=None, pkg_name=None):
Benjamin Petersonf6489f92009-11-25 17:46:26 +000080 """Helper to run code in new namespace with sys modified"""
Nick Coghlan260bd3e2009-11-16 06:49:25 +000081 with _TempModule(mod_name) as temp_module, _ModifiedArgv0(mod_fname):
82 mod_globals = temp_module.module.__dict__
Thomas Woutersed03b412007-08-28 21:37:11 +000083 _run_code(code, mod_globals, init_globals,
Nick Coghlan260bd3e2009-11-16 06:49:25 +000084 mod_name, mod_fname, mod_loader, pkg_name)
Thomas Woutersed03b412007-08-28 21:37:11 +000085 # Copy the globals of the temporary module, as they
86 # may be cleared when the temporary module goes away
87 return mod_globals.copy()
Thomas Woutersa9773292006-04-21 09:43:23 +000088
89
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000090# This helper is needed due to a missing component in the PEP 302
91# loader protocol (specifically, "get_filename" is non-standard)
Nick Coghlanf088e5e2008-12-14 11:50:48 +000092# Since we can't introduce new features in maintenance releases,
93# support was added to zipimporter under the name '_get_filename'
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094def _get_filename(loader, mod_name):
Nick Coghlanf088e5e2008-12-14 11:50:48 +000095 for attr in ("get_filename", "_get_filename"):
96 meth = getattr(loader, attr, None)
97 if meth is not None:
98 return meth(mod_name)
99 return None
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000100
Thomas Woutersed03b412007-08-28 21:37:11 +0000101# Helper to get the loader, code and filename for a module
102def _get_module_details(mod_name):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000103 loader = get_loader(mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000104 if loader is None:
Guido van Rossum806c2462007-08-06 23:33:07 +0000105 raise ImportError("No module named %s" % mod_name)
106 if loader.is_package(mod_name):
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000107 if mod_name == "__main__" or mod_name.endswith(".__main__"):
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000108 raise ImportError("Cannot use package as __main__ module")
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000109 try:
110 pkg_main_name = mod_name + ".__main__"
111 return _get_module_details(pkg_main_name)
112 except ImportError as e:
113 raise ImportError(("%s; %r is a package and cannot " +
114 "be directly executed") %(e, mod_name))
Thomas Woutersa9773292006-04-21 09:43:23 +0000115 code = loader.get_code(mod_name)
116 if code is None:
Guido van Rossum806c2462007-08-06 23:33:07 +0000117 raise ImportError("No code object available for %s" % mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000118 filename = _get_filename(loader, mod_name)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000119 return mod_name, loader, code, filename
Thomas Woutersed03b412007-08-28 21:37:11 +0000120
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000121# XXX ncoghlan: Should this be documented and made public?
122# (Current thoughts: don't repeat the mistake that lead to its
123# creation when run_module() no longer met the needs of
124# mainmodule.c, but couldn't be changed because it was public)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000125def _run_module_as_main(mod_name, alter_argv=True):
Thomas Woutersed03b412007-08-28 21:37:11 +0000126 """Runs the designated module in the __main__ namespace
127
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000128 Note that the executed module will have full access to the
129 __main__ namespace. If this is not desirable, the run_module()
R. David Murray445448c2009-12-20 17:28:31 +0000130 function should be used to run the module code in a fresh namespace.
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000131
132 At the very least, these variables in __main__ will be overwritten:
133 __name__
Thomas Woutersed03b412007-08-28 21:37:11 +0000134 __file__
Barry Warsaw28a691b2010-04-17 00:19:56 +0000135 __cached__
Thomas Woutersed03b412007-08-28 21:37:11 +0000136 __loader__
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000137 __package__
Thomas Woutersed03b412007-08-28 21:37:11 +0000138 """
Christian Heimesc3f30c42008-02-22 16:37:40 +0000139 try:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000140 if alter_argv or mod_name != "__main__": # i.e. -m switch
141 mod_name, loader, code, fname = _get_module_details(mod_name)
142 else: # i.e. directory or zipfile execution
143 mod_name, loader, code, fname = _get_main_module_details()
Christian Heimesc3f30c42008-02-22 16:37:40 +0000144 except ImportError as exc:
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000145 # Try to provide a good error message
146 # for directories, zip files and the -m switch
147 if alter_argv:
148 # For -m switch, just display the exception
149 info = str(exc)
150 else:
151 # For directories/zipfiles, let the user
152 # know what the code was looking for
Benjamin Petersone3607952009-11-25 18:38:11 +0000153 info = "can't find '__main__' module in %r" % sys.argv[0]
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000154 msg = "%s: %s" % (sys.executable, info)
Christian Heimesc3f30c42008-02-22 16:37:40 +0000155 sys.exit(msg)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000156 pkg_name = mod_name.rpartition('.')[0]
Thomas Woutersed03b412007-08-28 21:37:11 +0000157 main_globals = sys.modules["__main__"].__dict__
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000158 if alter_argv:
Thomas Woutersed03b412007-08-28 21:37:11 +0000159 sys.argv[0] = fname
160 return _run_code(code, main_globals, None,
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000161 "__main__", fname, loader, pkg_name)
Thomas Woutersed03b412007-08-28 21:37:11 +0000162
163def run_module(mod_name, init_globals=None,
164 run_name=None, alter_sys=False):
165 """Execute a module's code without importing it
166
167 Returns the resulting top level namespace dictionary
168 """
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000169 mod_name, loader, code, fname = _get_module_details(mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000170 if run_name is None:
171 run_name = mod_name
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000172 pkg_name = mod_name.rpartition('.')[0]
Thomas Woutersed03b412007-08-28 21:37:11 +0000173 if alter_sys:
174 return _run_module_code(code, init_globals, run_name,
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000175 fname, loader, pkg_name)
Thomas Woutersed03b412007-08-28 21:37:11 +0000176 else:
177 # Leave the sys module alone
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000178 return _run_code(code, {}, init_globals, run_name,
179 fname, loader, pkg_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000180
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000181def _get_main_module_details():
182 # Helper that gives a nicer error message when attempting to
183 # execute a zipfile or directory by invoking __main__.py
184 main_name = "__main__"
185 try:
186 return _get_module_details(main_name)
187 except ImportError as exc:
188 if main_name in str(exc):
189 raise ImportError("can't find %r module in %r" %
190 (main_name, sys.path[0]))
191 raise
192
Thomas Woutersa9773292006-04-21 09:43:23 +0000193
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000194# XXX (ncoghlan): Perhaps expose the C API function
195# as imp.get_importer instead of reimplementing it in Python?
196def _get_importer(path_name):
197 """Python version of PyImport_GetImporter C API function"""
198 cache = sys.path_importer_cache
199 try:
200 importer = cache[path_name]
201 except KeyError:
202 # Not yet cached. Flag as using the
203 # standard machinery until we finish
204 # checking the hooks
205 cache[path_name] = None
206 for hook in sys.path_hooks:
207 try:
208 importer = hook(path_name)
209 break
210 except ImportError:
211 pass
212 else:
213 # The following check looks a bit odd. The trick is that
214 # NullImporter throws ImportError if the supplied path is a
215 # *valid* directory entry (and hence able to be handled
216 # by the standard import machinery)
217 try:
218 importer = imp.NullImporter(path_name)
219 except ImportError:
220 return None
221 cache[path_name] = importer
222 return importer
223
224def _get_code_from_file(fname):
225 # Check for a compiled file first
226 with open(fname, "rb") as f:
227 code = read_code(f)
228 if code is None:
229 # That didn't work, so try it as normal source code
Victor Stinner6c471022011-07-04 01:45:39 +0200230 with open(fname, "rb") as f:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000231 code = compile(f.read(), fname, 'exec')
232 return code
233
234def run_path(path_name, init_globals=None, run_name=None):
235 """Execute code located at the specified filesystem location
236
237 Returns the resulting top level namespace dictionary
238
239 The file path may refer directly to a Python script (i.e.
240 one that could be directly executed with execfile) or else
241 it may refer to a zipfile or directory containing a top
242 level __main__.py script.
243 """
244 if run_name is None:
245 run_name = "<run_path>"
Nick Coghlan761bb112012-07-14 23:59:22 +1000246 pkg_name = run_name.rpartition(".")[0]
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000247 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)
Nick Coghlan761bb112012-07-14 23:59:22 +1000252 return _run_module_code(code, init_globals, run_name, path_name,
253 pkg_name=pkg_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000254 else:
255 # Importer is defined for path, so add it to
256 # the start of sys.path
257 sys.path.insert(0, path_name)
258 try:
259 # Here's where things are a little different from the run_module
260 # case. There, we only had to replace the module in sys while the
261 # code was running and doing so was somewhat optional. Here, we
262 # have no choice and we have to remove it even while we read the
263 # code. If we don't do this, a __loader__ attribute in the
264 # existing __main__ module may prevent location of the new module.
265 main_name = "__main__"
266 saved_main = sys.modules[main_name]
267 del sys.modules[main_name]
268 try:
269 mod_name, loader, code, fname = _get_main_module_details()
270 finally:
271 sys.modules[main_name] = saved_main
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000272 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 Peterson01e39792010-10-13 01:04:36 +0000276 run_name, fname, loader, pkg_name).copy()
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000277 finally:
278 try:
279 sys.path.remove(path_name)
280 except ValueError:
281 pass
282
283
Thomas Woutersa9773292006-04-21 09:43:23 +0000284if __name__ == "__main__":
285 # Run the module specified as the next command line argument
286 if len(sys.argv) < 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000287 print("No module specified for execution", file=sys.stderr)
Thomas Woutersa9773292006-04-21 09:43:23 +0000288 else:
289 del sys.argv[0] # Make the requested module sys.argv[0]
Thomas Woutersed03b412007-08-28 21:37:11 +0000290 _run_module_as_main(sys.argv[0])