Skip to content

Commit 516385c

Browse files
committed
py/objboundmeth: Optimise check for types in binary_op.
Signed-off-by: Damien George <damien@micropython.org>
1 parent 66c6235 commit 516385c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

py/objboundmeth.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ STATIC mp_obj_t bound_meth_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
9494
}
9595

9696
STATIC mp_obj_t bound_meth_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
97-
if (!mp_obj_is_type(rhs_in, &mp_type_bound_meth) || op != MP_BINARY_OP_EQUAL) {
97+
// The MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE flag is clear for this type, so if this
98+
// function is called with MP_BINARY_OP_EQUAL then lhs_in and rhs_in must have the
99+
// same type, which is mp_type_bound_meth.
100+
if (op != MP_BINARY_OP_EQUAL) {
98101
return MP_OBJ_NULL; // op not supported
99102
}
100103
mp_obj_bound_meth_t *lhs = MP_OBJ_TO_PTR(lhs_in);

tests/basics/boundmeth1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def h(self, a, b, c, d, e, f):
3333
except AttributeError:
3434
print("AttributeError")
3535

36+
print("--------")
37+
3638
# bound method comparison with same object
3739
a = A()
3840
m1 = a.f
@@ -51,6 +53,14 @@ def h(self, a, b, c, d, e, f):
5153
print(m2 == a1.f) # should result in False
5254
print(m1 != a2.f) # should result in True
5355

56+
# bound method comparison with non-bound-method objects
57+
print(A().f == None) # should result in False
58+
print(A().f != None) # should result in True
59+
print(None == A().f) # should result in False
60+
print(None != A().f) # should result in True
61+
62+
print("--------")
63+
5464
# bound method hashing
5565
a = A()
5666
m1 = a.f

0 commit comments

Comments
 (0)