diff options
Diffstat (limited to 'coverage')
-rw-r--r-- | coverage/phystokens.py | 2 | ||||
-rw-r--r-- | coverage/python.py | 21 |
2 files changed, 6 insertions, 17 deletions
diff --git a/coverage/phystokens.py b/coverage/phystokens.py index d21d401c..587ec1c6 100644 --- a/coverage/phystokens.py +++ b/coverage/phystokens.py @@ -283,7 +283,7 @@ def compile_unicode(source, filename, mode): try: code = compile(source, filename, mode) except SyntaxError as synerr: - if synerr.args[0] != "encoding declaration in Unicode string": + if "coding declaration in unicode string" not in synerr.args[0].lower(): raise source = neuter_encoding_declaration(source) code = compile(source, filename, mode) diff --git a/coverage/python.py b/coverage/python.py index f335f165..3e8e9f39 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': |