Skip to content

Commit 9d40a4c

Browse files
committed
Support list.__imul__
1 parent 180ec22 commit 9d40a4c

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

tests/snippets/list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
assert x * 0 == [], "list __mul__ by 0 failed"
1515
assert x * -1 == [], "list __mul__ by -1 failed"
1616
assert x * 2 == [1, 2, 3, 1, 2, 3], "list __mul__ by 2 failed"
17+
y = x
18+
x *= 2
19+
assert y is x
20+
assert x == [1, 2, 3] * 2
1721

1822
# index()
1923
assert ['a', 'b', 'c'].index('b') == 1

vm/src/obj/objlist.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ impl PyListRef {
395395
vm.ctx.new_list(new_elements)
396396
}
397397

398+
fn imul(self, counter: isize, _vm: &VirtualMachine) -> Self {
399+
let new_elements = seq_mul(&self.elements.borrow(), counter);
400+
self.elements.replace(new_elements);
401+
self
402+
}
403+
398404
fn count(self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
399405
let mut count: usize = 0;
400406
for element in self.elements.borrow().iter() {
@@ -823,6 +829,7 @@ pub fn init(context: &PyContext) {
823829
"__iter__" => context.new_rustfunc(PyListRef::iter),
824830
"__setitem__" => context.new_rustfunc(PyListRef::setitem),
825831
"__mul__" => context.new_rustfunc(PyListRef::mul),
832+
"__imul__" => context.new_rustfunc(PyListRef::imul),
826833
"__len__" => context.new_rustfunc(PyListRef::len),
827834
"__new__" => context.new_rustfunc(list_new),
828835
"__repr__" => context.new_rustfunc(PyListRef::repr),

0 commit comments

Comments
 (0)