summaryrefslogtreecommitdiff
path: root/coverage
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2013-10-20 07:58:57 -0400
committerNed Batchelder <ned@nedbatchelder.com>2013-10-20 07:58:57 -0400
commit249ad14dfedbe45919b48dbbff445394a64d985c (patch)
treef88f6356df0087c28e8d18a434e5e9142359352f /coverage
parentf0459a54bb3e703691359aa3078a8234173ec361 (diff)
downloadpython-coveragepy-249ad14dfedbe45919b48dbbff445394a64d985c.tar.gz
with statements: no more finally close
Diffstat (limited to 'coverage')
-rw-r--r--coverage/data.py10
-rw-r--r--coverage/execfile.py8
-rw-r--r--coverage/html.py24
-rw-r--r--coverage/parser.py10
4 files changed, 11 insertions, 41 deletions
diff --git a/coverage/data.py b/coverage/data.py
index 61b3554..042b640 100644
--- a/coverage/data.py
+++ b/coverage/data.py
@@ -128,11 +128,8 @@ class CoverageData(object):
self.debug.write("Writing data to %r" % (filename,))
# Write the pickle to the file.
- fdata = open(filename, 'wb')
- try:
+ with open(filename, 'wb') as fdata:
pickle.dump(data, fdata, 2)
- finally:
- fdata.close()
def read_file(self, filename):
"""Read the coverage data from `filename`."""
@@ -142,11 +139,8 @@ class CoverageData(object):
"""Return the raw pickled data from `filename`."""
if self.debug and self.debug.should('dataio'):
self.debug.write("Reading data from %r" % (filename,))
- fdata = open(filename, 'rb')
- try:
+ with open(filename, 'rb') as fdata:
data = pickle.load(fdata)
- finally:
- fdata.close()
return data
def _read_file(self, filename):
diff --git a/coverage/execfile.py b/coverage/execfile.py
index 5d4ae69..71ec931 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -129,10 +129,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 +148,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 +163,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
diff --git a/coverage/html.py b/coverage/html.py
index e026299..d877923 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -43,11 +43,8 @@ def data_filename(fname, pkgdir=""):
def data(fname):
"""Return the contents of a data file of ours."""
- data_file = open(data_filename(fname))
- try:
+ with open(data_filename(fname)) as data_file:
return data_file.read()
- finally:
- data_file.close()
class HtmlReporter(Reporter):
@@ -140,11 +137,8 @@ class HtmlReporter(Reporter):
def write_html(self, fname, html):
"""Write `html` to `fname`, properly encoded."""
- fout = open(fname, "wb")
- try:
+ with open(fname, "wb") as fout:
fout.write(html.encode('ascii', 'xmlcharrefreplace'))
- finally:
- fout.close()
def file_hash(self, source, cu):
"""Compute a hash that changes if the file needs to be re-reported."""
@@ -156,10 +150,8 @@ class HtmlReporter(Reporter):
def html_file(self, cu, analysis):
"""Generate an HTML file for one source file."""
source_file = cu.source_file()
- try:
+ with source_file:
source = source_file.read()
- finally:
- source_file.close()
# Find out if the file on disk is already correct.
flat_rootname = cu.flat_rootname()
@@ -309,11 +301,8 @@ class HtmlStatus(object):
usable = False
try:
status_file = os.path.join(directory, self.STATUS_FILE)
- fstatus = open(status_file, "rb")
- try:
+ with open(status_file, "rb") as fstatus:
status = pickle.load(fstatus)
- finally:
- fstatus.close()
except (IOError, ValueError):
usable = False
else:
@@ -338,11 +327,8 @@ class HtmlStatus(object):
'settings': self.settings,
'files': self.files,
}
- fout = open(status_file, "wb")
- try:
+ with open(status_file, "wb") as fout:
pickle.dump(status, fout)
- finally:
- fout.close()
def settings_hash(self):
"""Get the hash of the coverage.py settings."""
diff --git a/coverage/parser.py b/coverage/parser.py
index 37beede..d0fe997 100644
--- a/coverage/parser.py
+++ b/coverage/parser.py
@@ -25,11 +25,8 @@ class CodeParser(object):
self.text = text
if not self.text:
try:
- sourcef = open_source(self.filename)
- try:
+ with open_source(self.filename) as sourcef:
self.text = sourcef.read()
- finally:
- sourcef.close()
except IOError:
_, err, _ = sys.exc_info()
raise NoSource(
@@ -328,11 +325,8 @@ class ByteParser(object):
else:
if not text:
assert filename, "If no code or text, need a filename"
- sourcef = open_source(filename)
- try:
+ with open_source(filename) as sourcef:
text = sourcef.read()
- finally:
- sourcef.close()
self.text = text
try: