diff --git a/Lib/_weakrefset.py b/Lib/_weakrefset.py index c462e63f430ff7..02edce7656f69a 100644 --- a/Lib/_weakrefset.py +++ b/Lib/_weakrefset.py @@ -108,7 +108,19 @@ def intersection(self, other): def intersection_update(self, other): self.__iand__(other) def __iand__(self, other): - self.data.intersection_update(ref(item) for item in other) + # Keep matching referents alive until their callbacks can remove them + # from self.data. As with set.intersection_update(), stop when full. + matches = {} + try: + for item in other: + itemref = ref(item, self._remove) + if itemref in self.data: + matches.setdefault(itemref, item) + if len(matches) >= len(self.data): + break + self.data = set(matches) + finally: + matches.clear() return self def issubset(self, other): @@ -142,7 +154,14 @@ def __ixor__(self, other): if self is other: self.data.clear() else: - self.data.symmetric_difference_update(ref(item, self._remove) for item in other) + # Retain the first referent for each equal key until the update. + items = {} + try: + for item in other: + items.setdefault(ref(item, self._remove), item) + self.data.symmetric_difference_update(items) + finally: + items.clear() return self def union(self, other): diff --git a/Lib/test/test_weakset.py b/Lib/test/test_weakset.py index 60e1a154b3ec44..873dd327802623 100644 --- a/Lib/test/test_weakset.py +++ b/Lib/test/test_weakset.py @@ -278,6 +278,42 @@ def test_intersection_update(self): self.assertNotIn(c, self.s) self.assertRaises(TypeError, self.s.intersection_update, [[]]) + def test_intersection_update_removes_collected_member(self): + item = ustr('a') + s = WeakSetSubclass([item]) + s.intersection_update([item]) + self.assertIn(item, s) + del item + support.gc_collect() + self.assertEqual(list(s), []) + self.assertEqual(len(s), 0) + + def test_iand_equal_member(self): + item, equal_item = ustr('a'), ustr('a') + s = WeakSet([item]) + s &= iter([equal_item]) + self.assertIs(next(iter(s)), equal_item) + del equal_item + support.gc_collect() + self.assertEqual(list(s), []) + self.assertNotIn(item, s) + self.assertEqual(len(s), 0) + + def test_iand_temporary_member(self): + item, unmatched, absent = ustr('a'), ustr('b'), ustr('c') + s = WeakSet([item, unmatched]) + + def other(): + yield ustr('a') + yield absent + support.gc_collect() + + s &= other() + support.gc_collect() + self.assertEqual(list(s), []) + self.assertNotIn(item, s) + self.assertEqual(len(s), 0) + def test_iand(self): self.s &= set(self.items2) for c in (self.items + self.items2): @@ -315,6 +351,43 @@ def test_symmetric_difference_update(self): self.assertNotIn(c, self.s) self.assertRaises(TypeError, self.s.symmetric_difference_update, [[]]) + def test_symmetric_difference_update_temporary_member(self): + kept, shared, added = ustr('a'), ustr('b'), ustr('c') + + def other(): + yield ustr('d') + yield ustr('b') + yield shared + yield added + yield added + support.gc_collect() + + for operation in ('symmetric_difference_update', 'symmetric_difference'): + with self.subTest(operation=operation): + s = WeakSetSubclass([kept, shared]) + result = getattr(s, operation)(other()) + if operation == 'symmetric_difference_update': + self.assertIsNone(result) + result = s + else: + self.assertIs(type(result), WeakSetSubclass) + self.assertEqual(set(s), {kept, shared}) + support.gc_collect() + self.assertEqual(set(result), {kept, added}) + self.assertNotIn(shared, result) + self.assertEqual(len(result), 2) + + def test_ixor_equal_members(self): + first, second = ustr('a'), ustr('a') + s = WeakSet() + s ^= iter([first, second]) + self.assertEqual(len(s), 1) + self.assertIs(next(iter(s)), first) + del first + support.gc_collect() + self.assertNotIn(second, s) + self.assertEqual(len(s), 0) + def test_ixor(self): self.s ^= set(self.items2) for c in (self.items + self.items2): diff --git a/Misc/NEWS.d/next/Library/2026-11-26-14-21-45.gh-issue-162618.Ft8qJr.rst b/Misc/NEWS.d/next/Library/2026-11-26-14-21-45.gh-issue-162618.Ft8qJr.rst new file mode 100644 index 00000000000000..8f8a54156384da --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-26-14-21-45.gh-issue-162618.Ft8qJr.rst @@ -0,0 +1,2 @@ +Fix :class:`weakref.WeakSet` intersection and symmetric difference updates +leaving dead references that were still counted by :func:`len`.