[Zodb-checkins] CVS: Zope3/src/zodb - serialize.py:1.25

Jim Fulton jim at zope.com
Tue Jan 6 14:50:41 EST 2004


Update of /cvs-repository/Zope3/src/zodb
In directory cvs.zope.org:/tmp/cvs-serv13571/src/zodb

Modified Files:
	serialize.py 
Log Message:
Added initial support for zodb-based persistent weak references.


=== Zope3/src/zodb/serialize.py 1.24 => 1.25 ===
--- Zope3/src/zodb/serialize.py:1.24	Tue Dec  9 04:02:07 2003
+++ Zope3/src/zodb/serialize.py	Tue Jan  6 14:50:40 2004
@@ -70,6 +70,7 @@
 from cStringIO import StringIO
 
 from zodb.interfaces import ZERO, InvalidObjectReference
+from persistence.wref import WeakRefMarker, WeakRef
 
 
 def getClassMetadata(obj):
@@ -142,8 +143,34 @@
         # if isinstance(oid, types.MemberDescriptor):
         # -- but I can't because the type doesn't have a canonical name.
         # Instead, we'll assert that an oid must always be a string
+
         if not (oid is None or isinstance(oid, str)):
+            
+            if oid is WeakRefMarker:
+                # we have a weakref
+                # see weakref.py
+
+                oid = obj.oid
+                if oid is None:
+                    obj = obj() # get the referenced object
+                    oid = obj._p_oid
+                    if oid is None:
+                        # Here we are causing the object to be saved in
+                        # the database. One could argue that we shouldn't
+                        # do this, because a wekref should not cause an object
+                        # to be added.  We'll be optimistic, though, and
+                        # assume that the object will be added eventually.
+                        
+                        oid = self._jar.newObjectId()
+                        obj._p_jar = self._jar
+                        obj._p_oid = oid
+                        self._stack.append(obj)
+                return [oid]
+    
             # XXX log a warning
+
+            # XXX what is the rational for continueing here?
+            
             return None
 
         if oid is None:
@@ -291,6 +318,13 @@
 
             self._cache.set(oid, obj)
             return obj
+        elif isinstance(oid, list):
+            # see weakref.py
+            [oid] = oid
+            obj = WeakRef.__new__(WeakRef)
+            obj.oid = oid
+            obj.dm = self._conn
+            return obj
 
         obj = self._cache.get(oid)
         if obj is not None:
@@ -363,6 +397,6 @@
     for ref in L:
         if isinstance(ref, tuple):
             oids.append(ref[0])
-        else:
+        elif isinstance(ref, str):
             oids.append(ref)
     return oids




More information about the Zodb-checkins mailing list