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

Check that no reference is leaked when _PyTuple_Resize() fails.
diff --git a/Lib/test/test_capi/test_tuple.py b/Lib/test/test_capi/test_tuple.py
index 3f2318f..1f8f5da 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)