[Zope-Checkins] CVS: Zope3/lib/python/Interface - verify.py:1.3.136.2

Tres Seaver tseaver@zope.com
Fri, 30 Nov 2001 23:40:37 -0500


Update of /cvs-repository/Zope3/lib/python/Interface
In directory cvs.zope.org:/tmp/cvs-serv12451

Modified Files:
      Tag: Zope-3x-branch
	verify.py 
Log Message:


 - Allow 'verify' to check modules and instances, as well as classes;
   rename second arg 'klass' => 'candidate' to reflect this semantic.

 - Add test for module verification.


=== Zope3/lib/python/Interface/verify.py 1.3.136.1 => 1.3.136.2 ===
-from Exceptions import BrokenImplementation, DoesNotImplement, BrokenMethodImplementation
+from Exceptions import BrokenImplementation, DoesNotImplement
+from Exceptions import BrokenMethodImplementation
 from Method import Method
+
+from iclass import ClassTypes
+
 import types
 
-def verify(iface, klass, tentative=0):
+def verify(iface, candidate, tentative=0):
     """
 
-    Verify that 'klass' might correctly implements 'iface'.  This involves:
+    Verify that 'candidate' might correctly implements 'iface'.
+    This involves:
 
-      o Making sure the klass defines all the necessary methods
+      o Making sure the candidate defines all the necessary methods
 
       o Making sure the methods have the correct signature
 
-      o Making sure the klass asserts that it implements the interface
+      o Making sure the candidate asserts that it implements the interface
 
     Note that this isn't the same as verifying that the class does
     implement the interface.
 
     """
 
-    if not tentative and not iface.isImplementedByInstancesOf(klass):
-        raise DoesNotImplement(iface)
+    if type( candidate ) in ClassTypes:
+        tester = iface.isImplementedByInstancesOf
+    else:
+        tester = iface.isImplementedBy
+
+    if not tentative and not tester( candidate ):
+            raise DoesNotImplement(iface)
 
     for n, d in iface.namesAndDescriptions():
-        if not hasattr(klass, n):
+        if not hasattr(candidate, n):
             raise BrokenImplementation(iface, n)
 
-        attr = getattr(klass, n)
+        attr = getattr(candidate, n)
         if type(attr) is types.FunctionType:
             # should never get here
             meth = Method().fromFunction(attr, n)