diff options
author | Georg Brandl <georg@python.org> | 2008-05-18 16:27:29 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2008-05-18 16:27:29 (GMT) |
commit | 9dba5d9764350c9d71be15619995899bcad1ffbd (patch) | |
tree | 09306abe73abe34cac7613f549ffebe96a5fa0e4 /Lib/_weakrefset.py | |
parent | c4dc0d4000939955723f59fea04c7049a6b4689d (diff) | |
download | cpython-9dba5d9764350c9d71be15619995899bcad1ffbd.zip cpython-9dba5d9764350c9d71be15619995899bcad1ffbd.tar.gz cpython-9dba5d9764350c9d71be15619995899bcad1ffbd.tar.bz2 |
Add a test suite for WeakSet mostly derived from test_set and fix some
issues in the weakset implementation discovered with it.
Diffstat (limited to 'Lib/_weakrefset.py')
-rw-r--r-- | Lib/_weakrefset.py | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/Lib/_weakrefset.py b/Lib/_weakrefset.py index a6827e8..0046133 100644 --- a/Lib/_weakrefset.py +++ b/Lib/_weakrefset.py @@ -23,6 +23,9 @@ class WeakSet: if item is not None: yield item + def __len__(self): + return sum(x() is not None for x in self.data) + def __contains__(self, item): return ref(item) in self.data @@ -61,7 +64,9 @@ class WeakSet: else: for element in other: self.add(element) - __ior__ = update + def __ior__(self, other): + self.update(other) + return self # Helper functions for simple delegating methods. def _apply(self, other, method): @@ -72,43 +77,68 @@ class WeakSet: newset.data = newdata return newset - def _apply_mutate(self, other, method): - if not isinstance(other, self.__class__): - other = self.__class__(other) - method(other) - def difference(self, other): return self._apply(other, self.data.difference) __sub__ = difference def difference_update(self, other): - self._apply_mutate(self, self.data.difference_update) - __isub__ = difference_update + if self is other: + self.data.clear() + else: + self.data.difference_update(ref(item) for item in other) + def __isub__(self, other): + if self is other: + self.data.clear() + else: + self.data.difference_update(ref(item) for item in other) + return self def intersection(self, other): return self._apply(other, self.data.intersection) __and__ = intersection def intersection_update(self, other): - self._apply_mutate(self, self.data.intersection_update) - __iand__ = intersection_update + self.data.intersection_update(ref(item) for item in other) + def __iand__(self, other): + self.data.intersection_update(ref(item) for item in other) + return self def issubset(self, other): return self.data.issubset(ref(item) for item in other) __lt__ = issubset + def __le__(self, other): + return self.data <= set(ref(item) for item in other) + def issuperset(self, other): return self.data.issuperset(ref(item) for item in other) __gt__ = issuperset + def __ge__(self, other): + return self.data >= set(ref(item) for item in other) + + def __eq__(self, other): + return self.data == set(ref(item) for item in other) + def symmetric_difference(self, other): return self._apply(other, self.data.symmetric_difference) __xor__ = symmetric_difference def symmetric_difference_update(self, other): - self._apply_mutate(other, self.data.symmetric_difference_update) - __ixor__ = symmetric_difference_update + if self is other: + self.data.clear() + else: + self.data.symmetric_difference_update(ref(item) for item in other) + def __ixor__(self, other): + if self is other: + self.data.clear() + else: + self.data.symmetric_difference_update(ref(item) for item in other) + return self def union(self, other): return self._apply(other, self.data.union) __or__ = union + + def isdisjoint(self, other): + return len(self.intersection(other)) == 0 |