Пример #1
0
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
Пример #2
0
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
Пример #3
0
    def run(self, args):
        co = self.proc.curframe.f_code
        name = co.co_name
        last_i = self.proc.curframe.f_lasti
        if last_i == -1: last_i = 0

        sys_version = version_info[0] + (version_info[1] / 10.0)
        try:
            deparsed = deparse_code(sys_version, co, is_pypy=IS_PYPY)
            nodeInfo = deparsed_find((name, last_i), deparsed, co)
            if not nodeInfo:
                self.errmsg("Can't find exact offset %d" % last_i)
                return
        except:
            self.errmsg("error in deparsing code")
            return
        if '?' == args[0][-1]:
            extractInfo = deparsed.extract_node_info(nodeInfo.node)
        else:
            extractInfo, _ = deparsed.extract_parent_info(nodeInfo.node)
        text = extractInfo.selectedText
        text = text.strip()
        self.msg("Evaluating: %s" % text)
        try:
            self.proc.exec_line(text)
        except:
            pass
Пример #4
0
    def run(self, args):
        co = self.proc.curframe.f_code
        name = co.co_name
        last_i = self.proc.curframe.f_lasti
        if last_i == -1: last_i = 0

        sys_version = version_info[0] + (version_info[1] / 10.0)
        try:
            deparsed = deparse_code(sys_version, co, is_pypy=IS_PYPY)
            nodeInfo = deparsed_find((name, last_i), deparsed, co)
            if not nodeInfo:
                self.errmsg("Can't find exact offset %d" % last_i)
                return
        except:
            self.errmsg("error in deparsing code")
            return
        if '?' == args[0][-1]:
            extractInfo = deparsed.extract_node_info(nodeInfo.node)
        else:
            extractInfo, _ = deparsed.extract_parent_info(nodeInfo.node)
        text = extractInfo.selectedText
        text = text.strip()
        self.msg("Evaluating: %s" % text)
        try:
            self.proc.exec_line(text)
        except:
            pass
Пример #5
0
def check_expect(expect, parsed, fn_name):
    debug = False
    i = 2
    max_expect = len(expect)
    code = get_parsed_for_fn(fn_name)
    for name, offset in sorted(parsed.offsets.keys()):
        assert i + 1 <= max_expect, ("%s: ran out if items in testing node" %
                                     fn_name)
        nodeInfo = parsed.offsets[name, offset]
        node = nodeInfo.node
        nodeInfo2 = deparsed_find((name, offset), parsed, code)
        extractInfo = parsed.extract_node_info(node)

        assert expect[i] == extractInfo.selectedLine, \
          ('%s: line %s expect:\n%s\ngot:\n%s' %
               (fn_name, i, expect[i], extractInfo.selectedLine))
        assert expect[i+1] ==  extractInfo.markerLine, \
                     ('line %s expect:\n%s\ngot:\n%s' %
                     (i+1, expect[i+1], extractInfo.markerLine))
        i += 3
        if debug:
            print(node.offset)
            print(extractInfo.selectedLine)
            print(extractInfo.markerLine)

        extractInfo, p = parsed.extract_parent_info(node)
        if extractInfo:
            assert i + 1 < max_expect, "ran out of items in testing parent"
            if debug:
                print("Contained in...")
                print(extractInfo.selectedLine)
                print(extractInfo.markerLine)
            assert expect[i] == extractInfo.selectedLine, \
              ("parent line %s expect:\n%s\ngot:\n%s" %
               (i, expect[i], extractInfo.selectedLine))
            assert expect[i+1] == extractInfo.markerLine, \
              ("parent line %s expect:\n%s\ngot:\n%s" %
               (i+1, expect[i+1], extractInfo.markerLine))
            i += 3
        pass
    pass
Пример #6
0
    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

        try:
            float_version = sysinfo2float()
        except:
            self.errmsg(sys.exc_info()[1])
            return
        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 = deparse_code(float_version, co, is_pypy=IS_PYPY)
            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

        try:
            deparsed = deparse_code(float_version, co, is_pypy=IS_PYPY)
            nodeInfo = deparsed_find((name, last_i), deparsed, co)
            if not nodeInfo:
                self.errmsg(
                    "Can't find exact offset %d; giving inexact results" %
                    last_i)
                deparsed = deparse_code_around_offset(co.co_name,
                                                      last_i,
                                                      float_version,
                                                      co,
                                                      is_pypy=IS_PYPY)
        except:
            self.errmsg(sys.exc_info()[1])
            self.errmsg("error in deparsing code at offset %d" % last_i)
            return
        if not nodeInfo:
            nodeInfo = deparsed_find((name, last_i), deparsed, co)
        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