]> granicus.if.org Git - python/commitdiff
Issue 8750: Fixed MutableSet's methods to correctly handle reflexive operations,...
authorDaniel Stutzbach <daniel@stutzbachenterprises.com>
Tue, 24 Aug 2010 20:49:57 +0000 (20:49 +0000)
committerDaniel Stutzbach <daniel@stutzbachenterprises.com>
Tue, 24 Aug 2010 20:49:57 +0000 (20:49 +0000)
Lib/_abcoll.py
Lib/test/test_collections.py
Misc/NEWS

index d3e23c1e446c067f8865b7430dbf60df93c274b6..cac06e0825f48bd55cbdb5fddacff0fd116aa128 100644 (file)
@@ -321,18 +321,24 @@ class MutableSet(Set):
         return self
 
     def __ixor__(self, it: Iterable):
-        if not isinstance(it, Set):
-            it = self._from_iterable(it)
-        for value in it:
-            if value in self:
-                self.discard(value)
-            else:
-                self.add(value)
+        if it is self:
+            self.clear()
+        else:
+            if not isinstance(it, Set):
+                it = self._from_iterable(it)
+            for value in it:
+                if value in self:
+                    self.discard(value)
+                else:
+                    self.add(value)
         return self
 
     def __isub__(self, it: Iterable):
-        for value in it:
-            self.discard(value)
+        if it is self:
+            self.clear()
+        else:
+            for value in it:
+                self.discard(value)
         return self
 
 MutableSet.register(set)
index da80baa9970d84a768918b581c152f7e7b719430..75d660d6287915986d8a71ef651f06c5ae254719 100644 (file)
@@ -526,6 +526,21 @@ class TestCollectionABCs(ABCTestCase):
         s = MySet([5,43,2,1])
         self.assertEqual(s.pop(), 1)
 
+    def test_issue8750(self):
+        empty = WithSet()
+        full = WithSet(range(10))
+        s = WithSet(full)
+        s -= s
+        self.assertEqual(s, empty)
+        s = WithSet(full)
+        s ^= s
+        self.assertEqual(s, empty)
+        s = WithSet(full)
+        s &= s
+        self.assertEqual(s, full)
+        s |= s
+        self.assertEqual(s, full)
+
     def test_Mapping(self):
         for sample in [dict]:
             self.assertIsInstance(sample(), Mapping)
index 5d234647edd3be440c0ec4c22e33bc64eaec9e48..304977b385cc2fe9f54f00feaaab91ccfdc68752 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -126,6 +126,9 @@ Extensions
 Library
 -------
 
+- Issue #8750: Fixed MutableSet's methods to correctly handle
+  reflexive operations, namely x -= x and x ^= x.
+
 - Issue #9129: smtpd.py is vulnerable to DoS attacks deriving from missing 
   error handling when accepting a new connection.