Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Lib/_weakrefset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
73 changes: 73 additions & 0 deletions Lib/test/test_weakset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :class:`weakref.WeakSet` intersection and symmetric difference updates
leaving dead references that were still counted by :func:`len`.
Loading