diff --git a/Lib/_weakrefset.py b/Lib/_weakrefset.py index 43f9a6e447d..990c3a6bbcd 100644 --- a/Lib/_weakrefset.py +++ b/Lib/_weakrefset.py @@ -171,6 +171,12 @@ class WeakSet(object): return NotImplemented return self.data == set(ref(item) for item in other) + def __ne__(self, other): + opposite = self.__eq__(other) + if opposite is NotImplemented: + return NotImplemented + return not opposite + def symmetric_difference(self, other): newset = self.copy() newset.symmetric_difference_update(other) diff --git a/Lib/test/test_weakset.py b/Lib/test/test_weakset.py index d9e091ed02f..fb9e8d73db3 100644 --- a/Lib/test/test_weakset.py +++ b/Lib/test/test_weakset.py @@ -351,6 +351,12 @@ class TestWeakSet(unittest.TestCase): self.assertFalse(self.s == tuple(self.items)) self.assertFalse(self.s == 1) + def test_ne(self): + self.assertTrue(self.s != set(self.items)) + s1 = WeakSet() + s2 = WeakSet() + self.assertFalse(s1 != s2) + def test_weak_destroy_while_iterating(self): # Issue #7105: iterators shouldn't crash when a key is implicitly removed # Create new items to be sure no-one else holds a reference diff --git a/Misc/NEWS b/Misc/NEWS index 391ecc5288c..7a720309184 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -15,6 +15,8 @@ Core and Builtins Library ------- +- Implement inequality on weakref.WeakSet. + - Issue #17981: Closed socket on error in SysLogHandler. - Issue #17754: Make ctypes.util.find_library() independent of the locale.