diff --git a/tests/snippets/list.py b/tests/snippets/list.py index 65a031c63..cc12ded4c 100644 --- a/tests/snippets/list.py +++ b/tests/snippets/list.py @@ -67,3 +67,11 @@ except OverflowError: pass else: assert False, "OverflowError was not raised" + +x = [[], 2, {}] +y = x.copy() +assert x is not y +assert x == y +assert all(a is b for a, b in zip(x, y)) +y.append(4) +assert x != y diff --git a/vm/src/obj/objlist.rs b/vm/src/obj/objlist.rs index 10d5ccf34..8612d1f81 100644 --- a/vm/src/obj/objlist.rs +++ b/vm/src/obj/objlist.rs @@ -219,6 +219,12 @@ fn list_clear(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(vm.get_none()) } +fn list_copy(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { + arg_check!(vm, args, required = [(zelf, Some(vm.ctx.list_type()))]); + let elements = get_elements(zelf); + Ok(vm.ctx.new_list(elements.clone())) +} + fn list_count(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!( vm, @@ -439,6 +445,7 @@ pub fn init(context: &PyContext) { context.set_attr(&list_type, "__doc__", context.new_str(list_doc.to_string())); context.set_attr(&list_type, "append", context.new_rustfunc(list_append)); context.set_attr(&list_type, "clear", context.new_rustfunc(list_clear)); + context.set_attr(&list_type, "copy", context.new_rustfunc(list_copy)); context.set_attr(&list_type, "count", context.new_rustfunc(list_count)); context.set_attr(&list_type, "extend", context.new_rustfunc(list_extend)); context.set_attr(&list_type, "index", context.new_rustfunc(list_index));