Beispiel #1
0
 def load_code(self):
     argcount = _r_long(self)
     nlocals = _r_long(self)
     stacksize = _r_long(self)
     flags = _r_long(self)
     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 = _r_long(self)
     lnotab = self.load()
     if PYTHON3:
         return Code2(
             argcount,
             0,
             nlocals,
             stacksize,
             flags,
             code,
             consts,
             names,
             varnames,
             filename.decode(),
             name,
             firstlineno,
             lnotab,
             freevars,
             cellvars,
         )
     else:
         return types.CodeType(
             argcount,
             nlocals,
             stacksize,
             flags,
             code,
             consts,
             names,
             varnames,
             filename,
             name,
             firstlineno,
             lnotab,
             freevars,
             cellvars,
         )
Beispiel #2
0
 def load_code(self):
     argcount = self.r_long()
     if self.python_version and self.python_version >= '3.0':
         is_python3 = True
         kwonlyargcount = self.r_long()
     else:
         is_python3 = False
     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.load()
     if is_python3:
         if PYTHON3:
             return types.CodeType(argcount, kwonlyargcount, nlocals,
                                   stacksize, flags, code, consts, names,
                                   varnames, filename, name, firstlineno,
                                   lnotab, freevars, cellvars)
         else:
             return Code3(argcount, kwonlyargcount, nlocals, stacksize,
                          flags, code, consts, names, varnames, filename,
                          name, firstlineno, lnotab, freevars, cellvars)
     else:
         if PYTHON3:
             return Code2(argcount, nlocals, stacksize, flags, code, consts,
                          names, varnames, filename, name, firstlineno,
                          lnotab, freevars, cellvars)
         else:
             return types.CodeType(argcount, nlocals, stacksize, flags,
                                   code, consts, names, varnames, filename,
                                   name, firstlineno, lnotab, freevars,
                                   cellvars)
