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,14 +188,13 @@ class Pickler:
return GET + `i` + '\n' return GET + `i` + '\n'
def save(self, object, pers_save = 0): def save(self, object):
memo = self.memo memo = self.memo
if not pers_save: pid = self.persistent_id(object)
pid = self.persistent_id(object) if pid is not None:
if pid is not None: self.save_pers(pid)
self.save_pers(pid) return
return
d = id(object) d = id(object)
@ -215,11 +214,6 @@ class Pickler:
try: try:
f = self.dispatch[t] f = self.dispatch[t]
except KeyError: except KeyError:
pid = self.inst_persistent_id(object)
if pid is not None:
self.save_pers(pid)
return
try: try:
issc = issubclass(t, TypeType) issc = issubclass(t, TypeType)
except TypeError: # t is not a class except TypeError: # t is not a class
@ -279,14 +273,11 @@ class Pickler:
def persistent_id(self, object): def persistent_id(self, object):
return None return None
def inst_persistent_id(self, object):
return None
def save_pers(self, pid): def save_pers(self, pid):
if not self.bin: if not self.bin:
self.write(PERSID + str(pid) + '\n') self.write(PERSID + str(pid) + '\n')
else: else:
self.save(pid, 1) self.save(pid)
self.write(BINPERSID) self.write(BINPERSID)
def save_reduce(self, callable, arg_tup, state = None): def save_reduce(self, callable, arg_tup, state = None):

View File

@ -1,7 +1,8 @@
import pickle import pickle
import unittest import unittest
from cStringIO import StringIO from cStringIO import StringIO
from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests from test.pickletester import AbstractPickleTests, AbstractPickleModuleTests, \
AbstractPersistentPicklerTests
from test import test_support from test import test_support
class PickleTests(AbstractPickleTests, AbstractPickleModuleTests): class PickleTests(AbstractPickleTests, AbstractPickleModuleTests):
@ -29,11 +30,32 @@ class PicklerTests(AbstractPickleTests):
u = pickle.Unpickler(f) u = pickle.Unpickler(f)
return u.load() 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(): def test_main():
loader = unittest.TestLoader() loader = unittest.TestLoader()
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(loader.loadTestsFromTestCase(PickleTests)) suite.addTest(loader.loadTestsFromTestCase(PickleTests))
suite.addTest(loader.loadTestsFromTestCase(PicklerTests)) suite.addTest(loader.loadTestsFromTestCase(PicklerTests))
suite.addTest(loader.loadTestsFromTestCase(PersPicklerTests))
test_support.run_suite(suite) test_support.run_suite(suite)
if __name__ == "__main__": if __name__ == "__main__":