summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/list_tests.py10
-rw-r--r--Lib/test/test_bytes.py16
-rw-r--r--Lib/test/test_tuple.py5
-rw-r--r--Misc/NEWS4
-rw-r--r--Objects/bytearrayobject.c8
-rw-r--r--Objects/bytesobject.c2
-rw-r--r--Objects/listobject.c4
-rw-r--r--Objects/tupleobject.c2
8 files changed, 45 insertions, 6 deletions
diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py
index 42e118b..9069337 100644
--- a/Lib/test/list_tests.py
+++ b/Lib/test/list_tests.py
@@ -30,6 +30,12 @@ class CommonTest(seq_tests.CommonTest):
self.assertNotEqual(id(a), id(b))
self.assertEqual(a, b)
+ def test_getitem_error(self):
+ msg = "list indices must be integers or slices"
+ with self.assertRaisesRegex(TypeError, msg):
+ a = []
+ a['a'] = "python"
+
def test_repr(self):
l0 = []
l2 = [0, 1, 2]
@@ -120,6 +126,10 @@ class CommonTest(seq_tests.CommonTest):
a[-1] = 9
self.assertEqual(a, self.type2test([5,6,7,8,9]))
+ msg = "list indices must be integers or slices"
+ with self.assertRaisesRegex(TypeError, msg):
+ a['a'] = "python"
+
def test_delitem(self):
a = self.type2test([0, 1])
del a[1]
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index 43b6c82..db7c1b7 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -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())
diff --git a/Lib/test/test_tuple.py b/Lib/test/test_tuple.py
index 14c6430..0b19ea4 100644
--- a/Lib/test/test_tuple.py
+++ b/Lib/test/test_tuple.py
@@ -6,6 +6,11 @@ import pickle
class TupleTest(seq_tests.CommonTest):
type2test = tuple
+ def test_getitem_error(self):
+ msg = "tuple indices must be integers or slices"
+ with self.assertRaisesRegex(TypeError, msg):
+ ()['a']
+
def test_constructors(self):
super().test_constructors()
# calling built-in types without argument must return empty
diff --git a/Misc/NEWS b/Misc/NEWS
index f771885..b7d3fcd 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ Release date: TBA
Core and Builtins
-----------------
+- 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.
+
- Issue #18395: Rename ``_Py_char2wchar()`` to :c:func:`Py_DecodeLocale`,
rename ``_Py_wchar2char()`` to :c:func:`Py_EncodeLocale`, and document
these functions.
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 242b3b2..f6f370d 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -445,7 +445,9 @@ bytearray_subscript(PyByteArrayObject *self, PyObject *index)
}
}
else {
- PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers");
+ PyErr_Format(PyExc_TypeError,
+ "bytearray indices must be integers or slices, not %.200s",
+ Py_TYPE(index)->tp_name);
return NULL;
}
}
@@ -650,7 +652,9 @@ bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu
}
}
else {
- PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer");
+ PyErr_Format(PyExc_TypeError,
+ "bytearray indices must be integers or slices, not %.200s",
+ Py_TYPE(index)->tp_name);
return -1;
}
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index aff09cd..ca565eb 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -999,7 +999,7 @@ bytes_subscript(PyBytesObject* self, PyObject* item)
}
else {
PyErr_Format(PyExc_TypeError,
- "byte indices must be integers, not %.200s",
+ "byte indices must be integers or slices, not %.200s",
Py_TYPE(item)->tp_name);
return NULL;
}
diff --git a/Objects/listobject.c b/Objects/listobject.c
index fd5a72a..e7c4c82 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2444,7 +2444,7 @@ list_subscript(PyListObject* self, PyObject* item)
}
else {
PyErr_Format(PyExc_TypeError,
- "list indices must be integers, not %.200s",
+ "list indices must be integers or slices, not %.200s",
item->ob_type->tp_name);
return NULL;
}
@@ -2608,7 +2608,7 @@ list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
}
else {
PyErr_Format(PyExc_TypeError,
- "list indices must be integers, not %.200s",
+ "list indices must be integers or slices, not %.200s",
item->ob_type->tp_name);
return -1;
}
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 6fd4db3..753097b 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -746,7 +746,7 @@ tuplesubscript(PyTupleObject* self, PyObject* item)
}
else {
PyErr_Format(PyExc_TypeError,
- "tuple indices must be integers, not %.200s",
+ "tuple indices must be integers or slices, not %.200s",
Py_TYPE(item)->tp_name);
return NULL;
}