[Python-Dev] Exceptions in comparison operators (original) (raw)

Mark Shannon mark at hotpy.org
Tue Mar 13 19:35:33 CET 2012


Guido van Rossum wrote:

Mark, did you do anything with my reply?

Not yet.

I noticed the difference when developing my HotPy VM (latest incarnation thereof) which substitutes a sequence of low-level bytecodes for the high-level ones when tracing. (A bit like PyPy but much more Python-specific and amenable to interpretation, rather than compilation)

I generate all the code sequences for binary ops from a template and noticed the slight difference when running the test suite. My implementation of equals follows the same pattern as the arithmetic operators (which is why I was wondering if that were the correct behaviour).

My definition of op1 == op2:

def surrogate_eq(op1, op2): if $overrides(op1, op2, 'eq'): if op2?eq: result = op2$eq(op1) if result is not NotImplemented: return result if op1?eq: result = op1$eq(op2) if result is not NotImplemented: return result else: if op1?eq: result = op1$eq(op2) if result is not NotImplemented: return result if op2?eq: result = op2$eq(op1) if result is not NotImplemented: return result return op1 is op2

Where:

x$op means special lookup (bypassing the instance dictionary):

x?op means has the named special method i.e. any('op' in t.dict for t in type(op).mro))

and $overrides(op1, op2, 'xxx') means that type(op2) is a proper subtype of type(op1) and type(op1).dict['xxx'] != type(op2).dict['xxx']

It would appear that the current version is:

def surrogate_eq(op1, op2): if is_proper_subtype_of( type(op1), type(op1) ): if op2?eq: result = op2$eq(op1) if result is not NotImplemented: return result if op1?eq: result = op1$eq(op2) if result is not NotImplemented: return result else: if op1?eq: result = op1$eq(op2) if result is not NotImplemented: return result if op2?eq: result = op2$eq(op1) if result is not NotImplemented: return result return op1 is op2

Which means that == behaves differently to + for subtypes which do not override the eq method. Thus:

class MyValue1: def init(self, val): self.val = val

 def __lt__(self, other):
     print("lt")
     return self.val < other.val

 def __gt__(self, other):
     print("gt")
     return self.val > other.val

 def __add__(self, other):
     print("add")
     return self.val + other.val

 def __radd__(self, other):
     print("radd")
     return self.val + other.val

class MyValue2(MyValue1): pass

a = MyValue1(1) b = MyValue2(2)

print(a + b) print(a < b)

currently prints the following:

add 3 gt True

Cheers, Mark.

On Mon, Mar 5, 2012 at 10:41 AM, Guido van Rossum <guido at python.org> wrote: On Mon, Mar 5, 2012 at 4:41 AM, Mark Shannon <mark at hotpy.org> wrote:

Comparing two objects (of the same type for simplicity) involves a three stage lookup: The class has the operator C.eq It can be applied to operator (descriptor protocol): C().eq and it produces a result: C().eq(C())

Exceptions can be raised in all 3 phases, but an exception in the first phase is not really an error, its just says the operation is not supported. E.g. class C: pass C() == C() is False, rather than raising an Exception. If an exception is raised in the 3rd stage, then it is propogated, as follows: class C: def eq(self, other): raise Exception("I'm incomparable") C() == C() raises an exception However, if an exception is raised in the second phase (descriptor) then it is silenced: def noeq(self): raise Exception("I'm incomparable") class C: eq = property(noeq) C() == C() is False. But should it raise an exception? The behaviour for arithmetic is different. def noadd(self): raise Exception("I don't add up") class C: add = property(noadd) C() + C() raises an exception. So what is the "correct" behaviour? It is my opinion that comparisons should behave like arithmetic and raise an exception. I think you're probably right. This is one of those edge cases that are so rare (and always considered a bug in the user code) that we didn't define carefully what should happen. There are probably some implementation-specific reasons why it was done this way (comparisons use a very different code path from regular binary operators) but that doesn't sound like a very good reason. OTOH there is a difference: as you say, C() == C() is False when the class doesn't define eq, whereas C() + C() raises an exception if it doesn't define add. Still, this is more likely to have favored the wrong outcome for (2) by accident than by design. You'll have to dig through the CPython implementation and find out exactly what code needs to be changed before I could be sure though -- sometimes seeing the code jogs my memory. But I think of x==y as roughly equivalent to r = NotImplemented if hasattr(x, 'eq'): r = x.eq(y) if r is NotImplemented and hasattr(y, 'eq'): r = y.eq(x) if r is NotImplemented: r = False which would certainly suggest that (2) should raise an exception. A possibility is that the code looking for the eq attribute suppresses all exceptions instead of just AttributeError. If you change noeq() to return 42, for example, the comparison raises the much more reasonable TypeError: 'int' object is not callable. -- --Guido van Rossum (python.org/~guido)



More information about the Python-Dev mailing list