Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 7 additions & 3 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the type spec and remove PyType_GenericAlloc from tp_alloc?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch :)

if (so == NULL)
return NULL;

Expand All @@ -1370,6 +1372,8 @@ make_new_set(PyTypeObject *type, PyObject *iterable)
}
}

// Track only once fully built.
_PyObject_GC_TRACK(so);
return (PyObject *)so;
}

Expand Down Expand Up @@ -2885,7 +2889,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,
Expand Down Expand Up @@ -2977,7 +2981,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,
Expand Down
Loading