[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/keyreference/ Added a NotYet error and changed the persistent key-reference adapter

Jim Fulton jim at zope.com
Fri May 27 17:25:06 EDT 2005


Log message for revision 30545:
  Added a NotYet error and changed the persistent key-reference adapter
  to raise it id it can't find a connection.
  
  Change the IConnection adapter to return None if it can't find a
  connection.
  

Changed:
  U   Zope3/trunk/src/zope/app/keyreference/interfaces.py
  U   Zope3/trunk/src/zope/app/keyreference/persistent.py
  U   Zope3/trunk/src/zope/app/keyreference/persistent.txt

-=-
Modified: Zope3/trunk/src/zope/app/keyreference/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/app/keyreference/interfaces.py	2005-05-27 20:43:09 UTC (rev 30544)
+++ Zope3/trunk/src/zope/app/keyreference/interfaces.py	2005-05-27 21:24:36 UTC (rev 30545)
@@ -17,6 +17,13 @@
 """
 import zope.interface
 
+class NotYet(Exception):
+    """Can't compute a key reference for an object
+
+    It might b epossible to compute one later
+    (e.g. at the end of the transaction).
+    """
+
 class IKeyReference(zope.interface.Interface):
     """A reference to an object (similar to a weak reference).
 

Modified: Zope3/trunk/src/zope/app/keyreference/persistent.py
===================================================================
--- Zope3/trunk/src/zope/app/keyreference/persistent.py	2005-05-27 20:43:09 UTC (rev 30544)
+++ Zope3/trunk/src/zope/app/keyreference/persistent.py	2005-05-27 21:24:36 UTC (rev 30545)
@@ -34,7 +34,12 @@
 
     def __init__(self, object):
         if not getattr(object, '_p_oid', None):
-            IConnection(object).add(object)
+            connection = IConnection(object, None)
+            if connection is None:
+                raise zope.app.keyreference.interfaces.NotYet(object)
+
+            connection.add(object)
+
         self.object = object
 
     def __call__(self):
@@ -50,6 +55,7 @@
         return cmp(self.object._p_oid, other.object._p_oid)
 
 
+
 def connectionOfPersistent(ob):
     """An adapter which gets a ZODB connection of a persistent object.
 
@@ -62,6 +68,6 @@
     while not getattr(cur, '_p_jar', None):
         cur = getattr(cur, '__parent__', None)
         if cur is None:
-            raise ValueError('Can not get connection of %r' % (ob,))
+            return None
     return cur._p_jar
 

Modified: Zope3/trunk/src/zope/app/keyreference/persistent.txt
===================================================================
--- Zope3/trunk/src/zope/app/keyreference/persistent.txt	2005-05-27 20:43:09 UTC (rev 30544)
+++ Zope3/trunk/src/zope/app/keyreference/persistent.txt	2005-05-27 21:24:36 UTC (rev 30545)
@@ -83,8 +83,11 @@
     ... # doctest: +ELLIPSIS
     Traceback (most recent call last):
     ...
-    TypeError: ('Could not adapt', ...
+    NotYet: <persistent.dict.PersistentDict object at ...>
 
+Note that we get a NotYet error. This indicates that we might be able
+to get a key reference later.
+
 We can get references to unsaved objects if they have an adapter to
 `ZODB.interfaces.IConnection`.  The `add` method on the connection
 will be used to give the object an object id, which is enough
@@ -112,11 +115,8 @@
 
     >>> from zope.app.keyreference.persistent import connectionOfPersistent
     >>> ob3 = PersistentDict()
-    >>> connectionOfPersistent(ob3)
-    ... # doctest: +ELLIPSIS
-    Traceback (most recent call last):
-    ...
-    ValueError: Can not get connection of ...
+    >>> print connectionOfPersistent(ob3)
+    None
 
     >>> ob3.__parent__ = root2['ob1']
     >>> connectionOfPersistent(ob3) is conn2



More information about the Zope3-Checkins mailing list