summaryrefslogtreecommitdiff
path: root/coverage/execfile.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2011-02-06 23:02:12 -0500
committerNed Batchelder <ned@nedbatchelder.com>2011-02-06 23:02:12 -0500
commit2afc485b2724f496c67e0660c9c3683c52634515 (patch)
treedf91bb766f3836f428e533a9a6907569793e45b4 /coverage/execfile.py
parent50bc61cd0b0a66ec6cc497ea6870765d627fb9be (diff)
downloadpython-coveragepy-git-2afc485b2724f496c67e0660c9c3683c52634515.tar.gz
Add tests and doc for Brandon's -m flag.
Diffstat (limited to 'coverage/execfile.py')
-rw-r--r--coverage/execfile.py68
1 files changed, 41 insertions, 27 deletions
diff --git a/coverage/execfile.py b/coverage/execfile.py
index 335bf617..a32957c1 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -17,35 +17,48 @@ except KeyError:
def run_python_module(modulename, args):
"""Run a python module, as though with ``python -m name args...``.
+ `modulename` is the name of the module, possibly a dot-separated name.
+ `args` is the argument array to present as sys.argv, including the first
+ element naming the module being executed.
+
"""
- # Search for the module - inside its parent package, if any - using
- # standard import mechanics.
- if '.' in modulename:
- packagename, name = modulename.rsplit('.')
- package = __import__(packagename, fromlist=['__path__'])
- searchpath = package.__path__
- else:
- packagename = None
- name = modulename
- searchpath = None # means "top-level search" to find_module()
- openfile, pathname, description = imp.find_module(name, searchpath)
-
- # Complain if this is a magic non-file module.
- if openfile is None and pathname is None:
- raise NoSource("module does not live in a file: %r" % modulename)
-
- # If `modulename` is actually a package, not a mere module, then we
- # pretend to be Python 2.7 and try running its __main__.py script.
- if openfile is None:
- packagename = modulename
- name = '__main__'
-
- package = __import__(packagename, fromlist=['__path__'])
- searchpath = package.__path__
- openfile, pathname, description = imp.find_module(name, searchpath)
+ openfile = None
+ glo, loc = globals(), locals()
+ try:
+ try:
+ # Search for the module - inside its parent package, if any - using
+ # standard import mechanics.
+ if '.' in modulename:
+ packagename, name = modulename.rsplit('.', 1)
+ package = __import__(packagename, glo, loc, ['__path__'])
+ searchpath = package.__path__
+ else:
+ packagename, name = None, modulename
+ searchpath = None # "top-level search" in imp.find_module()
+ openfile, pathname, _ = imp.find_module(name, searchpath)
+
+ # Complain if this is a magic non-file module.
+ if openfile is None and pathname is None:
+ raise NoSource(
+ "module does not live in a file: %r" % modulename
+ )
+
+ # If `modulename` is actually a package, not a mere module, then we
+ # pretend to be Python 2.7 and try running its __main__.py script.
+ if openfile is None:
+ packagename = modulename
+ name = '__main__'
+ package = __import__(packagename, glo, loc, ['__path__'])
+ searchpath = package.__path__
+ openfile, pathname, _ = imp.find_module(name, searchpath)
+ except ImportError:
+ _, err, _ = sys.exc_info()
+ raise NoSource(str(err))
+ finally:
+ if openfile:
+ openfile.close()
# Finally, hand the file off to run_python_file for execution.
- openfile.close()
run_python_file(pathname, args, package=packagename)
@@ -54,7 +67,8 @@ def run_python_file(filename, args, package=None):
`filename` is the path to the file to execute, it need not be a .py file.
`args` is the argument array to present as sys.argv, including the first
- element representing the file being executed.
+ element naming the file being executed. `package` is the name of the
+ enclosing package, if any.
"""
# Create a module to serve as __main__