blob: 8096aac40c23e6651f674481957b766793d06123 [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
Phillip J. Ebyab1d2452006-04-17 20:17:25 +000014try:
15 from imp import get_loader
16except ImportError:
17 from pkgutil import get_loader
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000018
19__all__ = [
20 "run_module",
21]
22
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000023
Nick Coghlan56829d52006-07-06 12:53:04 +000024def _run_code(code, run_globals, init_globals,
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000025 mod_name, mod_fname, mod_loader):
26 """Helper for _run_module_code"""
27 if init_globals is not None:
28 run_globals.update(init_globals)
Nick Coghlan56829d52006-07-06 12:53:04 +000029 run_globals.update(__name__ = mod_name,
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000030 __file__ = mod_fname,
31 __loader__ = mod_loader)
32 exec code in run_globals
33 return run_globals
34
Nick Coghlan56829d52006-07-06 12:53:04 +000035def _run_module_code(code, init_globals=None,
Nick Coghlan13c25c02007-07-24 13:58:28 +000036 mod_name=None, mod_fname=None,
37 mod_loader=None, alter_sys=False):
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000038 """Helper for run_module"""
39 # Set up the top level namespace dictionary
40 if alter_sys:
Nick Coghlan13c25c02007-07-24 13:58:28 +000041 # Modify sys.argv[0] and sys.modules[mod_name]
Nick Coghlanc841bb62006-03-24 13:05:53 +000042 sys.argv[0] = mod_fname
Nick Coghlan13c25c02007-07-24 13:58:28 +000043 module = imp.new_module(mod_name)
44 sys.modules[mod_name] = module
45 mod_globals = module.__dict__
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000046 else:
47 # Leave the sys module alone
Nick Coghlan13c25c02007-07-24 13:58:28 +000048 mod_globals = {}
49 return _run_code(code, mod_globals, init_globals,
50 mod_name, mod_fname, mod_loader)
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000051
52
Phillip J. Ebyab1d2452006-04-17 20:17:25 +000053# This helper is needed due to a missing component in the PEP 302
54# loader protocol (specifically, "get_filename" is non-standard)
55def _get_filename(loader, mod_name):
56 try:
57 get_filename = loader.get_filename
58 except AttributeError:
59 return None
60 else:
61 return get_filename(mod_name)
62
63
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000064def run_module(mod_name, init_globals=None,
65 run_name=None, alter_sys=False):
66 """Execute a module's code without importing it
Tim Petersf99b8162006-03-15 18:08:37 +000067
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000068 Returns the resulting top level namespace dictionary
69 """
Phillip J. Ebyab1d2452006-04-17 20:17:25 +000070 loader = get_loader(mod_name)
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000071 if loader is None:
Nick Coghlanae21fc62007-07-23 13:41:45 +000072 raise ImportError("No module named %s" % mod_name)
73 if loader.is_package(mod_name):
74 raise ImportError(("%s is a package and cannot " +
75 "be directly executed") % mod_name)
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000076 code = loader.get_code(mod_name)
77 if code is None:
Nick Coghlanae21fc62007-07-23 13:41:45 +000078 raise ImportError("No code object available for %s" % mod_name)
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000079 filename = _get_filename(loader, mod_name)
80 if run_name is None:
81 run_name = mod_name
Tim Petersf99b8162006-03-15 18:08:37 +000082 return _run_module_code(code, init_globals, run_name,
Nick Coghlan56829d52006-07-06 12:53:04 +000083 filename, loader, alter_sys)
Nick Coghlane2ebb2d2006-03-15 11:00:26 +000084
85
86if __name__ == "__main__":
87 # Run the module specified as the next command line argument
88 if len(sys.argv) < 2:
89 print >> sys.stderr, "No module specified for execution"
90 else:
91 del sys.argv[0] # Make the requested module sys.argv[0]
92 run_module(sys.argv[0], run_name="__main__", alter_sys=True)