From c560df9658f1a24edea995fe6f9c84c55b37cfb3 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Thu, 10 Jul 2025 14:13:23 -0700 Subject: [PATCH] gh-136517: Print uncollectable objects if DEBUG_UNCOLLECTABLE mode was set (#136518) --- Lib/test/test_gc.py | 5 +++++ .../2025-07-10-23-23-50.gh-issue-136517._NHJyv.rst | 2 ++ Python/gc.c | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-07-10-23-23-50.gh-issue-136517._NHJyv.rst diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py index 85c43055d0dc..96ebb23f73de 100644 --- a/Lib/test/test_gc.py +++ b/Lib/test/test_gc.py @@ -732,6 +732,9 @@ class GCTests(unittest.TestCase): self.assertIn(b"ResourceWarning: gc: 2 uncollectable objects at " b"shutdown; use", stderr) self.assertNotIn(b"", stderr) + one_line_re = b"gc: uncollectable " + expected_re = one_line_re + b"\r?\n" + one_line_re + self.assertNotRegex(stderr, expected_re) # With DEBUG_UNCOLLECTABLE, the garbage list gets printed stderr = run_command(code % "gc.DEBUG_UNCOLLECTABLE") self.assertIn(b"ResourceWarning: gc: 2 uncollectable objects at " @@ -739,6 +742,8 @@ class GCTests(unittest.TestCase): self.assertTrue( (b"[, ]" in stderr) or (b"[, ]" in stderr), stderr) + # we expect two lines with uncollectable objects + self.assertRegex(stderr, expected_re) # With DEBUG_SAVEALL, no additional message should get printed # (because gc.garbage also contains normally reclaimable cyclic # references, and its elements get printed at runtime anyway). diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-07-10-23-23-50.gh-issue-136517._NHJyv.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-10-23-23-50.gh-issue-136517._NHJyv.rst new file mode 100644 index 000000000000..bf26c4eb0e4c --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-07-10-23-23-50.gh-issue-136517._NHJyv.rst @@ -0,0 +1,2 @@ +Fixed a typo that prevented printing of uncollectable objects when the +:const:`gc.DEBUG_UNCOLLECTABLE` mode was set. diff --git a/Python/gc.c b/Python/gc.c index 88849a43680d..4160f68c27a3 100644 --- a/Python/gc.c +++ b/Python/gc.c @@ -1782,7 +1782,7 @@ gc_collect_region(PyThreadState *tstate, Py_ssize_t n = 0; for (gc = GC_NEXT(&finalizers); gc != &finalizers; gc = GC_NEXT(gc)) { n++; - if (gcstate->debug & _PyGC_DEBUG_COLLECTABLE) + if (gcstate->debug & _PyGC_DEBUG_UNCOLLECTABLE) debug_cycle("uncollectable", FROM_GC(gc)); } stats->uncollectable = n; -- 2.47.2