summaryrefslogtreecommitdiff
path: root/coverage/html.py
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
commit6b6a4488adc12d390c5e0c8f13829dd9bf125309 (patch)
tree4eeeeb857c02023fd46d610612f600d533105afb /coverage/html.py
parent2e5688eedd1576e9b100386c0a9ae30828123f73 (diff)
downloadpython-coveragepy-git-6b6a4488adc12d390c5e0c8f13829dd9bf125309.tar.gz
with statements: no more finally close
--HG-- branch : 4.0
Diffstat (limited to 'coverage/html.py')
-rw-r--r--coverage/html.py24
1 files changed, 5 insertions, 19 deletions
diff --git a/coverage/html.py b/coverage/html.py
index e0262998..d8779233 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."""