8000 py/objtuple: Properly implement comparison with incompatible types. · micropython/micropython-esp32@1aaba5c · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 1aaba5c

Browse files
author
Paul Sokolovsky
committed
py/objtuple: Properly implement comparison with incompatible types.
Should raise TypeError, unless it's (in)equality comparison.
1 parent e354b0a commit 1aaba5c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

py/objtuple.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ STATIC mp_obj_t mp_obj_tuple_make_new(const mp_obj_type_t *type_in, size_t n_arg
101101
}
102102

103103
// Don't pass MP_BINARY_OP_NOT_EQUAL here
104-
STATIC bool tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in) {
104+
STATIC mp_obj_t tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in) {
105105
// type check is done on getiter method to allow tuple, namedtuple, attrtuple
106106
mp_check_self(mp_obj_get_type(self_in)->getiter == mp_obj_tuple_getiter);
107107
mp_obj_type_t *another_type = mp_obj_get_type(another_in);
@@ -110,12 +110,15 @@ STATIC bool tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in
110110
// Slow path for user subclasses
111111
another_in = mp_instance_cast_to_native_base(another_in, MP_OBJ_FROM_PTR(&mp_type_tuple));
112112
if (another_in == MP_OBJ_NULL) {
113-
return false;
113+
if (op == MP_BINARY_OP_EQUAL) {
114+
return mp_const_false;
115+
8000 }
116+
return MP_OBJ_NULL;
114117
}
115118
}
116119
mp_obj_tuple_t *another = MP_OBJ_TO_PTR(another_in);
117120

118-
return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
121+
return mp_obj_new_bool(mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len));
119122
}
120123

121124
mp_obj_t mp_obj_tuple_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
@@ -166,7 +169,7 @@ mp_obj_t mp_obj_tuple_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
166169
case MP_BINARY_OP_LESS_EQUAL:
167170
case MP_BINARY_OP_MORE:
168171
case MP_BINARY_OP_MORE_EQUAL:
169-
return mp_obj_new_bool(tuple_cmp_helper(op, lhs, rhs));
172+
return tuple_cmp_helper(op, lhs, rhs);
170173

171174
default:
172175
return MP_OBJ_NULL; // op not supported

tests/basics/tuple_compare.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,13 @@
5353
print((10, 0) < (1, 1))
5454
print((0, 0, 10, 0) > (0, 0, 1, 1))
5555
print((0, 0, 10, 0) < (0, 0, 1, 1))
56+
57+
58+
print(() == {})
59+
print(() != {})
60+
print((1,) == [1])
61+
62+
try:
63+
print(() < {})
64+
except TypeError:
65+
print("TypeError")

0 commit comments

Comments
 (0)
0