diff options
-rw-r--r-- | .hgignore | 1 | ||||
-rw-r--r-- | coverage/codeunit.py | 7 | ||||
-rw-r--r-- | coverage/execfile.py | 7 | ||||
-rw-r--r-- | coverage/html.py | 13 | ||||
-rw-r--r-- | coverage/parser.py | 6 |
5 files changed, 26 insertions, 8 deletions
@@ -9,6 +9,7 @@ syntax: glob *.bak
.coverage
.coverage.*
+*.swp
# Stuff in the root.
build
diff --git a/coverage/codeunit.py b/coverage/codeunit.py index dfc4560d..f54bda5e 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/execfile.py b/coverage/execfile.py index 333163f8..8fbf63b8 100644 --- a/coverage/execfile.py +++ b/coverage/execfile.py @@ -38,10 +38,15 @@ def run_python_file(filename, args): try: # Open the source file. try: - source = open(filename, 'rU').read() + source_file = open(filename, 'rU') except IOError: raise NoSource("No file to run: %r" % filename) + try: + 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. if source[-1] != '\n': diff --git a/coverage/html.py b/coverage/html.py index 76e28907..b68cc936 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 diff --git a/coverage/parser.py b/coverage/parser.py index ae618ce5..4dca5771 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -22,7 +22,10 @@ class CodeParser(object): self.text = text if not self.text: try: - sourcef = open(self.filename, 'rU') + if hasattr(tokenize, 'open'): # Python 3.2 and later + sourcef = tokenize.open(self.filename) + else: + sourcef = open(self.filename, 'rU') self.text = sourcef.read() sourcef.close() except IOError: @@ -30,7 +33,6 @@ class CodeParser(object): raise NoSource( "No source for code: %r: %s" % (self.filename, err) ) - self.text = self.text.replace('\r\n', '\n') self.exclude = exclude |