def deserialize_function(f: dict): code_fields = f[CODE_FIELD_NAME][VALUE_FIELD_NAME] code_args = [] for field in CODE_OBJECT_ARGS: arg = code_fields[field] if type(arg) == dict: code_args.append(deserialize_obj(arg)) # if arg[TYPE_FIELD_NAME] == "bytes": # code_args.append(bytes(arg[VALUE_FIELD_NAME])) # else: # code_args.append(tuple(arg[VALUE_FIELD_NAME])) else: code_args.append(arg) details = [CodeType(*code_args)] glob = {"__builtins__": __builtins__} for name, o in f[GLOBAL_FIELD_NAME].items(): glob[name] = deserialize_obj(o) details.append(glob) for attr in FUNCTION_ATTRS_NAMES: if attr == CODE_FIELD_NAME: continue details.append(deserialize_obj(f[attr])) result_func = FunctionType(*details) if result_func.__name__ in result_func.__getattribute__(GLOBAL_FIELD_NAME): result_func.__getattribute__(GLOBAL_FIELD_NAME)[ result_func.__name__] = result_func return result_func
def deserialize_func(func): code_fields = func[CODE_FIELD][VALUE_FIELD] code_args = [] for field in CODE_ARGS: arg = code_fields[field] if type(arg) == dict: if arg[TYPE_FIELD] == "bytes": code_args.append(bytes(arg[VALUE_FIELD])) else: code_args.append(tuple(arg[VALUE_FIELD])) else: code_args.append(arg) details = [CodeType(*code_args)] glob = {"__builtins__": __builtins__} for name, o in func[GLOBALS].items(): glob[name] = deserialize_obj(o) details.append(glob) for attr in FUNCTION_ATTRIBUTES: if attr == CODE_FIELD: continue details.append(deserialize_obj(func[attr])) result_func = FunctionType(*details) if result_func.__name__ in result_func.__getattribute__(GLOBALS): result_func.__getattribute__(GLOBALS)[ result_func.__name__] = result_func return result_func
def deserialize(d): d = dict((a, b) for a, b in d) object_type = d["type"] ans = None if object_type == "list": ans = [Serializer.deserialize(i) for i in d["value"]] elif object_type == "dict": ans = {} if "value" not in d: raise InvalidTypeSourceException() for i in d["value"]: val = Serializer.deserialize(i[1]) ans[Serializer.deserialize(i[0])] = val elif object_type == "tuple": ans = tuple([Serializer.deserialize(i) for i in d["value"]]) elif object_type == "function": func = [0] * 4 code = [0] * 16 glob = {"__builtins__": __builtins__} for i in d["value"]: key = Serializer.deserialize(i[0]) if key == "__globals__": globdict = Serializer.deserialize(i[1]) for globkey in globdict: glob[globkey] = globdict[globkey] elif key == "__code__": val = i[1][1][1] for arg in val: codeArgKey = Serializer.deserialize(arg[0]) if codeArgKey != "__doc__": codeArgVal = Serializer.deserialize(arg[1]) index = CODE_OBJECT_ARGS.index(codeArgKey) code[index] = codeArgVal code = CodeType(*code) else: index = FUNCTION_ATTRIBUTES.index(key) func[index] = (Serializer.deserialize(i[1])) func[0] = code func.insert(1, glob) ans = FunctionType(*func) if ans.__name__ in ans.__getattribute__("__globals__"): ans.__getattribute__("__globals__")[ans.__name__] = ans elif object_type == "NoneType": ans = None elif object_type == "bytes": ans = bytes([Serializer.deserialize(i) for i in d["value"]]) else: if object_type == "bool": ans = d["value"] == "True" else: ans = locate(object_type)(d["value"]) return ans
def deserialize(obj): d = dict((a, b) for a, b in obj) obj_type = d['type'] res = None if obj_type == 'list': res = [Serializer.deserialize(i) for i in d['value']] elif obj_type == 'tuple': res = tuple([Serializer.deserialize(i) for i in d['value']]) elif obj_type == 'dict': res = {} for i in d['value']: value = Serializer.deserialize(i[1]) res[Serializer.deserialize(i[0])] = value elif obj_type == 'function': func = [0] * 4 code = [0] * 16 glob = {'__builtins__': __builtins__} for i in d['value']: key = Serializer.deserialize(i[0]) if key == '__globals__': global_d = Serializer.deserialize(i[1]) for global_key in global_d: glob[global_key] = global_d[global_key] elif key == '__code__': value = i[1][1][1] for arg in value: codeArgKey = Serializer.deserialize(arg[0]) if codeArgKey != '__doc__': codeArgVal = Serializer.deserialize(arg[1]) index = CODE_OBJECT_ARGS.index(codeArgKey) code[index] = codeArgVal code = CodeType(*code) else: index = FUNC_ATTRS.index(key) func[index] = (Serializer.deserialize(i[1])) func[0] = code func.insert(1, glob) res = FunctionType(*func) if res.__name__ in res.__getattribute__('__globals__'): res.__getattribute__('__globals__')[res.__name__] = res elif obj_type == 'NoneType': res = None elif obj_type == 'bytes': res = bytes([Serializer.deserialize(i) for i in d['value']]) else: if obj_type == 'bool': res = d['value'] == 'True' else: res = locate(obj_type)(d['value']) return res
def dict_to_function(obj): is_recursive = False function_globals = obj['__globals__'] for name, out_obj in function_globals.items(): if name == obj['__name__']: is_recursive = True function_globals[name] = deserialize(out_obj) function_globals['__builtins__'] = __builtins__ code = obj['__code__'] for i in range(len(code)): if i == 13 and code[i] is None: code[i] = b'' if code[i] is None: code[i] = () elif isinstance(code[i], list): code[i] = bytes(code[i]) func = FunctionType(CodeType(*code), function_globals, obj['__name__'], obj['__defaults__'], None) if is_recursive: func.__getattribute__('__globals__')[obj['__name__']] = func return func
def deserialize_function(obj): recursive_flag = False globals = obj['__globals__'] for outer_obj_name, outer_obj in globals.items(): if outer_obj_name == obj['__name__']: recursive_flag = True globals[outer_obj_name] = deserialize(outer_obj) globals['__builtins__'] = __builtins__ code = obj['__code__'] for i in range(len(code)): # co_lnotab if i == 13 and code[i] is None: code[i] = b'' if code[i] is None: code[i] = () elif isinstance(code[i], list): code[i] = bytes(code[i]) func = FunctionType(CodeType(*code), globals, obj['__name__'], obj['__defaults__'], None) if recursive_flag: func.__getattribute__('__globals__')[obj['__name__']] = func return func