]> granicus.if.org Git - python/commitdiff
bpo-34427: Fix infinite loop when calling MutableSequence.extend() on self (GH-8813)
authorNaris R <nariscatboy@gmail.com>
Thu, 30 Aug 2018 16:56:14 +0000 (02:56 +1000)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>
Thu, 30 Aug 2018 16:56:14 +0000 (09:56 -0700)
Lib/_collections_abc.py
Lib/test/test_collections.py
Misc/NEWS.d/next/Library/2018-08-20-13-53-10.bpo-34427.tMRQjl.rst [new file with mode: 0644]

index dbe30dff1fe190a2d8cb79abb886f0f1f9939754..c363987970b4c669e5aa4246a02453b784f389f5 100644 (file)
@@ -986,6 +986,8 @@ class MutableSequence(Sequence):
 
     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)
 
index 2099d236d0c4a1f299023407e5c6bb39fd167645..0b7cb5848b1b8ff76d205f651b17fbfb3576544c 100644 (file)
@@ -1721,6 +1721,18 @@ class TestCollectionABCs(ABCTestCase):
         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 (file)
index 0000000..f6e0e03
--- /dev/null
@@ -0,0 +1 @@
+Fix infinite loop in ``a.extend(a)`` for ``MutableSequence`` subclasses.