https://github.com/python/cpython/commit/00a25859a94b6bf34e58a5176e2befab7e273d20
commit: 00a25859a94b6bf34e58a5176e2befab7e273d20
branch: main
author: Pieter Eendebak <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-03-13T15:42:19Z
summary:

gh-145376: Fix GC tracking in `structseq.__replace__` (#145820)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-03-11-21-27-28.gh-issue-145376.LfDvyw.rst
M Lib/test/test_structseq.py
M Objects/structseq.c

diff --git a/Lib/test/test_structseq.py b/Lib/test/test_structseq.py
index 9622151143cd78..74506fc54de50e 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 @@ def test_reference_cycle(self):
             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 00000000000000..476be205da8001
--- /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 b8bb041f0cff21..8fa9cbba3bcce3 100644
--- a/Objects/structseq.c
+++ b/Objects/structseq.c
@@ -445,6 +445,7 @@ structseq_replace(PyObject *op, PyObject *args, PyObject 
*kwargs)
         }
     }
 
+    _PyObject_GC_TRACK(result);
     return (PyObject *)result;
 
 error:

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to