Skip to content

Commit 74aea0d

Browse files
authored
[3.14] gh-159739: Add a test on _PyTuple_Resize() error (#161957) (#161965)
gh-159739: Add a test on _PyTuple_Resize() error (#161957) Check that no reference is leaked when _PyTuple_Resize() fails. (cherry picked from commit 660bc16)
1 parent fc2f913 commit 74aea0d

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

‎Lib/test/test_capi/test_tuple.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import unittest
21
import gc
2+
import unittest
3+
from test import support
34
from test.support import import_helper
45

56
_testcapi = import_helper.import_module('_testcapi')
@@ -257,6 +258,21 @@ def test__tuple_resize(self):
257258
self.assertTrue(checknull(b, 3))
258259
self.assertTrue(checknull(b, 4))
259260

261+
# Test _PyTuple_Resize() failure: inject MemoryError on
262+
# PyObject_GC_Resize(). Check that the test doesn't leak references
263+
# if the test is run with -R 3:3.
264+
a = tuple(object() for _ in range(10))
265+
got_memory_error = False
266+
# Test different start values since _testcapi _tuple_resize() also
267+
# allocates memory (to copy the tuple).
268+
for start in range(5):
269+
try:
270+
with support.inject_memory_error_cm(start=start):
271+
resize(a, 30)
272+
except MemoryError:
273+
got_memory_error = True
274+
self.assertTrue(got_memory_error)
275+
260276
a = ()
261277
self.assertRaises(MemoryError, resize, a, PY_SSIZE_T_MAX)
262278
self.assertRaises(SystemError, resize, a, -1)

0 commit comments

Comments
 (0)