diff options
Diffstat (limited to 'py')
-rw-r--r-- | py/objint.c | 2 | ||||
-rw-r--r-- | py/objlist.c | 3 | ||||
-rw-r--r-- | py/objstr.c | 10 | ||||
-rw-r--r-- | py/objtuple.c | 7 |
4 files changed, 17 insertions, 5 deletions
diff --git a/py/objint.c b/py/objint.c index c08bf7da6c..d088ae1a80 100644 --- a/py/objint.c +++ b/py/objint.c @@ -289,7 +289,7 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(int op, mp_obj_t lhs_in, mp_obj_t rhs_ // true acts as 0 return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(1)); } else if (op == MP_BINARY_OP_MULTIPLY) { - if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) { + if (MP_OBJ_IS_STR(rhs_in) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_bytes) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(rhs_in, &mp_type_list)) { // multiply is commutative for these types, so delegate to them return mp_binary_op(op, rhs_in, lhs_in); } diff --git a/py/objlist.c b/py/objlist.c index 655a78908e..578e39452a 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -131,6 +131,9 @@ STATIC mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { if (!mp_obj_get_int_maybe(rhs, &n)) { return MP_OBJ_NULL; // op not supported } + if (n < 0) { + n = 0; + } mp_obj_list_t *s = list_new(o->len * n); mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items); return s; diff --git a/py/objstr.c b/py/objstr.c index 35bb8e749c..e884794591 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -290,10 +290,16 @@ mp_obj_t mp_obj_str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { break; case MP_BINARY_OP_MULTIPLY: { - if (!MP_OBJ_IS_SMALL_INT(rhs_in)) { + mp_int_t n; + if (!mp_obj_get_int_maybe(rhs_in, &n)) { return MP_OBJ_NULL; // op not supported } - int n = MP_OBJ_SMALL_INT_VALUE(rhs_in); + if (n <= 0) { + if (lhs_type == &mp_type_str) { + return MP_OBJ_NEW_QSTR(MP_QSTR_); // empty str + } + n = 0; + } byte *data; mp_obj_t s = mp_obj_str_builder_start(lhs_type, lhs_len * n, &data); mp_seq_multiply(lhs_data, sizeof(*lhs_data), lhs_len, n, data); diff --git a/py/objtuple.c b/py/objtuple.c index 3dade2f748..377fbf5431 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -137,10 +137,13 @@ mp_obj_t mp_obj_tuple_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { return s; } case MP_BINARY_OP_MULTIPLY: { - if (!MP_OBJ_IS_SMALL_INT(rhs)) { + mp_int_t n; + if (!mp_obj_get_int_maybe(rhs, &n)) { return MP_OBJ_NULL; // op not supported } - int n = MP_OBJ_SMALL_INT_VALUE(rhs); + if (n <= 0) { + return mp_const_empty_tuple; + } mp_obj_tuple_t *s = mp_obj_new_tuple(o->len * n, NULL); mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items); return s; |