summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coverage/codeunit.py37
-rw-r--r--coverage/results.py3
2 files changed, 32 insertions, 8 deletions
diff --git a/coverage/codeunit.py b/coverage/codeunit.py
index 0b02f862..4c834117 100644
--- a/coverage/codeunit.py
+++ b/coverage/codeunit.py
@@ -4,6 +4,8 @@ import glob, os
from coverage.backward import open_source, string_class, StringIO
from coverage.misc import CoverageException
+from coverage.parser import CodeParser
+from coverage.results import Analysis
def code_unit_factory(morfs, file_locator):
@@ -29,7 +31,7 @@ def code_unit_factory(morfs, file_locator):
globbed.append(morf)
morfs = globbed
- code_units = [CodeUnit(morf, file_locator) for morf in morfs]
+ code_units = [PythonCodeUnit(morf, file_locator) for morf in morfs]
return code_units
@@ -44,6 +46,7 @@ class CodeUnit(object):
`relative` is a boolean.
"""
+
def __init__(self, morf, file_locator):
self.file_locator = file_locator
@@ -51,11 +54,7 @@ class CodeUnit(object):
f = morf.__file__
else:
f = morf
- # .pyc files should always refer to a .py instead.
- if f.endswith(('.pyc', '.pyo')):
- f = f[:-1]
- elif f.endswith('$py.class'): # Jython
- f = f[:-9] + ".py"
+ f = self.adjust_filename(f)
self.filename = self.file_locator.canonical_filename(f)
if hasattr(morf, '__name__'):
@@ -76,6 +75,9 @@ class CodeUnit(object):
def __repr__(self):
return "<CodeUnit name=%r filename=%r>" % (self.name, self.filename)
+ def adjust_filename(self, fname):
+ return fname
+
# Annoying comparison operators. Py3k wants __lt__ etc, and Py2k needs all
# of them defined.
@@ -130,6 +132,29 @@ class CodeUnit(object):
This is used to decide if a file reported as part of the execution of
a program was really likely to have contained Python in the first
place.
+ """
+ return False
+
+
+class PythonCodeUnit(CodeUnit):
+ """Represents a Python file."""
+
+ parser_class = CodeParser
+
+ def adjust_filename(self, fname):
+ # .pyc files should always refer to a .py instead.
+ if fname.endswith(('.pyc', '.pyo')):
+ fname = fname[:-1]
+ elif fname.endswith('$py.class'): # Jython
+ fname = fname[:-9] + ".py"
+ return fname
+
+ def should_be_python(self):
+ """Does it seem like this file should contain Python?
+
+ This is used to decide if a file reported as part of the execution of
+ a program was really likely to have contained Python in the first
+ place.
"""
# Get the file extension.
diff --git a/coverage/results.py b/coverage/results.py
index 0576ae1f..e63db0f5 100644
--- a/coverage/results.py
+++ b/coverage/results.py
@@ -5,7 +5,6 @@ import os
from coverage.backward import iitems
from coverage.misc import format_lines, join_regex, NoSource
-from coverage.parser import CodeParser
class Analysis(object):
@@ -18,7 +17,7 @@ class Analysis(object):
self.filename = self.code_unit.filename
actual_filename, source = self.find_source(self.filename)
- self.parser = CodeParser(
+ self.parser = code_unit.parser_class(
text=source, filename=actual_filename,
exclude=self.coverage._exclude_regex('exclude')
)