summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coverage/codeunit.py29
-rw-r--r--coverage/html.py4
2 files changed, 19 insertions, 14 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py
index 3ec9c390..4e752aaa 100644
--- a/coverage/codeunit.py
+++ b/coverage/codeunit.py
@@ -92,6 +92,8 @@ class CodeUnit(object):
self.name = n
self.modname = modname
+ self._source = None
+
def __repr__(self):
return "<CodeUnit name=%r filename=%r>" % (self.name, self.filename)
@@ -132,6 +134,11 @@ class CodeUnit(object):
return root.replace('\\', '_').replace('/', '_').replace('.', '_')
def source(self):
+ if self._source is None:
+ self._source = self.get_source()
+ return self._source
+
+ def get_source(self):
"""Return the source code, as a string."""
if os.path.exists(self.filename):
# A regular text file: open it.
@@ -148,10 +155,9 @@ class CodeUnit(object):
"No source for code '%s'." % self.filename
)
- def source_token_lines(self, source):
+ def source_token_lines(self):
"""Return the 'tokenized' text for the code."""
- # TODO: Taking source here is wrong, change it?
- for line in source.splitlines():
+ for line in self.source().splitlines():
yield [('txt', line)]
def should_be_python(self):
@@ -242,11 +248,11 @@ class PythonCodeUnit(CodeUnit):
# Everything else is probably not Python.
return False
- def source_token_lines(self, source):
- return source_token_lines(source)
+ def source_token_lines(self):
+ return source_token_lines(self.source())
- def source_encoding(self, source):
- return source_encoding(source)
+ def source_encoding(self):
+ return source_encoding(self.source())
class MakoParser(CodeParser):
@@ -275,26 +281,25 @@ class MakoCodeUnit(CodeUnit):
py_source = open(self.filename).read()
self.metadata = ModuleInfo.get_module_source_metadata(py_source, full_line_map=True)
- def source(self):
+ def get_source(self):
return open(self.metadata['filename']).read()
def get_parser(self, exclude=None):
return MakoParser(self.metadata)
- def source_encoding(self, source):
- # TODO: Taking source here is wrong, change it!
+ def source_encoding(self):
return self.metadata['source_encoding']
class DjangoCodeUnit(CodeUnit):
- def source(self):
+ def get_source(self):
with open(self.filename) as f:
return f.read()
def get_parser(self, exclude=None):
return DjangoParser(self.filename)
- def source_encoding(self, source):
+ def source_encoding(self):
return "utf8"
diff --git a/coverage/html.py b/coverage/html.py
index 85f47ab4..863d1508 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -164,7 +164,7 @@ class HtmlReporter(Reporter):
# If need be, determine the encoding of the source file. We use it
# later to properly write the HTML.
if sys.version_info < (3, 0):
- encoding = cu.source_encoding(source)
+ encoding = cu.source_encoding()
# Some UTF8 files have the dreaded UTF8 BOM. If so, junk it.
if encoding.startswith("utf-8") and source[:3] == "\xef\xbb\xbf":
source = source[3:]
@@ -184,7 +184,7 @@ class HtmlReporter(Reporter):
lines = []
- for lineno, line in enumerate(cu.source_token_lines(source), start=1):
+ for lineno, line in enumerate(cu.source_token_lines(), start=1):
# Figure out how to mark this line.
line_class = []
annotate_html = ""