8000 py/objlist: Properly implement comparison with incompatible types. · rlucia/micropython@5c603bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c603bd

Browse files
committed
py/objlist: Properly implement comparison with incompatible types.
Should raise TypeError, unless it's (in)equality comparison.
1 parent beeb748 commit 5c603bd

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

py/objlist.c

Lines changed: 12 additions & 14 deletions
< 8000 td data-grid-cell-id="diff-b34533ff7e90c535a18b55b97f73e708ed75b8e73a1a5491818d92bb90ba701f-102-90-2" data-line-anchor="diff-b34533ff7e90c535a18b55b97f73e708ed75b8e73a1a5491818d92bb90ba701fR90" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">
STATIC mp_obj_t list_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,6 @@ STATIC mp_obj_t list_make_new(const mp_obj_type_t *type_in, size_t n_args, size_
8787
}
8888
}
8989

90-
// Don't pass MP_BINARY_OP_NOT_EQUAL here
91-
STATIC bool list_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in) {
92-
mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_list));
93-
if (!MP_OBJ_IS_TYPE(another_in, &mp_type_list)) {
94-
return false;
95-
}
96-
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
97-
mp_obj_list_t *another = MP_OBJ_TO_PTR(another_in);
98-
99-
return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
100-
}
101-
10290
10391
mp_obj_list_t *self = MP_OBJ_TO_PTR(self_in);
10492
switch (op) {
@@ -146,8 +134,18 @@ STATIC mp_obj_t list_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
146134
case MP_BINARY_OP_LESS:
147135
case MP_BINARY_OP_LESS_EQUAL:
148136
case MP_BINARY_OP_MORE:
149-
case MP_BINARY_OP_MORE_EQUAL:
150-
return mp_obj_new_bool(list_cmp_helper(op, lhs, rhs));
137+
case MP_BINARY_OP_MORE_EQUAL: {
138+
if (!MP_OBJ_IS_TYPE(rhs, &mp_type_list)) {
139+
if (op == MP_BINARY_OP_EQUAL) {
140+
return mp_const_false;
141+
}
142+
return MP_OBJ_NULL; // op not supported
143+
}
144+
145+
mp_obj_list_t *another = MP_OBJ_TO_PTR(rhs);
146+
bool res = mp_seq_cmp_objs(op, o->items, o->len, another->items, another->len);
147+
return mp_obj_new_bool(res);
148+
}
151149

152150
default:
153151
return MP_OBJ_NULL; // op not supported

tests/basics/list_compare.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,13 @@
4848
print([1] <= [1, -1])
4949
print([1, 0] <= [1])
5050
print([1, -1] <= [1])
51+
52+
53+
print([] == {})
54+
print([] != {})
55+
print([1] == (1,))
56+
57+
try:
58+
print([] < {})
59+
except TypeError:
60+
print("TypeError")

0 commit comments

Comments
 (0)
0