当前位置: 首页>>代码示例>>Python>>正文


Python dis.Bytecode方法代码示例

本文整理汇总了Python中dis.Bytecode方法的典型用法代码示例。如果您正苦于以下问题:Python dis.Bytecode方法的具体用法?Python dis.Bytecode怎么用?Python dis.Bytecode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dis的用法示例。


在下文中一共展示了dis.Bytecode方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: scan_opcodes_34

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def scan_opcodes_34(self, co):
        i = 0
        bytecode = list(dis.Bytecode(co))
        while i < len(bytecode):
            if (
                bytecode[i].opname == "LOAD_CONST" and
                bytecode[i + 1].opname == "LOAD_CONST" and
                bytecode[i + 2].opname == "IMPORT_NAME"
            ):
                level = bytecode[i].argval
                fromlist = bytecode[i + 1].argval
                import_name = bytecode[i + 2].argval
                if level == 0:
                    yield "absolute_import", (fromlist, import_name)
                else:
                    yield "relative_import", (level, fromlist, import_name)
                i += 2
            i += 1 
开发者ID:thebjorn,项目名称:pydeps,代码行数:20,代码来源:mf27.py

示例2: test_async

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_async():
    # Write a coroutine, add2, that accepts two parameters,
    # adds them, then calls ``asyncio.sleep(0)``.
    # Finally it returns the sum.
    # ************************************
    res = await add2(2, 3)
    assert res == 5
    code = dis.Bytecode(add2)
    assert 'sleep' in code.dis()

    # Write a coroutine, avg, that accepts two parameters,
    # ``coroutines`` and ``size``.
    # It loops over coroutines and
    # gets values from them. When
    # it has pulled out size values,
    # it returns the average of those
    # values.
    # (Make sure you put an await call in it)
    # ************************************

    res3 = await avg([add2(1, 3),
                     add2(1, 4),
                     add2(1, 6),
                     ], 2)
    assert res3 == 4.5 
开发者ID:talkpython,项目名称:illustrated-python-3-course,代码行数:27,代码来源:async_test.py

示例3: _iter_instructions

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def _iter_instructions(co):
    if sys.version_info[0] < 3:
        iter_in = _iter_as_bytecode_as_instructions_py2(co)
    else:
        iter_in = dis.Bytecode(co)
    iter_in = list(iter_in)

    bytecode_to_instruction = {}
    for instruction in iter_in:
        bytecode_to_instruction[instruction.offset] = instruction

    if iter_in:
        for instruction in iter_in:
            yield instruction 
开发者ID:fabioz,项目名称:PyDev.Debugger,代码行数:16,代码来源:pydevd_collect_bytecode_info.py

示例4: debug_test_iter_bytecode

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def debug_test_iter_bytecode(data_regression):
    # Note: not run by default, only to help visualizing bytecode and comparing differences among versions.
    import dis

    basename = 'test_iter_bytecode.py%s%s' % (sys.version_info[:2])
    method_to_info = {}
    for key, method in sorted(dict(globals()).items()):
        if key.startswith('_method'):
            info = []

            if sys.version_info[0] < 3:
                from _pydevd_bundle.pydevd_collect_bytecode_info import _iter_as_bytecode_as_instructions_py2
                iter_in = _iter_as_bytecode_as_instructions_py2(method.__code__)
            else:
                iter_in = dis.Bytecode(method)

            for instruction in iter_in:
                info.append(_create_entry(instruction))

            msg = []
            for d in info:
                line = []
                for k, v in sorted(d.items()):
                    line.append(u'%s=%s' % (k, v))
                msg.append(u'   '.join(line))
            if isinstance(key, bytes):
                key = key.decode('utf-8')
            method_to_info[key] = msg

    data_regression.check(method_to_info, basename=basename) 
开发者ID:fabioz,项目名称:PyDev.Debugger,代码行数:32,代码来源:test_collect_bytecode_info.py

示例5: test_instantiation

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_instantiation(self):
        # Test with function, method, code string and code object
        for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
            with self.subTest(obj=obj):
                b = dis.Bytecode(obj)
                self.assertIsInstance(b.codeobj, types.CodeType)

        self.assertRaises(TypeError, dis.Bytecode, object()) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:10,代码来源:test_dis.py

