Skip to content

Commit 4b5f32e

Browse files
committed
gh-157242: Use support.inject_memory_error_cm() in tests (#157811)
Rename support.memory_error_cm() to support.inject_memory_error_cm(). (cherry picked from commit a92e0f9)
1 parent 9daa6bd commit 4b5f32e

8 files changed

Lines changed: 34 additions & 35 deletions

File tree

‎Lib/test/support/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3472,7 +3472,7 @@ def inject_memory_error(start=0, stop=0):
34723472

34733473

34743474
@contextlib.contextmanager
3475-
def memory_error_cm(start=0, stop=0):
3475+
def inject_memory_error_cm(start=0, stop=0):
34763476
"""
34773477
Similar to inject_memory_error() but can be used as a context manager.
34783478

‎Lib/test/test_bytes.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __index__(self):
5353
@contextlib.contextmanager
5454
def inject_memory_error(testcase, start=0):
5555
with testcase.assertRaises(MemoryError):
56-
with support.memory_error_cm(start):
56+
with support.inject_memory_error_cm(start):
5757
yield
5858

5959

‎Lib/test/test_capi/test_bytes.py‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,23 @@ def test_resize(self):
455455
writer.resize(_testcapi.PY_SSIZE_T_MAX)
456456
self.assertEqual(writer.finish(), b'x' * size)
457457

458+
@support.nomemtest
459+
def test_resize_error(self):
460+
# Test PyBytesWriter_Resize() error
461+
init = b'x' * self.LARGE_BUFFER
462+
writer = self.create_writer(len(init))
463+
writer.write(0, init)
464+
size = len(init) + 100
465+
with self.assertRaises(MemoryError):
466+
with support.inject_memory_error_cm():
467+
writer.resize(size)
468+
suffix = b'still working'
469+
writer.write_bytes(suffix, -1)
470+
self.assertEqual(writer.finish(), init + suffix)
471+
472+
# Note: PyBytesWriter_Resize() leaves the buffer unchanged (no resize)
473+
# if the new size is smaller than the allocated size
474+
458475
def test_grow(self):
459476
# Test PyBytesWriter_Grow()
460477
writer = self.create_writer(0)
@@ -513,24 +530,20 @@ def test_grow(self):
513530
self.assertEqual(writer.finish(), b'x' * size)
514531

515532
@support.nomemtest
516-
def test_resize_error(self):
517-
# Test PyBytesWriter_Resize() error
533+
def test_grow_error(self):
534+
# Test PyBytesWriter_Grow() error
518535
init = b'x' * self.LARGE_BUFFER
519536
writer = self.create_writer(len(init))
520537
writer.write(0, init)
521-
size = len(init) + 100
522-
try:
523-
with self.assertRaises(MemoryError):
524-
_testcapi.set_nomemory(0)
525-
writer.resize(size)
526-
finally:
527-
_testcapi.remove_mem_hooks()
538+
with self.assertRaises(MemoryError):
539+
with support.inject_memory_error_cm():
540+
writer.grow(100)
528541
suffix = b'still working'
529542
writer.write_bytes(suffix, -1)
530543
self.bytes_equal(writer.finish(), init + suffix)
531544

532-
# Note: PyBytesWriter_Resize() leaves the buffer unchanged (no resize)
533-
# if the new size is smaller than the allocated size
545+
# Note: PyBytesWriter_Grow() leaves the buffer unchanged (no resize)
546+
# if grow is negative.
534547

535548
def test_format_i(self):
536549
# Test PyBytesWriter_Format()

‎Lib/test/test_class.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ def __init__(self):
10301030
d = a.__dict__
10311031
try:
10321032
with support.catch_unraisable_exception() as ex:
1033-
with support.memory_error_cm(n, n + 1):
1033+
with support.inject_memory_error_cm(n, n + 1):
10341034
del a
10351035
exc_type = ex.unraisable and ex.unraisable.exc_type
10361036
except MemoryError:

‎Lib/test/test_interpreters/test_stress.py‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,9 @@ def run():
7878

7979
@support.nomemtest
8080
def test_create_interpreter_no_memory(self):
81-
import _testcapi
82-
83-
assertion = self.assertRaises(InterpreterError)
84-
try:
85-
_testcapi.set_nomemory(0, 1)
86-
with assertion:
81+
with self.assertRaises(InterpreterError):
82+
with support.inject_memory_error_cm(0, 1):
8783
_interpreters.create()
88-
finally:
89-
_testcapi.remove_mem_hooks()
9084

9185

9286
if __name__ == '__main__':

‎Lib/test/test_io/test_memoryio.py‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import unittest
77
from test import support
8-
from test.support import import_helper
98

109
import gc
1110
import io
@@ -654,18 +653,14 @@ def __buffer__(self, flags):
654653

655654
def test_memory_error(self):
656655
# gh-157242: io.BytesIO() must not close the file on MemoryError
657-
_testcapi = import_helper.import_module('_testcapi')
658656

659657
# write()
660658
stream = self.ioclass()
661659
stream.write(self.buftype('abc'))
660+
data = self.buftype('def')
662661
with self.assertRaises(MemoryError):
663-
try:
664-
data = self.buftype('def')
665-
_testcapi.set_nomemory(0)
662+
with support.inject_memory_error_cm():
666663
stream.write(data)
667-
finally:
668-
_testcapi.remove_mem_hooks()
669664
stream.write(self.buftype('123'))
670665
self.assertEqual(stream.getvalue(), self.buftype('abc123'))
671666

@@ -674,11 +669,8 @@ def test_memory_error(self):
674669
stream = self.ioclass()
675670
stream.write(data)
676671
with self.assertRaises(MemoryError):
677-
try:
678-
_testcapi.set_nomemory(0)
672+
with support.inject_memory_error_cm():
679673
stream.truncate(5)
680-
finally:
681-
_testcapi.remove_mem_hooks()
682674
self.assertEqual(stream.getvalue(), data)
683675

684676

‎Lib/test/test_pyexpat.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ def test_error_path_no_crash(self):
10291029
rc_before = sys.getrefcount(parser)
10301030

10311031
with self.assertRaises(MemoryError):
1032-
with support.memory_error_cm(1, 10):
1032+
with support.inject_memory_error_cm(1, 10):
10331033
parser.ExternalEntityParserCreate(None)
10341034

10351035
rc_after = sys.getrefcount(parser)

‎Lib/test/test_str.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ def test_replace_oom(self):
614614
s2 = "&"
615615
s3 = "&"
616616
with self.assertRaises(MemoryError):
617-
with support.memory_error_cm():
617+
with support.inject_memory_error_cm():
618618
s1.replace(s2, s3) # this line used to crash before
619619

620620
def test_repeat_id_preserving(self):

0 commit comments

Comments
 (0)