Skip to content

Commit ed10764

Browse files
authored
bpo-44110: Improve string's __getitem__ error message (pythonGH-26042)
1 parent 7569c0f commit ed10764

4 files changed

Lines changed: 13 additions & 3 deletions

File tree

Lib/test/string_tests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ class subtype(self.__class__.type2test):
8080
self.assertIsNot(obj, realresult)
8181

8282
# check that obj.method(*args) raises exc
83-
def checkraises(self, exc, obj, methodname, *args):
83+
def checkraises(self, exc, obj, methodname, *args, expected_msg=None):
8484
obj = self.fixtype(obj)
8585
args = self.fixtype(args)
8686
with self.assertRaises(exc) as cm:
8787
getattr(obj, methodname)(*args)
8888
self.assertNotEqual(str(cm.exception), '')
89+
if expected_msg is not None:
90+
self.assertEqual(str(cm.exception), expected_msg)
8991

9092
# call obj.method(*args) without any checks
9193
def checkcall(self, obj, methodname, *args):
@@ -1195,6 +1197,10 @@ def test_subscript(self):
11951197

11961198
self.checkraises(TypeError, 'abc', '__getitem__', 'def')
11971199

1200+
for idx_type in ('def', object()):
1201+
expected_msg = "string indices must be integers, not '{}'".format(type(idx_type).__name__)
1202+
self.checkraises(TypeError, 'abc', '__getitem__', idx_type, expected_msg=expected_msg)
1203+
11981204
def test_slice(self):
11991205
self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
12001206
self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))

Lib/test/test_userstring.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ def checkequal(self, result, object, methodname, *args, **kwargs):
2727
realresult
2828
)
2929

30-
def checkraises(self, exc, obj, methodname, *args):
30+
def checkraises(self, exc, obj, methodname, *args, expected_msg=None):
3131
obj = self.fixtype(obj)
3232
# we don't fix the arguments, because UserString can't cope with it
3333
with self.assertRaises(exc) as cm:
3434
getattr(obj, methodname)(*args)
3535
self.assertNotEqual(str(cm.exception), '')
36+
if expected_msg is not None:
37+
self.assertEqual(str(cm.exception), expected_msg)
3638

3739
def checkcall(self, object, methodname, *args):
3840
object = self.fixtype(object)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve :func:`str.__getitem__` error message

Objects/unicodeobject.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14279,7 +14279,8 @@ unicode_subscript(PyObject* self, PyObject* item)
1427914279
assert(_PyUnicode_CheckConsistency(result, 1));
1428014280
return result;
1428114281
} else {
14282-
PyErr_SetString(PyExc_TypeError, "string indices must be integers");
14282+
PyErr_Format(PyExc_TypeError, "string indices must be integers, not '%.200s'",
14283+
Py_TYPE(item)->tp_name);
1428314284
return NULL;
1428414285
}
1428514286
}

0 commit comments

Comments
 (0)