[Zope3-checkins] SVN: Zope3/trunk/src/zope/component/ Emit a sensible error message if you use provideAdapter incorrectly.

Marius Gedminas marius at pov.lt
Thu Dec 1 07:48:36 EST 2005


Log message for revision 40441:
  Emit a sensible error message if you use provideAdapter incorrectly.
  
  I had written code like this
  
    zope.component.provideAdapter(myAdapter, adapts=IFoo, provides=IBar)
  
  and got a completely incomprehensible error message
  
    TypeError: ('__getitem__', <InterfaceClass zope.interface.interfaces.IInterface>)
  
  because '__getitem__' happened to be the first name returned by list(IFoo).
  After my checkin the error message will say
  
    TypeError: the required argument should be a list of interfaces, not a single interface
  
  

Changed:
  U   Zope3/trunk/src/zope/component/site.py
  U   Zope3/trunk/src/zope/component/tests.py

-=-
Modified: Zope3/trunk/src/zope/component/site.py
===================================================================
--- Zope3/trunk/src/zope/component/site.py	2005-12-01 02:15:15 UTC (rev 40440)
+++ Zope3/trunk/src/zope/component/site.py	2005-12-01 12:48:35 UTC (rev 40441)
@@ -164,6 +164,9 @@
         >>> registry.queryMultiAdapter((O1(), O2()), R1, '').__class__
         <class 'zope.component.site.O3'>
         """
+        if IInterface.providedBy(required):
+            raise TypeError('the required argument should be a list of'
+                            ' interfaces, not a single interface')
         ifaces = []
         for iface in required:
             if not IInterface.providedBy(iface) and iface is not None:

Modified: Zope3/trunk/src/zope/component/tests.py
===================================================================
--- Zope3/trunk/src/zope/component/tests.py	2005-12-01 02:15:15 UTC (rev 40440)
+++ Zope3/trunk/src/zope/component/tests.py	2005-12-01 12:48:35 UTC (rev 40441)
@@ -257,6 +257,15 @@
       >>> zope.component.getGlobalSiteManager().provideAdapter(
       ...     (I1,), I2, '', Comp)
 
+    You should get a sensible error message if you forget that the 'requires'
+    argument is supposed to be a sequence
+
+      >>> zope.component.getGlobalSiteManager().provideAdapter(
+      ...     I1, I2, '', Comp)
+      Traceback (most recent call last):
+        ...
+      TypeError: the required argument should be a list of interfaces, not a single interface
+
     You can now simply access the adapter using the `getAdapter()` API
     function:
 



More information about the Zope3-Checkins mailing list