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

Jim Fulton jim at zope.com
Sun Oct 12 18:34:05 EDT 2003


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

Modified Files:
      Tag: adaptergeddon-branch
	declarations.py 
Log Message:
Refactord directly provided specifications so that they include the
class specification. This eliminated the need for
"ObjectSpecification" in most cases.


=== Zope3/src/zope/interface/declarations.py 1.17.10.3 => 1.17.10.4 ===
--- Zope3/src/zope/interface/declarations.py:1.17.10.3	Sun Oct 12 16:40:12 2003
+++ Zope3/src/zope/interface/declarations.py	Sun Oct 12 18:34:03 2003
@@ -455,10 +455,10 @@
     if (locals is frame.f_globals) or ('__module__' not in locals):
         raise TypeError(name+" can be used only from a class definition.")
 
-    if '__providedBy__' in locals:
+    if '__implements__' in locals:
         raise TypeError(name+" can be used only once in a class definition.")
 
-    locals["__implements__"] = interfaces, classImplements
+    locals['__implements__'] = interfaces, classImplements
     addClassAdvisor(_implements_advice, depth=3)
 
 def implements(*interfaces):
@@ -595,8 +595,12 @@
     When an object is pickled, we pickle the interfaces that it implements.
     """
 
+    def __init__(self, cls, *interfaces):
+        self.__args = (cls, ) + interfaces
+        Declaration.__init__(self, *(interfaces + (implementedBy(cls), )))
+
     def __reduce__(self):
-        return Provides, self.__bases__
+        return Provides, self.__args
 
     __module__ = 'zope.interface'
 
@@ -729,8 +733,10 @@
             raise TypeError("Attempt to make an interface declaration on a "
                             "non-descriptor-aware class")
 
-    interface = _normalizeargs(interfaces)
-    object.__provides__ = Provides(*interfaces)
+    interfaces = _normalizeargs(interfaces)
+    if cls is None:
+        cls = type(object)
+    object.__provides__ = Provides(cls, *interfaces)
 
 def directlyProvidedBy(object):
     """Return the interfaces directly provided by the given object
@@ -738,7 +744,10 @@
     The value returned is an IDeclaration.
 
     """
-    return getattr(object, "__provides__", _empty)
+    provides = getattr(object, "__provides__", None)
+    if provides is None:
+        return _empty
+    return Declaration(provides.__bases__[:-1])
 
 def classProvides(*interfaces):
     """Declare interfaces provided directly by a class
@@ -807,14 +816,21 @@
 
     # Try to make sure we were called from a class def
     if (locals is frame.f_globals) or ('__module__' not in locals):
-        raise TypeError(
-            "classProvides can only be used from a class definition.")
+        raise TypeError(name+" can be used only from a class definition.")
 
     if '__provides__' in locals:
         raise TypeError(
             "classProvides can only be used once in a class definition.")
 
-    locals["__provides__"] = Provides(*_normalizeargs(interfaces))
+    locals["__provides__"] = _normalizeargs(interfaces)
+
+    addClassAdvisor(_classProvides_advice, depth=2)
+
+def _classProvides_advice(cls):
+    interfaces = cls.__dict__['__provides__']
+    del cls.__provides__
+    directlyProvides(cls, *interfaces)
+    return cls
 
 def moduleProvides(*interfaces):
     """Declare interfaces provided by a module
@@ -853,7 +869,10 @@
         raise TypeError(
             "moduleProvides can only be used once in a module definition.")
 
-    locals["__provides__"] = Provides(*_normalizeargs(interfaces))
+    module = sys.modules[__name__]
+
+    locals["__provides__"] = Provides(type(module),
+                                      *_normalizeargs(interfaces))
 
 ##############################################################################
 #
@@ -952,32 +971,21 @@
 
     """
 
-    ospec = InstanceDeclarations.get((direct, cls))
-    if ospec is None:
-        ospec = Declaration(direct, implementedBy(cls))
-        InstanceDeclarations[(direct, cls)] = ospec
-
-    return ospec
-
+    return Provides(cls, direct)
 
 def getObjectSpecification(ob):
 
     provides = getattr(ob, '__provides__', None)
+    if provides is not None:
+        return provides
+    
     try:
         cls = ob.__class__
     except AttributeError:
         # We can't get the class, so just consider provides
-        if provides is not None:
-            # Just use the provides spec
-            return provides
-
-        # No interfaces
         return _empty
 
-    if provides is None:
-        return implementedBy(cls)
-
-    return ObjectSpecification(provides, cls)
+    return implementedBy(cls)
 
 def providedBy(ob):
 
@@ -1031,8 +1039,12 @@
         
         if inst is None:
             return getObjectSpecification(cls)
-        else:
-            return getObjectSpecification(inst)
+
+        provides = getattr(inst, '__provides__', None)
+        if provides is not None:
+            return provides
+
+        return implementedBy(cls)
 
 objectSpecificationDescriptor = ObjectSpecificationDescriptor()
 




More information about the Zodb-checkins mailing list