Skip to content

Commit 4b8caa0

Browse files
committed
Add test
1 parent 9e6b932 commit 4b8caa0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Lib/test/test_capi/test_tuple.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import sys
3+
import gc
34
from collections import namedtuple
45
from test.support import import_helper
56

@@ -257,5 +258,29 @@ def test__tuple_resize(self):
257258
self.assertRaises(SystemError, resize, [1, 2, 3], 0, False)
258259
self.assertRaises(SystemError, resize, NULL, 0, False)
259260

261+
def test_bug_59313(self):
262+
# Before 3.14, the C-API function PySequence_Tuple
263+
# would create incomplete tuples which were visible to
264+
# the cycle GC, and this test would crash the interpeter.
265+
TAG = object()
266+
tuples = []
267+
268+
def referrer_tuples():
269+
return [x for x in gc.get_referrers(TAG)
270+
if isinstance(x, tuple)]
271+
272+
def my_iter():
273+
nonlocal tuples
274+
yield TAG # 'tag' gets stored in the result tuple
275+
tuples += referrer_tuples()
276+
for x in range(10):
277+
tuples += referrer_tuples()
278+
# Prior to 3.13 would raise a SystemError when the tuple needs to be resized
279+
yield x
280+
281+
self.assertEqual(tuple(my_iter()), (TAG, *range(10)))
282+
self.assertEqual(tuples, [])
283+
284+
260285
if __name__ == "__main__":
261286
unittest.main()

0 commit comments

Comments
 (0)