Skip to content

Commit 37ab323

Browse files
committed
Always track frozenset in the GC
1 parent c9e96e6 commit 37ab323

2 files changed

Lines changed: 13 additions & 19 deletions

File tree

‎Lib/test/test_capi/test_set.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,10 @@ def test_update_array_steal(self):
391391
self.check_update_method('update_array_steal')
392392

393393
def test_finish(self):
394-
# Test PyFrozenSetWriter_Finish(): track the frozenset in the GC if at
395-
# least one item is tracked by the GC
394+
# Test PyFrozenSetWriter_Finish(): track the frozenset in the GC
396395
writer = self.create_writer()
397396
writer.update([2, 5, 7])
398-
self.assertFalse(gc.is_tracked(writer.finish()))
397+
self.assertTrue(gc.is_tracked(writer.finish()))
399398

400399
class MyObject:
401400
pass
@@ -418,19 +417,22 @@ def test_discard(self):
418417

419418
@threading_helper.requires_working_threading()
420419
def test_thread(self):
421-
# PyFrozenSetWriter can be used by multiple threads: it's up to the caller
422-
# to implement a lock to prevent concurrent accesses.
420+
# PyFrozenSetWriter can be used by multiple threads: it's up to the
421+
# caller to implement a lock to prevent concurrent accesses.
423422
writer = self.create_writer()
424423

425424
def thread_func(writer):
426-
writer.add('Hello')
427-
writer.update(['World!'])
425+
writer.add('a')
426+
writer.add_steal('b')
427+
writer.update(['c'])
428+
writer.update_array(['d'])
429+
writer.update_array_steal(['e'])
428430

429431
thread = threading.Thread(target=thread_func, args=(writer,))
430432
thread.start()
431433
threading_helper.join_thread(thread)
432434

433-
self.frozenset_equal(writer.finish(), {'Hello', 'World!'})
435+
self.frozenset_equal(writer.finish(), frozenset('abcde'))
434436

435437

436438
if __name__ == "__main__":

‎Objects/setobject.c‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,17 +3288,9 @@ PyFrozenSetWriter_Finish(PyFrozenSetWriter *writer)
32883288
assert(frozensetwriter_check_consistency(writer));
32893289
PySetObject *frozenset = _PySet_CAST(writer);
32903290

3291-
// Now that the frozenset is fully initialized, it's safe to track it in
3292-
// the GC. Track the frozenset if at least one item is tracked by the GC.
3293-
Py_ssize_t pos = 0;
3294-
setentry *entry;
3295-
while (set_next(frozenset, &pos, &entry)) {
3296-
PyObject *item = entry->key;
3297-
if (_PyObject_IS_GC(item) && _PyObject_GC_IS_TRACKED(item)) {
3298-
_PyObject_GC_TRACK(frozenset);
3299-
break;
3300-
}
3301-
}
3291+
// Now that the frozenset is fully initialized, it's safe to track it
3292+
// in the garbage collector.
3293+
_PyObject_GC_TRACK(frozenset);
33023294

33033295
return _PyObject_CAST(frozenset);
33043296
}

0 commit comments

Comments
 (0)