diff options
author | Brett Cannon <brett@python.org> | 2011-01-28 13:53:34 -0800 |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2011-01-28 13:53:34 -0800 |
commit | 7239baf097df53b1da81854452fe027855ca2b9c (patch) | |
tree | 4741c241d45705f5de1a728e97999e92294609b6 /coverage | |
parent | 9681a8f11e84ae57abc7cbfba807ca847884e5a5 (diff) | |
download | python-coveragepy-7239baf097df53b1da81854452fe027855ca2b9c.tar.gz |
Properly close files.
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/codeunit.py | 7 | ||||
-rw-r--r-- | coverage/html.py | 13 |
2 files changed, 15 insertions, 5 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py index dfc4560..f54bda5 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -1,6 +1,6 @@ """Code unit (module) handling for Coverage.""" -import glob, os +import glob, os, tokenize from coverage.backward import string_class, StringIO from coverage.misc import CoverageException @@ -104,7 +104,10 @@ class CodeUnit(object): """Return an open file for reading the source of the code unit.""" if os.path.exists(self.filename): # A regular text file: open it. - return open(self.filename) + if hasattr(tokenize, 'open'): # Python 3.2 and later + return tokenize.open(self.filename) + else: + return open(self.filename) # Maybe it's in a zip file? source = self.file_locator.get_zip_data(self.filename) diff --git a/coverage/html.py b/coverage/html.py index 76e2890..b68cc93 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -18,7 +18,11 @@ def data_filename(fname): def data(fname): """Return the contents of a data file of ours.""" - return open(data_filename(fname)).read() + data_file = open(data_filename(fname)) + try: + return data_file.read() + finally: + data_file.close() class HtmlReporter(Reporter): @@ -68,8 +72,11 @@ class HtmlReporter(Reporter): def html_file(self, cu, analysis): """Generate an HTML file for one source file.""" - - source = cu.source_file().read() + source_file = cu.source_file() + try: + source = source_file.read() + finally: + source_file.close() nums = analysis.numbers |