[Python-checkins] r72751 - in python/branches/py3k/Lib: _weakrefset.py test/test_weakset.py

robert.schuppenies python-checkins at python.org
Sun May 17 19:32:22 CEST 2009


Author: robert.schuppenies
Date: Sun May 17 19:32:20 2009
New Revision: 72751

Log:
Issue 5964: Fixed WeakSet __eq__ comparison to handle non-WeakSet objects.


Modified:
   python/branches/py3k/Lib/_weakrefset.py
   python/branches/py3k/Lib/test/test_weakset.py

Modified: python/branches/py3k/Lib/_weakrefset.py
==============================================================================
--- python/branches/py3k/Lib/_weakrefset.py	(original)
+++ python/branches/py3k/Lib/_weakrefset.py	Sun May 17 19:32:20 2009
@@ -118,6 +118,8 @@
         return self.data >= set(ref(item) for item in other)
 
     def __eq__(self, other):
+        if not isinstance(other, self.__class__):
+            return NotImplemented
         return self.data == set(ref(item) for item in other)
 
     def symmetric_difference(self, other):

Modified: python/branches/py3k/Lib/test/test_weakset.py
==============================================================================
--- python/branches/py3k/Lib/test/test_weakset.py	(original)
+++ python/branches/py3k/Lib/test/test_weakset.py	Sun May 17 19:32:20 2009
@@ -134,13 +134,11 @@
 
     def test_gc(self):
         # Create a nest of cycles to exercise overall ref count check
-        class A:
-            pass
-        s = set(A() for i in range(1000))
+        s = WeakSet(Foo() for i in range(1000))
         for elem in s:
             elem.cycle = s
             elem.sub = elem
-            elem.set = set([elem])
+            elem.set = WeakSet([elem])
 
     def test_subclass_with_custom_hash(self):
         # Bug #1257731
@@ -169,17 +167,12 @@
         t = WeakSet(s)
         self.assertNotEqual(id(s), id(t))
 
-    def test_set_literal(self):
-        s = set([1,2,3])
-        t = {1,2,3}
-        self.assertEqual(s, t)
-
     def test_hash(self):
         self.assertRaises(TypeError, hash, self.s)
 
     def test_clear(self):
         self.s.clear()
-        self.assertEqual(self.s, set())
+        self.assertEqual(self.s, WeakSet([]))
         self.assertEqual(len(self.s), 0)
 
     def test_copy(self):
@@ -304,6 +297,16 @@
         t ^= t
         self.assertEqual(t, WeakSet())
 
+    def test_eq(self):
+        # issue 5964
+        self.assertTrue(self.s == self.s)
+        self.assertTrue(self.s == WeakSet(self.items))
+        self.assertFalse(self.s == set(self.items))
+        self.assertFalse(self.s == list(self.items))
+        self.assertFalse(self.s == tuple(self.items))
+        self.assertFalse(self.s == WeakSet([Foo]))
+        self.assertFalse(self.s == 1)
+
 
 def test_main(verbose=None):
     support.run_unittest(TestWeakSet)


More information about the Python-checkins mailing list