From 688ecb98e29d9eb00e865813534e6e597c2b799d Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 17 Nov 2009 22:30:38 -0500 Subject: Ignore the branch of an except clause testing its type, since these aren't conceptually branches. Fixes #35. --- coverage/parser.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'coverage/parser.py') diff --git a/coverage/parser.py b/coverage/parser.py index 1e8b7792..de0e0c7f 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -262,6 +262,8 @@ OPS_NO_JUMP = _opcode_set('SETUP_EXCEPT', 'SETUP_FINALLY') # Individual opcodes we need below. OP_BREAK_LOOP = _opcode('BREAK_LOOP') OP_END_FINALLY = _opcode('END_FINALLY') +OP_COMPARE_OP = _opcode('COMPARE_OP') +COMPARE_EXCEPTION = 10 # just have to get this const from the code. class ByteParser(object): @@ -378,6 +380,10 @@ class ByteParser(object): # Each entry is a tuple: (block type, destination) block_stack = [] + # Some op codes are followed by branches that should be ignored. This + # is a count of how many ignores are left. + ignore_branch = 0 + for bc in ByteCodes(self.code.co_code): # Maybe have to start a new block if bc.offset in bytes_lines_map: @@ -392,8 +398,12 @@ class ByteParser(object): # Look at the opcode if bc.jump_to >= 0 and bc.op not in OPS_NO_JUMP: - # The opcode has a jump, it's an exit for this chunk. - chunk.exits.add(bc.jump_to) + if ignore_branch: + # Someone earlier wanted us to ignore this branch. + ignore_branch -= 1 + else: + # The opcode has a jump, it's an exit for this chunk. + chunk.exits.add(bc.jump_to) if bc.op in OPS_CODE_END: # The opcode can exit the code object. @@ -422,7 +432,11 @@ class ByteParser(object): if block_stack[iblock][0] in OPS_EXCEPT_BLOCKS: chunk.exits.add(block_stack[iblock][1]) break - + if bc.op == OP_COMPARE_OP and bc.arg == COMPARE_EXCEPTION: + # This is an except clause. We want to overlook the next + # branch, so that except's don't count as branches. + ignore_branch += 1 + if chunks: chunks[-1].length = bc.next_offset - chunks[-1].byte for i in range(len(chunks)-1): -- cgit v1.2.1