python类dis()的实例源码

recipe-574437.py 文件源码 项目:code 作者: ActiveState 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def compile_py3k(source, filename, mode, *args, **kwds):
    """
py3k example:
import py3to2; from py3to2 import print_py3k
src = "a, b, *c = 1, 2, 3, 4, 5; print('a =', a); print('b =', b); print('c =', c)"
code_object = py3to2.compile_py3k(src, '', 'exec')
exec(code_object, globals())

"""
    s = "x = compile(%s, %s, %s, *%s, **%s); x = codetree(x).py3to2()"%tuple(repr(x) for x in (source, filename, mode, args, kwds))
    server.input(s)
    x = server.get('x')
    x = eval(x)
    x = x.compile()
    if 1: dis.dis(x)
    return x
owner.py 文件源码 项目:lagbot 作者: mikevb1 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def get_env(ctx):
    return dict(
        print=print_,
        timeit=timeit,
        dis=dis,
        discord=discord,
        bot=ctx.bot,
        client=ctx.bot,
        ctx=ctx,
        con=ctx.con,
        msg=ctx.message,
        message=ctx.message,
        guild=ctx.guild,
        server=ctx.guild,
        channel=ctx.channel,
        me=ctx.me
    )
vm.py 文件源码 项目:PyVyM 作者: ssarangi 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def get_opcode(self):
        # Based on the settings decide to show the line-by-line trace
        # Get the current line being executed

        ip = self.__ip

        op = self.__code[self.__ip]
        ip += 1
        opmethod = "execute_%s" % dis.opname[op]

        oparg = None
        if op >= dis.HAVE_ARGUMENT:
            low = self.__code[ip]
            high = self.__code[ip + 1]
            oparg = (high << 8) | low

        return opmethod, oparg
test_dis.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def do_disassembly_test(self, func, expected):
        s = io.StringIO()
        save_stdout = sys.stdout
        sys.stdout = s
        dis.dis(func)
        sys.stdout = save_stdout
        got = s.getvalue()
        # Trim trailing blanks (if any).
        lines = got.split('\n')
        lines = [line.rstrip() for line in lines]
        expected = expected.split("\n")
        import difflib
        if expected != lines:
            self.fail(
                "events did not match expectation:\n" +
                "\n".join(difflib.ndiff(expected,
                                        lines)))
test_dis.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def do_disassembly_test(self, func, expected):
        s = StringIO.StringIO()
        save_stdout = sys.stdout
        sys.stdout = s
        dis.dis(func)
        sys.stdout = save_stdout
        got = s.getvalue()
        # Trim trailing blanks (if any).
        lines = got.split('\n')
        lines = [line.rstrip() for line in lines]
        expected = expected.split("\n")
        import difflib
        if expected != lines:
            self.fail(
                "events did not match expectation:\n" +
                "\n".join(difflib.ndiff(expected,
                                        lines)))
test_dis.py 文件源码 项目:python2-tracer 作者: extremecoders-re 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def do_disassembly_test(self, func, expected):
        s = StringIO.StringIO()
        save_stdout = sys.stdout
        sys.stdout = s
        dis.dis(func)
        sys.stdout = save_stdout
        got = s.getvalue()
        # Trim trailing blanks (if any).
        lines = got.split('\n')
        lines = [line.rstrip() for line in lines]
        expected = expected.split("\n")
        import difflib
        if expected != lines:
            self.fail(
                "events did not match expectation:\n" +
                "\n".join(difflib.ndiff(expected,
                                        lines)))
test_dis.py 文件源码 项目:pefile.pypy 作者: cloudtracer 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def do_disassembly_test(self, func, expected):
        s = StringIO.StringIO()
        save_stdout = sys.stdout
        sys.stdout = s
        dis.dis(func)
        sys.stdout = save_stdout
        got = s.getvalue()
        # Trim trailing blanks (if any).
        lines = got.split('\n')
        lines = [line.rstrip() for line in lines]
        expected = expected.split("\n")
        import difflib
        if expected != lines:
            self.fail(
                "events did not match expectation:\n" +
                "\n".join(difflib.ndiff(expected,
                                        lines)))
test_dis.py 文件源码 项目:ndk-python 作者: gittor 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def do_disassembly_test(self, func, expected):
        s = StringIO.StringIO()
        save_stdout = sys.stdout
        sys.stdout = s
        dis.dis(func)
        sys.stdout = save_stdout
        got = s.getvalue()
        # Trim trailing blanks (if any).
        lines = got.split('\n')
        lines = [line.rstrip() for line in lines]
        expected = expected.split("\n")
        import difflib
        if expected != lines:
            self.fail(
                "events did not match expectation:\n" +
                "\n".join(difflib.ndiff(expected,
                                        lines)))
