diff options
author | Miguel Brito <5544985+miguendes@users.noreply.github.com> | 2021-06-27 12:04:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-27 12:04:57 (GMT) |
commit | ed1076428cca3c8dc7d075c16a575aa390e25fb8 (patch) | |
tree | 4044a9672be5b9e2b86816e3c6751a9aeb92289c /Lib | |
parent | 7569c0fe91dfcf562dee8c29798ecda74d738aa8 (diff) | |
download | cpython-ed1076428cca3c8dc7d075c16a575aa390e25fb8.zip cpython-ed1076428cca3c8dc7d075c16a575aa390e25fb8.tar.gz cpython-ed1076428cca3c8dc7d075c16a575aa390e25fb8.tar.bz2 |
bpo-44110: Improve string's __getitem__ error message (GH-26042)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/string_tests.py | 8 | ||||
-rw-r--r-- | Lib/test/test_userstring.py | 4 |
2 files changed, 10 insertions, 2 deletions
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 840d7bb..089701c 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -80,12 +80,14 @@ class BaseTest: 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 @@ class MixinStrUnicodeUserStringTest: 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 4d1d8b6..51b4f60 100644 --- a/Lib/test/test_userstring.py +++ b/Lib/test/test_userstring.py @@ -27,12 +27,14 @@ class UserStringTest( 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) |