[Checkins] SVN: ZODB/branches/tseaver-python_picklecache-2/src/persistent/ Clear dict during invalidation, if present.

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


Log message for revision 120365:
  Clear dict during invalidation, if present.
  
  Also, allow deleting _p_serial.

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:31 UTC (rev 120364)
+++ ZODB/branches/tseaver-python_picklecache-2/src/persistent/pyPersistence.py	2011-02-16 05:04:32 UTC (rev 120365)
@@ -117,8 +117,11 @@
                 raise ValueError('Invalid SERIAL type: %s' % value)
         self.__serial = value
 
-    _p_serial = property(_get_serial, _set_serial)
+    def _del_serial(self):
+        self.__serial = None
 
+    _p_serial = property(_get_serial, _set_serial, _del_serial)
+
     # _p_changed:  see IPersistent.
     def _get_changed(self):
         if self.__flags is None: # ghost
@@ -293,6 +296,9 @@
         if self.__flags is not None and self.__flags & _STICKY:
             raise ValueError('Sticky')
         self.__flags = None
+        idict = getattr(self, '__dict__', None)
+        if idict is not None:
+            idict.clear()
 
     def _p_getattr(self, name):
         """ 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:31 UTC (rev 120364)
+++ ZODB/branches/tseaver-python_picklecache-2/src/persistent/tests/test_pyPersistence.py	2011-02-16 05:04:32 UTC (rev 120365)
@@ -558,10 +558,14 @@
         self.assertEqual(getattr(inst, 'normal', None), 'after')
 
     def test___delattr___p__names(self):
+        NAMES = ['_p_changed',
+                 '_p_serial',
+                ]
         inst, jar, OID = self._makeOneWithJar()
         jar._cache._mru = []
         jar._registered = []
-        delattr(inst, '_p_changed') #only del-able _p_ attribute.
+        for name in NAMES:
+            delattr(inst, name)
         self.assertEqual(jar._cache._mru, [])
         self.assertEqual(jar._registered, [])
 
@@ -690,6 +694,16 @@
         inst._p_deactivate()
         self.assertEqual(inst._p_status, 'new')
 
+    def test__p_deactivate_from_new_w_dict(self):
+        class Derived(self._getTargetClass()):
+            normal = 'before'
+            def __init__(self):
+                self.__dict__['normal'] = 'after'
+        inst = Derived()
+        inst._p_deactivate()
+        self.assertEqual(inst._p_status, 'new')
+        self.assertEqual(inst.__dict__, {'normal': 'after'})
+
     def test__p_deactivate_from_unsaved(self):
         inst = self._makeOne()
         inst._p_changed = True
@@ -697,6 +711,18 @@
         # can't transition 'unsaved' -> 'new'
         self.assertEqual(inst._p_status, 'unsaved')
 
+    def test__p_deactivate_from_unsaved_w_dict(self):
+        class Derived(self._getTargetClass()):
+            normal = 'before'
+            def __init__(self):
+                self.__dict__['normal'] = 'after'
+        inst = Derived()
+        inst._p_changed = True
+        inst._p_deactivate()
+        # can't transition 'unsaved' -> 'new'
+        self.assertEqual(inst._p_status, 'unsaved')
+        self.assertEqual(inst.__dict__, {'normal': 'after'})
+
     def test__p_deactivate_from_ghost(self):
         inst, jar, OID = self._makeOneWithJar()
         inst._p_deactivate()
@@ -713,7 +739,35 @@
         self.assertEqual(list(jar._loaded), [])
         self.assertEqual(list(jar._registered), [])
 
+    def test__p_deactivate_from_saved_w_dict(self):
+        class Derived(self._getTargetClass()):
+            normal = 'before'
+            def __init__(self):
+                self.__dict__['normal'] = 'after'
+        inst, jar, OID = self._makeOneWithJar(Derived)
+        inst._p_activate()
+        jar._loaded = []
+        inst._p_deactivate()
+        self.assertEqual(inst._p_status, 'ghost')
+        self.assertEqual(inst.__dict__, {})
+        self.assertEqual(list(jar._loaded), [])
+        self.assertEqual(list(jar._registered), [])
+
     def test__p_deactivate_from_changed(self):
+        class Derived(self._getTargetClass()):
+            normal = 'before'
+        inst, jar, OID = self._makeOneWithJar(Derived)
+        inst.normal = 'after'
+        jar._loaded = []
+        jar._registered = []
+        inst._p_deactivate()
+        # assigning None is ignored when dirty
+        self.assertEqual(inst._p_status, 'changed')
+        self.assertEqual(inst.__dict__, {'normal': 'after'})
+        self.assertEqual(list(jar._loaded), [])
+        self.assertEqual(list(jar._registered), [])
+
+    def test__p_deactivate_from_changed_w_dict(self):
         inst, jar, OID = self._makeOneWithJar()
         inst._p_activate()
         inst._p_changed = True
@@ -736,12 +790,33 @@
         inst._p_invalidate()
         self.assertEqual(inst._p_status, 'new')
 
+    def test__p_invalidate_from_new_w_dict(self):
+        class Derived(self._getTargetClass()):
+            normal = 'before'
+            def __init__(self):
+                self.__dict__['normal'] = 'after'
+        inst = Derived()
+        inst._p_invalidate()
+        self.assertEqual(inst._p_status, 'new')
+        self.assertEqual(inst.__dict__, {})
+
     def test__p_invalidate_from_unsaved(self):
         inst = self._makeOne()
         inst._p_changed = True
         inst._p_invalidate()
         self.assertEqual(inst._p_status, 'new')
 
+    def test__p_invalidate_from_unsaved_w_dict(self):
+        class Derived(self._getTargetClass()):
+            normal = 'before'
+            def __init__(self):
+                self.__dict__['normal'] = 'after'
+        inst = Derived()
+        inst._p_changed = True
+        inst._p_invalidate()
+        self.assertEqual(inst._p_status, 'new')
+        self.assertEqual(inst.__dict__, {})
+
     def test__p_invalidate_from_ghost(self):
         inst, jar, OID = self._makeOneWithJar()
         inst._p_invalidate()
@@ -759,6 +834,21 @@
         self.assertEqual(list(jar._loaded), [])
         self.assertEqual(list(jar._registered), [])
 
+    def test__p_invalidate_from_saved_w_dict(self):
+        class Derived(self._getTargetClass()):
+            normal = 'before'
+            def __init__(self):
+                self.__dict__['normal'] = 'after'
+        inst, jar, OID = self._makeOneWithJar(Derived)
+        inst._p_activate()
+        jar._loaded = []
+        jar._registered = []
+        inst._p_invalidate()
+        self.assertEqual(inst._p_status, 'ghost')
+        self.assertEqual(inst.__dict__, {})
+        self.assertEqual(list(jar._loaded), [])
+        self.assertEqual(list(jar._registered), [])
+
     def test__p_invalidate_from_changed(self):
         inst, jar, OID = self._makeOneWithJar()
         inst._p_activate()
@@ -770,6 +860,22 @@
         self.assertEqual(list(jar._loaded), [])
         self.assertEqual(list(jar._registered), [])
 
+    def test__p_invalidate_from_changed_w_dict(self):
+        class Derived(self._getTargetClass()):
+            normal = 'before'
+            def __init__(self):
+                self.__dict__['normal'] = 'after'
+        inst, jar, OID = self._makeOneWithJar(Derived)
+        inst._p_activate()
+        inst._p_changed = True
+        jar._loaded = []
+        jar._registered = []
+        inst._p_invalidate()
+        self.assertEqual(inst._p_status, 'ghost')
+        self.assertEqual(inst.__dict__, {})
+        self.assertEqual(list(jar._loaded), [])
+        self.assertEqual(list(jar._registered), [])
+
     def test__p_invalidate_when_sticky(self):
         inst, jar, OID = self._makeOneWithJar()
         inst._p_changed = False
@@ -826,11 +932,15 @@
         self.assertEqual(list(jar._loaded), [OID])
         self.assertEqual(list(jar._cache._mru), [OID])
 
-    def test__p_delattr_w__p__name(self):
+    def test__p_delattr_w__p__names(self):
+        NAMES = ['_p_changed',
+                 '_p_serial',
+                ]
         inst, jar, OID = self._makeOneWithJar()
         inst._p_changed = True
         jar._loaded = []
-        self.failUnless(inst._p_delattr('_p_changed'))
+        for name in NAMES:
+            self.failUnless(inst._p_delattr(name))
         self.assertEqual(inst._p_status, 'ghost')
         self.assertEqual(inst._p_changed, None)
         self.assertEqual(list(jar._loaded), [])



More information about the checkins mailing list