summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-07-23 12:38:48 -0400
committerNed Batchelder <ned@nedbatchelder.com>2015-07-23 12:38:48 -0400
commitd5722c5cdc1986fc3c430989f3e31007a518bc3b (patch)
tree55f3fd0a4f0062abbe805b2ae6bbcdd041c328e3
parent9466b308b1488d1e0ea3af2f3913955e60cb28e9 (diff)
downloadpython-coveragepy-d5722c5cdc1986fc3c430989f3e31007a518bc3b.tar.gz
Be sure to close the file we opened if something goes wrong.
-rw-r--r--coverage/data.py10
1 files changed, 7 insertions, 3 deletions
diff --git a/coverage/data.py b/coverage/data.py
index 26fee48..704a348 100644
--- a/coverage/data.py
+++ b/coverage/data.py
@@ -238,9 +238,13 @@ class CoverageData(object):
def _open_for_reading(cls, filename):
"""Open a file appropriately for reading data."""
f = open(filename, "r")
- go_away = f.read(len(cls.GO_AWAY))
- if go_away != cls.GO_AWAY:
- raise CoverageException("Doesn't seem to be a coverage.py data file")
+ try:
+ go_away = f.read(len(cls.GO_AWAY))
+ if go_away != cls.GO_AWAY:
+ raise CoverageException("Doesn't seem to be a coverage.py data file")
+ except Exception:
+ f.close()
+ raise
return f
@classmethod