mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
Merged revisions 75264,75268,75293,75318,75391-75392,75436,75478,75971,76003,76058,76140-76141,76231,76380,76428-76429 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r75264 | andrew.kuchling | 2009-10-05 17:30:22 -0500 (Mon, 05 Oct 2009) | 1 line Add various items ........ r75268 | andrew.kuchling | 2009-10-05 17:45:39 -0500 (Mon, 05 Oct 2009) | 1 line Remove two notes ........ r75293 | kristjan.jonsson | 2009-10-09 09:32:19 -0500 (Fri, 09 Oct 2009) | 2 lines http://bugs.python.org/issue7029 a non-default timer wasn't actually used by the individual Tests. ........ r75318 | benjamin.peterson | 2009-10-10 16:15:58 -0500 (Sat, 10 Oct 2009) | 1 line remove script which uses long gone module ........ r75391 | andrew.kuchling | 2009-10-13 10:49:33 -0500 (Tue, 13 Oct 2009) | 1 line Link to PEP ........ r75392 | andrew.kuchling | 2009-10-13 11:11:49 -0500 (Tue, 13 Oct 2009) | 1 line Various link, textual, and markup fixes ........ r75436 | benjamin.peterson | 2009-10-15 10:39:15 -0500 (Thu, 15 Oct 2009) | 1 line don't need to mess up sys.path ........ r75478 | senthil.kumaran | 2009-10-17 20:58:45 -0500 (Sat, 17 Oct 2009) | 3 lines Fix a typo. ........ r75971 | benjamin.peterson | 2009-10-30 22:56:15 -0500 (Fri, 30 Oct 2009) | 1 line add some checks for evaluation order with parenthesis #7210 ........ r76003 | antoine.pitrou | 2009-10-31 19:30:13 -0500 (Sat, 31 Oct 2009) | 6 lines Hopefully fix the buildbot problems on test_mailbox, by computing the maildir toc cache refresh date before actually refreshing the cache. (see #6896) ........ r76058 | benjamin.peterson | 2009-11-02 10:14:19 -0600 (Mon, 02 Nov 2009) | 1 line grant list.index() a more informative error message #7252 ........ r76140 | nick.coghlan | 2009-11-07 02:13:55 -0600 (Sat, 07 Nov 2009) | 1 line Add test for runpy.run_module package execution and use something other than logging as the example of a non-executable package ........ r76141 | nick.coghlan | 2009-11-07 02:15:01 -0600 (Sat, 07 Nov 2009) | 1 line Some minor cleanups to private runpy code and docstrings ........ r76231 | benjamin.peterson | 2009-11-12 17:42:23 -0600 (Thu, 12 Nov 2009) | 1 line this main is much more useful ........ r76380 | antoine.pitrou | 2009-11-18 14:20:46 -0600 (Wed, 18 Nov 2009) | 3 lines Mention Giampolo R's new FTP TLS support in the what's new file ........ r76428 | benjamin.peterson | 2009-11-19 20:15:50 -0600 (Thu, 19 Nov 2009) | 1 line turn goto into do while loop ........ r76429 | benjamin.peterson | 2009-11-19 20:56:43 -0600 (Thu, 19 Nov 2009) | 2 lines avoid doing an uneeded import in a function ........
This commit is contained in:
parent
5554993d37
commit
f6489f941c
12 changed files with 693 additions and 257 deletions
58
Lib/runpy.py
58
Lib/runpy.py
|
@ -62,7 +62,7 @@ class _ModifiedArgv0(object):
|
|||
def _run_code(code, run_globals, init_globals=None,
|
||||
mod_name=None, mod_fname=None,
|
||||
mod_loader=None, pkg_name=None):
|
||||
"""Helper for _run_module_code"""
|
||||
"""Helper to run code in nominated namespace"""
|
||||
if init_globals is not None:
|
||||
run_globals.update(init_globals)
|
||||
run_globals.update(__name__ = mod_name,
|
||||
|
@ -75,7 +75,7 @@ def _run_code(code, run_globals, init_globals=None,
|
|||
def _run_module_code(code, init_globals=None,
|
||||
mod_name=None, mod_fname=None,
|
||||
mod_loader=None, pkg_name=None):
|
||||
"""Helper for run_module"""
|
||||
"""Helper to run code in new namespace with sys modified"""
|
||||
with _TempModule(mod_name) as temp_module, _ModifiedArgv0(mod_fname):
|
||||
mod_globals = temp_module.module.__dict__
|
||||
_run_code(code, mod_globals, init_globals,
|
||||
|
@ -103,7 +103,7 @@ def _get_module_details(mod_name):
|
|||
raise ImportError("No module named %s" % mod_name)
|
||||
if loader.is_package(mod_name):
|
||||
if mod_name == "__main__" or mod_name.endswith(".__main__"):
|
||||
raise ImportError(("Cannot use package as __main__ module"))
|
||||
raise ImportError("Cannot use package as __main__ module")
|
||||
try:
|
||||
pkg_main_name = mod_name + ".__main__"
|
||||
return _get_module_details(pkg_main_name)
|
||||
|
@ -116,29 +116,22 @@ def _get_module_details(mod_name):
|
|||
filename = _get_filename(loader, mod_name)
|
||||
return mod_name, loader, code, filename
|
||||
|
||||
|
||||
def _get_main_module_details():
|
||||
# Helper that gives a nicer error message when attempting to
|
||||
# execute a zipfile or directory by invoking __main__.py
|
||||
main_name = "__main__"
|
||||
try:
|
||||
return _get_module_details(main_name)
|
||||
except ImportError as exc:
|
||||
if main_name in str(exc):
|
||||
raise ImportError("can't find %r module in %r" %
|
||||
(main_name, sys.path[0]))
|
||||
raise
|
||||
|
||||
# This function is the actual implementation of the -m switch and direct
|
||||
# execution of zipfiles and directories and is deliberately kept private.
|
||||
# This avoids a repeat of the situation where run_module() no longer met the
|
||||
# needs of mainmodule.c, but couldn't be changed because it was public
|
||||
# XXX ncoghlan: Should this be documented and made public?
|
||||
# (Current thoughts: don't repeat the mistake that lead to its
|
||||
# creation when run_module() no longer met the needs of
|
||||
# mainmodule.c, but couldn't be changed because it was public)
|
||||
def _run_module_as_main(mod_name, alter_argv=True):
|
||||
"""Runs the designated module in the __main__ namespace
|
||||
|
||||
These __*__ magic variables will be overwritten:
|
||||
Note that the executed module will have full access to the
|
||||
__main__ namespace. If this is not desirable, the run_module()
|
||||
function sbould be used to run the module code in a fresh namespace.
|
||||
|
||||
At the very least, these variables in __main__ will be overwritten:
|
||||
__name__
|
||||
__file__
|
||||
__loader__
|
||||
__package__
|
||||
"""
|
||||
try:
|
||||
if alter_argv or mod_name != "__main__": # i.e. -m switch
|
||||
|
@ -146,7 +139,16 @@ def _run_module_as_main(mod_name, alter_argv=True):
|
|||
else: # i.e. directory or zipfile execution
|
||||
mod_name, loader, code, fname = _get_main_module_details()
|
||||
except ImportError as exc:
|
||||
msg = "%s: %s" % (sys.executable, str(exc))
|
||||
# Try to provide a good error message
|
||||
# for directories, zip files and the -m switch
|
||||
if alter_argv:
|
||||
# For -m switch, just display the exception
|
||||
info = str(exc)
|
||||
else:
|
||||
# For directories/zipfiles, let the user
|
||||
# know what the code was looking for
|
||||
info = "can't find '__main__.py' in %r" % sys.argv[0]
|
||||
msg = "%s: %s" % (sys.executable, info)
|
||||
sys.exit(msg)
|
||||
pkg_name = mod_name.rpartition('.')[0]
|
||||
main_globals = sys.modules["__main__"].__dict__
|
||||
|
@ -173,6 +175,18 @@ def run_module(mod_name, init_globals=None,
|
|||
return _run_code(code, {}, init_globals, run_name,
|
||||
fname, loader, pkg_name)
|
||||
|
||||
def _get_main_module_details():
|
||||
# Helper that gives a nicer error message when attempting to
|
||||
# execute a zipfile or directory by invoking __main__.py
|
||||
main_name = "__main__"
|
||||
try:
|
||||
return _get_module_details(main_name)
|
||||
except ImportError as exc:
|
||||
if main_name in str(exc):
|
||||
raise ImportError("can't find %r module in %r" %
|
||||
(main_name, sys.path[0]))
|
||||
raise
|
||||
|
||||
|
||||
# XXX (ncoghlan): Perhaps expose the C API function
|
||||
# as imp.get_importer instead of reimplementing it in Python?
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue