https://github.com/python/cpython/commit/1b5f9c9653f348b0aa8b7ca39f8a9361150... commit: 1b5f9c9653f348b0aa8b7ca39f8a9361150f7dfc branch: master author: Naris R <nariscatboy@gmail.com> committer: Raymond Hettinger <rhettinger@users.noreply.github.com> date: 2018-08-30T09:56:14-07:00 summary: bpo-34427: Fix infinite loop when calling MutableSequence.extend() on self (GH-8813) files: A Misc/NEWS.d/next/Library/2018-08-20-13-53-10.bpo-34427.tMRQjl.rst M Lib/_collections_abc.py M Lib/test/test_collections.py diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py index dbe30dff1fe1..c363987970b4 100644 --- a/Lib/_collections_abc.py +++ b/Lib/_collections_abc.py @@ -986,6 +986,8 @@ def reverse(self): def extend(self, values): 'S.extend(iterable) -- extend sequence by appending elements from the iterable' + if values is self: + values = list(values) for v in values: self.append(v) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 2099d236d0c4..0b7cb5848b1b 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -1721,6 +1721,18 @@ def insert(self, index, value): mss.clear() self.assertEqual(len(mss), 0) + # issue 34427 + # extending self should not cause infinite loop + items = 'ABCD' + mss2 = MutableSequenceSubclass() + mss2.extend(items + items) + mss.clear() + mss.extend(items) + mss.extend(mss) + self.assertEqual(len(mss), len(mss2)) + self.assertEqual(list(mss), list(mss2)) + + ################################################################################ ### Counter ################################################################################ diff --git a/Misc/NEWS.d/next/Library/2018-08-20-13-53-10.bpo-34427.tMRQjl.rst b/Misc/NEWS.d/next/Library/2018-08-20-13-53-10.bpo-34427.tMRQjl.rst new file mode 100644 index 000000000000..f6e0e030b7fe --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-08-20-13-53-10.bpo-34427.tMRQjl.rst @@ -0,0 +1 @@ +Fix infinite loop in ``a.extend(a)`` for ``MutableSequence`` subclasses.