summaryrefslogtreecommitdiff
path: root/coverage/python.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2015-05-17 22:28:57 -0400
committerNed Batchelder <ned@nedbatchelder.com>2015-05-17 22:28:57 -0400
commite4e17518c99d4503c513410d164f28723968bdb7 (patch)
treef2a53fe98ea05444a9633ca68714ff4a0cc7d144 /coverage/python.py
parentd0d1f4ad6f0190309be76ee486253995a547bf34 (diff)
downloadpython-coveragepy-e4e17518c99d4503c513410d164f28723968bdb7.tar.gz
Files with incorrect encoding declarations are no longer ignored. #351
Diffstat (limited to 'coverage/python.py')
-rw-r--r--coverage/python.py21
1 files changed, 5 insertions, 16 deletions
diff --git a/coverage/python.py b/coverage/python.py
index f335f16..3e8e9f3 100644
--- a/coverage/python.py
+++ b/coverage/python.py
@@ -1,8 +1,6 @@
"""Python source expertise for coverage.py"""
import os.path
-import sys
-import tokenize
import zipimport
from coverage import env
@@ -14,21 +12,15 @@ from coverage.phystokens import source_token_lines, source_encoding
from coverage.plugin import FileReporter
-@contract(returns='str')
+@contract(returns='bytes')
def read_python_source(filename):
"""Read the Python source text from `filename`.
- Returns a str: unicode on Python 3, bytes on Python 2.
+ Returns bytes.
"""
- # Python 3.2 provides `tokenize.open`, the best way to open source files.
- if sys.version_info >= (3, 2):
- f = tokenize.open(filename)
- else:
- f = open(filename, "rU")
-
- with f:
- return f.read()
+ with open(filename, "rb") as f:
+ return f.read().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
@contract(returns='unicode')
@@ -50,15 +42,12 @@ def get_python_source(filename):
# Maybe it's in a zip file?
source = get_zip_bytes(try_filename)
if source is not None:
- if env.PY3:
- source = source.decode(source_encoding(source), "replace")
break
else:
# Couldn't find source.
raise NoSource("No source for code: '%s'." % filename)
- if env.PY2:
- source = source.decode(source_encoding(source), "replace")
+ source = source.decode(source_encoding(source), "replace")
# Python code should always end with a line with a newline.
if source and source[-1] != '\n':