[Python-checkins] bpo-44110: Improve string's __getitem__ error message (GH-26042)

serhiy-storchaka webhook-mailer at python.org
Sun Jun 27 08:05:16 EDT 2021


https://github.com/python/cpython/commit/ed1076428cca3c8dc7d075c16a575aa390e25fb8
commit: ed1076428cca3c8dc7d075c16a575aa390e25fb8
branch: main
author: Miguel Brito <5544985+miguendes at users.noreply.github.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-06-27T15:04:57+03:00
summary:

bpo-44110: Improve string's __getitem__ error message (GH-26042)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-05-11-21-52-44.bpo-44110.VqbAsB.rst
M Lib/test/string_tests.py
M Lib/test/test_userstring.py
M Objects/unicodeobject.c

diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 840d7bb7550f7..089701c983a14 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -80,12 +80,14 @@ class subtype(self.__class__.type2test):
                 self.assertIsNot(obj, realresult)
 
     # check that obj.method(*args) raises exc
-    def checkraises(self, exc, obj, methodname, *args):
+    def checkraises(self, exc, obj, methodname, *args, expected_msg=None):
         obj = self.fixtype(obj)
         args = self.fixtype(args)
         with self.assertRaises(exc) as cm:
             getattr(obj, methodname)(*args)
         self.assertNotEqual(str(cm.exception), '')
+        if expected_msg is not None:
+            self.assertEqual(str(cm.exception), expected_msg)
 
     # call obj.method(*args) without any checks
     def checkcall(self, obj, methodname, *args):
@@ -1195,6 +1197,10 @@ def test_subscript(self):
 
         self.checkraises(TypeError, 'abc', '__getitem__', 'def')
 
+        for idx_type in ('def', object()):
+            expected_msg = "string indices must be integers, not '{}'".format(type(idx_type).__name__)
+            self.checkraises(TypeError, 'abc', '__getitem__', idx_type, expected_msg=expected_msg)
+
     def test_slice(self):
         self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
         self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py
index 4d1d8b6b6fe2d..51b4f6041e49b 100644
--- a/Lib/test/test_userstring.py
+++ b/Lib/test/test_userstring.py
@@ -27,12 +27,14 @@ def checkequal(self, result, object, methodname, *args, **kwargs):
             realresult
         )
 
-    def checkraises(self, exc, obj, methodname, *args):
+    def checkraises(self, exc, obj, methodname, *args, expected_msg=None):
         obj = self.fixtype(obj)
         # we don't fix the arguments, because UserString can't cope with it
         with self.assertRaises(exc) as cm:
             getattr(obj, methodname)(*args)
         self.assertNotEqual(str(cm.exception), '')
+        if expected_msg is not None:
+            self.assertEqual(str(cm.exception), expected_msg)
 
     def checkcall(self, object, methodname, *args):
         object = self.fixtype(object)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-05-11-21-52-44.bpo-44110.VqbAsB.rst b/Misc/NEWS.d/next/Core and Builtins/2021-05-11-21-52-44.bpo-44110.VqbAsB.rst
new file mode 100644
index 0000000000000..3b82e425ab4a7
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-05-11-21-52-44.bpo-44110.VqbAsB.rst	
@@ -0,0 +1 @@
+Improve :func:`str.__getitem__` error message
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index c316cafdc7ffc..3e6b70bf4b6f5 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -14279,7 +14279,8 @@ unicode_subscript(PyObject* self, PyObject* item)
         assert(_PyUnicode_CheckConsistency(result, 1));
         return result;
     } else {
-        PyErr_SetString(PyExc_TypeError, "string indices must be integers");
+        PyErr_Format(PyExc_TypeError, "string indices must be integers, not '%.200s'",
+                     Py_TYPE(item)->tp_name);
         return NULL;
     }
 }



More information about the Python-checkins mailing list