From bac689126e5b77b1780dfbcf213d2e8df07f93fc Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Thu, 10 Jul 2025 23:08:25 +0500 Subject: [PATCH 1/5] Print uncollectable objects if DEBUG_UNCOLLECTABLE mode was set --- Lib/test/test_gc.py | 5 +++++ Python/gc.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py index 85c43055d0dcec..d3bf17337c7e00 100644 --- a/Lib/test/test_gc.py +++ b/Lib/test/test_gc.py @@ -732,6 +732,9 @@ def run_command(code): 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 @@ def run_command(code): 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/Python/gc.c b/Python/gc.c index 88849a43680d2e..4160f68c27a3ef 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; From 9b03b440438b1ec730de47399f946c583619ece6 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Thu, 10 Jul 2025 23:24:05 +0500 Subject: [PATCH 2/5] Add news entry --- .../2025-07-10-23-23-50.gh-issue-136517._NHJyv.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-07-10-23-23-50.gh-issue-136517._NHJyv.rst 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 00000000000000..9146fa65051dd5 --- /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 +DEBUG_UNCOLLECTABLE mode was set From 361f23e7d6b5691d074a0371db0c07c514898e70 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Thu, 10 Jul 2025 23:59:00 +0500 Subject: [PATCH 3/5] Fix regex --- Lib/test/test_gc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py index d3bf17337c7e00..8b38ac8a89409a 100644 --- a/Lib/test/test_gc.py +++ b/Lib/test/test_gc.py @@ -732,8 +732,8 @@ def run_command(code): 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 + one_line_re = b"gc: uncollectable " + expected_re = one_line_re + b".*" + one_line_re self.assertNotRegex(stderr, expected_re) # With DEBUG_UNCOLLECTABLE, the garbage list gets printed stderr = run_command(code % "gc.DEBUG_UNCOLLECTABLE") From 094172329064db7da294b6636014f9bd37ff9229 Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Thu, 10 Jul 2025 23:59:35 +0500 Subject: [PATCH 4/5] Update Misc/NEWS.d/next/Core_and_Builtins/2025-07-10-23-23-50.gh-issue-136517._NHJyv.rst Co-authored-by: Kirill Podoprigora --- .../2025-07-10-23-23-50.gh-issue-136517._NHJyv.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 9146fa65051dd5..bf26c4eb0e4c74 100644 --- 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 @@ -1,2 +1,2 @@ Fixed a typo that prevented printing of uncollectable objects when the -DEBUG_UNCOLLECTABLE mode was set +:const:`gc.DEBUG_UNCOLLECTABLE` mode was set. From 9c13a57b1f99e3a347cfb3f40dd0fc2b76dcfaed Mon Sep 17 00:00:00 2001 From: Sergey Miryanov Date: Fri, 11 Jul 2025 00:25:06 +0500 Subject: [PATCH 5/5] Fix newlines in regex --- Lib/test/test_gc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_gc.py b/Lib/test/test_gc.py index 8b38ac8a89409a..96ebb23f73de9d 100644 --- a/Lib/test/test_gc.py +++ b/Lib/test/test_gc.py @@ -733,7 +733,7 @@ def run_command(code): b"shutdown; use", stderr) self.assertNotIn(b"", stderr) one_line_re = b"gc: uncollectable " - expected_re = one_line_re + b".*" + one_line_re + 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")