[Python-checkins] cpython (2.7): #6780: fix starts/endswith error message to mention that tuples are accepted

ezio.melotti python-checkins at python.org
Tue Apr 26 05:45:17 CEST 2011


http://hg.python.org/cpython/rev/3ceeccbc2c3b
changeset:   69562:3ceeccbc2c3b
branch:      2.7
parent:      69558:48758cd0769b
user:        Ezio Melotti
date:        Tue Apr 26 05:12:51 2011 +0300
summary:
  #6780: fix starts/endswith error message to mention that tuples are accepted too.

files:
  Lib/test/test_str.py     |  13 ++++++++++++-
  Lib/test/test_unicode.py |  11 +++++++++++
  Misc/NEWS                |   3 +++
  Objects/stringobject.c   |  12 ++++++++++--
  Objects/unicodeobject.c  |  13 ++++++++++---
  5 files changed, 46 insertions(+), 6 deletions(-)


diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py
--- a/Lib/test/test_str.py
+++ b/Lib/test/test_str.py
@@ -414,7 +414,18 @@
         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)
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@
 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]).
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -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 --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -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);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list