diff -r d6729016eedc Lib/test/test_str.py --- a/Lib/test/test_str.py Sat Apr 23 15:51:38 2011 -0700 +++ b/Lib/test/test_str.py Sun Apr 24 04:57:53 2011 +0300 @@ -414,7 +414,18 @@ self.assertEqual('Andr202円 x'.decode('ascii', 'replace'), 'Andr202円 x'.decode(encoding='ascii', errors='replace')) - + def test_startswith_endswith_errors(self): + with self.assertRaises(UnicodeDecodeError): + '\xff'.startswith(u'x') + with self.assertRaises(UnicodeDecodeError): + '\xff'.endswith(u'x') + for meth in ('foo'.startswith, 'foo'.endswith): + with self.assertRaises(TypeError) as cm: + meth(['f']) + exc = str(cm.exception) + self.assertIn('unicode', exc) + self.assertIn('str', exc) + self.assertIn('tuple', exc) def test_main(): test_support.run_unittest(StrTest) diff -r d6729016eedc Lib/test/test_unicode.py --- a/Lib/test/test_unicode.py Sat Apr 23 15:51:38 2011 -0700 +++ b/Lib/test/test_unicode.py Sun Apr 24 04:57:53 2011 +0300 @@ -442,6 +442,17 @@ return u'\u1234' self.assertEqual('%s' % Wrapper(), u'\u1234') + def test_startswith_endswith_errors(self): + for meth in (u'foo'.startswith, u'foo'.endswith): + with self.assertRaises(UnicodeDecodeError): + meth('\xff') + with self.assertRaises(TypeError) as cm: + meth(['f']) + exc = str(cm.exception) + self.assertIn('unicode', exc) + self.assertIn('str', exc) + self.assertIn('tuple', exc) + @test_support.run_with_locale('LC_ALL', 'de_DE', 'fr_FR') def test_format_float(self): # should not format with a comma, but always with C locale diff -r d6729016eedc Objects/stringobject.c --- a/Objects/stringobject.c Sat Apr 23 15:51:38 2011 -0700 +++ b/Objects/stringobject.c Sun Apr 24 04:57:53 2011 +0300 @@ -2918,8 +2918,12 @@ Py_RETURN_FALSE; } result = _string_tailmatch(self, subobj, start, end, -1); - if (result == -1) + if (result == -1) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) + PyErr_Format(PyExc_TypeError, "startswith first arg must be str, " + "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name); return NULL; + } else return PyBool_FromLong(result); } @@ -2958,8 +2962,12 @@ Py_RETURN_FALSE; } result = _string_tailmatch(self, subobj, start, end, +1); - if (result == -1) + if (result == -1) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) + PyErr_Format(PyExc_TypeError, "endswith first arg must be str, " + "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name); return NULL; + } else return PyBool_FromLong(result); } diff -r d6729016eedc Objects/unicodeobject.c --- a/Objects/unicodeobject.c Sat Apr 23 15:51:38 2011 -0700 +++ b/Objects/unicodeobject.c Sun Apr 24 04:57:53 2011 +0300 @@ -7666,8 +7666,12 @@ Py_RETURN_FALSE; } substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); - if (substring == NULL) + if (substring == NULL) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) + PyErr_Format(PyExc_TypeError, "startswith first arg must be str, " + "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name); return NULL; + } result = tailmatch(self, substring, start, end, -1); Py_DECREF(substring); return PyBool_FromLong(result); @@ -7710,9 +7714,12 @@ Py_RETURN_FALSE; } substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj); - if (substring == NULL) + if (substring == NULL) { + if (PyErr_ExceptionMatches(PyExc_TypeError)) + PyErr_Format(PyExc_TypeError, "endswith first arg must be str, " + "unicode, or tuple, not %s", Py_TYPE(subobj)->tp_name); return NULL; - + } result = tailmatch(self, substring, start, end, +1); Py_DECREF(substring); return PyBool_FromLong(result);