From 074d3b5f869a2bad646ba6c2626b05d405e21f41 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 10 Jan 2014 18:12:25 +0200 Subject: [PATCH] list: Implement list multiplication. --- py/objlist.c | 15 +++++++++++++++ tests/basics/tests/list_mult.py | 4 ++++ 2 files changed, 19 insertions(+) create mode 100644 tests/basics/tests/list_mult.py diff --git a/py/objlist.c b/py/objlist.c index 9d8caee339..ce55aa5100 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -81,6 +81,21 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len); return s; } + case RT_BINARY_OP_MULTIPLY: + { + if (!MP_OBJ_IS_SMALL_INT(rhs)) { + return NULL; + } + int n = MP_OBJ_SMALL_INT_VALUE(rhs); + int len = o->len; + mp_obj_list_t *s = list_new(len * n); + mp_obj_t *dest = s->items; + for (int i = 0; i < n; i++) { + memcpy(dest, o->items, sizeof(mp_obj_t) * len); + dest += len; + } + return s; + } default: // op not supported return NULL; diff --git a/tests/basics/tests/list_mult.py b/tests/basics/tests/list_mult.py new file mode 100644 index 0000000000..ec65fbb3f4 --- /dev/null +++ b/tests/basics/tests/list_mult.py @@ -0,0 +1,4 @@ +print([0] * 5) +a = [1, 2, 3] +c = a * 3 +print(c)