Skip to content

Commit 795d32f

Browse files
committed
gh-152235: Defer GC tracking of a set / frozenset construction from iterable.
1 parent 55bc312 commit 795d32f

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Defer GC tracking of a :class:`set` or :class:`frozenset` to the end of its
2+
construction from iterable. Patch by Donghee Na.

Objects/setobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,9 @@ make_new_set(PyTypeObject *type, PyObject *iterable)
13511351
assert(PyType_Check(type));
13521352
PySetObject *so;
13531353

1354-
so = (PySetObject *)type->tp_alloc(type, 0);
1354+
// Allocate untracked: the fill below runs user code, and a half-built
1355+
// set must not be reachable from another thread via gc.get_objects().
1356+
so = (PySetObject *)_PyType_AllocNoTrack(type, 0);
13551357
if (so == NULL)
13561358
return NULL;
13571359

@@ -1370,6 +1372,8 @@ make_new_set(PyTypeObject *type, PyObject *iterable)
13701372
}
13711373
}
13721374

1375+
// Track only once fully built.
1376+
_PyObject_GC_TRACK(so);
13731377
return (PyObject *)so;
13741378
}
13751379

0 commit comments

Comments
 (0)