def test_find_linestarts(): co = bug_loop.__code__ # start_line = co.co_firstlineno got_no_dups = list(findlinestarts(co)) if sys.version_info[0:2] == (2, 7): # FIXME base off of start_line expect = [(0, 17), (6, 18), (9, 19), (19, 20), (32, 21), (42, 22), (67, 23)] assert got_no_dups == expect got_with_dups = list(findlinestarts(bug_loop.__code__, dup_lines=True)) assert len(got_no_dups) < len(got_with_dups)
def run(self, args): """Program counter.""" proc = self.proc curframe = proc.curframe if curframe: line_no = inspect.getlineno(curframe) offset = curframe.f_lasti self.msg("PC offset is %d." % offset) offset = max(offset, 0) code = curframe.f_code co_code = code.co_code disassemble_bytes( self.msg, self.msg_nocr, code = co_code, lasti = offset, cur_line = line_no, start_line = line_no - 1, end_line = line_no + 1, varnames=code.co_varnames, names=code.co_names, constants=code.co_consts, cells=code.co_cellvars, freevars=code.co_freevars, linestarts=dict(findlinestarts(code)), end_offset=offset + 10, opc=proc.vm.opc, ) pass return False
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 next_linestart(co, offset, count=1): linestarts = dict(xdis.findlinestarts(co)) code = co.co_code # n = len(code) # contains_cond_jump = False for op, offset in next_opcode(code, offset): if offset in linestarts: count -= 1 if 0 == count: return linestarts[offset] pass pass return -1000
def run(self, args): """Program counter.""" mainfile = self.core.filename(None) if self.core.is_running(): curframe = self.proc.curframe if curframe: line_no = inspect.getlineno(curframe) offset = curframe.f_lasti self.msg("PC offset is %d." % offset) offset = max(offset, 0) code = curframe.f_code co_code = code.co_code disassemble_bytes( self.msg, self.msg_nocr, code=co_code, lasti=offset, cur_line=line_no, start_line=line_no - 1, end_line=line_no + 1, varnames=code.co_varnames, names=code.co_names, constants=code.co_consts, cells=code.co_cellvars, freevars=code.co_freevars, linestarts=dict(findlinestarts(code)), end_offset=offset + 10, opc=proc.vm.opc, ) pass pass else: if mainfile: part1 = "Python program '%s'" % mainfile msg = "is not currently running. " self.msg(wrapped_lines(part1, msg, self.settings["width"])) else: self.msg("No Python program is currently running.") pass self.msg(self.core.execution_status) pass return False
def stmt_contains_opcode(co, lineno, query_opcode): linestarts = dict(xdis.findlinestarts(co)) code = co.co_code found_start = False for offset, start_line in list(linestarts.items()): if start_line == lineno: found_start = True break pass if not found_start: return False for op, offset in next_opcode(code, offset): if -1000 == offset or linestarts.get(offset): return False opcode = opname[op] # debug: print opcode if query_opcode == opcode: return True pass return False
def disassemble( msg, msg_nocr, section, co, lasti=-1, start_line=-1, end_line=None, relative_pos=False, highlight="light", start_offset=0, end_offset=None, asm_format="extended", ): """Disassemble a code object.""" return disassemble_bytes( msg, msg_nocr, co.co_code, lasti, co.co_firstlineno, start_line, end_line, relative_pos, co.co_varnames, co.co_names, co.co_consts, co.co_cellvars, co.co_freevars, dict(findlinestarts(co)), highlight, start_offset=start_offset, end_offset=end_offset, opc=opc, asm_format=asm_format, )