[Checkins] SVN: Zope3/trunk/ - fixed annotation factory to work together with containment proxies

Christian Theune ct at gocept.com
Fri Oct 20 09:52:31 EDT 2006


Log message for revision 70846:
   - fixed annotation factory to work together with containment proxies 
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/annotation/README.txt
  U   Zope3/trunk/src/zope/annotation/factory.py
  U   Zope3/trunk/src/zope/annotation/tests/test_attributeannotations.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2006-10-20 13:20:35 UTC (rev 70845)
+++ Zope3/trunk/doc/CHANGES.txt	2006-10-20 13:52:31 UTC (rev 70846)
@@ -137,6 +137,9 @@
 
     Bug fixes
 
+      - Fixed zope.annotation.factory to correctly setup containment for
+        objects that do not implement IContained.
+
       - Fixed encoding of newlines, carriage returns, and tabs when
         encoding attributes for widgets so we're consistent under all
         Python 2.4.x versions (including 2.4.4, which failed tests before).

Modified: Zope3/trunk/src/zope/annotation/README.txt
===================================================================
--- Zope3/trunk/src/zope/annotation/README.txt	2006-10-20 13:20:35 UTC (rev 70845)
+++ Zope3/trunk/src/zope/annotation/README.txt	2006-10-20 13:52:31 UTC (rev 70846)
@@ -45,7 +45,7 @@
 Note that we do not need to specify what the adapter provides or what
 it adapts - we already do this on the annotation class itself.
 
-Now we let's make an instance of `Foo`, and make an annotation for it.
+Now let's make an instance of `Foo`, and make an annotation for it.
 
   >>> foo = Foo()
   >>> bar = IBar(foo)
@@ -92,3 +92,29 @@
   >>> isinstance(IHoi(foo), Hoi)
   True
 
+Containment
+-----------
+
+Annotation factories are put into the containment hierarchy with their parent
+pointing to the annotated object and the name to the dotted name of the
+annotation's class (or the name the adapter was registered under):
+
+  >>> foo3 = Foo()
+  >>> new_hoi = IHoi(foo3)
+  >>> new_hoi.__parent__
+  <Foo object at 0x...>
+  >>> new_hoi.__name__
+  'my.unique.key'
+
+Please notice, that our Hoi object does not implement IContained, so a
+containment proxy will be used. This has to be re-established every time we
+retrieve the object
+
+(Guard against former bug: proxy wasn't established when the annotation
+existed already.)a
+
+  >>> old_hoi = IHoi(foo3)
+  >>> old_hoi.__parent__
+  <Foo object at 0x...>
+  >>> old_hoi.__name__
+  'my.unique.key'

Modified: Zope3/trunk/src/zope/annotation/factory.py
===================================================================
--- Zope3/trunk/src/zope/annotation/factory.py	2006-10-20 13:20:35 UTC (rev 70845)
+++ Zope3/trunk/src/zope/annotation/factory.py	2006-10-20 13:52:31 UTC (rev 70846)
@@ -37,13 +37,16 @@
     def getAnnotation(context):
         annotations = IAnnotations(context)
         try:
-            return annotations[key]
+            result = annotations[key]
         except KeyError:
             result = factory()
             annotations[key] = result
-            zope.app.container.contained.contained(
-                result, context, key)
-            return result
+        # Containment has to be set up late to allow containment proxies
+        # to be applied, if needed. This does not trigger an event and is idempotent
+        # if containment is set up already.
+        contained_result = zope.app.container.contained.contained(
+            result, context, key)
+        return contained_result
 
     # Convention to make adapter introspectable, used by apidoc
     getAnnotation.factory = factory 

Modified: Zope3/trunk/src/zope/annotation/tests/test_attributeannotations.py
===================================================================
--- Zope3/trunk/src/zope/annotation/tests/test_attributeannotations.py	2006-10-20 13:20:35 UTC (rev 70845)
+++ Zope3/trunk/src/zope/annotation/tests/test_attributeannotations.py	2006-10-20 13:52:31 UTC (rev 70846)
@@ -37,14 +37,15 @@
 def setUp(test=None):
     cleanup.setUp()
     component.provideAdapter(AttributeAnnotations)
-    
+
 def tearDown(test=None):
     cleanup.tearDown()
-    
+
 def test_suite():
     return unittest.TestSuite((
         unittest.makeSuite(AttributeAnnotationsTest),
-        doctest.DocFileSuite('../README.txt', setUp=setUp, tearDown=tearDown)
+        doctest.DocFileSuite('../README.txt', setUp=setUp, tearDown=tearDown,
+            optionflags=doctest.ELLIPSIS)
         ))
 
 if __name__=='__main__':



More information about the Checkins mailing list