summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coverage/execfile.py7
-rw-r--r--coverage/parser.py6
2 files changed, 10 insertions, 3 deletions
diff --git a/coverage/execfile.py b/coverage/execfile.py
index 333163f..8fbf63b 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/parser.py b/coverage/parser.py
index ae618ce..4dca577 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