[Zope3-checkins] SVN: Zope3/trunk/ If the adapter factory returns `None` as adapter instance, the default

Stephan Richter srichter at cosmos.phy.tufts.edu
Sat Sep 25 13:05:28 EDT 2004


Log message for revision 27686:
  If the adapter factory returns `None` as adapter instance, the default
  is returned instead of `None`. For benefits and the discussions see
  http://mail.zope.org/pipermail/interface-dev/2004-September/000070.html
  


Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/interface/adapter.py
  U   Zope3/trunk/src/zope/interface/adapter.txt
  U   Zope3/trunk/src/zope/interface/tests/test_adapter.py


-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2004-09-25 01:07:46 UTC (rev 27685)
+++ Zope3/trunk/doc/CHANGES.txt	2004-09-25 17:05:28 UTC (rev 27686)
@@ -10,6 +10,10 @@
 
     New features
 
+      - If the adapter factory returns `None` as adapter instance, the default
+        is returned instead of `None`. For benefits and the discussions see
+        http://mail.zope.org/pipermail/interface-dev/2004-September/000070.html
+
       - Added new API method `getAdapters(objects, provided)` to `zapi`. It
         will return all available adapters for this configuration. Note that
         every name will be only returned once.

Modified: Zope3/trunk/src/zope/interface/adapter.py
===================================================================
--- Zope3/trunk/src/zope/interface/adapter.py	2004-09-25 01:07:46 UTC (rev 27685)
+++ Zope3/trunk/src/zope/interface/adapter.py	2004-09-25 17:05:28 UTC (rev 27686)
@@ -387,11 +387,16 @@
 
         When called from Interface.__adapt__, only the interface and
         object parameters will be passed.
-        
+
+        If the factory produces `None`, then the default is returned. This
+        allows us to prevent adaptation (if desired) and make the factory
+        decide whether an adapter will be available.
         """
         factory = self.lookup1(providedBy(object), interface, name)
         if factory is not None:
-            return factory(object)
+            adapter = factory(object)
+            if adapter is not None:
+                return adapter
 
         return default
 

Modified: Zope3/trunk/src/zope/interface/adapter.txt
===================================================================
--- Zope3/trunk/src/zope/interface/adapter.txt	2004-09-25 01:07:46 UTC (rev 27685)
+++ Zope3/trunk/src/zope/interface/adapter.txt	2004-09-25 17:05:28 UTC (rev 27686)
@@ -170,6 +170,30 @@
   >>> y.context is x
   True
 
+When the adapter factory produces `None`, then this is treated as if no
+adapter has been found. This allows us to prevent adaptation (when desired)
+and let the adapter factory determine whether adaptation is possible based on
+the state of the object being adapted.
+
+  >>> def factory(context):
+  ...     if context.name == 'object':
+  ...         return 'adapter'
+  ...     return None
+
+  >>> class Object(object):
+  ...     zope.interface.implements(IR)
+  ...     name = 'object'
+
+  >>> registry.register([IR], IP1, 'conditional', factory) 
+  >>> obj = Object()
+  >>> registry.queryAdapter(obj, IP1, 'conditional')
+  'adapter'
+  >>> obj.name = 'no object'
+  >>> registry.queryAdapter(obj, IP1, 'conditional') is None
+  True
+  >>> registry.queryAdapter(obj, IP1, 'conditional', 'default')
+  'default'
+
 An alternate method that provides the same function as `queryAdapter()` is
 `adapter_hook()`::
 

Modified: Zope3/trunk/src/zope/interface/tests/test_adapter.py
===================================================================
--- Zope3/trunk/src/zope/interface/tests/test_adapter.py	2004-09-25 01:07:46 UTC (rev 27685)
+++ Zope3/trunk/src/zope/interface/tests/test_adapter.py	2004-09-25 17:05:28 UTC (rev 27686)
@@ -205,7 +205,32 @@
     1
     """
 
+def test_adapter_hook_with_factory_producing_None():
+    """
+    >>> registry = AdapterRegistry()
+    >>> default = object()
+    
+    >>> class Object1(object):
+    ...     zope.interface.implements(IF0)
+    >>> class Object2(object):
+    ...     zope.interface.implements(IF0)
 
+    >>> def factory(context):
+    ...     if isinstance(context, Object1):
+    ...         return 'adapter'
+    ...     return None
+
+    >>> registry.register([IF0], IB0, '', factory)
+
+    >>> registry.adapter_hook(IB0, Object1())
+    'adapter'
+    >>> registry.adapter_hook(IB0, Object2()) is None
+    True
+    >>> registry.adapter_hook(IB0, Object2(), default=default) is default
+    True
+    """
+    
+
 def test_suite():
     from zope.testing.doctestunit import DocFileSuite
     return unittest.TestSuite((



More information about the Zope3-Checkins mailing list