py: Fix mult by negative number of tuple, list, str, bytes.

Multiplication of a tuple, list, str or bytes now yields an empty
sequence (instead of crashing).  Addresses issue #799

Also added ability to mult bytes on LHS by integer.
This commit is contained in:
Damien George
2014-08-13 13:22:24 +01:00
parent 9d02780eaf
commit 9b7a8ee8f1
8 changed files with 59 additions and 7 deletions

View File

@@ -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;