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


Python dis._unpack_opargs方法代码示例

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


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

示例1: scan_opcodes

# 需要导入模块: import dis [as 别名]
# 或者: from dis import _unpack_opargs [as 别名]
def scan_opcodes(self, co):
        # Scan the code, and yield 'interesting' opcode combinations
        code = co.co_code
        names = co.co_names
        consts = co.co_consts
        opargs = [(op, arg) for _, op, arg in dis._unpack_opargs(code)
                  if op != EXTENDED_ARG]
        for i, (op, oparg) in enumerate(opargs):
            if op in STORE_OPS:
                yield "store", (names[oparg],)
                continue
            if (op == IMPORT_NAME and i >= 2
                    and opargs[i-1][0] == opargs[i-2][0] == LOAD_CONST):
                level = consts[opargs[i-2][1]]
                fromlist = consts[opargs[i-1][1]]
                if level == 0: # absolute import
                    yield "absolute_import", (fromlist, names[oparg])
                else: # relative import
                    yield "relative_import", (level, fromlist, names[oparg])
                continue 
开发者ID:CedricGuillemet,项目名称:Imogen,代码行数:22,代码来源:modulefinder.py

示例2: scan_opcodes_25

# 需要导入模块: import dis [as 别名]
# 或者: from dis import _unpack_opargs [as 别名]
def scan_opcodes_25(self, co,
                     unpack = struct.unpack):
        # Scan the code, and yield 'interesting' opcode combinations
        code = co.co_code
        names = co.co_names
        consts = co.co_consts
        opargs = [(op, arg) for _, op, arg in dis._unpack_opargs(code)
                  if op != EXTENDED_ARG]
        for i, (op, oparg) in enumerate(opargs):
            if op in STORE_OPS:
                yield "store", (names[oparg],)
                continue
            if (op == IMPORT_NAME and i >= 2
                    and opargs[i-1][0] == opargs[i-2][0] == LOAD_CONST):
                level = consts[opargs[i-2][1]]
                fromlist = consts[opargs[i-1][1]]
                if level == 0: # absolute import
                    yield "absolute_import", (fromlist, names[oparg])
                else: # relative import
                    yield "relative_import", (level, fromlist, names[oparg])
                continue 
开发者ID:ShikyoKira,项目名称:Project-New-Reign---Nemesis-Main,代码行数:23,代码来源:modulefinder.py

示例3: _UnpackOpArgs

# 需要导入模块: import dis [as 别名]
# 或者: from dis import _unpack_opargs [as 别名]
def _UnpackOpArgs(self, code):
        """Unpack the operations and arguments from the byte code. From Python
           3.5 onwards this is found in the private method _unpack_opargs
           but for earlier releases this wasn't available as a separate
           method."""
        opIndex = 0
        numOps = len(code)
        is3 = sys.version_info[0] >= 3
        while opIndex < numOps:
            offset = opIndex
            if is3:
                op = code[opIndex]
            else:
                op = ord(code[opIndex])
            opIndex += 1
            arg = None
            if op >= dis.HAVE_ARGUMENT:
                if is3:
                    arg = code[opIndex] + code[opIndex + 1] * 256
                else:
                    arg = ord(code[opIndex]) + ord(code[opIndex + 1]) * 256
                opIndex += 2
            yield (offset, op, arg) 
开发者ID:tensorlang,项目名称:tensorlang,代码行数:25,代码来源:finder.py

示例4: compare_bytes_sequence

