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

Jim Fulton jim at zope.com
Thu Mar 18 07:19:29 EST 2004


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

Modified Files:
	adapter.py 
Log Message:
Fixed an internal bug, revealed by functional tests,
that caused some non-determinism in multi-adapter lookup
due to a faulty sorting approach.


=== Zope3/src/zope/interface/adapter.py 1.10 => 1.11 ===
--- Zope3/src/zope/interface/adapter.py:1.10	Mon Mar 15 15:41:55 2004
+++ Zope3/src/zope/interface/adapter.py	Thu Mar 18 07:19:29 2004
@@ -180,7 +180,6 @@
                         oldwithobs.update(withobs)
 
         # Now flatten with mappings to tuples
-        withcmp_ = withcmp
         for key, v in implied.iteritems():
             if isinstance(key, tuple) and key[0] == 's':
                 # subscriptions
@@ -192,10 +191,7 @@
                     if isinstance(value, dict):
                         # We have {with -> value}
                         # convert it to sorted [(with, value]
-                        value = value.items()
-                        value.sort(withcmp_)
-                        byname[name] = value
-
+                        byname[name] = orderwith(value)
 
         self.get = implied.get
 
@@ -254,13 +250,31 @@
     def __repr__(self):
         return '<%s(%s)>' % (self.__class__.__name__, self.spec())
 
-def withcmp((with1, v1), (with2, v2)):
+
+def orderwith(bywith):
+
+    # Convert {with -> adapter} to withs, [(with, value)]
+    # such that there are no i, j, i < j, such that
+    #           withs[j][0] extends withs[i][0].
+
+    withs = []
+    for with, value in bywith.iteritems():
+        for i, (w, v) in enumerate(withs):
+            if withextends(with, w):
+                withs.insert(i, (with, value))
+                break
+        else:
+            withs.append((with, value))
+            
+    return withs
+    
+def withextends(with1, with2):
     for spec1, spec2 in zip(with1, with2):
         if spec1.extends(spec2):
-            return -1
-        if spec2.extends(spec1):
-            return 1
-    return 0
+            return True
+        if spec1 != spec2:
+            break
+    return False
 
 
 class AdapterRegistry(object):




More information about the Zope3-Checkins mailing list