summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-04 04:25:46 -0800
committerDamien George <damien.p.george@gmail.com>2014-01-04 04:25:46 -0800
commit597bb2f2496fd2d89a69ad6b00ba212359aa96bf (patch)
treee071ed5cf40ef915265a9cb4b26afecf3dab05f6
parentc8d1384fc0c7aafa5dee3445ece20f4e43dfa9c1 (diff)
parent26c211648bc00085233ece9c53f19a64ecca41fa (diff)
downloadmicropython-597bb2f2496fd2d89a69ad6b00ba212359aa96bf.tar.gz
micropython-597bb2f2496fd2d89a69ad6b00ba212359aa96bf.zip
Merge pull request #60 from chipaca/list_copy
Implemented list.copy. Fixes issue #54.
-rw-r--r--py/objlist.c8
-rw-r--r--tests/basics/tests/list_copy.py7
2 files changed, 15 insertions, 0 deletions
diff --git a/py/objlist.c b/py/objlist.c
index 28b373b782..e371057981 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -134,8 +134,15 @@ static mp_obj_t list_clear(mp_obj_t self_in) {
return mp_const_none;
}
+static mp_obj_t list_copy(mp_obj_t self_in) {
+ assert(MP_OBJ_IS_TYPE(self_in, &list_type));
+ mp_obj_list_t *self = self_in;
+ return mp_obj_new_list(self->len, self->items);
+}
+
static MP_DEFINE_CONST_FUN_OBJ_2(list_append_obj, mp_obj_list_append);
static MP_DEFINE_CONST_FUN_OBJ_1(list_clear_obj, list_clear);
+static MP_DEFINE_CONST_FUN_OBJ_1(list_copy_obj, list_copy);
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
@@ -151,6 +158,7 @@ const mp_obj_type_t list_type = {
{ // method list
{ "append", &list_append_obj },
{ "clear", &list_clear_obj },
+ { "copy", &list_copy_obj },
{ "pop", &list_pop_obj },
{ "sort", &list_sort_obj },
{ NULL, NULL }, // end-of-list sentinel
diff --git a/tests/basics/tests/list_copy.py b/tests/basics/tests/list_copy.py
new file mode 100644
index 0000000000..bc450782e3
--- /dev/null
+++ b/tests/basics/tests/list_copy.py
@@ -0,0 +1,7 @@
+# list copy tests
+a = [1, 2, []]
+b = a.copy()
+a[-1].append(1)
+a.append(4)
+print(a)
+print(b)