@@ -50,16 +50,13 @@ def __index__(self):
5050
5151
5252@contextlib .contextmanager
53- def inject_memory_error (testcase , start ):
53+ def inject_memory_error (testcase , start = 0 ):
5454 # Raise SkipTest if _testcapi extension module is missing
5555 _testcapi = import_helper .import_module ('_testcapi' )
5656
5757 with testcase .assertRaises (MemoryError ):
58- try :
59- _testcapi .set_nomemory (start )
58+ with support .with_memory_error (start ):
6059 yield
61- finally :
62- _testcapi .remove_mem_hooks ()
6360
6461
6562class BaseBytesTest :
@@ -1585,7 +1582,7 @@ def test_resize_error(self):
15851582 del ba [:offset ]
15861583 else :
15871584 expected = ba .copy ()
1588- with inject_memory_error (self , 0 ):
1585+ with inject_memory_error (self ):
15891586 ba .resize (1024 )
15901587 self .assertEqual (ba , expected )
15911588
@@ -1596,7 +1593,7 @@ def test_resize_error(self):
15961593 del ba [:offset ]
15971594 else :
15981595 expected = ba .copy ()
1599- with inject_memory_error (self , 0 ):
1596+ with inject_memory_error (self ):
16001597 ba .resize (1 )
16011598 self .assertEqual (ba , expected )
16021599
@@ -1669,21 +1666,20 @@ def test_take_bytes_error(self):
16691666 # gh-157242: If bytearray.take_bytes() fails (MemoryError),
16701667 # the bytearray must be left unchanged.
16711668
1672- for logical_offset , to_take , mem_errors in (
1669+ for logical_offset , to_take , start_list in (
16731670 (True , 5 , (0 , 1 )),
16741671 (False , 5 , (0 , 1 )),
16751672 (True , None , (0 ,)),
16761673 ):
1677- for mem_error in mem_errors :
1678- with self .subTest (logical_offset = logical_offset ,
1679- to_take = to_take , mem_error = mem_error ):
1674+ for start in start_list :
1675+ with self .subTest (logical_offset = logical_offset , start = start ):
16801676 ba = bytearray (b'0123456789' )
16811677 if logical_offset :
16821678 expected = ba [3 :]
16831679 del ba [:3 ]
16841680 else :
16851681 expected = ba .copy ()
1686- with inject_memory_error (self , mem_error ):
1682+ with inject_memory_error (self , start ):
16871683 ba .take_bytes (to_take )
16881684 self .assertEqual (ba , expected )
16891685
0 commit comments