Skip to content

Commit dcb29ab

Browse files
Add list.__iadd__
1 parent b310d5e commit dcb29ab

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

tests/snippets/list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@
7070
assert not 1 in a
7171

7272
assert_raises(ValueError, lambda: a.remove(10), 'Remove not exist element')
73+
74+
foo = bar = [1]
75+
foo += [2]
76+
assert (foo, bar) == ([1, 2], [1, 2])

vm/src/obj/objlist.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ fn list_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
181181
}
182182
}
183183

184+
fn list_iadd(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
185+
arg_check!(
186+
vm,
187+
args,
188+
required = [(zelf, Some(vm.ctx.list_type())), (other, None)]
189+
);
190+
191+
if objtype::isinstance(other, &vm.ctx.list_type()) {
192+
let mut elements = get_mut_elements(zelf);
193+
for elem in get_elements(other).iter() {
194+
elements.push(elem.clone());
195+
}
196+
Ok(zelf.clone())
197+
} else {
198+
Ok(vm.ctx.not_implemented())
199+
}
200+
}
201+
184202
fn list_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
185203
arg_check!(vm, args, required = [(o, Some(vm.ctx.list_type()))]);
186204

@@ -443,6 +461,7 @@ pub fn init(context: &PyContext) {
443461
The argument must be an iterable if specified.";
444462

445463
context.set_attr(&list_type, "__add__", context.new_rustfunc(list_add));
464+
context.set_attr(&list_type, "__iadd__", context.new_rustfunc(list_iadd));
446465
context.set_attr(
447466
&list_type,
448467
"__contains__",

0 commit comments

Comments
 (0)