]> granicus.if.org Git - python/commitdiff
#10360: catch TypeError in WeakSet.__contains__, just like WeakKeyDictionary does.
authorGeorg Brandl <georg@python.org>
Fri, 3 Dec 2010 07:55:44 +0000 (07:55 +0000)
committerGeorg Brandl <georg@python.org>
Fri, 3 Dec 2010 07:55:44 +0000 (07:55 +0000)
Lib/_weakrefset.py
Lib/test/test_weakset.py
Misc/NEWS

index 3de3bda695f5773bd0caf6a4db68ee005504dbef..42653699a32b16626678e8afd562e628adfac68e 100644 (file)
@@ -66,7 +66,11 @@ class WeakSet:
         return sum(x() is not None for x in self.data)
 
     def __contains__(self, item):
-        return ref(item) in self.data
+        try:
+            wr = ref(item)
+        except TypeError:
+            return False
+        return wr in self.data
 
     def __reduce__(self):
         return (self.__class__, (list(self),),
index fe68b665c8bc71009ef6c312a427b99c722e0bee..58a1f8790ae23cc1b80cd12f43881cf22c9777d8 100644 (file)
@@ -50,7 +50,8 @@ class TestWeakSet(unittest.TestCase):
     def test_contains(self):
         for c in self.letters:
             self.assertEqual(c in self.s, c in self.d)
-        self.assertRaises(TypeError, self.s.__contains__, [[]])
+        # 1 is not weakref'able, but that TypeError is caught by __contains__
+        self.assertNotIn(1, self.s)
         self.assertIn(self.obj, self.fs)
         del self.obj
         self.assertNotIn(ustr('F'), self.fs)
index a6afdb947543f09c0fee6890307d9f245ef68e25..b15721bc085db60cfe81fd13743901bf427f382b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -33,6 +33,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10360: In WeakSet, do not raise TypeErrors when testing for
+  membership of non-weakrefable objects.
+
 - Issue #940286: pydoc.Helper.help() ignores input/output init parameters.
 
 - Issue #1745035: Add a command size and data size limit to smtpd.py, to