diff --git a/py/objtype.c b/py/objtype.c index b760b3240c..ed323ed1d2 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -381,11 +381,12 @@ STATIC const qstr binary_op_method_name[] = { MP_BINARY_OP_INPLACE_MODULO, MP_BINARY_OP_INPLACE_POWER,*/ [MP_BINARY_OP_LESS] = MP_QSTR___lt__, - /*MP_BINARY_OP_MORE, - MP_BINARY_OP_EQUAL, - MP_BINARY_OP_LESS_EQUAL, - MP_BINARY_OP_MORE_EQUAL, - MP_BINARY_OP_NOT_EQUAL, + [MP_BINARY_OP_MORE] = MP_QSTR___gt__, + [MP_BINARY_OP_EQUAL] = MP_QSTR___eq__, + [MP_BINARY_OP_LESS_EQUAL] = MP_QSTR___le__, + [MP_BINARY_OP_MORE_EQUAL] = MP_QSTR___ge__, + /* + MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result */ [MP_BINARY_OP_IN] = MP_QSTR___contains__, /* diff --git a/py/qstrdefs.h b/py/qstrdefs.h index c83b54c241..2574443662 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -64,6 +64,10 @@ Q(__getattr__) Q(__del__) Q(__call__) Q(__lt__) +Q(__gt__) +Q(__eq__) +Q(__le__) +Q(__ge__) Q(micropython) Q(bytecode) diff --git a/tests/basics/class_binop.py b/tests/basics/class_binop.py new file mode 100644 index 0000000000..774f0afaf9 --- /dev/null +++ b/tests/basics/class_binop.py @@ -0,0 +1,31 @@ +class foo(object): + def __init__(self, value): + self.x = value + + def __eq__(self, other): + print('eq') + return self.x == other.x + + def __lt__(self, other): + print('lt') + return self.x < other.x + + def __gt__(self, other): + print('gt') + return self.x > other.x + + def __le__(self, other): + print('le') + return self.x <= other.x + + def __ge__(self, other): + print('ge') + return self.x >= other.x + +for i in range(3): + for j in range(3): + print(foo(i) == foo(j)) + print(foo(i) < foo(j)) + print(foo(i) > foo(j)) + print(foo(i) <= foo(j)) + print(foo(i) >= foo(j))