https://github.com/python/cpython/commit/660bc166b46683578397ee4d54533df5a0f33ff6
commit: 660bc166b46683578397ee4d54533df5a0f33ff6
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-11-14T00:59:53+01:00
summary:

gh-159739: Add a test on _PyTuple_Resize() error (#161957)

Check that no reference is leaked when _PyTuple_Resize() fails.

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 3f2318f303d57a..1f8f5da0e51e90 100644
--- a/Lib/test/test_capi/test_tuple.py
+++ b/Lib/test/test_capi/test_tuple.py
@@ -1,6 +1,7 @@
 import gc
 import sys
 import unittest
+from test import support
 from test.support import import_helper
 
 _testcapi = import_helper.import_module('_testcapi')
@@ -364,6 +365,21 @@ def resize(tup, new_size, new=True, compute_hash=False):
         with self.assertRaises(SystemError):
             resize(a, 3, True, compute_hash=True)
 
+        # 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