Commit 038ca2a5 authored by Raymond Hettinger's avatar Raymond Hettinger

Teach the sets module to correctly compute s-=s and s^=s as the empty set.

parent f98e6b15
...@@ -480,6 +480,8 @@ class Set(BaseSet): ...@@ -480,6 +480,8 @@ class Set(BaseSet):
value = True value = True
if not isinstance(other, BaseSet): if not isinstance(other, BaseSet):
other = Set(other) other = Set(other)
if self is other:
self.clear()
for elt in other: for elt in other:
if elt in data: if elt in data:
del data[elt] del data[elt]
...@@ -497,6 +499,8 @@ class Set(BaseSet): ...@@ -497,6 +499,8 @@ class Set(BaseSet):
data = self._data data = self._data
if not isinstance(other, BaseSet): if not isinstance(other, BaseSet):
other = Set(other) other = Set(other)
if self is other:
self.clear()
for elt in ifilter(data.has_key, other): for elt in ifilter(data.has_key, other):
del data[elt] del data[elt]
......
...@@ -243,6 +243,19 @@ class TestBinaryOps(unittest.TestCase): ...@@ -243,6 +243,19 @@ class TestBinaryOps(unittest.TestCase):
self.assertRaises(TypeError, cmp, a, 12) self.assertRaises(TypeError, cmp, a, 12)
self.assertRaises(TypeError, cmp, "abc", a) 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): class TestUpdateOps(unittest.TestCase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment