gh-102013: Add PyUnstable_GC_VisitObjects (#102014)

This commit is contained in:
Jacob Bower
2023-03-13 20:35:54 -05:00
committed by GitHub
parent 457e4d1a51
commit cbd3fbfb6e
5 changed files with 146 additions and 0 deletions

View File

@@ -2401,3 +2401,27 @@ PyObject_GC_IsFinalized(PyObject *obj)
}
return 0;
}
void
PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg)
{
size_t i;
GCState *gcstate = get_gc_state();
int origenstate = gcstate->enabled;
gcstate->enabled = 0;
for (i = 0; i < NUM_GENERATIONS; i++) {
PyGC_Head *gc_list, *gc;
gc_list = GEN_HEAD(gcstate, i);
for (gc = GC_NEXT(gc_list); gc != gc_list; gc = GC_NEXT(gc)) {
PyObject *op = FROM_GC(gc);
Py_INCREF(op);
int res = callback(op, arg);
Py_DECREF(op);
if (!res) {
goto done;
}
}
}
done:
gcstate->enabled = origenstate;
}