[Zodb-checkins] CVS: ZODB4/Interface - Implements.py:1.4

Jeremy Hylton jeremy@zope.com
Wed, 9 Oct 2002 18:34:52 -0400


Update of /cvs-repository/ZODB4/Interface
In directory cvs.zope.org:/tmp/cvs-serv28717/Interface

Modified Files:
	Implements.py 
Log Message:
Improvements depending on Python 2.2+ to all PersistentMetaClass to work.

getImplementsOfInstances() should use an isinstance() test rather than
direct type comparisons.  type(Persistent) is a subtype of type but is
not equal to type.

Simplify defintion of ClassTypes: type(object) == type

Avoid hasattr() followed by getattr() of the same object, since this
gets the attribute twice.

Eliminate performance hack default arg in getImplementsOfInstances().



=== ZODB4/Interface/Implements.py 1.3 => 1.4 ===
--- ZODB4/Interface/Implements.py:1.3	Mon Jun 10 19:27:40 2002
+++ ZODB4/Interface/Implements.py	Wed Oct  9 18:34:51 2002
@@ -27,33 +27,34 @@
 # what its class supports.
 CLASS_INTERFACES = 1
 
-ClassTypes = (ClassType, type(object))
+ClassTypes = ClassType, type
 
-_typeImplements={}
+_typeImplements = {}
 
 def getImplements(object):
     t = type(object)
     if t in ClassTypes:
-        if hasattr(object, '__class_implements__'):
-            return object.__class_implements__
-    elif hasattr(object, '__implements__'):
-        return object.__implements__
-
-    return _typeImplements.get(t, None)
+        ci = getattr(object, '__class_implements__', None)
+        if ci is not None:
+            return ci
+    else:
+        impl = getattr(object, '__implements__', None)
+        if impl is not None:
+            return impl
+
+    return _typeImplements.get(t)
 
 
-def getImplementsOfInstances(klass, tiget=_typeImplements.get):
-    if type(klass) in ClassTypes:
-        if hasattr(klass, '__implements__'):
-            return klass.__implements__
-        else:
-            return None
+def getImplementsOfInstances(klass):
+    if isinstance(klass, ClassTypes):
+        return getattr(klass, '__implements__', None)
     else:
-        return tiget(klass, None)
+        return _typeImplements.get(klass)
 
 
 def visitImplements(implements, object, visitor, getInterface=None):
-    """
+    """Call visitor for each interace.
+    
     Visits the interfaces described by an __implements__ attribute,
     invoking the visitor for each interface object.
     If the visitor returns anything true, the loop stops.
@@ -102,7 +103,7 @@
     """Assign a set of interfaces to a Python type such as int, str, tuple,
        list and dict.
     """
-    _typeImplements[type]=interfaces
+    _typeImplements[type] = interfaces
 
 def objectImplements(object, getInterface=None):
     r = []
@@ -120,7 +121,6 @@
     visitImplements(implements, klass, r.append, getInterface)
     return r
 
-
 def _flatten(i, append):
     append(i)
     bases = i.getBases()
@@ -128,7 +128,6 @@
         for b in bases:
             _flatten(b, append)
 
-
 def flattenInterfaces(interfaces, remove_duplicates=1):
     res = []
     for i in interfaces:
@@ -151,7 +150,7 @@
     if check:
         verifyClass(interface, klass, tentative=1)
 
-    old=getattr(klass, '__implements__', None)
+    old = getattr(klass, '__implements__', None)
     if old is None:
         klass.__implements__ = interface
     else: