diff options
-rw-r--r-- | coverage/codeunit.py | 10 | ||||
-rw-r--r-- | coverage/files.py | 4 | ||||
-rw-r--r-- | coverage/phystokens.py | 44 | ||||
-rw-r--r-- | coverage/summary.py | 2 |
4 files changed, 37 insertions, 23 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py index 2c066fc3..8265a786 100644 --- a/coverage/codeunit.py +++ b/coverage/codeunit.py @@ -4,8 +4,8 @@ import os import sys from coverage.backward import string_class, unicode_class -from coverage.files import get_python_source, get_zip_bytes -from coverage.misc import CoverageException, NoSource +from coverage.files import get_python_source +from coverage.misc import CoverageException from coverage.parser import PythonParser from coverage.phystokens import source_token_lines, source_encoding @@ -84,8 +84,6 @@ class CodeUnit(object): self.name = n self.modname = modname - self._source = None - def __repr__(self): return "<{self.__class__.__name__} name={self.name!r} filename={self.filename!r}>".format(self=self) @@ -151,6 +149,10 @@ class CodeUnit(object): class PythonCodeUnit(CodeUnit): """Represents a Python file.""" + def __init__(self, morf, file_locator): + super(PythonCodeUnit, self).__init__(morf, file_locator) + self._source = None + def _adjust_filename(self, fname): # .pyc files should always refer to a .py instead. if fname.endswith(('.pyc', '.pyo')): diff --git a/coverage/files.py b/coverage/files.py index c66327d3..7f2431d3 100644 --- a/coverage/files.py +++ b/coverage/files.py @@ -77,9 +77,7 @@ def get_python_source(filename): return source # Couldn't find source. - raise NoSource( - "No source for code: '%s'." % filename - ) + raise NoSource("No source for code: %r." % filename) def get_zip_bytes(filename): diff --git a/coverage/phystokens.py b/coverage/phystokens.py index 70deb800..bf55e8a3 100644 --- a/coverage/phystokens.py +++ b/coverage/phystokens.py @@ -147,16 +147,12 @@ class CachedTokenizer(object): generate_tokens = CachedTokenizer().generate_tokens -def source_encoding(source): +def _source_encoding_py2(source): """Determine the encoding for `source` (a string), according to PEP 263. Returns a string, the name of the encoding. """ - if sys.version_info >= (3, 0): - readline = iter(source.splitlines(True)).__next__ - return tokenize.detect_encoding(readline)[0] - # Do this so the detect_encode code we copied will work. readline = iter(source.splitlines(True)).next @@ -243,15 +239,33 @@ def source_encoding(source): return default -# Reading Python source and interpreting the coding comment is a big deal. +def _source_encoding_py3(source): + """Determine the encoding for `source` (a string), according to PEP 263. + + Returns a string, the name of the encoding. + + """ + readline = iter(source.splitlines(True)).__next__ + return tokenize.detect_encoding(readline)[0] + + if sys.version_info >= (3, 0): - # Python 3.2 provides `tokenize.open`, the best way to open source files. - def read_python_source(filename): - # Returns unicode on Py3, bytes on Py2 - with tokenize.open(filename) as f: - return f.read() + source_encoding = _source_encoding_py3 else: - def read_python_source(filename): - # Returns unicode on Py3, bytes on Py2 - with open(filename, "rU") as f: - return f.read() + source_encoding = _source_encoding_py2 + + +def read_python_source(filename): + """Read the Python source text from `filename`. + + Returns unicode on Python 3, bytes on Python 2. + + """ + # 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() diff --git a/coverage/summary.py b/coverage/summary.py index cfcdfcda..10ac7e2c 100644 --- a/coverage/summary.py +++ b/coverage/summary.py @@ -4,7 +4,7 @@ import sys from coverage.report import Reporter from coverage.results import Numbers -from coverage.misc import CoverageException, NotPython +from coverage.misc import NotPython class SummaryReporter(Reporter): |