From: Raymond Hettinger Date: Sat, 13 Aug 2005 02:29:58 +0000 (+0000) Subject: Teach the sets module to correctly compute s-=s and s^=s as the empty set. X-Git-Tag: v2.5a0~1521 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=038ca2a5516f6b445a8548f3999d1db3b6b8abb1;p=thirdparty%2FPython%2Fcpython.git Teach the sets module to correctly compute s-=s and s^=s as the empty set. --- diff --git a/Lib/sets.py b/Lib/sets.py index 8ec7e2fa8c50..32a0dd64ff87 100644 --- a/Lib/sets.py +++ b/Lib/sets.py @@ -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] diff --git a/Lib/test/test_sets.py b/Lib/test/test_sets.py index c5a48b1c1ec8..ff834e0aa0d7 100644 --- a/Lib/test/test_sets.py +++ b/Lib/test/test_sets.py @@ -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):