]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#6780: fix starts/endswith error message to mention that tuples are accepted too.
authorEzio Melotti <ezio.melotti@gmail.com>
Tue, 26 Apr 2011 03:09:45 +0000 (06:09 +0300)
committerEzio Melotti <ezio.melotti@gmail.com>
Tue, 26 Apr 2011 03:09:45 +0000 (06:09 +0300)
Lib/test/test_bytes.py
Lib/test/test_unicode.py
Misc/NEWS
Objects/bytearrayobject.c
Objects/bytesobject.c
Objects/unicodeobject.c

index a607bef2584c1634249cfe019c91c86197999c31..a1e08cc735eff2976a01c15ec97c525acf624c50 100644 (file)
@@ -290,6 +290,14 @@ class BaseBytesTest(unittest.TestCase):
         self.assertTrue(b.startswith(b"h"))
         self.assertFalse(b.startswith(b"hellow"))
         self.assertFalse(b.startswith(b"ha"))
+        try:
+            b.startswith([b'h'])
+        except TypeError as err:
+            exc = str(err)
+        else:
+            self.fail('startswith unexpectedly succeeded')
+        self.assertIn('bytes', exc)
+        self.assertIn('tuple', exc)
 
     def test_endswith(self):
         b = self.type2test(b'hello')
@@ -299,6 +307,14 @@ class BaseBytesTest(unittest.TestCase):
         self.assertTrue(b.endswith(b"o"))
         self.assertFalse(b.endswith(b"whello"))
         self.assertFalse(b.endswith(b"no"))
+        try:
+            b.endswith([b'o'])
+        except TypeError as err:
+            exc = str(err)
+        else:
+            self.fail('endswith unexpectedly succeeded')
+        self.assertIn('bytes', exc)
+        self.assertIn('tuple', exc)
 
     def test_find(self):
         b = self.type2test(b'mississippi')
index 4793707ac9ce09cb2e82af720de7254825e5559b..772ea35d3f295022340a5c9d7f919b101899a7c8 100644 (file)
@@ -789,6 +789,17 @@ class UnicodeTest(
         self.assertEqual('%f' % INF, 'inf')
         self.assertEqual('%F' % INF, 'INF')
 
+    def test_startswith_endswith_errors(self):
+        for meth in ('foo'.startswith, 'foo'.endswith):
+            try:
+                meth(['f'])
+            except TypeError as err:
+                exc = str(err)
+            else:
+                self.fail('starts/endswith unexpectedly succeeded')
+            self.assertIn('str', exc)
+            self.assertIn('tuple', exc)
+
     @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 663846f702631afa04efc0359f64d3397c87f607..b248d5b3510aca5b18715e9a5a813f835a296f4a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.1.4?
 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. "\U00012345"[0]).
index 6fc229df4619df11ca86839ca3ec7a3fe473c158..27affb53d2273134ebb2eaf0c908417db2be4548 100644 (file)
@@ -1281,7 +1281,7 @@ PyDoc_STRVAR(startswith__doc__,
 Return True if B starts with the specified prefix, False otherwise.\n\
 With optional start, test B beginning at that position.\n\
 With optional end, stop comparing B at that position.\n\
-prefix can also be a tuple of strings to try.");
+prefix can also be a tuple of bytes to try.");
 
 static PyObject *
 bytearray_startswith(PyByteArrayObject *self, PyObject *args)
@@ -1308,8 +1308,12 @@ bytearray_startswith(PyByteArrayObject *self, PyObject *args)
         Py_RETURN_FALSE;
     }
     result = _bytearray_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 bytes "
+                         "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
         return NULL;
+    }
     else
         return PyBool_FromLong(result);
 }
@@ -1320,7 +1324,7 @@ PyDoc_STRVAR(endswith__doc__,
 Return True if B ends with the specified suffix, False otherwise.\n\
 With optional start, test B beginning at that position.\n\
 With optional end, stop comparing B at that position.\n\
-suffix can also be a tuple of strings to try.");
+suffix can also be a tuple of bytes to try.");
 
 static PyObject *
 bytearray_endswith(PyByteArrayObject *self, PyObject *args)
@@ -1347,8 +1351,12 @@ bytearray_endswith(PyByteArrayObject *self, PyObject *args)
         Py_RETURN_FALSE;
     }
     result = _bytearray_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 bytes or "
+                         "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
         return NULL;
+    }
     else
         return PyBool_FromLong(result);
 }
index 616f390fb0f0a55add7a2e40eb1b109049dc99f8..f2ee1310fa4fc151f28d8dbaa2a9a89a5bcf5d53 100644 (file)
@@ -2654,8 +2654,12 @@ bytes_startswith(PyBytesObject *self, PyObject *args)
         Py_RETURN_FALSE;
     }
     result = _bytes_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 bytes "
+                         "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
         return NULL;
+    }
     else
         return PyBool_FromLong(result);
 }
@@ -2694,8 +2698,12 @@ bytes_endswith(PyBytesObject *self, PyObject *args)
         Py_RETURN_FALSE;
     }
     result = _bytes_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 bytes or "
+                         "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
         return NULL;
+    }
     else
         return PyBool_FromLong(result);
 }
index 877640d995bdb31692b1139d3eddb7871c98b541..2cdbc0e6890d0072323f6646de2d632060846477 100644 (file)
@@ -1554,7 +1554,7 @@ PyUnicode_FSConverter(PyObject* arg, void* addr)
         arg = PyUnicode_FromObject(arg);
         if (!arg)
             return 0;
-        output = PyUnicode_AsEncodedObject(arg, 
+        output = PyUnicode_AsEncodedObject(arg,
                                            Py_FileSystemDefaultEncoding,
                                            "surrogateescape");
         Py_DECREF(arg);
@@ -1569,7 +1569,7 @@ PyUnicode_FSConverter(PyObject* arg, void* addr)
     if (PyBytes_Check(output)) {
          size = PyBytes_GET_SIZE(output);
          data = PyBytes_AS_STRING(output);
-    } 
+    }
     else {
          size = PyByteArray_GET_SIZE(output);
          data = PyByteArray_AS_STRING(output);
@@ -2148,7 +2148,7 @@ char utf8_code_length[256] = {
        illegal prefix.  See RFC 3629 for details */
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 00-0F */
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
-    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
+    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@@ -2631,7 +2631,7 @@ PyUnicode_DecodeUTF32Stateful(const char *s,
 #endif
     PyObject *errorHandler = NULL;
     PyObject *exc = NULL;
-    
+
     q = (unsigned char *)s;
     e = q + size;
 
@@ -8743,8 +8743,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 or "
+                         "a tuple of str, not %s", Py_TYPE(subobj)->tp_name);
         return NULL;
+    }
     result = tailmatch(self, substring, start, end, -1);
     Py_DECREF(substring);
     return PyBool_FromLong(result);
@@ -8787,9 +8791,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 or "
+                         "a tuple of str, not %s", Py_TYPE(subobj)->tp_name);
         return NULL;
-
+    }
     result = tailmatch(self, substring, start, end, +1);
     Py_DECREF(substring);
     return PyBool_FromLong(result);