Skip to content

Commit d5b42ce

Browse files
authored
[3.15] gh-159739: Add a test on _PyTuple_Resize() error (#161957) (#161964)
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 71f7343 commit d5b42ce

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,6 +1,7 @@
1-
import unittest
21
import gc
2+
import unittest
33
from sys import getrefcount
4+
from test import support
45
from test.support import import_helper
56

67
_testcapi = import_helper.import_module('_testcapi')
@@ -323,6 +324,21 @@ def test__tuple_resize(self):
323324
self.assertTrue(checknull(b, 3))
324325
self.assertTrue(checknull(b, 4))
325326

327+
# Test _PyTuple_Resize() failure: inject MemoryError on
328+
# PyObject_GC_Resize(). Check that the test doesn't leak references
329+
# if the test is run with -R 3:3.
330+
a = tuple(object() for _ in range(10))
331+
got_memory_error = False
332+
# Test different start values since _testcapi _tuple_resize() also
333+
# allocates memory (to copy the tuple).
334+
for start in range(5):
335+
try:
336+
with support.inject_memory_error_cm(start=start):
337+
resize(a, 30)
338+
except MemoryError:
339+
got_memory_error = True
340+
self.assertTrue(got_memory_error)
341+
326342
a = ()
327343
self.assertRaises(MemoryError, resize, a, PY_SSIZE_T_MAX)
328344
self.assertRaises(SystemError, resize, a, -1)

0 commit comments

Comments
 (0)