summaryrefslogtreecommitdiff
path: root/coverage/html.py
diff options
context:
space:
mode:
Diffstat (limited to 'coverage/html.py')
-rw-r--r--coverage/html.py29
1 files changed, 7 insertions, 22 deletions
diff --git a/coverage/html.py b/coverage/html.py
index 5242236c..d168e351 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()
@@ -195,8 +187,7 @@ class HtmlReporter(Reporter):
lines = []
- for lineno, line in enumerate(source_token_lines(source)):
- lineno += 1 # 1-based line numbers.
+ for lineno, line in enumerate(source_token_lines(source), start=1):
# Figure out how to mark this line.
line_class = []
annotate_html = ""
@@ -271,7 +262,7 @@ class HtmlReporter(Reporter):
data("index.html"), self.template_globals
)
- self.totals = sum([f['nums'] for f in self.files])
+ self.totals = sum(f['nums'] for f in self.files)
html = index_tmpl.render({
'arcs': self.arcs,
@@ -310,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:
@@ -339,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."""