[Zodb-checkins] SVN: ZODB/branches/tseaver-python_picklecache-2/src/persistent/ Fix reduce to match expectations in C module.

Tres Seaver tseaver at palladion.com
Wed Feb 16 00:04:39 EST 2011


Log message for revision 120370:
  Fix reduce to match expectations in C module.

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:37 UTC (rev 120369)
+++ ZODB/branches/tseaver-python_picklecache-2/src/persistent/pyPersistence.py	2011-02-16 05:04:38 UTC (rev 120370)
@@ -11,6 +11,7 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
+from copy_reg import __newobj__
 import datetime
 import struct
 import sys
@@ -271,19 +272,26 @@
     def __getstate__(self):
         """ See IPersistent.
         """
+        idict = getattr(self, '__dict__', None)
+        if idict is not None:
+            return idict
         return ()
 
     def __setstate__(self, state):
         """ See IPersistent.
         """
-        if state != ():
-            raise ValueError('No state allowed on base Persistent class')
+        idict = getattr(self, '__dict__', None)
+        if idict is not None:
+            idict.update(state)
+        else:
+            if state != ():
+                raise ValueError('No state allowed on base Persistent class')
 
     def __reduce__(self):
         """ See IPersistent.
         """
         gna = getattr(self, '__getnewargs__', lambda: ())
-        return ((type(self),) + gna(), self.__getstate__())
+        return (__newobj__, (type(self),) + gna(), self.__getstate__())
 
     def _p_activate(self):
         """ See IPersistent.

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:37 UTC (rev 120369)
+++ ZODB/branches/tseaver-python_picklecache-2/src/persistent/tests/test_pyPersistence.py	2011-02-16 05:04:38 UTC (rev 120370)
@@ -679,30 +679,36 @@
         self.assertEqual(inst._p_sticky, False)
 
     def test___reduce__(self):
+        from copy_reg import __newobj__
         inst = self._makeOne()
-        first, second = inst.__reduce__()
-        self.assertEqual(first, (self._getTargetClass(),))
-        self.assertEqual(second, ())
+        first, second, third = inst.__reduce__()
+        self.failUnless(first is __newobj__)
+        self.assertEqual(second, (self._getTargetClass(),))
+        self.assertEqual(third, ())
 
     def test___reduce__w_subclass_having_getstate(self):
+        from copy_reg import __newobj__
         class Derived(self._getTargetClass()):
             def __getstate__(self):
                 return {}
         inst = Derived()
-        first, second = inst.__reduce__()
-        self.assertEqual(first, (Derived,))
-        self.assertEqual(second, {})
+        first, second, third = inst.__reduce__()
+        self.failUnless(first is __newobj__)
+        self.assertEqual(second, (Derived,))
+        self.assertEqual(third, {})
 
     def test___reduce__w_subclass_having_gna_and_getstate(self):
+        from copy_reg import __newobj__
         class Derived(self._getTargetClass()):
             def __getnewargs__(self):
                 return ('a', 'b')
             def __getstate__(self):
                 return {'foo': 'bar'}
         inst = Derived()
-        first, second = inst.__reduce__()
-        self.assertEqual(first, (Derived, 'a', 'b'))
-        self.assertEqual(second, {'foo': 'bar'})
+        first, second, third = inst.__reduce__()
+        self.failUnless(first is __newobj__)
+        self.assertEqual(second, (Derived, 'a', 'b'))
+        self.assertEqual(third, {'foo': 'bar'})
 
     def test__p_activate_from_new(self):
         inst = self._makeOne()



More information about the Zodb-checkins mailing list