Issue #22077: Improve index error messages for bytearrays, bytes, lists, and

tuples by adding 'or slices'. Added ', not <typename' for bytearrays.
Original patch by Claudiu Popa.
This commit is contained in:
Terry Jan Reedy 2014-08-02 01:30:37 -04:00
parent 7f9cc9359b
commit ffff1440d1
8 changed files with 45 additions and 6 deletions

View file

@ -699,6 +699,11 @@ class BaseBytesTest:
class BytesTest(BaseBytesTest, unittest.TestCase):
type2test = bytes
def test_getitem_error(self):
msg = "byte indices must be integers or slices"
with self.assertRaisesRegex(TypeError, msg):
b'python'['a']
def test_buffer_is_readonly(self):
fd = os.open(__file__, os.O_RDONLY)
with open(fd, "rb", buffering=0) as f:
@ -753,6 +758,17 @@ class BytesTest(BaseBytesTest, unittest.TestCase):
class ByteArrayTest(BaseBytesTest, unittest.TestCase):
type2test = bytearray
def test_getitem_error(self):
msg = "bytearray indices must be integers or slices"
with self.assertRaisesRegex(TypeError, msg):
bytearray(b'python')['a']
def test_setitem_error(self):
msg = "bytearray indices must be integers or slices"
with self.assertRaisesRegex(TypeError, msg):
b = bytearray(b'python')
b['a'] = "python"
def test_nohash(self):
self.assertRaises(TypeError, hash, bytearray())