]> granicus.if.org Git - python/commitdiff
#6780: fix starts/endswith error message to mention that tuples are accepted too.
authorEzio Melotti <none@none>
Tue, 26 Apr 2011 02:12:51 +0000 (05:12 +0300)
committerEzio Melotti <none@none>
Tue, 26 Apr 2011 02:12:51 +0000 (05:12 +0300)
Lib/test/test_str.py
Lib/test/test_unicode.py
Misc/NEWS
Objects/stringobject.c
Objects/unicodeobject.c

index 4f88b2884881a2981231a5eaa690a5f4e5b4f90b..2ecf3276b401343153b27a1589209eede09356c1 100644 (file)
@@ -414,7 +414,18 @@ class StrTest(
         self.assertEqual('Andr\202 x'.decode('ascii', 'replace'),
                          'Andr\202 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)
index ae4c355f48d9ce90c18dcbe6605ee5ae4d5340ae..bd43dbc57b7ff47778131a282b190166398dbfd1 100644 (file)
@@ -442,6 +442,17 @@ class UnicodeTest(
                 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
index b1d078faf2e34111097022ff62cd86f78a58a834..29169e79376914567385c2a22646f973403cd75b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@ What's New in Python 2.7.2?
 Core and Builtins
 -----------------
 
+- Issue #6780: fix starts/endswith error message to mention that tuples are
+  accepted too.
+
 - Issue #5057: fix a bug in the peepholer that led to non-portable pyc files
   between narrow and wide builds while optimizing BINARY_SUBSCR on non-BMP
   chars (e.g. u"\U00012345"[0]).
index 373439bca15f656136469dd16f6ae96ed9312f5e..693f7733d9041560cb826f24e5cef337a2b41390 100644 (file)
@@ -2918,8 +2918,12 @@ string_startswith(PyStringObject *self, PyObject *args)
         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 @@ string_endswith(PyStringObject *self, PyObject *args)
         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);
 }
index 4098fb38aeda24f968876cdfd76f38077660178b..b2332a536081eb66687f69bf45a4114f59119ecc 100644 (file)
@@ -7666,8 +7666,12 @@ unicode_startswith(PyUnicodeObject *self,
         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 @@ unicode_endswith(PyUnicodeObject *self,
         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);