summaryrefslogtreecommitdiff
path: root/coverage/execfile.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2013-12-13 22:45:10 -0500
committerNed Batchelder <ned@nedbatchelder.com>2013-12-13 22:45:10 -0500
commit2df9b1c35cbb5c92204fc5923368a3d619a34f6d (patch)
treed1ede8ffef812ba4e345b08f698f001ebe69cb56 /coverage/execfile.py
parent84221611890880b749dbb650e8d07ac8918dba46 (diff)
parent7c66441eab3af17539c478a2cb4e19cd93ba0cf4 (diff)
downloadpython-coveragepy-git-2df9b1c35cbb5c92204fc5923368a3d619a34f6d.tar.gz
Merged 4.0 to default
Diffstat (limited to 'coverage/execfile.py')
-rw-r--r--coverage/execfile.py64
1 files changed, 29 insertions, 35 deletions
diff --git a/coverage/execfile.py b/coverage/execfile.py
index f6ebdf79..f90096e9 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -31,35 +31,33 @@ def run_python_module(modulename, args):
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 = rsplit1(modulename, '.')
- package = __import__(packagename, glo, loc, ['__path__'])
- searchpath = package.__path__
- else:
- packagename, name = None, modulename
- searchpath = None # "top-level search" in imp.find_module()
+ # Search for the module - inside its parent package, if any - using
+ # standard import mechanics.
+ if '.' in modulename:
+ packagename, name = rsplit1(modulename, '.')
+ 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)
-
- # 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))
+ except ImportError as err:
+ raise NoSource(str(err))
finally:
if openfile:
openfile.close()
@@ -94,7 +92,7 @@ def run_python_file(filename, args, package=None):
try:
# Make a code object somehow.
- if filename.endswith(".pyc") or filename.endswith(".pyo"):
+ if filename.endswith((".pyc", ".pyo")):
code = make_code_from_pyc(filename)
else:
code = make_code_from_py(filename)
@@ -129,10 +127,8 @@ def make_code_from_py(filename):
except IOError:
raise NoSource("No file to run: %r" % filename)
- try:
+ with source_file:
source = source_file.read()
- finally:
- source_file.close()
# We have the source. `compile` still needs the last line to be clean,
# so make sure it is, then compile a code object from it.
@@ -150,7 +146,7 @@ def make_code_from_pyc(filename):
except IOError:
raise NoCode("No file to run: %r" % filename)
- try:
+ with fpyc:
# First four bytes are a version-specific magic number. It has to
# match or we won't run the file.
magic = fpyc.read(4)
@@ -165,7 +161,5 @@ def make_code_from_pyc(filename):
# The rest of the file is the code object we want.
code = marshal.load(fpyc)
- finally:
- fpyc.close()
return code