Beispiel #3
0
def load_code_type(fp, magic_int, bytes_for_s=False, code_objects={}):
    # FIXME: use tables to simplify this?
    # Python [1.0 .. 2.2)
    v10_to_12 = magic_int in (39170, 39171)

    # FIXME: find out what magics were for 1.3
    v13_to_22 = magic_int in (11913, 5892, 20121, 50428, 50823, 60202, 60717)
    v11_to_14 = magic_int in (39170, 39171, 11913, 5892)

    # Python [1.5 .. 2.2)
    v15_to_22 = magic_int in (20121, 50428, 50823, 60202, 60717)
    v13_to_20 = magic_int in (11913, 5892, 20121, 50428, 50823)
    v21_to_27 = not v13_to_20 and 60202 <= magic_int <= 63000

    if v13_to_22:
        co_argcount = unpack("<h", fp.read(2))[0]
    elif v10_to_12:
        co_argcount = 0
    else:
        co_argcount = unpack("<i", fp.read(4))[0]

    if magic_int in (3412, 3413):
        co_posonlyargcount = unpack("<i", fp.read(4))[0]
    else:
        co_posonlyargcount = None

    if ((3020 < magic_int < 20121) or magic_int in (160, )) and not v11_to_14:
        kwonlyargcount = unpack("<i", fp.read(4))[0]
    else:
        kwonlyargcount = 0

    if v13_to_22:
        co_nlocals = unpack("<h", fp.read(2))[0]
    elif v10_to_12:
        co_nlocals = 0
    else:
        co_nlocals = unpack("<i", fp.read(4))[0]

    if v15_to_22:
        co_stacksize = unpack("<h", fp.read(2))[0]
    elif v11_to_14 or v10_to_12:
        co_stacksize = 0
    else:
        co_stacksize = unpack("<i", fp.read(4))[0]

    if v13_to_22:
        co_flags = unpack("<h", fp.read(2))[0]
    elif v10_to_12:
        co_flags = 0
    else:
        co_flags = unpack("<i", fp.read(4))[0]

    co_code = load_code_internal(fp,
                                 magic_int,
                                 bytes_for_s=True,
                                 code_objects=code_objects)
    co_consts = load_code_internal(fp, magic_int, code_objects=code_objects)
    co_names = load_code_internal(fp, magic_int, code_objects=code_objects)

    # FIXME: only if >= 1.3
    if v10_to_12:
        co_varnames = []
    else:
        co_varnames = load_code_internal(fp,
                                         magic_int,
                                         code_objects=code_objects)

    if not (v13_to_20 or v10_to_12):
        co_freevars = load_code_internal(fp,
                                         magic_int,
                                         code_objects=code_objects)
        co_cellvars = load_code_internal(fp,
                                         magic_int,
                                         code_objects=code_objects)
    else:
        co_freevars = tuple()
        co_cellvars = tuple()

    co_filename = load_code_internal(fp, magic_int, code_objects=code_objects)
    co_name = load_code_internal(fp, magic_int)

    if v15_to_22:
        co_firstlineno = unpack("<h", fp.read(2))[0]
    elif v11_to_14:
        # < 1.5 there is no lnotab, so no firstlineno.
        # SET_LINENO is used instead.
        co_firstlineno = -1  # Bogus sentinal value
    else:
        co_firstlineno = unpack("<i", fp.read(4))[0]

    if v11_to_14:
        # < 1.5 uses SET_LINENO only
        co_lnotab = ""
    else:
        co_lnotab = load_code_internal(fp,
                                       magic_int,
                                       code_objects=code_objects)

    # The Python3 code object is different than Python2's which
    # we are reading if we get here.
    # Also various parameters which were strings are now
    # bytes (which is probably more logical).
    if PYTHON3:
        Code = types.CodeType
        if PYTHON_MAGIC_INT > 3020:
            # Check for Python 3 interpreter reading Python 2 bytecode.
            # Python 3's code objects are bytes while Python 2's are strings.
            #
            # In later Python3 magic_ints, there is a
            # kwonlyargcount parameter which we set to 0.
            if v10_to_12 or v13_to_22 or v21_to_27:
                code = Code2(
                    co_argcount,
                    kwonlyargcount,
                    co_nlocals,
                    co_stacksize,
                    co_flags,
                    co_code,
                    co_consts,
                    co_names,
                    co_varnames,
                    co_filename,
                    co_name,
                    co_firstlineno,
                    co_lnotab,
                    co_freevars,
                    co_cellvars,
                )
            else:
                if PYTHON_MAGIC_INT in (3412, 3413):
                    if co_posonlyargcount is not None:
                        # Python3.8 to Python3.8: Ok to use native Python3.8's code type
                        code = Code(
                            co_argcount,
                            co_posonlyargcount,
                            kwonlyargcount,
                            co_nlocals,
                            co_stacksize,
                            co_flags,
                            co_code,
                            co_consts,
                            co_names,
                            co_varnames,
                            co_filename,
                            co_name,
                            co_firstlineno,
                            bytes(co_lnotab, encoding="utf-8"),
                            co_freevars,
                            co_cellvars,
                        )
                    else:
                        if not isinstance(co_lnotab, bytes):
                            co_lnotab = bytes(co_lnotab, encoding="utf-8")
                        code = Code3(
                            co_argcount,
                            kwonlyargcount,
                            co_nlocals,
                            co_stacksize,
                            co_flags,
                            co_code,
                            co_consts,
                            co_names,
                            co_varnames,
                            co_filename,
                            co_name,
                            co_firstlineno,
                            co_lnotab,
                            co_freevars,
                            co_cellvars,
                        )
                elif co_posonlyargcount is not None:
                    code = Code38(
                        co_argcount,
                        co_posonlyargcount,
                        kwonlyargcount,
                        co_nlocals,
                        co_stacksize,
                        co_flags,
                        co_code,
                        co_consts,
                        co_names,
                        co_varnames,
                        co_filename,
                        co_name,
                        co_firstlineno,
                        bytes(co_lnotab, encoding="utf-8"),
                        co_freevars,
                        co_cellvars,
                    )

                else:
                    # Python3 (< 3.8) to Python3: Ok to use native Python3's code type
                    if not isinstance(co_lnotab, bytes):
                        co_lnotab = bytes(co_lnotab, encoding="utf-8")
                    code = Code(
                        co_argcount,
                        kwonlyargcount,
                        co_nlocals,
                        co_stacksize,
                        co_flags,
                        co_code,
                        co_consts,
                        co_names,
                        co_varnames,
                        co_filename,
                        co_name,
                        co_firstlineno,
                        co_lnotab,
                        co_freevars,
                        co_cellvars,
                    )
                    pass
                pass
        else:
            code = Code(
                co_argcount,
                kwonlyargcount,
                co_nlocals,
                co_stacksize,
                co_flags,
                co_code,
                tuple(co_consts),
                co_names,
                co_varnames,
                co_filename,
                co_name,
                co_firstlineno,
                bytes(co_lnotab, encoding="utf-8"),
                co_freevars,
                co_cellvars,
            )
    else:
        if v11_to_14:
            code = Code2Compat(
                co_argcount,
                co_nlocals,
                co_stacksize,
                co_flags,
                co_code,
                co_consts,
                co_names,
                co_varnames,
                co_filename,
                co_name,
                co_firstlineno,
                co_lnotab,
                co_freevars,
                co_cellvars,
            )
        else:
            if 3000 <= magic_int < 20121:
                # Python 3 encodes some fields as Unicode while Python2
                # requires the corresponding field to have string values
                co_consts = tuple([
                    compat_u2s(s) if isinstance(s, unicode) else s
                    for s in co_consts
                ])
                co_names = tuple([
                    compat_u2s(s) if isinstance(s, unicode) else s
                    for s in co_names
                ])
                co_varnames = tuple([
                    compat_u2s(s) if isinstance(s, unicode) else s
                    for s in co_varnames
                ])
                co_filename = str(co_filename)
                co_name = str(co_name)

            if 3020 < magic_int <= 20121 or magic_int in IS_PYPY3:
                code = Code3(
                    co_argcount,
                    kwonlyargcount,
                    co_nlocals,
                    co_stacksize,
                    co_flags,
                    co_code,
                    co_consts,
                    co_names,
                    co_varnames,
                    co_filename,
                    co_name,
                    co_firstlineno,
                    co_lnotab,
                    co_freevars,
                    co_cellvars,
                )
            else:
                if magic_int in (160, ):
                    co_filename = str(co_filename)
                    co_name = str(co_name)
                    co_varnames = tuple([str(t) for t in co_varnames])
                    co_consts = tuple([str(t) for t in co_consts])
                    co_freevars = tuple([str(t) for t in co_freevars])
                    co_cellvars = tuple([str(t) for t in co_cellvars])

                Code = types.CodeType
                code = Code(
                    co_argcount,
                    co_nlocals,
                    co_stacksize,
                    co_flags,
                    co_code,
                    co_consts,
                    co_names,
                    co_varnames,
                    co_filename,
                    co_name,
                    co_firstlineno,
                    co_lnotab,
                    co_freevars,
                    co_cellvars,
                )
                pass
            pass
        pass
    code_objects[str(code)] = code
    return code