stacks.py 文件源码 项目:networking 作者: RussianOtter 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def func_bytes(fl):
    import console, dis
    """
    This function reads a script and
    prints it's memory address.
    """
    f = open(fl).readlines()
    for line in f:
        try:
            co = compile(line,"<none>","exec")
            t = str(str(co).split("at ")[1]).split(",")[0]
            print t, co.consts
        except:
            pass
    console.set_font("Menlo",10)
    f = open(fl).readlines()
    for line in f:
        try:
            dis.dis(line)
        except:
            pass
    console.set_font()
vmtest.py 文件源码 项目:tailbiter 作者: darius 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def dis_code(code):
    """Disassemble `code` and all the code it refers to."""
    for const in code.co_consts:
        if isinstance(const, types.CodeType):
            dis_code(const)

    print("")
    print(code)
    dis.dis(code)
recipe-574437.py 文件源码 项目:code 作者: ActiveState 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def test():
    global xxx
    if 1: # reflect self
      c0 = compile(TESTCODE, '', 'exec')
      t0 = codetree(c0); s = repr(t0); t0 = eval(s); c = t0.compile()
      t0 = codetree(c); s = repr(t0)
      print 'exec'; exec(c0, globals()); exec(c, globals()); print dis.dis(c)
      # compile, expand, recompile, exec
      if PYVERSION == 'py2x': print s
      if 0: print s
      else:
        t = t0.py3to2() if hasattr(t0, 'py3to2') else t0; xxx = t; s = repr(t)
recipe-574437.py 文件源码 项目:code 作者: ActiveState 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def test():
    self = _server
    global xxx
    if 1: # server init
      self.restart()
      self.input("1, 2;3")
    if 0: # py3k codetree
        server.input("codetree.test()")
        if 1: # 3to2
          s = server.get('xxx')
          t = eval(s)
          c = t.compile()
          print dis.dis(c)
          exec(c, globals())
recipe-574436.py 文件源码 项目:code 作者: ActiveState 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def test():
    global xxx
    if 1: # reflect self
      c0 = compile(TESTCODE, '', 'exec')
      t0 = codetree(c0); s = repr(t0); t0 = eval(s); c = t0.compile()
      t0 = codetree(c); s = repr(t0)
      print 'exec'; exec(c0, globals()); exec(c, globals()); print dis.dis(c)
      # compile, expand, recompile, exec
      if PYVERSION == 'py2x': print s
      if 0: print s
      else:
        t = t0.py3to2() if hasattr(t0, 'py3to2') else t0; xxx = t; s = repr(t)
recipe-574436.py 文件源码 项目:code 作者: ActiveState 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def test():
    self = _server
    global xxx
    if 1: # server init
      self.restart()
      self.input("1, 2;3")
    if 0: # py3k codetree
        server.input("codetree.test()")
        if 1: # 3to2
          s = server.get('xxx')
          t = eval(s)
          c = t.compile()
          print dis.dis(c)
          exec(c, globals())
vm.py 文件源码 项目:PyVyM 作者: ssarangi 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def execute_MAKE_FUNCTION(self, argc):
        """
        Pushes a new function object on the stack. From bottom to top, the consumed stack must consist of argc & 0xFF default argument
        objects in positional order (argc >> 8) & 0xFF pairs of name and default argument, with the name just below the object on the
        stack, for keyword-only parameters (argc >> 16) & 0x7FFF parameter annotation objects a tuple listing the parameter names for the
        annotations (only if there are ony annotation objects) the code associated with the function (at TOS1) the qualified name of the
        function (at TOS)
        """
        num_default_args = argc & 0xFF
        num_kw_args = (argc >> 8) & 0xFF

        name = self.__stack.pop()

        name = name.replace(self.__class_name + ".", "")
        special_func = False
        if is_special_func(name):
            special_func = True

        code = self.__stack.pop()
        defaults = self.popn(num_default_args)

        fn = Function(name, defaults)
        fn.code = code
        if special_func == True:
            self.__klass.add_special_func(fn)
        else:
            self.__klass.add_normal_func(fn)
        self.__vm_state = VMState.BUILD_FUNC

        if self.__config.show_disassembly:
            draw_header("FUNCTION CODE: %s" % name)
            dis.dis(code)
vm.py 文件源码 项目:PyVyM 作者: ssarangi 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def get_opcode(self):
        # Based on the settings decide to show the line-by-line trace
        # Get the current line being executed
        current_lineno = self.__exec_frame.line_no_obj.line_number(self.__exec_frame.ip)

        ip = self.__exec_frame.ip
            # Update the line number only if the currently executing line has changed.
        if self.__exec_frame.line_no_obj.currently_executing_line is None or \
            self.__exec_frame.line_no_obj.currently_executing_line != current_lineno:
            self.__exec_frame.line_no_obj.currently_executing_line = current_lineno
            self.__exec_frame.line_no_obj.currently_executing_line = current_lineno

        if self.__config.show_line_execution:
            current_line = self.__exec_frame.line_no_obj.get_source_line(current_lineno)
            print("Execution Line: %s" % current_line)

        op = self.__exec_frame.program[self.__exec_frame.ip]
        ip += 1
        opmethod = "execute_%s" % dis.opname[op]

        oparg = None
        if op >= dis.HAVE_ARGUMENT:
            low = self.__exec_frame.program[ip]
            high = self.__exec_frame.program[ip + 1]
            oparg = (high << 8) | low

        return opmethod, oparg, current_lineno
