Skip to content

Commit b32a2ab

Browse files
committed
gh-159739: Add a regression test for the _PyTuple_Resize() leak
Check that the items of a non-empty tuple are released when the reallocation in _PyTuple_Resize() fails. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EyLWRaCmeq66VhJpyTESsw
1 parent 29fd3b8 commit b32a2ab

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

‎Lib/test/test_capi/test_tuple.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,15 @@ def test__tuple_resize(self):
327327
self.assertRaises(MemoryError, resize, a, PY_SSIZE_T_MAX)
328328
self.assertRaises(SystemError, resize, a, -1)
329329
self.assertRaises(SystemError, resize, a, PY_SSIZE_T_MIN)
330+
# gh-159739: items are released if the reallocation fails
331+
x = object()
332+
refcnt = getrefcount(x)
333+
a = (x, x, x)
334+
# Too large to allocate, but small enough not to overflow.
335+
newsize = PY_SSIZE_T_MAX // _testcapi.SIZEOF_VOID_P - 100
336+
self.assertRaises(MemoryError, resize, a, newsize)
337+
del a
338+
self.assertEqual(getrefcount(x), refcnt)
330339
# refcount > 1
331340
a = (1, 2, 3)
332341
self.assertRaises(SystemError, resize, a, 3, False)

0 commit comments

Comments
 (0)