]> granicus.if.org Git - python/commitdiff
Issue #22077: Improve index error messages for bytearrays, bytes, lists, and
authorTerry Jan Reedy <tjreedy@udel.edu>
Sat, 2 Aug 2014 05:30:37 +0000 (01:30 -0400)
committerTerry Jan Reedy <tjreedy@udel.edu>
Sat, 2 Aug 2014 05:30:37 +0000 (01:30 -0400)
tuples by adding 'or slices'. Added ', not <typename' for bytearrays.
Original patch by Claudiu Popa.

Lib/test/list_tests.py
Lib/test/test_bytes.py
Lib/test/test_tuple.py
Misc/NEWS
Objects/bytearrayobject.c
Objects/bytesobject.c
Objects/listobject.c
Objects/tupleobject.c

index 42e118ba8f8ffc87eede5edaec86ca950d37d48a..906933796d23551afa3193eafe831962478c978e 100644 (file)
@@ -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]
index 43b6c824a8383a33567b734c9434d8895e9d795f..db7c1b704b67798ed3a8dd7ea9ccd7c9d0bdfd91 100644 (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())
 
index 14c64308694f8cd63e8c0b0706f63d4c482c8a58..0b19ea4f794a1d73c548b6aeb65c8289cb6d7b35 100644 (file)
@@ -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
index f771885581decbae462418b51d9b81ce4e06ed12..b7d3fcdb57d35d17ae2c4875c78c54ea17b2a967 100644 (file)
--- 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.
index 242b3b2253b48b97e732b2cd5c32b86ed96dded9..f6f370d42c3529e94b586779c39eb61911d0ef41 100644 (file)
@@ -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;
     }
 
index aff09cd58df3112a29fbf62cc2a791c38d0ac575..ca565eb5666de86de2f1bcf60f7858259047ef21 100644 (file)
@@ -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;
     }
index fd5a72af8d17201583bef005619d62e898c6dfd5..e7c4c82703ff58e5641f1ebb9fc28410d367c25a 100644 (file)
@@ -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;
     }
index 6fd4db3ae01289fa81f74e7a28a373ea9b625a60..753097bef2635b24d38f2cc206a37e9f937ce926 100644 (file)
@@ -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;
     }