[Checkins] SVN: persistent/trunk/ Fix __getstate__ when derived class has slots.

Tres Seaver cvs-admin at zope.org
Thu Jun 28 18:50:10 UTC 2012


Log message for revision 127146:
  Fix __getstate__ when derived class has slots.

Changed:
  _U  persistent/trunk/
  U   persistent/trunk/persistent/pyPersistence.py
  U   persistent/trunk/persistent/tests/test_pyPersistence.py

-=-
Modified: persistent/trunk/persistent/pyPersistence.py
===================================================================
--- persistent/trunk/persistent/pyPersistence.py	2012-06-28 18:50:00 UTC (rev 127145)
+++ persistent/trunk/persistent/pyPersistence.py	2012-06-28 18:50:06 UTC (rev 127146)
@@ -262,6 +262,14 @@
             return dict([x for x in idict.items()
                             if not x[0].startswith('_p_') and
                                not x[0].startswith('_v_')])
+        slots = getattr(type(self), '__slots__', None)
+        if slots is not None:
+            slots = [x for x in slots
+                            if not x.startswith('_p_') and
+                               not x.startswith('_v_') and
+                               x not in Persistent.__slots__]
+            if slots:
+                return None, dict([(x, getattr(self, x)) for x in slots])
         return None
 
     def __setstate__(self, state):
@@ -358,6 +366,7 @@
         if self.__jar is not None and self.__oid is not None:
             self.__jar._cache.mru(self.__oid)
 
+
 def _estimated_size_in_24_bits(value):
     if value > 1073741696:
         return 16777215

Modified: persistent/trunk/persistent/tests/test_pyPersistence.py
===================================================================
--- persistent/trunk/persistent/tests/test_pyPersistence.py	2012-06-28 18:50:00 UTC (rev 127145)
+++ persistent/trunk/persistent/tests/test_pyPersistence.py	2012-06-28 18:50:06 UTC (rev 127146)
@@ -701,6 +701,15 @@
         inst._v_qux = 'spam'
         self.assertEqual(inst.__getstate__(), {'foo': 'bar'})
 
+    def test___getstate___derived_w_slots(self):
+        class Derived(self._getTargetClass()):
+            __slots__ = ('foo', '_p_baz', '_v_qux')
+        inst = Derived()
+        inst.foo = 'bar'
+        inst._p_baz = 'bam'
+        inst._v_qux = 'spam'
+        self.assertEqual(inst.__getstate__(), (None, {'foo': 'bar'}))
+
     def test___setstate___empty(self):
         inst = self._makeOne()
         inst.__setstate__(None) # doesn't raise, but doesn't change anything



More information about the checkins mailing list