From b9abab3c661e30b6f64d476340c4254fa11d6da9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 18 Apr 2026 15:11:14 +0300 Subject: [PATCH] gh-148653: Fix reference leaks in test_marshal introduced in gh-148698 (GH-148725) (cherry picked from commit 7ce737ea11919aebf7eef174f910759e74d0ea50) Co-authored-by: Serhiy Storchaka --- Lib/test/test_marshal.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py index f6962f1e07053a..b67044e66aa331 100644 --- a/Lib/test/test_marshal.py +++ b/Lib/test/test_marshal.py @@ -357,6 +357,9 @@ def f(): code = f.__code__ a = [] code = code.replace(co_consts=code.co_consts + (a,)) + # This test creates a reference loop which leads to reference leaks, + # so we need to break the loop manually. See gh-148722. + self.addCleanup(a.clear) a.append(code) for v in range(marshal.version + 1): self.assertRaises(ValueError, marshal.dumps, code, v) @@ -404,10 +407,12 @@ def test_loads_abnormal_reference_loops(self): self.assertIs(a[0][None], a) # Direct self-reference which cannot be created in Python. - data = b'\xa8\x01\x00\x00\x00r\x00\x00\x00\x00' # (,) - a = marshal.loads(data) - self.assertIsInstance(a, tuple) - self.assertIs(a[0], a) + # This creates a reference loop which cannot be collected. + if False: + data = b'\xa8\x01\x00\x00\x00r\x00\x00\x00\x00' # (,) + a = marshal.loads(data) + self.assertIsInstance(a, tuple) + self.assertIs(a[0], a) # Direct self-references which cannot be created in Python # because of unhashability.