From 516385c4ccbb1f7fa9dbd37a44ed8ae33dcf1942 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 13 Oct 2023 15:27:05 +1100 Subject: [PATCH] py/objboundmeth: Optimise check for types in binary_op. Signed-off-by: Damien George --- py/objboundmeth.c | 5 ++++- tests/basics/boundmeth1.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/py/objboundmeth.c b/py/objboundmeth.c index 10f52016c5..b0be810c5c 100644 --- a/py/objboundmeth.c +++ b/py/objboundmeth.c @@ -94,7 +94,10 @@ STATIC mp_obj_t bound_meth_unary_op(mp_unary_op_t op, mp_obj_t self_in) { } STATIC mp_obj_t bound_meth_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { - if (!mp_obj_is_type(rhs_in, &mp_type_bound_meth) || op != MP_BINARY_OP_EQUAL) { + // The MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE flag is clear for this type, so if this + // function is called with MP_BINARY_OP_EQUAL then lhs_in and rhs_in must have the + // same type, which is mp_type_bound_meth. + if (op != MP_BINARY_OP_EQUAL) { return MP_OBJ_NULL; // op not supported } mp_obj_bound_meth_t *lhs = MP_OBJ_TO_PTR(lhs_in); diff --git a/tests/basics/boundmeth1.py b/tests/basics/boundmeth1.py index e72153a41a..fe1b454688 100644 --- a/tests/basics/boundmeth1.py +++ b/tests/basics/boundmeth1.py @@ -33,6 +33,8 @@ try: except AttributeError: print("AttributeError") +print("--------") + # bound method comparison with same object a = A() m1 = a.f @@ -51,6 +53,14 @@ print(m1 == a2.f) # should result in False print(m2 == a1.f) # should result in False print(m1 != a2.f) # should result in True +# bound method comparison with non-bound-method objects +print(A().f == None) # should result in False +print(A().f != None) # should result in True +print(None == A().f) # should result in False +print(None != A().f) # should result in True + +print("--------") + # bound method hashing a = A() m1 = a.f