Skip to content

Commit

Permalink
python#6780: merge with 3.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezio-melotti committed Apr 26, 2011
2 parents 0fb5b39 + ba42fd5 commit f2b3f78
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
10 changes: 10 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@ def test_startswith(self):
self.assertTrue(b.startswith(b"h"))
self.assertFalse(b.startswith(b"hellow"))
self.assertFalse(b.startswith(b"ha"))
with self.assertRaises(TypeError) as cm:
b.startswith([b'h'])
exc = str(cm.exception)
self.assertIn('bytes', exc)
self.assertIn('tuple', exc)

def test_endswith(self):
b = self.type2test(b'hello')
Expand All @@ -312,6 +317,11 @@ def test_endswith(self):
self.assertTrue(b.endswith(b"o"))
self.assertFalse(b.endswith(b"whello"))
self.assertFalse(b.endswith(b"no"))
with self.assertRaises(TypeError) as cm:
b.endswith([b'o'])
exc = str(cm.exception)
self.assertIn('bytes', exc)
self.assertIn('tuple', exc)

def test_find(self):
b = self.type2test(b'mississippi')
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,14 @@ def __str__(self):
self.assertEqual('%f' % INF, 'inf')
self.assertEqual('%F' % INF, 'INF')

def test_startswith_endswith_errors(self):
for meth in ('foo'.startswith, 'foo'.endswith):
with self.assertRaises(TypeError) as cm:
meth(['f'])
exc = str(cm.exception)
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
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ What's New in Python 3.2.1?
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]).
Expand Down
16 changes: 12 additions & 4 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,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)
Expand All @@ -1307,8 +1307,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);
}
Expand All @@ -1319,7 +1323,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)
Expand All @@ -1346,8 +1350,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);
}
Expand Down
12 changes: 10 additions & 2 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2228,8 +2228,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);
}
Expand Down Expand Up @@ -2268,8 +2272,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);
}
Expand Down
13 changes: 10 additions & 3 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -9029,8 +9029,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);
Expand Down Expand Up @@ -9073,9 +9077,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);
Expand Down

0 comments on commit f2b3f78

Please sign in to comment.