# 需要导入模块: import dis [as 别名]
# 或者: from dis import _unpack_opargs [as 别名]
def compare_bytes_sequence(self, code1, code2, inserted_code_size):
        """
        Compare code after modification and the real code
        Since we add POP_JUMP_IF_TRUE instruction, we can't compare modified code and the real code. That's why we
        allow some inaccuracies while code comparison
        :param code1: result code after modification
        :param code2: a real code for checking
        :param inserted_code_size: size of inserted code
        """
        seq1 = [(offset, op, arg) for offset, op, arg in dis._unpack_opargs(code1)]
        seq2 = [(offset, op, arg) for offset, op, arg in dis._unpack_opargs(code2)]
        assert len(seq1) == len(seq2), "Bytes sequences have different lengths %s != %s" % (len(seq1), len(seq2))
        for i in range(len(seq1)):
            of, op1, arg1 = seq1[i]
            _, op2, arg2 = seq2[i]
            if op1 != op2:
                if op1 == 115 and op2 == 1:
                    # it's ok, because we added POP_JUMP_IF_TRUE manually, but it's POP_TOP in the real code
                    # inserted code - 2 (removed return instruction) - real code inserted
                    # Jump should be done to the beginning of inserted fragment
                    self.assertEqual(arg1, of - (inserted_code_size - 2))
                    continue
                elif op1 == EXTENDED_ARG and op2 == 12:
                    # we added a real UNARY_NOT to balance EXTENDED_ARG added by new jump instruction
                    # i.e. inserted code size was increased as well
                    inserted_code_size += 2
                    continue

            self.assertEqual(op1, op2, "Different operators at offset {}".format(of))
            if arg1 != arg2:
                if op1 in (100, 101, 106, 116):
                    # Sometimes indexes of variable names and consts may be different, when we insert them, it's ok
                    continue
                else:
                    self.assertEqual(arg1, arg2, "Different arguments at offset {}".format(of)) 
开发者ID:fabioz,项目名称:PyDev.Debugger,代码行数:37,代码来源:test_bytecode_modification.py

示例5: iter_opcodes

# 需要导入模块: import dis [as 别名]
# 或者: from dis import _unpack_opargs [as 别名]
def iter_opcodes(code):
    """Iterate over (op, arg) parameters in the bytecode of code.

    Taken from the code of the dis module.

    """
    try:
        unpack_opargs = dis._unpack_opargs
    except AttributeError:
        # In Python (< 3.5) we have to parse the bytecode ourselves
        if PY2:
            ord_ = ord
        else:
            def ord_(c):
                return c
        n = len(code)
        i = 0
        extended_arg = 0
        while i < n:
            op = ord_(code[i])
            i = i + 1
            if op >= dis.HAVE_ARGUMENT:
                oparg = ord_(code[i]) + ord_(code[i + 1]) * 256 + extended_arg
                extended_arg = 0
                i = i + 2
                if op == dis.EXTENDED_ARG:
                    extended_arg = oparg * long(65536)
                else:
                    yield op, oparg
    else:
        # More recent Python 3 has a function for this, though it is
        # not a public API.
        for _, op, arg in unpack_opargs(code):
            yield (op, arg) 
开发者ID:lordmauve,项目名称:chopsticks,代码行数:36,代码来源:serialise_main.py

示例6: _ScanCode

# 需要导入模块: import dis [as 别名]
# 或者: from dis import _unpack_opargs [as 别名]
def _ScanCode(self, co, module, deferredImports, topLevel = True):
        """Scan code, looking for imported modules and keeping track of the
           constants that have been created in order to better tell which
           modules are truly missing."""
        arguments = []
        importedModule = None
        method = dis._unpack_opargs if sys.version_info[:2] >= (3, 5) \
                else self._UnpackOpArgs
        for opIndex, op, opArg in method(co.co_code):

            # keep track of constants (these are used for importing)
            # immediately restart loop so arguments are retained
            if op == LOAD_CONST:
                arguments.append(co.co_consts[opArg])
                continue

            # import statement: attempt to import module
            elif op == IMPORT_NAME:
                name = co.co_names[opArg]
                if len(arguments) >= 2:
                    relativeImportIndex, fromList = arguments[-2:]
                else:
                    relativeImportIndex = -1
                    fromList, = arguments
                if name not in module.excludeNames:
                    importedModule = self._ImportModule(name, deferredImports,
                            module, relativeImportIndex)
                    if importedModule is not None:
                        if fromList and fromList != ("*",) \
                                and importedModule.path is not None:
                            self._EnsureFromList(module, importedModule,
                                    fromList, deferredImports)

            # import * statement: copy all global names
            elif op == IMPORT_STAR and topLevel and importedModule is not None:
                module.globalNames.update(importedModule.globalNames)

            # store operation: track only top level
            elif topLevel and op in STORE_OPS:
                name = co.co_names[opArg]
                module.globalNames[name] = None

            # reset arguments; these are only needed for import statements so
            # ignore them in all other cases!
            arguments = []

        # Scan the code objects from function & class definitions
        for constant in co.co_consts:
            if isinstance(constant, type(co)):
                self._ScanCode(constant, module, deferredImports,
                        topLevel = False) 
开发者ID:tensorlang,项目名称:tensorlang,代码行数:53,代码来源:finder.py


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