diff --git a/py/obj.h b/py/obj.h index f372030b7e..43c0ed5e78 100644 --- a/py/obj.h +++ b/py/obj.h @@ -398,6 +398,7 @@ typedef struct _mp_obj_static_class_method_t { // sequence helpers void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest); bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end); -#define m_seq_copy(dest, src, len, item_sz) memcpy(dest, src, len * sizeof(item_sz)) +#define m_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t)) +#define m_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, len1 * sizeof(item_t)); memcpy(dest + len1, src2, len2 * sizeof(item_t)); } bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2); bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2); diff --git a/py/objlist.c b/py/objlist.c index 29d0c92c52..9f20acbd41 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -114,8 +114,7 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { } mp_obj_list_t *p = rhs; mp_obj_list_t *s = list_new(o->len + p->len); - memcpy(s->items, o->items, sizeof(mp_obj_t) * o->len); - memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len); + m_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t); return s; } case RT_BINARY_OP_INPLACE_ADD: diff --git a/py/objtuple.c b/py/objtuple.c index 8c1320a5f7..b7710e1d53 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -114,6 +114,16 @@ static mp_obj_t tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { uint index = mp_get_index(o->base.type, o->len, rhs); return o->items[index]; } + case RT_BINARY_OP_ADD: + { + if (!MP_OBJ_IS_TYPE(rhs, &tuple_type)) { + return NULL; + } + 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); + return s; + } case RT_BINARY_OP_EQUAL: case RT_BINARY_OP_LESS: case RT_BINARY_OP_LESS_EQUAL: diff --git a/tests/basics/tuple1.py b/tests/basics/tuple1.py index b64720b3eb..53eac2a306 100644 --- a/tests/basics/tuple1.py +++ b/tests/basics/tuple1.py @@ -14,3 +14,5 @@ except AttributeError: print(x[1:]) print(x[:-1]) print(x[2:3]) + +print(x + (10, 100, 10000))