]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Teach the sets module to correctly compute s-=s and s^=s as the empty set.
authorRaymond Hettinger <python@rcn.com>
Sat, 13 Aug 2005 02:29:58 +0000 (02:29 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 13 Aug 2005 02:29:58 +0000 (02:29 +0000)
Lib/sets.py
Lib/test/test_sets.py

index 8ec7e2fa8c50ec94929154b52656fd1a06556051..32a0dd64ff871dc15ef658b699096931c7afa405 100644 (file)
@@ -480,6 +480,8 @@ class Set(BaseSet):
         value = True
         if not isinstance(other, BaseSet):
             other = Set(other)
+        if self is other:
+            self.clear()
         for elt in other:
             if elt in data:
                 del data[elt]
@@ -497,6 +499,8 @@ class Set(BaseSet):
         data = self._data
         if not isinstance(other, BaseSet):
             other = Set(other)
+        if self is other:
+            self.clear()
         for elt in ifilter(data.has_key, other):
             del data[elt]
 
index c5a48b1c1ec80d6221cbb0f4ac083d40ff342ebc..ff834e0aa0d7ab64e98c1f7b883c2e30713a32b3 100644 (file)
@@ -243,6 +243,19 @@ class TestBinaryOps(unittest.TestCase):
         self.assertRaises(TypeError, cmp, a, 12)
         self.assertRaises(TypeError, cmp, "abc", a)
 
+    def test_inplace_on_self(self):
+        t = self.set.copy()
+        t |= t
+        self.assertEqual(t, self.set)
+        t &= t
+        self.assertEqual(t, self.set)
+        t -= t
+        self.assertEqual(len(t), 0)
+        t = self.set.copy()
+        t ^= t
+        self.assertEqual(len(t), 0)
+
+
 #==============================================================================
 
 class TestUpdateOps(unittest.TestCase):