[Zope-CVS] CVS: Products/Ape/lib/apelib/zodb3 - connection.py:1.12

Shane Hathaway shane at zope.com
Wed Mar 17 20:08:15 EST 2004


Update of /cvs-repository/Products/Ape/lib/apelib/zodb3
In directory cvs.zope.org:/tmp/cvs-serv8576/zodb3

Modified Files:
	connection.py 
Log Message:
It turns out that some applications ghostify unmanaged persistent objects.

In particular, ZCTextIndex does this.  But now the jar for unmanaged
objects saves the state and restores it to de-ghostify.


=== Products/Ape/lib/apelib/zodb3/connection.py 1.11 => 1.12 ===
--- Products/Ape/lib/apelib/zodb3/connection.py:1.11	Tue Mar 16 22:58:26 2004
+++ Products/Ape/lib/apelib/zodb3/connection.py	Wed Mar 17 20:08:14 2004
@@ -18,7 +18,7 @@
 
 import sys
 from time import time
-from types import StringType, TupleType
+from types import StringType, TupleType, DictType
 from cStringIO import StringIO
 from cPickle import Unpickler, Pickler
 
@@ -404,9 +404,11 @@
             if hasattr(o, '_p_oid'):  # Looks like a persistent object
                 if o._p_jar is None:
                     o._p_oid = 'unmanaged'
-                    o._p_jar = UnmanagedJar(self, obj._p_oid)
+                    o._p_jar = UnmanagedJar(self, obj._p_oid, o)
                 else:
-                    o._p_changed = 0
+                    assert o._p_oid == 'unmanaged'
+                    if o._p_changed is not None:
+                        o._p_jar.saveState(o)
 
 
     # IObjectDatabase implementation
@@ -548,19 +550,31 @@
     There is one such jar for each unmanaged persistent object.  All
     it does is notify the managed persistent object of changes.
 
-    Note that unmanaged persistent objects should never be ghosted!
-    Instead, when the managed persistent object gets ghosted, it
-    usually removes the last reference to the unmanaged object, which
-    is then deallocated.
+    Some applications ghostify unmanaged persistent objects.  To
+    restore the state after ghostification, this jar keeps a reference
+    to the state and restores it in setstate().  Note that when the
+    managed persistent object that holds the unmanaged object gets
+    ghosted, it usually removes the last reference to the unmanaged
+    object, which is then deallocated.
     """
 
-    def __init__(self, real_jar, real_oid):
+    def __init__(self, real_jar, real_oid, obj):
         self.real_jar = real_jar
         self.real_oid = real_oid
+        self.saveState(obj)
 
-    def register(self, ob):
+    def saveState(self, obj):
+        s = obj.__getstate__()
+        if isinstance(s, DictType):
+            s = s.copy()
+        self.state = s
+
+    def register(self, obj):
         o = self.real_jar[self.real_oid]
         o._p_changed = 1
+
+    def setstate(self, obj):
+        obj.__setstate__(self.state)
 
     def modifiedInVersion(self, oid):
         # XXX PersistentExtra wants this




More information about the Zope-CVS mailing list