summaryrefslogtreecommitdiffstatshomepage
path: root/py/objtuple.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/objtuple.c')
-rw-r--r--py/objtuple.c34
1 files changed, 20 insertions, 14 deletions
diff --git a/py/objtuple.c b/py/objtuple.c
index 9434b418cd..ca65b28e31 100644
--- a/py/objtuple.c
+++ b/py/objtuple.c
@@ -99,12 +99,20 @@ mp_obj_t mp_obj_tuple_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
// Don't pass MP_BINARY_OP_NOT_EQUAL here
STATIC bool tuple_cmp_helper(int op, mp_obj_t self_in, mp_obj_t another_in) {
- assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
- if (!MP_OBJ_IS_TYPE(another_in, &mp_type_tuple)) {
- return false;
+ mp_obj_type_t *self_type = mp_obj_get_type(self_in);
+ if (self_type->getiter != tuple_getiter) {
+ assert(0);
}
+ mp_obj_type_t *another_type = mp_obj_get_type(another_in);
mp_obj_tuple_t *self = self_in;
mp_obj_tuple_t *another = another_in;
+ if (another_type->getiter != tuple_getiter) {
+ // Slow path for user subclasses
+ another = mp_instance_cast_to_native_base(another, &mp_type_tuple);
+ if (another == MP_OBJ_NULL) {
+ return false;
+ }
+ }
return mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len);
}
@@ -121,20 +129,18 @@ mp_obj_t tuple_unary_op(int op, mp_obj_t self_in) {
mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
mp_obj_tuple_t *o = lhs;
switch (op) {
- case MP_BINARY_OP_ADD:
- {
+ case MP_BINARY_OP_ADD: {
if (!mp_obj_is_subclass_fast(mp_obj_get_type(rhs), (mp_obj_t)&mp_type_tuple)) {
- return NULL;
+ return MP_OBJ_NOT_SUPPORTED;
}
mp_obj_tuple_t *p = rhs;
mp_obj_tuple_t *s = mp_obj_new_tuple(o->len + p->len, NULL);
- m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
+ mp_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
return s;
}
- case MP_BINARY_OP_MULTIPLY:
- {
+ case MP_BINARY_OP_MULTIPLY: {
if (!MP_OBJ_IS_SMALL_INT(rhs)) {
- return NULL;
+ return MP_OBJ_NOT_SUPPORTED;
}
int n = MP_OBJ_SMALL_INT_VALUE(rhs);
mp_obj_tuple_t *s = mp_obj_new_tuple(o->len * n, NULL);
@@ -150,7 +156,7 @@ mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
default:
// op not supported
- return NULL;
+ return MP_OBJ_NOT_SUPPORTED;
}
}
@@ -161,11 +167,11 @@ mp_obj_t tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
#if MICROPY_ENABLE_SLICE
if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
machine_uint_t start, stop;
- if (!m_seq_get_fast_slice_indexes(self->len, index, &start, &stop)) {
+ if (!mp_seq_get_fast_slice_indexes(self->len, index, &start, &stop)) {
assert(0);
}
mp_obj_tuple_t *res = mp_obj_new_tuple(stop - start, NULL);
- m_seq_copy(res->items, self->items + start, res->len, mp_obj_t);
+ mp_seq_copy(res->items, self->items + start, res->len, mp_obj_t);
return res;
}
#endif
@@ -176,7 +182,7 @@ mp_obj_t tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
}
}
-STATIC mp_obj_t tuple_getiter(mp_obj_t o_in) {
+mp_obj_t tuple_getiter(mp_obj_t o_in) {
return mp_obj_new_tuple_iterator(o_in, 0);
}