Skip to content

Commit dfb05bf

Browse files
committed
Add one more take_bytes() error test
Test without logical offset.
1 parent 0a33fd4 commit dfb05bf

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,7 @@ def test_resize(self):
15701570
self.assertRaises(MemoryError, bytearray(1000).resize, sys.maxsize)
15711571

15721572
def test_resize_error(self):
1573-
# gh-157242: If bytearray.resize() fails (memory allocation failure),
1573+
# gh-157242: If bytearray.resize() fails (MemoryError),
15741574
# the bytearray must be left unchanged.
15751575

15761576
offset = 3
@@ -1663,18 +1663,23 @@ def test_take_bytes(self):
16631663
self.assertEqual(ord(b'c'), ord('c'))
16641664

16651665
def test_take_bytes_error(self):
1666-
# gh-157242: If bytearray.take_bytes() fails (memory allocation
1667-
# failure), the bytearray must be left unchanged.
1666+
# gh-157242: If bytearray.take_bytes() fails (MemoryError),
1667+
# the bytearray must be left unchanged.
16681668

1669-
for to_take, mem_errors in (
1670-
(5, (0, 1)),
1671-
(None, (0,)),
1669+
for logical_offset, to_take, mem_errors in (
1670+
(True, 5, (0, 1)),
1671+
(False, 5, (0, 1)),
1672+
(True, None, (0,)),
16721673
):
16731674
for mem_error in mem_errors:
1674-
with self.subTest(mem_error=mem_error, to_take=to_take):
1675+
with self.subTest(logical_offset=logical_offset,
1676+
to_take=to_take, mem_error=mem_error):
16751677
ba = bytearray(b'0123456789')
1676-
expected = ba[3:]
1677-
del ba[:3]
1678+
if logical_offset:
1679+
expected = ba[3:]
1680+
del ba[:3]
1681+
else:
1682+
expected = ba.copy()
16781683
with inject_memory_error(self, mem_error):
16791684
ba.take_bytes(to_take)
16801685
self.assertEqual(ba, expected)

0 commit comments

Comments
 (0)