https://github.com/python/cpython/commit/d5b42ce496bfb0393e324d7bf4808413804ee847
commit: d5b42ce496bfb0393e324d7bf4808413804ee847
branch: 3.15
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-11-14T00:45:23Z
summary:

[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 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 fe5d694ea7d831..4e52def7e2996b 100644
--- a/Lib/test/test_capi/test_tuple.py
+++ b/Lib/test/test_capi/test_tuple.py
@@ -1,6 +1,7 @@
-import unittest
 import gc
+import unittest
 from sys import getrefcount
+from test import support
 from test.support import import_helper
 
 _testcapi = import_helper.import_module('_testcapi')
@@ -323,6 +324,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