[Zope3-checkins] CVS: Zope3/src/zope/interface - interface.py:1.4

Jim Fulton jim@zope.com
Mon, 20 Jan 2003 15:02:31 -0500


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

Modified Files:
	interface.py 
Log Message:
Added a a simple cache for isImplementedBy.

Also added a cache for repr. This was needed because the repr of an
interface is needed for generating cache keys, and generation of cache
keys wants to be fast.

In the long run, we want some other way to generate cache keys more
quickly.



=== Zope3/src/zope/interface/interface.py 1.3 => 1.4 ===
--- Zope3/src/zope/interface/interface.py:1.3	Mon Dec 30 09:01:45 2002
+++ Zope3/src/zope/interface/interface.py	Mon Jan 20 15:02:29 2003
@@ -134,7 +134,7 @@
 
     def extends(self, other, strict=True):
         """Does an interface extend another?"""
-        if not strict and self is other:
+        if not strict and self == other:
             return True
 
         for b in self.__bases__:
@@ -150,11 +150,28 @@
 
     def isImplementedBy(self, object):
         """Does the given object implement the interface?"""
-        i = getImplements(object)
-        if i is not None:
-            return visitImplements(
-                i, object, self.isEqualOrExtendedBy, self._getInterface)
-        return False
+        
+        # OPT Cache implements lookups
+        implements = getImplements(object)
+        if implements is None:
+            return False
+
+        cache = getattr(self, '_v_cache', self)
+        if cache is self:
+            cache = self._v_cache = {}
+
+        key = `implements`
+
+        r = cache.get(key)
+        if r is None:
+            r = bool(
+                visitImplements(
+                  implements, object, self.isEqualOrExtendedBy,
+                  self._getInterface)
+                )
+            cache[key] = r
+            
+        return r
 
     def isImplementedByInstancesOf(self, klass):
         """Do instances of the given class implement the interface?"""
@@ -250,11 +267,15 @@
         for b in self.__bases__: b.__d(dict)
 
     def __repr__(self):
-        name = self.__name__
-        m = self.__module__
-        if m:
-            name = '%s.%s' % (m, name)
-        return "<%s %s at %x>" % (self.__class__.__name__, name, id(self))
+        r = getattr(self, '_v_repr', self)
+        if r is self:
+            name = self.__name__
+            m = self.__module__
+            if m:
+                name = '%s.%s' % (m, name)
+            r = "<%s %s at %x>" % (self.__class__.__name__, name, id(self))
+            self._v_repr = r
+        return r
 
     def __reduce__(self):
         return self.__name__