Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1 | """runpy.py - locating and running Python code using the module namespace |
| 2 | |
| 3 | Provides support for locating and running Python scripts using the Python |
| 4 | module namespace instead of the native filesystem. |
| 5 | |
| 6 | This allows Python code to play nicely with non-filesystem based PEP 302 |
| 7 | importers 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 | |
| 12 | import sys |
| 13 | import imp |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14 | try: |
| 15 | from imp import get_loader |
| 16 | except ImportError: |
| 17 | from pkgutil import get_loader |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 18 | |
| 19 | __all__ = [ |
| 20 | "run_module", |
| 21 | ] |
| 22 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 23 | |
| 24 | def _run_code(code, run_globals, init_globals, |
| 25 | 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) |
| 29 | run_globals.update(__name__ = mod_name, |
| 30 | __file__ = mod_fname, |
| 31 | __loader__ = mod_loader) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 32 | exec(code, run_globals) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 33 | return run_globals |
| 34 | |
| 35 | def _run_module_code(code, init_globals=None, |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 36 | mod_name=None, mod_fname=None, |
| 37 | mod_loader=None, alter_sys=False): |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 38 | """Helper for run_module""" |
| 39 | # Set up the top level namespace dictionary |
| 40 | if alter_sys: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 41 | # Modify sys.argv[0] and sys.modules[mod_name] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 42 | sys.argv[0] = mod_fname |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 43 | module = imp.new_module(mod_name) |
| 44 | sys.modules[mod_name] = module |
| 45 | mod_globals = module.__dict__ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 46 | else: |
| 47 | # Leave the sys module alone |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 48 | mod_globals = {} |
| 49 | return _run_code(code, mod_globals, init_globals, |
| 50 | mod_name, mod_fname, mod_loader) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 51 | |
| 52 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 53 | # This helper is needed due to a missing component in the PEP 302 |
| 54 | # loader protocol (specifically, "get_filename" is non-standard) |
| 55 | def _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 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 64 | def run_module(mod_name, init_globals=None, |
| 65 | run_name=None, alter_sys=False): |
| 66 | """Execute a module's code without importing it |
| 67 | |
| 68 | Returns the resulting top level namespace dictionary |
| 69 | """ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 70 | loader = get_loader(mod_name) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 71 | if loader is None: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 72 | 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) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 76 | code = loader.get_code(mod_name) |
| 77 | if code is None: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 78 | raise ImportError("No code object available for %s" % mod_name) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 79 | filename = _get_filename(loader, mod_name) |
| 80 | if run_name is None: |
| 81 | run_name = mod_name |
| 82 | return _run_module_code(code, init_globals, run_name, |
| 83 | filename, loader, alter_sys) |
| 84 | |
| 85 | |
| 86 | if __name__ == "__main__": |
| 87 | # Run the module specified as the next command line argument |
| 88 | if len(sys.argv) < 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 89 | print("No module specified for execution", file=sys.stderr) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 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) |