def deparse_text(co, name, last_i): text = [] try: deparsed = code_deparse(co) nodeInfo = deparsed_find((name, last_i), deparsed, co) if not nodeInfo: # FIXME: change deparse_code_around_offset() so we # don't need py_str2float from xdis.magics import py_str2float float_version = py_str2float() deparsed = deparse_code_around_offset(co.co_name, last_i, float_version, co) except: return None if not nodeInfo: nodeInfo = deparsed_find((name, last_i), deparsed, co) if nodeInfo: extractInfo = deparsed.extract_node_info(nodeInfo) if extractInfo: text.append(" instruction: %s" % (nodeInfo.node)) text.append(extractInfo.selectedLine) text.append(extractInfo.markerLine + "\n") pass return text
def deparse_offset(co, name, last_i, errmsg_fn): nodeInfo = None deparsed = deparse_cache.get(co, None) if not deparsed: out = StringIO() try: # FIXME: cache co deparsed = code_deparse(co, out) from trepan.api import debug debug() except: print(sys.exc_info()[1]) if errmsg_fn: errmsg_fn(sys.exc_info()[1]) errmsg_fn("error in deparsing code") deparse_cache[co] = deparsed try: nodeInfo = deparsed_find((name, last_i), deparsed, co) except: if errmsg_fn: errmsg_fn(sys.exc_info()[1]) errmsg_fn("error in deparsing code at offset %d" % last_i) if not nodeInfo: nodeInfo = deparsed_find((name, last_i), deparsed, co) return deparsed, nodeInfo
def deparse_fn(code): try: from uncompyle6.semantics.fragments import code_deparse except ImportError: return None try: deparsed = code_deparse(code) return deparsed.text.strip() except: raise return None
def run(self, args): co = self.proc.curframe.f_code name = co.co_name try: opts, args = getopt(args[1:], "hpPAto:O", [ "help", "parent", "pretty", "AST", 'tree', "offset=", "offsets" ]) except GetoptError as err: # print help information and exit: print(str( err)) # will print something like "option -a not recognized" return show_parent = False show_ast = False offset = None show_offsets = False for o, a in opts: if o in ("-h", "--help"): self.proc.commands['help'].run(['help', 'deparse']) return elif o in ("-O", "--offsets"): show_offsets = True elif o in ("-p", "--parent"): show_parent = True elif o in ("-A", "--tree", '--AST'): show_ast = True elif o in ("-o", '--offset'): offset = a else: self.errmsg("unhandled option '%s'" % o) pass pass nodeInfo = None if len(args) >= 1 and args[0] == '.': temp_filename, name_for_code = deparse_and_cache(co, self.errmsg) if not temp_filename: return self.print_text(''.join(getlines(temp_filename))) return elif show_offsets: deparsed = code_deparse(co) self.section("Offsets known:") m = self.columnize_commands( list(sorted(deparsed.offsets.keys(), key=lambda x: str(x[0])))) self.msg_nocr(m) return elif offset is not None: mess = ("The 'deparse' command when given an argument requires an" " instruction offset. Got: '%s'" % offset) last_i = self.proc.get_an_int(offset, mess) if last_i is None: return else: last_i = self.proc.curframe.f_lasti if last_i == -1: last_i = 0 deparsed, nodeInfo = deparse_offset(co, name, last_i, self.errmsg) if not deparsed: return if nodeInfo: extractInfo = deparsed.extract_node_info(nodeInfo) parentInfo = None # print extractInfo if show_ast: p = deparsed.ast if show_parent: parentInfo, p = deparsed.extract_parent_info(nodeInfo.node) self.msg(p) if extractInfo: self.rst_msg("*instruction:* %s" % (nodeInfo.node)) self.print_text(extractInfo.selectedLine) self.msg(extractInfo.markerLine) if show_parent: if not parentInfo: parentInfo, p = deparsed.extract_parent_info( nodeInfo.node) if parentInfo: self.section("Contained in...") self.rst_msg("\t*Grammar Symbol:* %s" % p.kind) self.print_text(parentInfo.selectedLine) self.msg(parentInfo.markerLine) pass pass pass elif last_i == -1: if name: self.msg("At beginning of %s " % name) elif self.core.filename(None): self.msg("At beginning of program %s" % self.core.filename(None)) else: self.msg("At beginning") else: self.errmsg( "haven't recorded info for offset %d. Offsets I know are:" % last_i) m = self.columnize_commands( list(sorted(deparsed.offsets.keys(), key=lambda x: str(x[0])))) self.msg_nocr(m) return
def code_deparse_with_fragments_and_map(*args, **kwargs): """ Like code_deparse_with_map but saves fragments. """ kwargs['walker'] = LineMapFragmentWalker return fragments.code_deparse(*args, **kwargs)