Skip to content

gh-112529: Stop the world around gc.get_referents #114823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2024
Merged
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
41 changes: 28 additions & 13 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,26 @@ referentsvisit(PyObject *obj, void *arg)
return PyList_Append(list, obj) < 0;
}

static int
append_referrents(PyObject *result, PyObject *args)
{
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(args); i++) {
PyObject *obj = PyTuple_GET_ITEM(args, i);
if (!_PyObject_IS_GC(obj)) {
continue;
Copy link
Member

Choose a reason for hiding this comment

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

Small nit: Do you mind adding braces here to conform with PEP 7? (I know we are just moving code around but now that we are changing it it's a good opportunity)

}

traverseproc traverse = Py_TYPE(obj)->tp_traverse;
if (!traverse) {
continue;
}
if (traverse(obj, referentsvisit, result)) {
return -1;
}
}
return 0;
}

/*[clinic input]
gc.get_referents

Expand All @@ -247,29 +267,24 @@ static PyObject *
gc_get_referents_impl(PyObject *module, PyObject *args)
/*[clinic end generated code: output=d47dc02cefd06fe8 input=b3ceab0c34038cbf]*/
{
Py_ssize_t i;
if (PySys_Audit("gc.get_referents", "(O)", args) < 0) {
return NULL;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
PyObject *result = PyList_New(0);

if (result == NULL)
return NULL;

for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
traverseproc traverse;
PyObject *obj = PyTuple_GET_ITEM(args, i);
// NOTE: stop the world is a no-op in default build
_PyEval_StopTheWorld(interp);
int err = append_referrents(result, args);
_PyEval_StartTheWorld(interp);

if (!_PyObject_IS_GC(obj))
continue;
traverse = Py_TYPE(obj)->tp_traverse;
if (! traverse)
continue;
if (traverse(obj, referentsvisit, result)) {
Py_DECREF(result);
return NULL;
}
if (err < 0) {
Py_CLEAR(result);
}

return result;
}

Expand Down