[Checkins] SVN: zope.interface/trunk/ verifyObject: use getattr instead of hasattr to test for object attributes

Thomas Lotze tl at gocept.com
Tue Oct 28 13:12:35 EDT 2008


Log message for revision 92658:
  verifyObject: use getattr instead of hasattr to test for object attributes
  in order to let exceptions other than AttributeError raised by properties
  propagate to the caller
  

Changed:
  U   zope.interface/trunk/CHANGES.txt
  U   zope.interface/trunk/src/zope/interface/verify.py
  U   zope.interface/trunk/src/zope/interface/verify.txt

-=-
Modified: zope.interface/trunk/CHANGES.txt
===================================================================
--- zope.interface/trunk/CHANGES.txt	2008-10-28 17:04:41 UTC (rev 92657)
+++ zope.interface/trunk/CHANGES.txt	2008-10-28 17:12:35 UTC (rev 92658)
@@ -5,7 +5,11 @@
 3.5.1 (unreleased)
 ==================
 
+- verifyObject: use getattr instead of hasattr to test for object attributes
+  in order to let exceptions other than AttributeError raised by properties
+  propagate to the caller
 
+
 ==================
 3.5.0 (2008-10-26)
 ==================

Modified: zope.interface/trunk/src/zope/interface/verify.py
===================================================================
--- zope.interface/trunk/src/zope/interface/verify.py	2008-10-28 17:04:41 UTC (rev 92657)
+++ zope.interface/trunk/src/zope/interface/verify.py	2008-10-28 17:12:35 UTC (rev 92658)
@@ -52,7 +52,9 @@
 
     # Here the `desc` is either an `Attribute` or `Method` instance
     for name, desc in iface.namesAndDescriptions(1):
-        if not hasattr(candidate, name):
+        try:
+            attr = getattr(candidate, name)
+        except AttributeError:
             if (not isinstance(desc, Method)) and vtype == 'c':
                 # We can't verify non-methods on classes, since the
                 # class may provide attrs in it's __init__.
@@ -60,7 +62,6 @@
 
             raise BrokenImplementation(iface, name)
 
-        attr = getattr(candidate, name)
         if not isinstance(desc, Method):
             # If it's not a method, there's nothing else we can test
             continue

Modified: zope.interface/trunk/src/zope/interface/verify.txt
===================================================================
--- zope.interface/trunk/src/zope/interface/verify.txt	2008-10-28 17:04:41 UTC (rev 92657)
+++ zope.interface/trunk/src/zope/interface/verify.txt	2008-10-28 17:12:35 UTC (rev 92658)
@@ -87,8 +87,8 @@
 BrokenImplementation: An object has failed to implement interface <InterfaceClass __builtin__.IFoo>
     The x attribute was not provided.
 
-Any other exception raised by a property should propagate to the caller of
-``verifyObject``, but currently the attribute is just considered missing:
+Any other exception raised by a property will propagate to the caller of
+``verifyObject``:
 
 >>> class Foo(object):
 ...     implements(IFoo)
@@ -98,8 +98,7 @@
 
 >>> verifyObject(IFoo, Foo())
 Traceback (most recent call last):
-BrokenImplementation: An object has failed to implement interface <InterfaceClass __builtin__.IFoo>
-    The x attribute was not provided.
+Exception
 
 Of course, broken properties that are not required by the interface don't do
 any harm:



More information about the Checkins mailing list