[Zope-Checkins] CVS: Zope3/lib/python/Interface - Implements.py:1.1.2.4

Steve Alexander steve@cat-box.net
Tue, 21 May 2002 10:26:20 -0400


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

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

I'm assuming that the Interfaces package is supposed to not break for
earlier versions of python.

Fixed bug where the message given with a BadImplements exception required
the implements object to have a __class__ attribute.

Made visitImplements method respect the __class__ attribute of the
implements argument, if it has one. If it has no __class__ attribute,
it falls back to using isinstance or type, as before.

Using the __class__ attribute is needed for using the Interfaces
package in the presence of transparent proxies.
This won't be so necessary in Python 2.3, when isinstance will take
account of the __class__ attribute.



=== Zope3/lib/python/Interface/Implements.py 1.1.2.3 => 1.1.2.4 ===
     This does not, and should not, visit superinterfaces.
     """
-
-    if isinstance(implements, InterfaceClass):
+    # this allows us to work with proxy wrappers in Python 2.2,
+    # yet remain compatible with earlier versions of python.
+    implements_class = getattr(implements, '__class__', None)
+    
+    if implements_class == InterfaceClass or \
+       isinstance(implements, InterfaceClass):
         return visitor(implements)
     elif implements == CLASS_INTERFACES:
         klass = getattr(object, '__class__', None)
@@ -68,22 +72,29 @@
             i = getImplementsOfInstances(klass)
             if i:
                 return visitImplements(i, object, visitor, getInterface)
-    elif type(implements) is StringType:
+    elif implements_class == StringType or type(implements) is StringType:
         if getInterface is not None:
             # Look up a named interface.
             i = getInterface(object, implements)
             if i is not None:
                 return visitImplements(i, object, visitor, getInterface)
-    elif type(implements) is TupleType:
+    elif implements_class == TupleType or type(implements) is TupleType:
         for i in implements:
             r = visitImplements(i, object, visitor, getInterface)
             if r:
                 # If the visitor returns anything true, stop.
                 return r
     else:
+        if implements_class is not None and \
+           type(implements) != implements_class:
+            raise Exceptions.BadImplements(
+                """__implements__ should be an interface or tuple,
+                not a %s pretending to be a %s"""
+                % (type(implements).__name__, implements_class.__name__)
+                )
         raise Exceptions.BadImplements(
             """__implements__ should be an interface or tuple,
-            not a %s""" % implements.__class__.__name__)
+            not a %s""" % type(implements).__name__)
     return None