Remove inst_persistent_id() WANNI (we ain't never needed it).

Add some simple tests of the persistence hooks.
This commit is contained in:
Jeremy Hylton 2002-11-13 22:01:27 +00:00
parent 0e1183ddff
commit 5e0f4e73a9
2 changed files with 29 additions and 16 deletions

View File

@ -188,10 +188,9 @@ 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)
@ -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):

View File

@ -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__":