check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
authorSylvain Thenault <sylvain.thenault@logilab.fr>
Fri, 05 Dec 2008 18:07:37 +0100
changeset 177 73aa03734425
parent 176 312024cbe717
child 178 b5478a96da3d
check we don't get selection ambiguity: if yes, log error in production env, raise in other modes
vregistry.py
--- a/vregistry.py	Fri Dec 05 18:06:58 2008 +0100
+++ b/vregistry.py	Fri Dec 05 18:07:37 2008 +0100
@@ -334,14 +334,24 @@
 
         raise NoSelectableObject if not object apply
         """
-        score, winner = 0, None
+        score, winners = 0, []
         for vobject in vobjects:
             vobjectscore = vobject.__select__(*args, **kwargs)
             if vobjectscore > score:
-                score, winner = vobjectscore, vobject
-        if winner is None:
+                score, winners = vobjectscore, [vobject]
+            elif vobjectscore > 0 and vobjectscore == score:
+                winners.append(vobject)
+        if not winners:
             raise NoSelectableObject('args: %s\nkwargs: %s %s'
                                      % (args, kwargs.keys(), [repr(v) for v in vobjects]))
+        if len(winners) > 1:
+            if self.config.mode == 'installed':
+                self.error('select ambiguity, args: %s\nkwargs: %s %s',
+                           args, kwargs.keys(), [repr(v) for v in winners])
+            else:
+                raise Exception('select ambiguity, args: %s\nkwargs: %s %s'
+                                % (args, kwargs.keys(), [repr(v) for v in winners]))
+        winner = winners[0]
         # return the result of the .selected method of the vobject
         return winner.selected(*args, **kwargs)