Skip to content

Commit 79e3700

Browse files
authored
Merge pull request #1479 from HyeockJinKim/issue1475
Fixed list's __iadd__
2 parents 84f08c8 + 56108c1 commit 79e3700

2 files changed

Lines changed: 90 additions & 3 deletions

File tree

tests/snippets/list.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,42 @@
1111
y.extend(x)
1212
assert y == [2, 1, 2, 3, 1, 2, 3]
1313

14+
a = []
15+
a.extend((1,2,3,4))
16+
assert a == [1, 2, 3, 4]
17+
18+
a.extend('abcdefg')
19+
assert a == [1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g']
20+
21+
a.extend(range(10))
22+
assert a == [1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
23+
24+
a = []
25+
a.extend({1,2,3,4})
26+
assert a == [1, 2, 3, 4]
27+
28+
a.extend({'a': 1, 'b': 2, 'z': 51})
29+
assert a == [1, 2, 3, 4, 'a', 'b', 'z']
30+
31+
class Iter:
32+
def __iter__(self):
33+
yield 12
34+
yield 28
35+
36+
a.extend(Iter())
37+
assert a == [1, 2, 3, 4, 'a', 'b', 'z', 12, 28]
38+
39+
a.extend(bytes(b'hello world'))
40+
assert a == [1, 2, 3, 4, 'a', 'b', 'z', 12, 28, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
41+
42+
class Next:
43+
def __next__(self):
44+
yield 12
45+
yield 28
46+
47+
assert_raises(TypeError, lambda: [].extend(3))
48+
assert_raises(TypeError, lambda: [].extend(slice(0, 10, 1)))
49+
1450
assert x * 0 == [], "list __mul__ by 0 failed"
1551
assert x * -1 == [], "list __mul__ by -1 failed"
1652
assert x * 2 == [1, 2, 3, 1, 2, 3], "list __mul__ by 2 failed"
@@ -532,3 +568,54 @@ def bad_iter_assign():
532568
assert [float('inf'), float('inf')] >= [float('inf'), float('inf')]
533569
assert not [float('inf'), float('inf')] < [float('inf'), float('inf')]
534570
assert not [float('inf'), float('inf')] > [float('inf'), float('inf')]
571+
572+
# list __iadd__
573+
a = []
574+
a += [1, 2, 3]
575+
assert a == [1, 2, 3]
576+
577+
a = []
578+
a += (1,2,3,4)
579+
assert a == [1, 2, 3, 4]
580+
581+
a += 'abcdefg'
582+
assert a == [1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g']
583+
584+
a += range(10)
585+
assert a == [1, 2, 3, 4, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
586+
587+
a = []
588+
a += {1,2,3,4}
589+
assert a == [1, 2, 3, 4]
590+
591+
a += {'a': 1, 'b': 2, 'z': 51}
592+
assert a == [1, 2, 3, 4, 'a', 'b', 'z']
593+
594+
class Iter:
595+
def __iter__(self):
596+
yield 12
597+
yield 28
598+
599+
a += Iter()
600+
assert a == [1, 2, 3, 4, 'a', 'b', 'z', 12, 28]
601+
602+
a += bytes(b'hello world')
603+
assert a == [1, 2, 3, 4, 'a', 'b', 'z', 12, 28, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
604+
605+
class Next:
606+
def __next__(self):
607+
yield 12
608+
yield 28
609+
610+
def iadd_int():
611+
a = []
612+
a += 3
613+
614+
def iadd_slice():
615+
a = []
616+
a += slice(0, 10, 1)
617+
618+
assert_raises(TypeError, iadd_int)
619+
assert_raises(TypeError, iadd_slice)
620+
621+

vm/src/obj/objlist.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ impl PyListRef {
145145
}
146146

147147
fn iadd(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
148-
if objtype::isinstance(&other, &vm.ctx.list_type()) {
149-
let e = get_elements_list(&other).clone();
150-
self.elements.borrow_mut().extend_from_slice(&e);
148+
if let Ok(new_elements) = vm.extract_elements(&other) {
149+
let mut e = new_elements;
150+
self.elements.borrow_mut().append(&mut e);
151151
Ok(self.into_object())
152152
} else {
153153
Ok(vm.ctx.not_implemented())

0 commit comments

Comments
 (0)