[3.15] gh-152235: Defer GC tracking of set and frozenset to end of construction (gh-152237) (gh-152242)

gh-152235: Defer GC tracking of set and frozenset to end of construction (gh-152237)
(cherry picked from commit 908f438e19)

Co-authored-by: Donghee Na <donghee.na@python.org>
This commit is contained in:
Miss Islington (bot)
2026-06-26 05:49:15 +02:00
committed by GitHub
parent 30f9c8a3ef
commit b7a41375ff
2 changed files with 9 additions and 3 deletions
@@ -0,0 +1,2 @@
Defer GC tracking of a :class:`set` or :class:`frozenset` to the end of its
construction from iterable. Patch by Donghee Na.
+7 -3
View File
@@ -1351,7 +1351,9 @@ make_new_set(PyTypeObject *type, PyObject *iterable)
assert(PyType_Check(type));
PySetObject *so;
so = (PySetObject *)type->tp_alloc(type, 0);
// Allocate untracked: the fill below runs user code, and a half-built
// set must not be reachable from another thread via gc.get_objects().
so = (PySetObject *)_PyType_AllocNoTrack(type, 0);
if (so == NULL)
return NULL;
@@ -1370,6 +1372,8 @@ make_new_set(PyTypeObject *type, PyObject *iterable)
}
}
// Track only once fully built.
_PyObject_GC_TRACK(so);
return (PyObject *)so;
}
@@ -2875,7 +2879,7 @@ PyTypeObject PySet_Type = {
0, /* tp_descr_set */
0, /* tp_dictoffset */
set_init, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
_PyType_AllocNoTrack, /* tp_alloc */
set_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
.tp_vectorcall = set_vectorcall,
@@ -2967,7 +2971,7 @@ PyTypeObject PyFrozenSet_Type = {
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
PyType_GenericAlloc, /* tp_alloc */
_PyType_AllocNoTrack, /* tp_alloc */
frozenset_new, /* tp_new */
PyObject_GC_Del, /* tp_free */
.tp_vectorcall = frozenset_vectorcall,