]> granicus.if.org Git - python/commitdiff
Merged revisions 84301 via svnmerge from
authorDaniel Stutzbach <daniel@stutzbachenterprises.com>
Tue, 24 Aug 2010 21:00:32 +0000 (21:00 +0000)
committerDaniel Stutzbach <daniel@stutzbachenterprises.com>
Tue, 24 Aug 2010 21:00:32 +0000 (21:00 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r84301 | daniel.stutzbach | 2010-08-24 15:49:57 -0500 (Tue, 24 Aug 2010) | 1 line

  Issue 8750: Fixed MutableSet's methods to correctly handle reflexive operations, namely x -= x and x ^= x
........

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 be41fcda0bcde65066b65ac3042aaff33f2a2607..5e22afc2a9e795fdfd97e4ce58882f6c175f37cc 100644 (file)
@@ -494,6 +494,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.assertTrue(isinstance(sample(), Mapping))
index 4c2ebe4322f1cdbbb388370dcc16c67870097417..3344d27ee10769987da3c57ae2d83ba9f0b16157 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -95,6 +95,9 @@ C-API
 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.