Skip to content

Commit f8de3fe

Browse files
committed
#10360: catch TypeError in WeakSet.__contains__, just like WeakKeyDictionary does.
1 parent 3b9406b commit f8de3fe

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Lib/_weakrefset.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ def __len__(self):
6666
return sum(x() is not None for x in self.data)
6767

6868
def __contains__(self, item):
69-
return ref(item) in self.data
69+
try:
70+
wr = ref(item)
71+
except TypeError:
72+
return False
73+
return wr in self.data
7074

7175
def __reduce__(self):
7276
return (self.__class__, (list(self),),

Lib/test/test_weakset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def test_len(self):
5050
def test_contains(self):
5151
for c in self.letters:
5252
self.assertEqual(c in self.s, c in self.d)
53-
self.assertRaises(TypeError, self.s.__contains__, [[]])
53+
# 1 is not weakref'able, but that TypeError is caught by __contains__
54+
self.assertNotIn(1, self.s)
5455
self.assertIn(self.obj, self.fs)
5556
del self.obj
5657
self.assertNotIn(ustr('F'), self.fs)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Core and Builtins
3333
Library
3434
-------
3535

36+
- Issue #10360: In WeakSet, do not raise TypeErrors when testing for
37+
membership of non-weakrefable objects.
38+
3639
- Issue #940286: pydoc.Helper.help() ignores input/output init parameters.
3740

3841
- Issue #1745035: Add a command size and data size limit to smtpd.py, to

0 commit comments

Comments
 (0)