[Zodb-checkins] SVN: ZODB/branches/tseaver-python_picklecache-2/src/persistent/ Extend __getstate__ / __setstate__ for common case of subclass w/ dict.
Tres Seaver
tseaver at palladion.com
Wed Feb 16 00:04:40 EST 2011
Log message for revision 120371:
Extend __getstate__ / __setstate__ for common case of subclass w/ dict.
Changed:
U ZODB/branches/tseaver-python_picklecache-2/src/persistent/pyPersistence.py
U ZODB/branches/tseaver-python_picklecache-2/src/persistent/tests/test_pyPersistence.py
-=-
Modified: ZODB/branches/tseaver-python_picklecache-2/src/persistent/pyPersistence.py
===================================================================
--- ZODB/branches/tseaver-python_picklecache-2/src/persistent/pyPersistence.py 2011-02-16 05:04:38 UTC (rev 120370)
+++ ZODB/branches/tseaver-python_picklecache-2/src/persistent/pyPersistence.py 2011-02-16 05:04:40 UTC (rev 120371)
@@ -282,6 +282,7 @@
"""
idict = getattr(self, '__dict__', None)
if idict is not None:
+ idict.clear()
idict.update(state)
else:
if state != ():
Modified: ZODB/branches/tseaver-python_picklecache-2/src/persistent/tests/test_pyPersistence.py
===================================================================
--- ZODB/branches/tseaver-python_picklecache-2/src/persistent/tests/test_pyPersistence.py 2011-02-16 05:04:38 UTC (rev 120370)
+++ ZODB/branches/tseaver-python_picklecache-2/src/persistent/tests/test_pyPersistence.py 2011-02-16 05:04:40 UTC (rev 120371)
@@ -665,6 +665,13 @@
inst = self._makeOne()
self.assertEqual(inst.__getstate__(), ())
+ def test___getstate___derived_w_dict(self):
+ class Derived(self._getTargetClass()):
+ pass
+ inst = Derived()
+ inst.foo = 'bar'
+ self.assertEqual(inst.__getstate__(), {'foo': 'bar'})
+
def test___setstate___empty(self):
inst = self._makeOne()
inst.__setstate__(()) # doesn't raise, but doesn't change anything
@@ -678,6 +685,14 @@
self.assertEqual(inst._p_changed, None)
self.assertEqual(inst._p_sticky, False)
+ def test___setstate___nonempty_derived_w_dict(self):
+ class Derived(self._getTargetClass()):
+ pass
+ inst = Derived()
+ inst.foo = 'bar'
+ inst.__setstate__({'baz': 'bam'})
+ self.assertEqual(inst.__dict__, {'baz': 'bam'})
+
def test___reduce__(self):
from copy_reg import __newobj__
inst = self._makeOne()
More information about the Zodb-checkins
mailing list