mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	bpo-44110: Improve string's __getitem__ error message (GH-26042)
This commit is contained in:
		
							parent
							
								
									7569c0fe91
								
							
						
					
					
						commit
						ed1076428c
					
				
					 4 changed files with 13 additions and 3 deletions
				
			
		| 
						 | 
					@ -80,12 +80,14 @@ class subtype(self.__class__.type2test):
 | 
				
			||||||
                self.assertIsNot(obj, realresult)
 | 
					                self.assertIsNot(obj, realresult)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # check that obj.method(*args) raises exc
 | 
					    # 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)
 | 
					        obj = self.fixtype(obj)
 | 
				
			||||||
        args = self.fixtype(args)
 | 
					        args = self.fixtype(args)
 | 
				
			||||||
        with self.assertRaises(exc) as cm:
 | 
					        with self.assertRaises(exc) as cm:
 | 
				
			||||||
            getattr(obj, methodname)(*args)
 | 
					            getattr(obj, methodname)(*args)
 | 
				
			||||||
        self.assertNotEqual(str(cm.exception), '')
 | 
					        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
 | 
					    # call obj.method(*args) without any checks
 | 
				
			||||||
    def checkcall(self, obj, methodname, *args):
 | 
					    def checkcall(self, obj, methodname, *args):
 | 
				
			||||||
| 
						 | 
					@ -1195,6 +1197,10 @@ def test_subscript(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.checkraises(TypeError, 'abc', '__getitem__', 'def')
 | 
					        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):
 | 
					    def test_slice(self):
 | 
				
			||||||
        self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
 | 
					        self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000))
 | 
				
			||||||
        self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
 | 
					        self.checkequal('abc', 'abc', '__getitem__', slice(0, 3))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,12 +27,14 @@ def checkequal(self, result, object, methodname, *args, **kwargs):
 | 
				
			||||||
            realresult
 | 
					            realresult
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def checkraises(self, exc, obj, methodname, *args):
 | 
					    def checkraises(self, exc, obj, methodname, *args, expected_msg=None):
 | 
				
			||||||
        obj = self.fixtype(obj)
 | 
					        obj = self.fixtype(obj)
 | 
				
			||||||
        # we don't fix the arguments, because UserString can't cope with it
 | 
					        # we don't fix the arguments, because UserString can't cope with it
 | 
				
			||||||
        with self.assertRaises(exc) as cm:
 | 
					        with self.assertRaises(exc) as cm:
 | 
				
			||||||
            getattr(obj, methodname)(*args)
 | 
					            getattr(obj, methodname)(*args)
 | 
				
			||||||
        self.assertNotEqual(str(cm.exception), '')
 | 
					        self.assertNotEqual(str(cm.exception), '')
 | 
				
			||||||
 | 
					        if expected_msg is not None:
 | 
				
			||||||
 | 
					            self.assertEqual(str(cm.exception), expected_msg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def checkcall(self, object, methodname, *args):
 | 
					    def checkcall(self, object, methodname, *args):
 | 
				
			||||||
        object = self.fixtype(object)
 | 
					        object = self.fixtype(object)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					Improve :func:`str.__getitem__` error message
 | 
				
			||||||
| 
						 | 
					@ -14279,7 +14279,8 @@ unicode_subscript(PyObject* self, PyObject* item)
 | 
				
			||||||
        assert(_PyUnicode_CheckConsistency(result, 1));
 | 
					        assert(_PyUnicode_CheckConsistency(result, 1));
 | 
				
			||||||
        return result;
 | 
					        return result;
 | 
				
			||||||
    } else {
 | 
					    } 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;
 | 
					        return NULL;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue