diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2009-07-07 23:04:52 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2009-07-07 23:04:52 -0400 |
commit | 57259a56cd909f1b066dcc0fa0f3661e3e3fe3e2 (patch) | |
tree | 6c2b6d41465d30c919e8d08809ec4971ab57b688 /coverage/parser.py | |
parent | a5d36187bd3ffacb67401af9f2237d6248958877 (diff) | |
download | python-coveragepy-57259a56cd909f1b066dcc0fa0f3661e3e3fe3e2.tar.gz |
Make reading the lnotab structure work in both 2.x and 3.x
Diffstat (limited to 'coverage/parser.py')
-rw-r--r-- | coverage/parser.py | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/coverage/parser.py b/coverage/parser.py index 3f0011e..0ce7e22 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -1,12 +1,13 @@ """Code parsing for Coverage.""" -import re, token, tokenize, types +import re, sys, token, tokenize, types import cStringIO as StringIO from coverage.misc import nice_pair, CoverageException from coverage.backward import set # pylint: disable-msg=W0622 - + + class CodeParser: """Parse code to find executable lines, excluded lines, etc.""" @@ -28,6 +29,16 @@ class CodeParser: # The line numbers that start statements. self.statement_starts = set() + # Getting numbers from the lnotab value changed in Py3.0. + if sys.hexversion >= 0x03000000: + def lnotab_increments(self, lnotab): + """Return a list of ints from the lnotab bytes in 3.x""" + return list(lnotab) + else: + def lnotab_increments(self, lnotab): + """Return a list of ints from the lnotab string in 2.x""" + return [ord(c) for c in lnotab] + def find_statement_starts(self, code): """Find the starts of statements in compiled code. @@ -36,8 +47,8 @@ class CodeParser: """ # Adapted from dis.py in the standard library. - byte_increments = [ord(c) for c in code.co_lnotab[0::2]] - line_increments = [ord(c) for c in code.co_lnotab[1::2]] + byte_increments = self.lnotab_increments(code.co_lnotab[0::2]) + line_increments = self.lnotab_increments(code.co_lnotab[1::2]) last_line_num = None line_num = code.co_firstlineno |