[Checkins] SVN: zope.annotation/branches/3.4/ Annotation factories take care not to store proxies in the database, so

Marius Gedminas marius at pov.lt
Tue Aug 26 17:01:12 EDT 2008


Log message for revision 90365:
  Annotation factories take care not to store proxies in the database, so
  adapting an object wrapped in a LocationProxy works correctly.
  
  Fixes https://bugs.launchpad.net/zope3/+bug/261620
  
  

Changed:
  U   zope.annotation/branches/3.4/CHANGES.txt
  U   zope.annotation/branches/3.4/setup.py
  U   zope.annotation/branches/3.4/src/zope/annotation/README.txt
  U   zope.annotation/branches/3.4/src/zope/annotation/factory.py

-=-
Modified: zope.annotation/branches/3.4/CHANGES.txt
===================================================================
--- zope.annotation/branches/3.4/CHANGES.txt	2008-08-26 20:58:46 UTC (rev 90364)
+++ zope.annotation/branches/3.4/CHANGES.txt	2008-08-26 21:01:12 UTC (rev 90365)
@@ -4,6 +4,15 @@
 
 
 ==================
+3.4.1 (unreleased)
+==================
+
+- Annotation factories take care not to store proxies in the database, so
+  adapting an object wrapped in a LocationProxy works correctly.
+  Fixes https://bugs.launchpad.net/zope3/+bug/261620
+
+
+==================
 3.4.0 (2007-08-29)
 ==================
 

Modified: zope.annotation/branches/3.4/setup.py
===================================================================
--- zope.annotation/branches/3.4/setup.py	2008-08-26 20:58:46 UTC (rev 90364)
+++ zope.annotation/branches/3.4/setup.py	2008-08-26 21:01:12 UTC (rev 90365)
@@ -60,6 +60,7 @@
                         'zope.interface',
                         'zope.component',
                         'zope.location>=3.4.0b1.dev-r78903',
+                        'zope.proxy',
                         ],
     extras_require = dict(
         test = ['zope.testing',

Modified: zope.annotation/branches/3.4/src/zope/annotation/README.txt
===================================================================
--- zope.annotation/branches/3.4/src/zope/annotation/README.txt	2008-08-26 20:58:46 UTC (rev 90364)
+++ zope.annotation/branches/3.4/src/zope/annotation/README.txt	2008-08-26 21:01:12 UTC (rev 90365)
@@ -124,3 +124,46 @@
   'my.unique.key'
   >>> zope.location.interfaces.ILocation.providedBy(old_hoi)
   True
+
+
+LocationProxies
+---------------
+
+Suppose your annotation proxy provides ILocation.
+
+  >>> class IPolloi(interface.Interface):
+  ...     pass
+  >>> class Polloi(Persistent):
+  ...     interface.implements(IPolloi, zope.location.interfaces.ILocation)
+  ...     component.adapts(IFoo)
+  ...     __name__ = __parent__ = 0
+  >>> component.provideAdapter(factory(Polloi, 'my.other.key'))
+
+Sometimes you're adapting an object wrapped in a LocationProxy.
+
+  >>> foo4 = Foo()
+  >>> import zope.location.location
+  >>> wrapped_foo4 = zope.location.location.LocationProxy(foo4, None, 'foo4')
+  >>> located_polloi = IPolloi(wrapped_foo4)
+
+At first glance it looks as if located_polloi is located under wrapped_foo4.
+
+  >>> located_polloi.__parent__ is wrapped_foo4
+  True
+  >>> located_polloi.__name__
+  'my.other.key'
+
+but that's because we received a LocationProxy
+
+  >>> print type(located_polloi).__name__
+  LocationProxy
+
+If we unwrap located_polloi and look at it directly, we'll see it stores a
+reference to the real Foo object
+
+  >>> from zope.proxy import removeAllProxies
+  >>> removeAllProxies(located_polloi).__parent__ is foo4
+  True
+  >>> removeAllProxies(located_polloi).__name__
+  'my.other.key'
+

Modified: zope.annotation/branches/3.4/src/zope/annotation/factory.py
===================================================================
--- zope.annotation/branches/3.4/src/zope/annotation/factory.py	2008-08-26 20:58:46 UTC (rev 90364)
+++ zope.annotation/branches/3.4/src/zope/annotation/factory.py	2008-08-26 21:01:12 UTC (rev 90365)
@@ -18,6 +18,8 @@
 import zope.component
 import zope.interface
 import zope.location.location
+import zope.location.interfaces
+import zope.proxy
 
 import zope.annotation.interfaces
 
@@ -43,11 +45,14 @@
         except KeyError:
             result = factory()
             annotations[key] = result
-        # Location has to be set up late to allow location proxies
-        # to be applied, if needed. This does not trigger an event and is idempotent
-        # if location or containment is set up already.
-        located_result = zope.location.location.located(result, context, key)
-        return located_result
+            if zope.location.interfaces.ILocation.providedBy(result):
+                zope.location.location.locate(result,
+                        zope.proxy.removeAllProxies(context), key)
+        if not (zope.location.interfaces.ILocation.providedBy(result)
+                and result.__parent__ is context
+                and result.__name__ == key):
+            result = zope.location.location.LocationProxy(result, context, key)
+        return result
 
     # Convention to make adapter introspectable, used by apidoc
     getAnnotation.factory = factory



More information about the Checkins mailing list