Skip to content

Commit e22d76e

Browse files
committed
py: Fix up object equality test.
It regressed a bit after implementing float/complex equality. Now it should be improved, and support more equality tests.
1 parent a9ddd6d commit e22d76e

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

py/obj.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,35 @@ machine_int_t mp_obj_hash(mp_obj_t o_in) {
143143
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2) {
144144
if (o1 == o2) {
145145
return true;
146-
} else if (o1 == mp_const_none || o2 == mp_const_none) {
146+
}
147+
if (o1 == mp_const_none || o2 == mp_const_none) {
147148
return false;
148-
} else if (MP_OBJ_IS_SMALL_INT(o1) || MP_OBJ_IS_SMALL_INT(o2)) {
149-
if (MP_OBJ_IS_SMALL_INT(o1) && MP_OBJ_IS_SMALL_INT(o2)) {
149+
}
150+
151+
// fast path for small ints
152+
if (MP_OBJ_IS_SMALL_INT(o1)) {
153+
if (MP_OBJ_IS_SMALL_INT(o2)) {
154+
// both SMALL_INT, and not equal if we get here
150155
return false;
151156
} else {
152-
if (MP_OBJ_IS_SMALL_INT(o1)) {
153-
mp_obj_t temp = o2; o2 = o1; o1 = temp;
154-
}
155-
// o2 is the SMALL_INT, o1 is not
157+
mp_obj_t temp = o2; o2 = o1; o1 = temp;
158+
// o2 is now the SMALL_INT, o1 is not
156159
// fall through to generic op
157160
}
158-
} else if (MP_OBJ_IS_STR(o1) && MP_OBJ_IS_STR(o2)) {
159-
return mp_obj_str_equal(o1, o2);
161+
}
162+
163+
// fast path for strings
164+
if (MP_OBJ_IS_STR(o1)) {
165+
if (MP_OBJ_IS_STR(o2)) {
166+
// both strings, use special function
167+
return mp_obj_str_equal(o1, o2);
168+
} else {
169+
// a string is never equal to anything else
170+
return false;
171+
}
172+
} else if (MP_OBJ_IS_STR(o2)) {
173+
// o1 is not a string (else caught above), so the objects are not equal
174+
return false;
160175
}
161176

162177
// generic type, call binary_op(MP_BINARY_OP_EQUAL)

py/objdict.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
9999
return MP_BOOL(elem != NULL);
100100
}
101101
case MP_BINARY_OP_EQUAL: {
102-
// TODO: Support equality to other object types
103102
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
104103
mp_obj_dict_t *rhs = rhs_in;
105104
if (o->map.used != rhs->map.used) {
@@ -118,6 +117,9 @@ STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
118117
}
119118
}
120119
return mp_const_true;
120+
} else {
121+
// dict is not equal to instance of any other type
122+
return mp_const_false;
121123
}
122124
}
123125
default:

0 commit comments

Comments
 (0)