How to use the bytecode.Bytecode function in bytecode

To help you get started, we’ve selected a few bytecode examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vstinner / bytecode / test_peephole_opt.py View on Github external
def test_bin_op_dont_optimize(self):
        # 1 / 0
        code = Bytecode([Instr('LOAD_CONST', 1),
                         Instr('LOAD_CONST', 0),
                         Instr('BINARY_TRUE_DIVIDE'),
                         Instr('POP_TOP'),
                         Instr('LOAD_CONST', None),
                         Instr('RETURN_VALUE')])
        self.check_dont_optimize(code)

        # 1 // 0
        code = Bytecode([Instr('LOAD_CONST', 1),
                         Instr('LOAD_CONST', 0),
                         Instr('BINARY_FLOOR_DIVIDE'),
                         Instr('POP_TOP'),
                         Instr('LOAD_CONST', None),
                         Instr('RETURN_VALUE')])
        self.check_dont_optimize(code)
github vstinner / bytecode / test_peephole_opt.py View on Github external
def test_compare_op_unary_not(self):
        # FIXME: use constants, not hardcoded values
        for op, not_op in (
            (6, 7), # in => not in
            (7, 6), # not in => in
            (8, 9), # is => is not
            (9, 8),
        ):
            code = Bytecode([Instr('LOAD_NAME', 'a'),
                             Instr('LOAD_NAME', 'b'),
                             Instr('COMPARE_OP', op),
                             Instr('UNARY_NOT'),
                             Instr('STORE_NAME', 'x')])
            self.check(code,
                       Instr('LOAD_NAME', 'a'),
                       Instr('LOAD_NAME', 'b'),
                       Instr('COMPARE_OP', not_op),
                       Instr('STORE_NAME', 'x'))

        # don't optimize:
        # x = not (a and b is True)
        label_instr5 = Label()
        code = Bytecode([Instr('LOAD_NAME', 'a'),
                         Instr('JUMP_IF_FALSE_OR_POP', label_instr5),
                         Instr('LOAD_NAME', 'b'),
github nucleic / enaml / enaml / core / operators.py View on Github external
Parameters
    ----------
    code : CodeType
        The code object created by the Enaml compiler.

    f_globals : dict
        The global scope for the returned function.

    Returns
    -------
    result : FunctionType
        A new function with optimized local variable access.

    """
    bc_code = bc.Bytecode.from_code(code)
    optimize_locals(bc_code)
    bc_code.flags ^= (bc_code.flags & bc.CompilerFlags.NEWLOCALS)
    new_code = bc_code.to_code()
    return FunctionType(new_code, f_globals)
github Xython / YAPyPy / yapypy / utils / namedlist.py View on Github external
def get_func_from_code(code_object, fn_name):
    executor_code = Bytecode()

    executor_code.append(Instr('LOAD_CONST', code_object))
    executor_code.append(Instr('LOAD_CONST', fn_name))
    executor_code.append(Instr('MAKE_FUNCTION', 0))
    executor_code.append(Instr('RETURN_VALUE'))

    executor_code.flags = CompilerFlags.OPTIMIZED | CompilerFlags.NEWLOCALS | CompilerFlags.NOFREE
    return eval(executor_code.to_code())
github vstinner / bytecode / bytecode / cfg.py View on Github external
labels[id(block)] = new_label
                instructions.append(new_label)

            for instr in block:
                # don't copy SetLineno objects
                if isinstance(instr, (Instr, ConcreteInstr)):
                    instr = instr.copy()
                    if isinstance(instr.arg, BasicBlock):
                        jumps.append(instr)
                instructions.append(instr)

        # Map to new labels
        for instr in jumps:
            instr.arg = labels[id(instr.arg)]

        bytecode = _bytecode.Bytecode()
        bytecode._copy_attr_from(self)
        bytecode.argnames = list(self.argnames)
        bytecode[:] = instructions

        return bytecode
github danilobellini / pyscanprev / pyscanprev.py View on Github external
def _enable_scan_single_bytecode(code, name):
    """
    Part of the ``_enable_scan`` that applies the scan behavior on a single
    given list/set comprehension or generator expression code.
    """
    bc = bytecode.Bytecode.from_code(code)
    Instr = bytecode.Instr

    # Updates LOAD_GLOBAL to LOAD_FAST when arg is name
    for instr in bc:
        if isinstance(instr, Instr) \
        and instr.name == "LOAD_GLOBAL" and instr.arg == name:
            instr.set("LOAD_FAST", name)

    # Some needed information from the first/main FOR_ITER and the heading
    # "filter" part of the generator expression or list/set comprehension
    for_idx = next(idx for idx, instr in enumerate(bc)
                       if getattr(instr, "name", None) == "FOR_ITER")
    for_instr = bc[for_idx]
    begin_label_idx = for_idx - 1
    try:
        filter_last_idx = last(idx for idx, instr in enumerate(bc)
github Xython / YAPyPy / snippet.py View on Github external
if debug:
        code_obj2 = compile(code, "", "exec")
        with open('out_yapypy_bc.log', 'w') as yapypy_bc, open(
                'out_yapypy_info.log', 'w') as yapypy_info, open(
                    'out_cpy_bc.log', 'w') as cpy_bc, open(
                        'out_cpy_info.log', 'w') as cpy_info:

            dis_code(code_obj, yapypy_bc)
            show_code(code_obj, yapypy_info)
            dis_code(code_obj2, cpy_bc)
            show_code(code_obj2, cpy_info)

        print('python:')
        exec(Bytecode.from_code(code_obj2).to_code(), ctx or {})
        print('yapypy')
        exec(Bytecode.from_code(code_obj).to_code(), ctx or {})

    else:
        exec(code_obj, ctx)