diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2014-12-26 10:48:29 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2014-12-26 10:48:29 -0500 |
commit | 135467172407a7f40907cc752f282bf0e3cce306 (patch) | |
tree | 48cc1c881f8cda7c2a5180e19b61e257e7c90d74 /coverage/parser.py | |
parent | a6e77b38f2341ae2ff4713119fdcad0bb3515e11 (diff) | |
download | python-coveragepy-git-135467172407a7f40907cc752f282bf0e3cce306.tar.gz |
Unify and clarify reading Python source. Probably broke .pyw files
Diffstat (limited to 'coverage/parser.py')
-rw-r--r-- | coverage/parser.py | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/coverage/parser.py b/coverage/parser.py index 317f7ec7..b50bc578 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -54,6 +54,7 @@ class PythonParser(CodeParser): ) if self.text: + assert isinstance(self.text, str) # Scrap the BOM if it exists. if ord(self.text[0]) == 0xfeff: self.text = self.text[1:] @@ -90,8 +91,7 @@ class PythonParser(CodeParser): def byte_parser(self): """Create a ByteParser on demand.""" if not self._byte_parser: - self._byte_parser = \ - ByteParser(text=self.text, filename=self.filename) + self._byte_parser = ByteParser(self.text, filename=self.filename) return self._byte_parser def lines_matching(self, *regexes): @@ -343,16 +343,11 @@ OP_RETURN_VALUE = _opcode('RETURN_VALUE') class ByteParser(object): """Parse byte codes to understand the structure of code.""" - def __init__(self, code=None, text=None, filename=None): + def __init__(self, text, code=None, filename=None): + self.text = text if code: self.code = code - self.text = text else: - if not text: - assert filename, "If no code or text, need a filename" - text = get_python_source(filename) - self.text = text - try: self.code = compile(text, filename, "exec") except SyntaxError as synerr: @@ -378,7 +373,7 @@ class ByteParser(object): """ children = CodeObjects(self.code) - return (ByteParser(code=c, text=self.text) for c in children) + return (ByteParser(self.text, code=c) for c in children) def _bytes_lines(self): """Map byte offsets to line numbers in `code`. |