diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2011-05-17 10:06:40 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2011-05-17 10:06:40 -0400 |
commit | 34413aab8a61579789faa342331ac9c8a55ee5a4 (patch) | |
tree | 47850ea0d7622978a1ea2484f1d5955364896076 /coverage/parser.py | |
parent | 6754971e7ef16e5cee60ae5532de8f6069768ce8 (diff) | |
download | python-coveragepy-git-34413aab8a61579789faa342331ac9c8a55ee5a4.tar.gz |
WIP for partial branch exclusion. #113
Diffstat (limited to 'coverage/parser.py')
-rw-r--r-- | coverage/parser.py | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/coverage/parser.py b/coverage/parser.py index d033f6d2..cbbb5a6a 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -5,7 +5,7 @@ import glob, opcode, os, re, sys, token, tokenize from coverage.backward import set, sorted, StringIO # pylint: disable=W0622 from coverage.backward import open_source from coverage.bytecode import ByteCodes, CodeObjects -from coverage.misc import nice_pair, expensive +from coverage.misc import nice_pair, expensive, join_regex from coverage.misc import CoverageException, NoSource, NotPython @@ -15,7 +15,7 @@ class CodeParser(object): def __init__(self, text=None, filename=None, exclude=None): """ Source can be provided as `text`, the text itself, or `filename`, from - which text will be read. Excluded lines are those that match + which the text will be read. Excluded lines are those that match `exclude`, a regex. """ @@ -68,6 +68,21 @@ class CodeParser(object): return self._byte_parser byte_parser = property(_get_byte_parser) + def lines_matching(self, *regexes): + """Find the lines matching one of a list of regexes. + + Returns a set of line numbers, the lines that contain a match for one + of the regexes in `regexes`. The entire line needn't match, just a + part of it. + + """ + regex_c = re.compile(join_regex(regexes)) + matches = set() + for i, ltext in enumerate(self.lines): + if regex_c.search(ltext): + matches.add(i+1) + return matches + def _raw_parse(self): """Parse the source to find the interesting facts about its lines. @@ -76,10 +91,7 @@ class CodeParser(object): """ # Find lines which match an exclusion pattern. if self.exclude: - re_exclude = re.compile(self.exclude) - for i, ltext in enumerate(self.lines): - if re_exclude.search(ltext): - self.excluded.add(i+1) + self.excluded = self.lines_matching(self.exclude) # Tokenize, to find excluded suites, to find docstrings, and to find # multi-line statements. |