Skip to content

Commit 1b5f9c9

Browse files
KittenHerorhettinger
authored andcommitted
bpo-34427: Fix infinite loop when calling MutableSequence.extend() on self (GH-8813)
1 parent e6dac00 commit 1b5f9c9

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/_collections_abc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,8 @@ def reverse(self):
986986

987987
def extend(self, values):
988988
'S.extend(iterable) -- extend sequence by appending elements from the iterable'
989+
if values is self:
990+
values = list(values)
989991
for v in values:
990992
self.append(v)
991993

Lib/test/test_collections.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,18 @@ def insert(self, index, value):
17211721
mss.clear()
17221722
self.assertEqual(len(mss), 0)
17231723

1724+
# issue 34427
1725+
# extending self should not cause infinite loop
1726+
items = 'ABCD'
1727+
mss2 = MutableSequenceSubclass()
1728+
mss2.extend(items + items)
1729+
mss.clear()
1730+
mss.extend(items)
1731+
mss.extend(mss)
1732+
self.assertEqual(len(mss), len(mss2))
1733+
self.assertEqual(list(mss), list(mss2))
1734+
1735+
17241736
################################################################################
17251737
### Counter
17261738
################################################################################
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix infinite loop in ``a.extend(a)`` for ``MutableSequence`` subclasses.

0 commit comments

Comments
 (0)