Source

## One-liner implementation of cPickle#frompickleimport*frompickleimport__doc__,__version__,format_version,compatible_formatstry:from__pypy__importbuiltinifyexceptImportError:builtinify=lambdaf:fBadPickleGet=KeyErrorUnpickleableError=PicklingError# ____________________________________________________________# XXX some temporary dark magic to produce pickled dumps that are# closer to the ones produced by cPickle in CPythonfrompickleimportStringIOPythonPickler=PicklerclassPickler(PythonPickler):def__init__(self,*args,**kw):self.__f=Noneiflen(args)==1andisinstance(args[0],int):self.__f=StringIO()PythonPickler.__init__(self,self.__f,args[0],**kw)else:PythonPickler.__init__(self,*args,**kw)defmemoize(self,obj):self.memo[id(None)]=None# cPickle starts counting at onereturnPythonPickler.memoize(self,obj)defgetvalue(self):returnself.__fandself.__f.getvalue()@builtinifydefdump(obj,file,protocol=None):Pickler(file,protocol).dump(obj)@builtinifydefdumps(obj,protocol=None):file=StringIO()Pickler(file,protocol).dump(obj)returnfile.getvalue()