def save_global(self, obj, name=None, pack=struct.pack): """ Save a "global". The name of this method is somewhat misleading: all types get dispatched here. """ if obj is type(None): # noqa return self.save_reduce(type, (None, ), obj=obj) elif obj is type(Ellipsis): return self.save_reduce(type, (Ellipsis, ), obj=obj) elif obj is type(NotImplemented): return self.save_reduce(type, (NotImplemented, ), obj=obj) elif obj in _BUILTIN_TYPE_NAMES: return self.save_reduce(_builtin_type, (_BUILTIN_TYPE_NAMES[obj], ), obj=obj) if sys.version_info[:2] < (3, 7) and _is_parametrized_type_hint( obj): # noqa # pragma: no branch # Parametrized typing constructs in Python < 3.7 are not # compatible with type checks and ``isinstance`` semantics. For # this reason, it is easier to detect them using a # duck-typing-based check (``_is_parametrized_type_hint``) than # to populate the Pickler's dispatch with type-specific savers. self.save_reduce(_create_parametrized_type_hint, parametrized_type_hint_getinitargs(obj), obj=obj) elif name is not None: Pickler.save_global(self, obj, name=name) elif not _is_importable(obj, name=name): self._save_reduce_pickle5(*_dynamic_class_reduce(obj), obj=obj) else: Pickler.save_global(self, obj, name=name)
def __init__(self, file, protocol=None, buffer_callback=None): if protocol is None: protocol = DEFAULT_PROTOCOL Pickler.__init__(self, file, protocol=protocol, buffer_callback=buffer_callback) self.globals_ref = {} self.proto = int(protocol)
def __init__(self, file, protocol=None): if protocol is None: protocol = DEFAULT_PROTOCOL Pickler.__init__(self, file, protocol=protocol) # map functions __globals__ attribute ids, to ensure that functions # sharing the same global namespace at pickling time also share # their global namespace at unpickling time. self.globals_ref = {} assert hasattr(self, 'proto')
def check(Pickler): for proto in range(pickle.HIGHEST_PROTOCOL + 1): f = io.BytesIO() pickler = Pickler(f, proto) pickler.dump('abc') self.assertEqual(self.loads(f.getvalue()), 'abc') pickler = Pickler(io.BytesIO()) self.assertEqual(pickler.persistent_id('def'), 'def') r = weakref.ref(pickler) del pickler self.assertIsNone(r())
def __init__(self, file, protocol=None, buffer_callback=None): if protocol is None: protocol = DEFAULT_PROTOCOL Pickler.__init__(self, file, protocol=protocol, buffer_callback=buffer_callback) # map functions __globals__ attribute ids, to ensure that functions # sharing the same global namespace at pickling time also share # their global namespace at unpickling time. self.globals_ref = {} self.proto = int(protocol)
def __init__(self, file, protocol=None, buffer_callback=None): if protocol is None: protocol = DEFAULT_PROTOCOL Pickler.__init__(self, file, protocol=protocol, buffer_callback=buffer_callback) # map functions __globals__ attribute ids, to ensure that functions # sharing the same global namespace at pickling time also share their # global namespace at unpickling time. self.globals_ref = {} # Take into account potential custom reducers registered by external # modules self.dispatch_table = copyreg.dispatch_table.copy() self.dispatch_table.update(self.dispatch) self.proto = int(protocol)
def dump(self, obj): try: return Pickler.dump(self, obj) except RuntimeError as e: if "recursion" in e.args[0]: msg = ("Could not pickle object as excessively deep recursion " "required.") raise pickle.PicklingError(msg) else: raise
def __setitem__(self, key, value): if type(value) is dict: blue = Blue_dict(key, value, self, self) self.__vars__[key] = blue self.__blues__[key] = blue with open(f'{self.name}.blue', 'wb') as fp: p = Pickler(fp) p.dump(self) else: self.__vars__[key] = value with open(f'{self.name}.blue', 'wb') as fp: p = Pickler(fp) p.dump(self) self.__base_reload__()
def save_function(self, obj, name=None): """ Registered with the dispatch to handle all function types. Determines what kind of function obj is (e.g. lambda, defined at interactive prompt, etc) and handles the pickling appropriately. """ if _is_importable(obj, name=name): return Pickler.save_global(self, obj, name=name) elif PYPY and isinstance(obj.__code__, builtin_code_type): return self.save_pypy_builtin_func(obj) else: return self._save_reduce_pickle5( *self._dynamic_function_reduce(obj), obj=obj)
def __init__(self, name, **kwargs): self.name = name self.__vars__ = {} self.__blues__ = {} try: with open(f'{self.name}.blue', 'rb') as fp: p = Unpickler(fp) q = p.load() self.__vars__ = q.__vars__ self.__blues__ = q.__blues__ self.__loaded__ = q except: with open(f'{self.name}.blue', 'wb') as fp: p = Pickler(fp) p.dump(self)
def test_custom_pickler_dispatch_table_memleak(self): # See https://github.com/python/cpython/issues/89988 class Pickler(self.pickler): def __init__(self, *args, **kwargs): self.dispatch_table = table super().__init__(*args, **kwargs) class DispatchTable: pass table = DispatchTable() pickler = Pickler(io.BytesIO()) self.assertIs(pickler.dispatch_table, table) table_ref = weakref.ref(table) self.assertIsNotNone(table_ref()) del pickler del table support.gc_collect() self.assertIsNone(table_ref())
def saveturn(): filename = 'lastturn' if lexists(filename): file = open(filename, 'wb') else: file = open(filename, 'xb') saver = Pickler(file) saver.dump(summoners) saver.dump(teams) saver.dump(board) saver.dump(boardwidth) saver.dump(boardheight) saver.dump(modifiers) saver.dump(gamemods) saver.dump(whoseturn) saver.dump(phase) saver.dump(subphase) #cannot save action, because will give error with inner methods (like render). file.close()
def save_input(): print("Saving...") pf = open(options.pickle, "wb") Pickler(pf).dump(inp) pf.close()
def __delitem__(self, key): del self.__vars__[key] with open(f'{self.name}.blue', 'wb') as thing: p = Pickler(thing) p.dump(self) self.__base_reload__()