vm.py 文件源码 项目:PyVyM 作者: ssarangi 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def execute_MAKE_FUNCTION(self, argc):
        """
        Pushes a new function object on the stack. From bottom to top, the consumed stack must consist of argc & 0xFF default argument
        objects in positional order (argc >> 8) & 0xFF pairs of name and default argument, with the name just below the object on the
        stack, for keyword-only parameters (argc >> 16) & 0x7FFF parameter annotation objects a tuple listing the parameter names for the
        annotations (only if there are ony annotation objects) the code associated with the function (at TOS1) the qualified name of the
        function (at TOS)
        """
        num_default_args = argc & 0xFF
        num_kw_args = (argc >> 8) & 0xFF

        name = self.__exec_frame.pop()
        code = self.__exec_frame.pop()
        defaults = self.__exec_frame.popn(num_default_args)

        if self.__BUILD_CLASS_STATE == True:
            build_class = BuildClass(name, code, self.__config, self.__module)
            self.__exec_frame.append(build_class)
            self.__BUILD_CLASS_STATE = False
        else:
            fn = Function(name, defaults)
            fn.code = code
            self.__exec_frame.add_global(name, fn)
            self.__exec_frame.vm_state = VMState.BUILD_FUNC

        if self.__config.show_disassembly:
            draw_header("FUNCTION CODE: %s" % name)
            dis.dis(code)
main.py 文件源码 项目:PyVyM 作者: ssarangi 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def main():
    filename = sys.argv[1]
    fptr = open(filename, "r")
    source = fptr.read()
    fptr.seek(0)
    source_lines = format_source_lines(fptr.readlines())
    fptr.close()

    draw_header("Source")
    display_source(source_lines)
    code = compile(source, filename, "exec")

    vm = BytecodeVM(code, source_lines, filename)

    WITH_DEBUGGER = False

    if not WITH_DEBUGGER:
        draw_header("Disassembly")
        dis.dis(code)
        #  Configure the VM and set the settings based on command line. For now use defaults
        config = configure_vm()
        config.show_disassembly = True
        vm.config = config
        vm.execute()
    else:
        debugger = Debugger(code, source_lines, filename)
        debugger.execute(False)
test_peepholer.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def disassemble(func):
    f = StringIO()
    tmp = sys.stdout
    sys.stdout = f
    dis.dis(func)
    sys.stdout = tmp
    result = f.getvalue()
    f.close()
    return result
test_dis.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_opmap(self):
        self.assertEqual(dis.opmap["STOP_CODE"], 0)
        self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
        self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
test_dis.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_opname(self):
        self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
test_dis.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_boundaries(self):
        self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
        self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
test_dis.py 文件源码 项目:zippy 作者: securesystemslab 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_show_code(self):
        self.maxDiff = 1000
        for x, expected in self.test_pairs:
            with captured_stdout() as output:
                dis.show_code(x)
            self.assertRegex(output.getvalue(), expected+"\n")
disassembler.py 文件源码 项目:unravelr 作者: msarfati 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('payload', type=str, help='Payload as code, to be analyzed by Unravelr.')
        args = parser.parse_args()

        try:
            Binary = compile(args['payload'], "<string>", "exec")
            result = StringIO()
            with captureStdOut(result):
                dis.dis(Binary)
            # import ipdb; ipdb.set_trace()
            return jsonify(dict(result=result.getvalue()))
        except:
            abort(404)
vmtest.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def dis_code(code):
    """Disassemble `code` and all the code it refers to."""
    for const in code.co_consts:
        if isinstance(const, types.CodeType):
            dis_code(const)

    print("")
    print(code)
    dis.dis(code)
stacktest.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def compare(a, b):
    if not (a.co_name == "?" or a.co_name.startswith('<lambda')):
        assert a.co_name == b.co_name, (a, b)
    if a.co_stacksize != b.co_stacksize:
        print("stack mismatch %s: %d vs. %d" % (a.co_name,
                                                a.co_stacksize,
                                                b.co_stacksize))
        if a.co_stacksize > b.co_stacksize:
            print("good code")
            dis.dis(a)
            print("bad code")
            dis.dis(b)
            assert 0
dumppyc.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def walk(co, match=None):
    if match is None or co.co_name == match:
        dump(co)
        print()
        dis.dis(co)
    for obj in co.co_consts:
        if type(obj) == types.CodeType:
            walk(obj, match)
test_peepholer.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def disassemble(func):
    f = StringIO()
    tmp = sys.stdout
    sys.stdout = f
    dis.dis(func)
    sys.stdout = tmp
    result = f.getvalue()
    f.close()
    return result
test_dis.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_opname(self):
        self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
test_dis.py 文件源码 项目:oil 作者: oilshell 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_boundaries(self):
        self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
        self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)


问题


面经


文章

微信
公众号

扫码关注公众号