From dba200ef912438b7e183a78f57997bfbca29a982 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 26 Mar 2025 09:45:29 +0900 Subject: [PATCH 1/2] [3.13] gh-131740: Update PyUnstable_GC_VisitObjects to traverse perm gen (gh-131744) (cherry picked from commit 7bb41aef4b7b8f3c3f07c11b801c5b7f8afaac7f) --- ...-03-26-06-56-40.gh-issue-131740.9PdxxQ.rst | 1 + Python/gc.c | 28 +++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/C API/2025-03-26-06-56-40.gh-issue-131740.9PdxxQ.rst diff --git a/Misc/NEWS.d/next/C API/2025-03-26-06-56-40.gh-issue-131740.9PdxxQ.rst b/Misc/NEWS.d/next/C API/2025-03-26-06-56-40.gh-issue-131740.9PdxxQ.rst new file mode 100644 index 00000000000000..585f07aaddd8c8 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2025-03-26-06-56-40.gh-issue-131740.9PdxxQ.rst @@ -0,0 +1 @@ +Update PyUnstable_GC_VisitObjects to traverse perm gen. diff --git a/Python/gc.c b/Python/gc.c index 8dbcb340d4089d..cd63778126e7a3 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -1966,6 +1966,23 @@ PyObject_GC_IsFinalized(PyObject *obj) return 0; } +static int +visit_generation(gcvisitobjects_t callback, void *arg, struct gc_generation *gen) +{ + PyGC_Head *gc_list, *gc; + gc_list = &gen->head; + 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) { + return -1; + } + } + return 0; +} + void PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg) { @@ -1976,16 +1993,11 @@ PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg) 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; - } + if (visit_generation(callback, arg, &gcstate->generations[i])) { + goto done; } } + visit_generation(callback, arg, &gcstate->permanent_generation); done: gcstate->enabled = origenstate; } From 1e2d439683b718ee9a23f5df7ce36525c4fb92aa Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 26 Mar 2025 18:58:54 +0900 Subject: [PATCH 2/2] fix --- Python/gc.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Python/gc.c b/Python/gc.c index cd63778126e7a3..81871c5f3d6bd1 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -1991,8 +1991,6 @@ PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void *arg) 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); if (visit_generation(callback, arg, &gcstate->generations[i])) { goto done; }