55"""
66
77import array
8+ import contextlib
89import operator
910import os
1011import re
@@ -48,6 +49,19 @@ def __index__(self):
4849 return self .value
4950
5051
52+ @contextlib .contextmanager
53+ def inject_memory_error (testcase , start ):
54+ # Raise SkipTest if _testcapi extension module is missing
55+ _testcapi = import_helper .import_module ('_testcapi' )
56+
57+ with testcase .assertRaises (MemoryError ):
58+ try :
59+ _testcapi .set_nomemory (start )
60+ yield
61+ finally :
62+ _testcapi .remove_mem_hooks ()
63+
64+
5165class BaseBytesTest :
5266
5367 def assertTypedEqual (self , actual , expected ):
@@ -1558,41 +1572,28 @@ def test_resize(self):
15581572 def test_resize_error (self ):
15591573 # gh-157242: If bytearray.resize() fails (memory allocation failure),
15601574 # the bytearray must be left unchanged.
1561- _testcapi = import_helper .import_module ('_testcapi' )
15621575
15631576 # Simple bytearray
15641577 data = b'some data'
15651578 ba = bytearray (data )
1566- with self .assertRaises (MemoryError ):
1567- try :
1568- _testcapi .set_nomemory (0 )
1569- ba .resize (1024 )
1570- finally :
1571- _testcapi .remove_mem_hooks ()
1579+ with inject_memory_error (self , 0 ):
1580+ ba .resize (1024 )
15721581 self .assertEqual (ba , bytearray (data ))
15731582
15741583 # growing bytearray with non-zero logical start
15751584 ba = bytearray (b'0123456789' )
15761585 expected = ba [3 :]
15771586 del ba [:3 ]
1578- with self .assertRaises (MemoryError ):
1579- try :
1580- _testcapi .set_nomemory (0 )
1581- ba .resize (1024 )
1582- finally :
1583- _testcapi .remove_mem_hooks ()
1587+ with inject_memory_error (self , 0 ):
1588+ ba .resize (1024 )
15841589 self .assertEqual (ba , expected )
15851590
15861591 # shrink bytearray with non-zero logical start
15871592 ba = bytearray (b'0123456789' )
15881593 expected = ba [3 :]
15891594 del ba [:3 ]
1590- with self .assertRaises (MemoryError ):
1591- try :
1592- _testcapi .set_nomemory (0 )
1593- ba .resize (1 )
1594- finally :
1595- _testcapi .remove_mem_hooks ()
1595+ with inject_memory_error (self , 0 ):
1596+ ba .resize (1 )
15961597 self .assertEqual (ba , expected )
15971598
15981599 def test_take_bytes (self ):
@@ -1662,7 +1663,6 @@ def test_take_bytes(self):
16621663 def test_take_bytes_error (self ):
16631664 # gh-157242: If bytearray.take_bytes() fails (memory allocation
16641665 # failure), the bytearray must be left unchanged.
1665- _testcapi = import_helper .import_module ('_testcapi' )
16661666
16671667 for to_take , mem_errors in (
16681668 (5 , (0 , 1 )),
@@ -1673,12 +1673,8 @@ def test_take_bytes_error(self):
16731673 ba = bytearray (b'0123456789' )
16741674 expected = ba [3 :]
16751675 del ba [:3 ]
1676- with self .assertRaises (MemoryError ):
1677- try :
1678- _testcapi .set_nomemory (mem_error )
1679- ba .take_bytes (to_take )
1680- finally :
1681- _testcapi .remove_mem_hooks ()
1676+ with inject_memory_error (self , mem_error ):
1677+ ba .take_bytes (to_take )
16821678 self .assertEqual (ba , expected )
16831679
16841680 @support .cpython_only # tests an implementation detail
0 commit comments