Skip to content

Commit ffff144

Browse files
committed
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.
1 parent 7f9cc93 commit ffff144

File tree

8 files changed

+45
-6
lines changed

8 files changed

+45
-6
lines changed

Lib/test/list_tests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ def test_init(self):
3030
self.assertNotEqual(id(a), id(b))
3131
self.assertEqual(a, b)
3232

33+
def test_getitem_error(self):
34+
msg = "list indices must be integers or slices"
35+
with self.assertRaisesRegex(TypeError, msg):
36+
a = []
37+
a['a'] = "python"
38+
3339
def test_repr(self):
3440
l0 = []
3541
l2 = [0, 1, 2]
@@ -120,6 +126,10 @@ def test_setitem(self):
120126
a[-1] = 9
121127
self.assertEqual(a, self.type2test([5,6,7,8,9]))
122128

129+
msg = "list indices must be integers or slices"
130+
with self.assertRaisesRegex(TypeError, msg):
131+
a['a'] = "python"
132+
123133
def test_delitem(self):
124134
a = self.type2test([0, 1])
125135
del a[1]

Lib/test/test_bytes.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,11 @@ def test_find_etc_raise_correct_error_messages(self):
699699
class BytesTest(BaseBytesTest, unittest.TestCase):
700700
type2test = bytes
701701

702+
def test_getitem_error(self):
703+
msg = "byte indices must be integers or slices"
704+
with self.assertRaisesRegex(TypeError, msg):
705+
b'python'['a']
706+
702707
def test_buffer_is_readonly(self):
703708
fd = os.open(__file__, os.O_RDONLY)
704709
with open(fd, "rb", buffering=0) as f:
@@ -753,6 +758,17 @@ def test_from_format(self):
753758
class ByteArrayTest(BaseBytesTest, unittest.TestCase):
754759
type2test = bytearray
755760

761+
def test_getitem_error(self):
762+
msg = "bytearray indices must be integers or slices"
763+
with self.assertRaisesRegex(TypeError, msg):
764+
bytearray(b'python')['a']
765+
766+
def test_setitem_error(self):
767+
msg = "bytearray indices must be integers or slices"
768+
with self.assertRaisesRegex(TypeError, msg):
769+
b = bytearray(b'python')
770+
b['a'] = "python"
771+
756772
def test_nohash(self):
757773
self.assertRaises(TypeError, hash, bytearray())
758774

Lib/test/test_tuple.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
class TupleTest(seq_tests.CommonTest):
77
type2test = tuple
88

9+
def test_getitem_error(self):
10+
msg = "tuple indices must be integers or slices"
11+
with self.assertRaisesRegex(TypeError, msg):
12+
()['a']
13+
914
def test_constructors(self):
1015
super().test_constructors()
1116
# calling built-in types without argument must return empty

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #22077: Improve index error messages for bytearrays, bytes, lists,
14+
and tuples by adding 'or slices'. Added ', not <typename' for bytearrays.
15+
Original patch by Claudiu Popa.
16+
1317
- Issue #18395: Rename ``_Py_char2wchar()`` to :c:func:`Py_DecodeLocale`,
1418
rename ``_Py_wchar2char()`` to :c:func:`Py_EncodeLocale`, and document
1519
these functions.

Objects/bytearrayobject.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,9 @@ bytearray_subscript(PyByteArrayObject *self, PyObject *index)
445445
}
446446
}
447447
else {
448-
PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers");
448+
PyErr_Format(PyExc_TypeError,
449+
"bytearray indices must be integers or slices, not %.200s",
450+
Py_TYPE(index)->tp_name);
449451
return NULL;
450452
}
451453
}
@@ -650,7 +652,9 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
650652
}
651653
}
652654
else {
653-
PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer");
655+
PyErr_Format(PyExc_TypeError,
656+
"bytearray indices must be integers or slices, not %.200s",
657+
Py_TYPE(index)->tp_name);
654658
return -1;
655659
}
656660

Objects/bytesobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ bytes_subscript(PyBytesObject* self, PyObject* item)
999999
}
10001000
else {
10011001
PyErr_Format(PyExc_TypeError,
1002-
"byte indices must be integers, not %.200s",
1002+
"byte indices must be integers or slices, not %.200s",
10031003
Py_TYPE(item)->tp_name);
10041004
return NULL;
10051005
}

Objects/listobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,7 +2444,7 @@ list_subscript(PyListObject* self, PyObject* item)
24442444
}
24452445
else {
24462446
PyErr_Format(PyExc_TypeError,
2447-
"list indices must be integers, not %.200s",
2447+
"list indices must be integers or slices, not %.200s",
24482448
item->ob_type->tp_name);
24492449
return NULL;
24502450
}
@@ -2608,7 +2608,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
26082608
}
26092609
else {
26102610
PyErr_Format(PyExc_TypeError,
2611-
"list indices must be integers, not %.200s",
2611+
"list indices must be integers or slices, not %.200s",
26122612
item->ob_type->tp_name);
26132613
return -1;
26142614
}

Objects/tupleobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
746746
}
747747
else {
748748
PyErr_Format(PyExc_TypeError,
749-
"tuple indices must be integers, not %.200s",
749+
"tuple indices must be integers or slices, not %.200s",
750750
Py_TYPE(item)->tp_name);
751751
return NULL;
752752
}

0 commit comments

Comments
 (0)