Skip to content

Commit 597bb2f

Browse files
committed
Merge pull request adafruit#60 from chipaca/list_copy
Implemented list.copy. Fixes issue adafruit#54.
2 parents c8d1384 + 26c2116 commit 597bb2f

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

py/objlist.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,15 @@ static mp_obj_t list_clear(mp_obj_t self_in) {
134134
return mp_const_none;
135135
}
136136

137+
static mp_obj_t list_copy(mp_obj_t self_in) {
138+
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
139+
mp_obj_list_t *self = self_in;
140+
return mp_obj_new_list(self->len, self->items);
141+
}
142+
137143
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
138144
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
145+
static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
139146
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
140147
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
141148

@@ -151,6 +158,7 @@ const mp_obj_type_t list_type = {
151158
{ // method list
152159
{ "append", &list_append_obj },
153160
{ "clear", &list_clear_obj },
161+
{ "copy", &list_copy_obj },
154162
{ "pop", &list_pop_obj },
155163
{ "sort", &list_sort_obj },
156164
{ NULL, NULL }, // end-of-list sentinel

tests/basics/tests/list_copy.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# list copy tests
2+
a = [1, 2, []]
3+
b = a.copy()
4+
a[-1].append(1)
5+
a.append(4)
6+
print(a)
7+
print(b)

0 commit comments

Comments
 (0)