diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2009-02-08 01:58:26 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2009-02-08 01:58:26 (GMT) |
commit | 3f48ae35c710c5e5101d5de6721b1e2ccb56df68 (patch) | |
tree | 971feab37df37b7ab70429c75069d36b2c8b9557 /Lib/runpy.py | |
parent | f72d9fb02f2ff536c64f1e65780b4a414eb3bb4d (diff) | |
download | cpython-3f48ae35c710c5e5101d5de6721b1e2ccb56df68.zip cpython-3f48ae35c710c5e5101d5de6721b1e2ccb56df68.tar.gz cpython-3f48ae35c710c5e5101d5de6721b1e2ccb56df68.tar.bz2 |
Merged revisions 69419-69420 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r69419 | nick.coghlan | 2009-02-08 11:26:34 +1000 (Sun, 08 Feb 2009) | 1 line
Issue 4195: Restore the ability to execute packages with the -m switch (but this time in a way that leaves the import machinery in a valid state). (Original patch by Andi Vajda)
........
r69420 | nick.coghlan | 2009-02-08 11:46:01 +1000 (Sun, 08 Feb 2009) | 1 line
Mention patch submitter in NEWS entry for r69419
........
Diffstat (limited to 'Lib/runpy.py')
-rwxr-xr-x | Lib/runpy.py | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/Lib/runpy.py b/Lib/runpy.py index 22a2989..e0aabeb 100755 --- a/Lib/runpy.py +++ b/Lib/runpy.py @@ -80,13 +80,19 @@ def _get_module_details(mod_name): if loader is None: raise ImportError("No module named %s" % mod_name) if loader.is_package(mod_name): - raise ImportError(("%s is a package and cannot " + - "be directly executed") % mod_name) + if mod_name == "__main__" or mod_name.endswith(".__main__"): + raise ImportError(("Cannot use package as __main__ module")) + try: + pkg_main_name = mod_name + ".__main__" + return _get_module_details(pkg_main_name) + except ImportError as e: + raise ImportError(("%s; %r is a package and cannot " + + "be directly executed") %(e, mod_name)) code = loader.get_code(mod_name) if code is None: raise ImportError("No code object available for %s" % mod_name) filename = _get_filename(loader, mod_name) - return loader, code, filename + return mod_name, loader, code, filename # XXX ncoghlan: Should this be documented and made public? @@ -101,12 +107,12 @@ def _run_module_as_main(mod_name, set_argv0=True): __loader__ """ try: - loader, code, fname = _get_module_details(mod_name) + mod_name, loader, code, fname = _get_module_details(mod_name) except ImportError as exc: # Try to provide a good error message # for directories, zip files and the -m switch if set_argv0: - # For -m switch, just disply the exception + # For -m switch, just display the exception info = str(exc) else: # For directories/zipfiles, let the user @@ -127,7 +133,7 @@ def run_module(mod_name, init_globals=None, Returns the resulting top level namespace dictionary """ - loader, code, fname = _get_module_details(mod_name) + mod_name, loader, code, fname = _get_module_details(mod_name) if run_name is None: run_name = mod_name pkg_name = mod_name.rpartition('.')[0] |