[Zodb-checkins] CVS: Zope3/src/zope/interface - declarations.py:1.2

Jim Fulton jim at zope.com
Wed Apr 30 17:13:24 EDT 2003


Update of /cvs-repository/Zope3/src/zope/interface
In directory cvs.zope.org:/tmp/cvs-serv29228/src/zope/interface

Modified Files:
	declarations.py 
Log Message:
zope.app.xml.w3schemalocations.setInstanceInterfaces had a more
sophisticated alogorithm for setting instance interface declarations
than the quick bit of code I put in
zope.interface.declarations.directlyProvides.  I moved this code to
directlyProvides and simplified the code in setInstanceInterfaces to
*just* call directlyProvides.



=== Zope3/src/zope/interface/declarations.py 1.1 => 1.2 ===
--- Zope3/src/zope/interface/declarations.py:1.1	Fri Apr 18 18:12:32 2003
+++ Zope3/src/zope/interface/declarations.py	Wed Apr 30 16:13:23 2003
@@ -21,11 +21,49 @@
 _ClassTypes = ClassType, type
 del ClassType
 
-def directlyProvides(object, *interfaces):
-    if isinstance(object, _ClassTypes):
-        object.__class_implements__ = interfaces
+def directlyProvides(ob, *interfaces):
+    
+    if isinstance(ob, _ClassTypes):
+        ob.__class_implements__ = interfaces
+        return
+
+
+    #XXX this is really a hack. interfacegeddon will hopefully provide a better
+    # way to do this
+    
+    # if there are no interfaces, then we go back to whatever the class
+    # implements
+    if not interfaces:
+        try:
+            del ob.__implements__
+        except AttributeError:
+            pass
+        return
+
+    cls = ob.__class__
+    implements = getattr(cls, '__implements__', ())
+    if isinstance(implements, tuple):
+        implements = list(implements)
     else:
-        object.__implements__ = interfaces
+        implements = [implements]
+
+    orig_implements = implements[:]
+        
+    for interface in interfaces:
+        if interface not in implements:
+            implements.append(interface)
+
+    # if there are no changes in the interfaces, go back to whatever
+    # the class implements
+    if implements == orig_implements:
+        try:
+            del ob.__implements__
+        except AttributeError:
+            pass
+        return
+
+    ob.__implements__ = tuple(implements)
+
 
 def classProvides(*interfaces):
     f = sys._getframe(1)




More information about the Zodb-checkins mailing list