Skip to content

Commit bb96c2a

Browse files
committed
GH-91153: Move mutating index bytearray test to it's own separate test
1 parent b2f7df5 commit bb96c2a

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

Lib/test/test_bytes.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,40 +1889,43 @@ def __index__(self):
18891889
with self.assertRaises(IndexError):
18901890
self._testlimitedcapi.sequence_setitem(b, 0, Boom())
18911891

1892-
class BoomContinued:
1892+
def test_mutating_index_inbounds(self):
1893+
class MutatesOnIndex:
1894+
new_ba: bytearray
1895+
1896+
def __init__(self):
1897+
self.ba = bytearray(0x180)
1898+
18931899
def __index__(self):
1894-
nonlocal new_ba
18951900
# Clear the original bytearray, mutating it during index assignment.
18961901
# If the internal buffers are held over this operation, they become dangling
18971902
# However, this will fail a bounds check as above (as the clear sets bounds to zero)
1898-
ba.clear()
1903+
self.ba.clear()
18991904
# At this moment, the bytearray potentially has a dangling pointer
19001905
# Create a new bytearray to catch any writes
1901-
new_ba = bytearray(0x180)
1906+
self.new_ba = bytearray(0x180)
19021907
# Ensure bounds check passes
1903-
ba.extend([0] * 0x180)
1908+
self.ba.extend([0] * 0x180)
19041909
return 0
19051910

19061911
with self.subTest("skip_bounds_safety"):
1907-
new_ba: bytearray
1908-
ba = bytearray(0x180)
1909-
ba[BoomContinued()] = ord("?")
1910-
self.assertEqual(ba[0], ord("?"), "Assigned bytearray not altered")
1911-
self.assertEqual(new_ba, bytearray(0x180), "Wrong object altered")
1912+
instance = MutatesOnIndex()
1913+
instance.ba[instance] = ord("?")
1914+
self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered")
1915+
self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered")
19121916

19131917
with self.subTest("skip_bounds_safety_capi"):
1914-
new_ba: bytearray
1915-
ba = bytearray(0x180)
1916-
self._testlimitedcapi.sequence_setitem(ba, BoomContinued(), ord("?"))
1917-
self.assertEqual(ba[0], ord("?"), "Assigned bytearray not altered")
1918-
self.assertEqual(new_ba, bytearray(0x180), "Wrong object altered")
1918+
instance = MutatesOnIndex()
1919+
instance.ba[instance] = ord("?")
1920+
self._testlimitedcapi.sequence_setitem(instance.ba, instance, ord("?"))
1921+
self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered")
1922+
self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered")
19191923

19201924
with self.subTest("skip_bounds_safety_slice"):
1921-
new_ba: bytearray
1922-
ba = bytearray(0x180)
1923-
ba[BoomContinued():1] = [ord("?")]
1924-
self.assertEqual(ba[0], ord("?"), "Assigned bytearray not altered")
1925-
self.assertEqual(new_ba, bytearray(0x180), "Wrong object altered")
1925+
instance = MutatesOnIndex()
1926+
instance.ba[instance:1] = [ord("?")]
1927+
self.assertEqual(instance.ba[0], ord("?"), "Assigned bytearray not altered")
1928+
self.assertEqual(instance.new_ba, bytearray(0x180), "Wrong object altered")
19261929

19271930

19281931
class AssortedBytesTest(unittest.TestCase):

0 commit comments

Comments
 (0)