GH-91153: Handle mutating __index__ methods in bytearray item assignment (GH-94891)

(cherry picked from commit f36589510b)

Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
This commit is contained in:
Miss Islington (bot) 2022-07-19 10:12:39 -07:00 committed by GitHub
parent d2be44230e
commit 9487e8d250
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 11 deletions

View file

@ -1710,6 +1710,23 @@ class ByteArrayTest(BaseBytesTest, unittest.TestCase):
self.assertEqual(b1, b)
self.assertEqual(b3, b'xcxcxc')
def test_mutating_index(self):
class Boom:
def __index__(self):
b.clear()
return 0
with self.subTest("tp_as_mapping"):
b = bytearray(b'Now you see me...')
with self.assertRaises(IndexError):
b[0] = Boom()
with self.subTest("tp_as_sequence"):
_testcapi = import_helper.import_module('_testcapi')
b = bytearray(b'Now you see me...')
with self.assertRaises(IndexError):
_testcapi.sequence_setitem(b, 0, Boom())
class AssortedBytesTest(unittest.TestCase):
#