Skip to content

Commit 46c9e97

Browse files
committed
Merge pull request adafruit#134 from pfalcon/list-mul
list: Implement list multiplication.
2 parents 745ce4c + 074d3b5 commit 46c9e97

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

py/objlist.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
8181
memcpy(s->items + o->len, p->items, sizeof(mp_obj_t) * p->len);
8282
return s;
8383
}
84+
case RT_BINARY_OP_MULTIPLY:
85+
{
86+
if (!MP_OBJ_IS_SMALL_INT(rhs)) {
87+
return NULL;
88+
}
89+
int n = MP_OBJ_SMALL_INT_VALUE(rhs);
90+
int len = o->len;
91+
mp_obj_list_t *s = list_new(len * n);
92+
mp_obj_t *dest = s->items;
93+
for (int i = 0; i < n; i++) {
94+
memcpy(dest, o->items, sizeof(mp_obj_t) * len);
95+
dest += len;
96+
}
97+
return s;
98+
}
8499
default:
85100
// op not supported
86101
return NULL;

tests/basics/tests/list_mult.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
print([0] * 5)
2+
a = [1, 2, 3]
3+
c = a * 3
4+
print(c)

0 commit comments

Comments
 (0)