def test_offset2line(): ary = ((20, 1), (40, 10), (60, 45)) for offset, expect in ( (61, 45), (20, 1), (40, 10), (39, 1), (41, 10), (60, 45), (21, 1)): got = offset2line(offset, ary) assert expect == got, \ ("offset=%d, got=%d, expect=%d" % (offset, got, expect)) ary = ((20, 1), (50, 10)) for offset, expect in ( (10, 0), (55, 10), (20, 1), (50, 10)): got = offset2line(offset, ary) assert expect == got, \ ("offset=%d, got=%d, expect=%d" % (offset, got, expect)) ary = ((25, 1),) for offset, expect in ( (10, 0), (30, 1), (25, 1)): got = offset2line(offset, ary) assert expect == got, \ ("offset=%d, got=%d, expect=%d" % (offset, got, expect)) ary = tuple() for offset, expect in ((10, 0),): got = offset2line(offset, ary) assert expect == got, \ ("offset=%d, got=%d, expect=%d" % (offset, got, expect))
def number_loop(queue, mappings, opc): while len(queue) > 0: code1 = queue.popleft() code2 = queue.popleft() assert code1.co_name == code2.co_name linestarts_orig = findlinestarts(code1) linestarts_uncompiled = list(findlinestarts(code2)) mappings += [ [line, offset2line(offset, linestarts_uncompiled)] for offset, line in linestarts_orig ] bytecode1 = Bytecode(code1, opc) bytecode2 = Bytecode(code2, opc) instr2s = bytecode2.get_instructions(code2) seen = set([code1.co_name]) for instr in bytecode1.get_instructions(code1): next_code1 = None if iscode(instr.argval): next_code1 = instr.argval if next_code1: next_code2 = None while not next_code2: try: instr2 = next(instr2s) if iscode(instr2.argval): next_code2 = instr2.argval pass except StopIteration: break pass if next_code2: assert next_code1.co_name == next_code2.co_name if next_code1.co_name not in seen: seen.add(next_code1.co_name) queue.append(next_code1) queue.append(next_code2) pass pass pass pass
def number_loop(queue, mappings, opc): while len(queue) > 0: code1 = queue.popleft() code2 = queue.popleft() assert code1.co_name == code2.co_name linestarts_orig = findlinestarts(code1) linestarts_uncompiled = list(findlinestarts(code2)) mappings += [[line, offset2line(offset, linestarts_uncompiled)] for offset, line in linestarts_orig] bytecode1 = Bytecode(code1, opc) bytecode2 = Bytecode(code2, opc) instr2s = bytecode2.get_instructions(code2) seen = set([code1.co_name]) for instr in bytecode1.get_instructions(code1): next_code1 = None if iscode(instr.argval): next_code1 = instr.argval if next_code1: next_code2 = None while not next_code2: try: instr2 = next(instr2s) if iscode(instr2.argval): next_code2 = instr2.argval pass except StopIteration: break pass if next_code2: assert next_code1.co_name == next_code2.co_name if next_code1.co_name not in seen: seen.add(next_code1.co_name) queue.append(next_code1) queue.append(next_code2) pass pass pass pass