# HG changeset patch # User Paul Boddie # Date 1481374964 -3600 # Node ID 32942668c75caeace7969d1246450aa35160a36c # Parent 0978d9f032c93009afb36101621b9dc3fe4dd6ba Handle TypeError, not AttributeError, when operations are not found on operands. Added testing of operator methods in new classes. diff -r 0978d9f032c9 -r 32942668c75c lib/operator/core.py --- a/lib/operator/core.py Sat Dec 10 00:56:32 2016 +0100 +++ b/lib/operator/core.py Sat Dec 10 14:02:44 2016 +0100 @@ -37,7 +37,7 @@ try: fn = left_accessor(a) - except AttributeError: + except TypeError: pass else: result = fn(b) @@ -49,7 +49,7 @@ try: fn = right_accessor(b) - except AttributeError: + except TypeError: pass else: result = fn(a) @@ -76,7 +76,7 @@ try: fn = accessor(a) - except AttributeError: + except TypeError: pass else: result = fn() @@ -106,7 +106,7 @@ try: fn = augmented_accessor(a) - except AttributeError: + except TypeError: pass else: result = fn(b) diff -r 0978d9f032c9 -r 32942668c75c tests/operator_support.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/operator_support.py Sat Dec 10 14:02:44 2016 +0100 @@ -0,0 +1,28 @@ +class A: + def __init__(self, x): + self.x = x + def __sub__(self, other): + return self.x - other.x + +class B: + def __init__(self, x): + self.x = x + def __rsub__(self, other): + return other.x - self.x + +class C: + def __init__(self, x): + self.x = x + +a = A(10) +b = B(5) +c = C(3) + +print a - b # 5 +print c - b # -2 +print a - c # 7 + +try: + print b - c # should raise an exception +except TypeError: + print "b - c: b and c do not respectively support the __sub__ and __rsub__ operations"