summaryrefslogtreecommitdiff
path: root/py/objstr.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-08-13 13:22:24 +0100
committerDamien George <damien.p.george@gmail.com>2014-08-13 13:22:24 +0100
commit9b7a8ee8f1032a1c9172afe0bd3ac5be7780e4a2 (patch)
tree79b3b74827761d1db8b2081a2e1e2be24322a527 /py/objstr.c
parent9d02780eafd9546354fd3ac429b0211f52331650 (diff)
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.
Diffstat (limited to 'py/objstr.c')
-rw-r--r--py/objstr.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/py/objstr.c b/py/objstr.c
index 35bb8e749..e88479459 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);