Remove inst_persistent_id() WANNI (we ain't never needed it).
Add some simple tests of the persistence hooks.
This commit is contained in:
parent
0e1183ddff
commit
5e0f4e73a9
|
@ -188,14 +188,13 @@ class Pickler:
|
|||
|
||||
return GET + `i` + '\n'
|
||||
|
||||
def save(self, object, pers_save = 0):
|
||||
def save(self, object):
|
||||
memo = self.memo
|
||||
|
||||
if not pers_save:
|
||||
pid = self.persistent_id(object)
|
||||
if pid is not None:
|
||||
self.save_pers(pid)
|
||||
return
|
||||
pid = self.persistent_id(object)
|
||||
if pid is not None:
|
||||
self.save_pers(pid)
|
||||
return
|
||||
|
||||
d = id(object)
|
||||
|
||||
|
@ -215,11 +214,6 @@ class Pickler:
|
|||
try:
|
||||
f = self.dispatch[t]
|
||||
except KeyError:
|
||||
pid = self.inst_persistent_id(object)
|
||||
if pid is not None:
|
||||
self.save_pers(pid)
|
||||
return
|
||||
|
||||
try:
|
||||
issc = issubclass(t, TypeType)
|
||||
except TypeError: # t is not a class
|
||||
|
@ -279,14 +273,11 @@ class Pickler:
|
|||
def persistent_id(self, object):
|
||||
return None
|
||||
|
||||
def inst_persistent_id(self, object):
|
||||
return None
|
||||
|
||||
def save_pers(self, pid):
|
||||
if not self.bin:
|
||||
self.write(PERSID + str(pid) + '\n')
|
||||
else:
|
||||
self.save(pid, 1)
|
||||
self.save(pid)
|
||||
self.write(BINPERSID)
|
||||
|
||||
def save_reduce(self, callable, arg_tup, state = None):
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import pickle
|
||||
import unittest
|
||||
from cStringIO import StringIO
|
||||
from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests
|
||||
from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests, \
|
||||
AbstractPersistentPicklerTests
|
||||
from test import test_support
|
||||
|
||||
class PickleTests(AbstractPickleTests, AbstractPickleModuleTests):
|
||||
|
@ -29,11 +30,32 @@ class PicklerTests(AbstractPickleTests):
|
|||
u = pickle.Unpickler(f)
|
||||
return u.load()
|
||||
|
||||
class PersPicklerTests(AbstractPersistentPicklerTests):
|
||||
|
||||
def dumps(self, arg, bin=0):
|
||||
class PersPickler(pickle.Pickler):
|
||||
def persistent_id(subself, obj):
|
||||
return self.persistent_id(obj)
|
||||
f = StringIO()
|
||||
p = PersPickler(f, bin)
|
||||
p.dump(arg)
|
||||
f.seek(0)
|
||||
return f.read()
|
||||
|
||||
def loads(self, buf):
|
||||
class PersUnpickler(pickle.Unpickler):
|
||||
def persistent_load(subself, obj):
|
||||
return self.persistent_load(obj)
|
||||
f = StringIO(buf)
|
||||
u = PersUnpickler(f)
|
||||
return u.load()
|
||||
|
||||
def test_main():
|
||||
loader = unittest.TestLoader()
|
||||
suite = unittest.TestSuite()
|
||||
suite.addTest(loader.loadTestsFromTestCase(PickleTests))
|
||||
suite.addTest(loader.loadTestsFromTestCase(PicklerTests))
|
||||
suite.addTest(loader.loadTestsFromTestCase(PersPicklerTests))
|
||||
test_support.run_suite(suite)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in New Issue