diff options
author | Georg Brandl <georg@python.org> | 2010-12-03 07:55:44 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-12-03 07:55:44 (GMT) |
commit | f8de3fea1280d55377d40c6e04b64114f9da2fa6 (patch) | |
tree | bf723ad86ffcf6bdc2550fe6304fd384d9445e08 | |
parent | 3b9406b08aad04c1f92d7a2ab03c8a42c3afb8be (diff) | |
download | cpython-f8de3fea1280d55377d40c6e04b64114f9da2fa6.zip cpython-f8de3fea1280d55377d40c6e04b64114f9da2fa6.tar.gz cpython-f8de3fea1280d55377d40c6e04b64114f9da2fa6.tar.bz2 |
#10360: catch TypeError in WeakSet.__contains__, just like WeakKeyDictionary does.
-rw-r--r-- | Lib/_weakrefset.py | 6 | ||||
-rw-r--r-- | Lib/test/test_weakset.py | 3 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 10 insertions, 2 deletions
diff --git a/Lib/_weakrefset.py b/Lib/_weakrefset.py index 3de3bda..4265369 100644 --- a/Lib/_weakrefset.py +++ b/Lib/_weakrefset.py @@ -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),), diff --git a/Lib/test/test_weakset.py b/Lib/test/test_weakset.py index fe68b66..58a1f87 100644 --- a/Lib/test/test_weakset.py +++ b/Lib/test/test_weakset.py @@ -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) @@ -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 |