https://github.com/python/cpython/commit/a92e0f9b9fb2569776e9c8451dddeea589423de9
commit: a92e0f9b9fb2569776e9c8451dddeea589423de9
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-09-19T17:53:35Z
summary:

gh-157242: Use support.inject_memory_error_cm() in tests (#157811)

Rename support.memory_error_cm() to support.inject_memory_error_cm().

files:
M Lib/test/support/__init__.py
M Lib/test/test_bytes.py
M Lib/test/test_capi/test_bytes.py
M Lib/test/test_class.py
M Lib/test/test_interpreters/test_stress.py
M Lib/test/test_io/test_memoryio.py
M Lib/test/test_pyexpat.py
M Lib/test/test_str.py

diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 21ff5df44aad373..f348c9a5bbb31cb 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -3555,7 +3555,7 @@ def inject_memory_error(start=0, stop=0):
 
 
 @contextlib.contextmanager
-def memory_error_cm(start=0, stop=0):
+def inject_memory_error_cm(start=0, stop=0):
     """
     Similar to inject_memory_error() but can be used as a context manager.
 
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index bab6006df36a44c..419bee5583de47b 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -52,7 +52,7 @@ def __index__(self):
 @contextlib.contextmanager
 def inject_memory_error(testcase, start=0):
     with testcase.assertRaises(MemoryError):
-        with support.memory_error_cm(start):
+        with support.inject_memory_error_cm(start):
             yield
 
 
diff --git a/Lib/test/test_capi/test_bytes.py b/Lib/test/test_capi/test_bytes.py
index 1356e5d6c51c144..733a8ebbf1e5c36 100644
--- a/Lib/test/test_capi/test_bytes.py
+++ b/Lib/test/test_capi/test_bytes.py
@@ -511,12 +511,9 @@ def test_resize_error(self):
         writer = self.create_writer(len(init))
         writer.write(0, init)
         size = len(init) + 100
-        try:
-            with self.assertRaises(MemoryError):
-                _testcapi.set_nomemory(0)
+        with self.assertRaises(MemoryError):
+            with support.inject_memory_error_cm():
                 writer.resize(size)
-        finally:
-            _testcapi.remove_mem_hooks()
         suffix = b'still working'
         writer.write_bytes(suffix, -1)
         self.assertEqual(writer.finish(), init + suffix)
@@ -590,12 +587,9 @@ def test_grow_error(self):
         init = b'x' * self.LARGE_BUFFER
         writer = self.create_writer(len(init))
         writer.write(0, init)
-        try:
-            with self.assertRaises(MemoryError):
-                _testcapi.set_nomemory(0)
+        with self.assertRaises(MemoryError):
+            with support.inject_memory_error_cm():
                 writer.grow(100)
-        finally:
-            _testcapi.remove_mem_hooks()
         suffix = b'still working'
         writer.write_bytes(suffix, -1)
         self.assertEqual(writer.finish(), init + suffix)
diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py
index b8b35f68aa7a051..3fdc87700f99adf 100644
--- a/Lib/test/test_class.py
+++ b/Lib/test/test_class.py
@@ -1030,7 +1030,7 @@ def __init__(self):
             d = a.__dict__
             try:
                 with support.catch_unraisable_exception() as ex:
-                    with support.memory_error_cm(n, n + 1):
+                    with support.inject_memory_error_cm(n, n + 1):
                         del a
                     exc_type = ex.unraisable and ex.unraisable.exc_type
             except MemoryError:
diff --git a/Lib/test/test_interpreters/test_stress.py 
b/Lib/test/test_interpreters/test_stress.py
index 50d2444a4c72d31..56bfc12f951f795 100644
--- a/Lib/test/test_interpreters/test_stress.py
+++ b/Lib/test/test_interpreters/test_stress.py
@@ -78,15 +78,9 @@ def run():
 
     @support.nomemtest
     def test_create_interpreter_no_memory(self):
-        import _testcapi
-
-        assertion = self.assertRaises(InterpreterError)
-        try:
-            _testcapi.set_nomemory(0, 1)
-            with assertion:
+        with self.assertRaises(InterpreterError):
+            with support.inject_memory_error_cm(0, 1):
                 _interpreters.create()
-        finally:
-            _testcapi.remove_mem_hooks()
 
 
 if __name__ == '__main__':
diff --git a/Lib/test/test_io/test_memoryio.py 
b/Lib/test/test_io/test_memoryio.py
index 59e0dc4435d1f3b..b378505aa8f7db2 100644
--- a/Lib/test/test_io/test_memoryio.py
+++ b/Lib/test/test_io/test_memoryio.py
@@ -5,7 +5,6 @@
 
 import unittest
 from test import support
-from test.support import import_helper
 
 import gc
 import io
@@ -757,18 +756,14 @@ def __buffer__(self, flags):
     @support.nomemtest
     def test_memory_error(self):
         # gh-157242: io.BytesIO() must not close the file on MemoryError
-        _testcapi = import_helper.import_module('_testcapi')
 
         # write()
         stream = self.ioclass()
         stream.write(self.buftype('abc'))
+        data = self.buftype('def')
         with self.assertRaises(MemoryError):
-            try:
-                data = self.buftype('def')
-                _testcapi.set_nomemory(0)
+            with support.inject_memory_error_cm():
                 stream.write(data)
-            finally:
-                _testcapi.remove_mem_hooks()
         stream.write(self.buftype('123'))
         self.assertEqual(stream.getvalue(), self.buftype('abc123'))
 
@@ -777,11 +772,8 @@ def test_memory_error(self):
         stream = self.ioclass()
         stream.write(data)
         with self.assertRaises(MemoryError):
-            try:
-                _testcapi.set_nomemory(0)
+            with support.inject_memory_error_cm():
                 stream.truncate(5)
-            finally:
-                _testcapi.remove_mem_hooks()
         self.assertEqual(stream.getvalue(), data)
 
 
diff --git a/Lib/test/test_pyexpat.py b/Lib/test/test_pyexpat.py
index 23b82dc1fd2179e..fc5c7b311934e58 100644
--- a/Lib/test/test_pyexpat.py
+++ b/Lib/test/test_pyexpat.py
@@ -1078,7 +1078,7 @@ def test_error_path_no_crash(self):
         rc_before = sys.getrefcount(parser)
 
         with self.assertRaises(MemoryError):
-            with support.memory_error_cm(1, 10):
+            with support.inject_memory_error_cm(1, 10):
                 parser.ExternalEntityParserCreate(None)
 
         rc_after = sys.getrefcount(parser)
diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py
index 17163182be08c4c..bef0e62fb42bccc 100644
--- a/Lib/test/test_str.py
+++ b/Lib/test/test_str.py
@@ -614,7 +614,7 @@ def test_replace_oom(self):
         s2 = "&"
         s3 = "&amp;"
         with self.assertRaises(MemoryError):
-            with support.memory_error_cm():
+            with support.inject_memory_error_cm():
                 s1.replace(s2, s3)  # this line used to crash before
 
     def test_repeat_id_preserving(self):

_______________________________________________
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