Skip to content

Commit 00a2585

Browse files
authored
pythongh-145376: Fix GC tracking in structseq.__replace__ (python#145820)
1 parent 79b91e7 commit 00a2585

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Lib/test/test_structseq.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import copy
2+
import gc
23
import os
34
import pickle
45
import re
@@ -355,6 +356,14 @@ def test_reference_cycle(self):
355356
type(t).refcyle = t
356357
"""))
357358

359+
def test_replace_gc_tracked(self):
360+
# Verify that __replace__ results are properly GC-tracked
361+
time_struct = time.gmtime(0)
362+
lst = []
363+
replaced_struct = time_struct.__replace__(tm_year=lst)
364+
lst.append(replaced_struct)
365+
366+
self.assertTrue(gc.is_tracked(replaced_struct))
358367

359368
if __name__ == "__main__":
360369
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix GC tracking in ``structseq.__replace__()``.

Objects/structseq.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ structseq_replace(PyObject *op, PyObject *args, PyObject *kwargs)
445445
}
446446
}
447447

448+
_PyObject_GC_TRACK(result);
448449
return (PyObject *)result;
449450

450451
error:

0 commit comments

Comments
 (0)