示例6: test_iteration

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_iteration(self):
        for obj in [_f, _C(1).__init__, "a=1", _f.__code__]:
            with self.subTest(obj=obj):
                via_object = list(dis.Bytecode(obj))
                via_generator = list(dis.get_instructions(obj))
                self.assertEqual(via_object, via_generator) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:8,代码来源:test_dis.py

示例7: test_explicit_first_line

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_explicit_first_line(self):
        actual = dis.Bytecode(outer, first_line=expected_outer_line)
        self.assertEqual(list(actual), expected_opinfo_outer) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:5,代码来源:test_dis.py

示例8: test_source_line_in_disassembly

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_source_line_in_disassembly(self):
        # Use the line in the source code
        actual = dis.Bytecode(simple).dis()[:3]
        expected = "{:>3}".format(simple.__code__.co_firstlineno)
        self.assertEqual(actual, expected)
        # Use an explicit first line number
        actual = dis.Bytecode(simple, first_line=350).dis()[:3]
        self.assertEqual(actual, "350") 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:10,代码来源:test_dis.py

示例9: test_info

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_info(self):
        self.maxDiff = 1000
        for x, expected in CodeInfoTests.test_pairs:
            b = dis.Bytecode(x)
            self.assertRegex(b.info(), expected) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:7,代码来源:test_dis.py

示例10: test_from_traceback

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_from_traceback(self):
        tb = get_tb()
        b = dis.Bytecode.from_traceback(tb)
        while tb.tb_next: tb = tb.tb_next

        self.assertEqual(b.current_offset, tb.tb_lasti) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:8,代码来源:test_dis.py

示例11: test_from_traceback_dis

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def test_from_traceback_dis(self):
        tb = get_tb()
        b = dis.Bytecode.from_traceback(tb)
        self.assertEqual(b.dis(), dis_traceback) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:6,代码来源:test_dis.py

示例12: instructions

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def instructions(code_obj):
    # easy for python 3.4+
    if sys.version_info >= (3, 4):
        for inst in dis.Bytecode(code_obj):
            yield inst
    else:
        # otherwise we have to manually parse
        code = code_obj.co_code
        NewInstruction = namedtuple('Instruction', ('opcode', 'arg'))
        if six.PY2:
            code = map(ord, code)
        i, L = 0, len(code)
        extended_arg = 0
        while i < L:
            op = code[i]
            i += 1
            if op < opcode.HAVE_ARGUMENT:
                yield NewInstruction(op, None)
                continue
            oparg = code[i] + (code[i + 1] << 8) + extended_arg
            extended_arg = 0
            i += 2
            if op == opcode.EXTENDED_ARG:
                extended_arg = oparg << 16
                continue
            yield NewInstruction(op, oparg) 
开发者ID:alfa-addon,项目名称:addon,代码行数:28,代码来源:injector.py

示例13: instructions

# 需要导入模块: import dis [as 别名]
# 或者: from dis import Bytecode [as 别名]
def instructions(code_obj):
    # easy for python 3.4+
    if sys.version_info >= (3, 4):
        for inst in dis.Bytecode(code_obj):
            yield inst
    else:
        # otherwise we have to manually parse
        code = code_obj.co_code
        NewInstruction = namedtuple('Instruction', ('opcode', 'arg'))
        if six.PY2:
            code = map(ord, code)
        i, L = 0, len(code)
        extended_arg = 0
        while i < L:
            op = code[i]
            i+= 1
            if op < opcode.HAVE_ARGUMENT:
                yield NewInstruction(op, None)
                continue
            oparg = code[i] + (code[i+1] << 8) + extended_arg
            extended_arg = 0
            i += 2
            if op == opcode.EXTENDED_ARG:
                extended_arg = oparg << 16
                continue
            yield NewInstruction(op, oparg) 
开发者ID:taxigps,项目名称:xbmc-addons-chinese,代码行数:28,代码来源:injector.py


注:本文中的dis.Bytecode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。