https://github.com/python/cpython/commit/74aea0df81b4d9d1fecb00edeba291cea7c47bf6
commit: 74aea0df81b4d9d1fecb00edeba291cea7c47bf6
branch: 3.14
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-11-14T00:41:27Z
summary:

[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 660bc166b46683578397ee4d54533df5a0f33ff6)

files:
M Lib/test/test_capi/test_tuple.py

diff --git a/Lib/test/test_capi/test_tuple.py b/Lib/test/test_capi/test_tuple.py
index 3b5f0336d696bc..d880e9bffdb270 100644
--- a/Lib/test/test_capi/test_tuple.py
+++ b/Lib/test/test_capi/test_tuple.py
@@ -1,5 +1,6 @@
-import unittest
 import gc
+import unittest
+from test import support
 from test.support import import_helper
 
 _testcapi = import_helper.import_module('_testcapi')
@@ -257,6 +258,21 @@ def test__tuple_resize(self):
         self.assertTrue(checknull(b, 3))
         self.assertTrue(checknull(b, 4))
 
+        # Test _PyTuple_Resize() failure: inject MemoryError on
+        # PyObject_GC_Resize(). Check that the test doesn't leak references
+        # if the test is run with -R 3:3.
+        a = tuple(object() for _ in range(10))
+        got_memory_error = False
+        # Test different start values since _testcapi _tuple_resize() also
+        # allocates memory (to copy the tuple).
+        for start in range(5):
+            try:
+                with support.inject_memory_error_cm(start=start):
+                    resize(a, 30)
+            except MemoryError:
+                got_memory_error = True
+        self.assertTrue(got_memory_error)
+
         a = ()
         self.assertRaises(MemoryError, resize, a, PY_SSIZE_T_MAX)
         self.assertRaises(SystemError, resize, a, -1)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to