From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Mon, 16 Mar 2026 09:08:24 +0000 (+0100) Subject: [3.13] gh-145376: Fix GC tracking in `structseq.__replace__` (GH-145820) (#145924) X-Git-Tag: v3.13.13~87 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1d6e037b624534ef60176ef5d34af26d69431df9;p=thirdparty%2FPython%2Fcpython.git [3.13] gh-145376: Fix GC tracking in `structseq.__replace__` (GH-145820) (#145924) gh-145376: Fix GC tracking in `structseq.__replace__` (GH-145820) (cherry picked from commit 00a25859a94b6bf34e58a5176e2befab7e273d20) Co-authored-by: Pieter Eendebak --- diff --git a/Lib/test/test_structseq.py b/Lib/test/test_structseq.py index d0bc0bd7b615..1f259ab4572d 100644 --- a/Lib/test/test_structseq.py +++ b/Lib/test/test_structseq.py @@ -1,4 +1,5 @@ import copy +import gc import os import pickle import re @@ -355,6 +356,14 @@ class StructSeqTest(unittest.TestCase): type(t).refcyle = t """)) + def test_replace_gc_tracked(self): + # Verify that __replace__ results are properly GC-tracked + time_struct = time.gmtime(0) + lst = [] + replaced_struct = time_struct.__replace__(tm_year=lst) + lst.append(replaced_struct) + + self.assertTrue(gc.is_tracked(replaced_struct)) if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-11-21-27-28.gh-issue-145376.LfDvyw.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-11-21-27-28.gh-issue-145376.LfDvyw.rst new file mode 100644 index 000000000000..476be205da80 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-11-21-27-28.gh-issue-145376.LfDvyw.rst @@ -0,0 +1 @@ +Fix GC tracking in ``structseq.__replace__()``. diff --git a/Objects/structseq.c b/Objects/structseq.c index ee3dbf9d4c04..efcadf50078a 100644 --- a/Objects/structseq.c +++ b/Objects/structseq.c @@ -455,6 +455,7 @@ structseq_replace(PyStructSequence *self, PyObject *args, PyObject *kwargs) } } + _PyObject_GC_TRACK(result); return (PyObject *)result; error: