cpython: f393c507717a (original) (raw)
Mercurial > cpython
changeset 69564:f393c507717a 3.2
#6780: merge with 3.1. [#6780]
Ezio Melotti ezio.melotti@gmail.com | |
---|---|
date | Tue, 26 Apr 2011 06:40:59 +0300 |
parents | cca4c92bf337(current diff)bcbf8c3c4a88(diff) |
children | a1a1296556d7 cca6dc29a900 |
files | Lib/test/test_bytes.py Lib/test/test_unicode.py Misc/NEWS Objects/bytearrayobject.c Objects/bytesobject.c Objects/unicodeobject.c |
diffstat | 6 files changed, 55 insertions(+), 11 deletions(-)[+] [-] Lib/test/test_bytes.py 10 Lib/test/test_unicode.py 8 Misc/NEWS 3 Objects/bytearrayobject.c 16 Objects/bytesobject.c 12 Objects/unicodeobject.c 17 |
line wrap: on
line diff
--- a/Lib/test/test_bytes.py +++ b/Lib/test/test_bytes.py @@ -303,6 +303,11 @@ class BaseBytesTest(unittest.TestCase): self.assertTrue(b.startswith(b"h")) self.assertFalse(b.startswith(b"hellow")) self.assertFalse(b.startswith(b"ha"))
with self.assertRaises(TypeError) as cm:[](#l1.7)
b.startswith([b'h'])[](#l1.8)
exc = str(cm.exception)[](#l1.9)
self.assertIn('bytes', exc)[](#l1.10)
self.assertIn('tuple', exc)[](#l1.11)
def test_endswith(self): b = self.type2test(b'hello') @@ -312,6 +317,11 @@ class BaseBytesTest(unittest.TestCase): self.assertTrue(b.endswith(b"o")) self.assertFalse(b.endswith(b"whello")) self.assertFalse(b.endswith(b"no"))
with self.assertRaises(TypeError) as cm:[](#l1.19)
b.endswith([b'o'])[](#l1.20)
exc = str(cm.exception)[](#l1.21)
self.assertIn('bytes', exc)[](#l1.22)
self.assertIn('tuple', exc)[](#l1.23)
def test_find(self): b = self.type2test(b'mississippi')
--- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -819,6 +819,14 @@ class UnicodeTest(string_tests.CommonTes self.assertEqual('%f' % INF, 'inf') self.assertEqual('%F' % INF, 'INF')
- def test_startswith_endswith_errors(self):
for meth in ('foo'.startswith, 'foo'.endswith):[](#l2.8)
with self.assertRaises(TypeError) as cm:[](#l2.9)
meth(['f'])[](#l2.10)
exc = str(cm.exception)[](#l2.11)
self.assertIn('str', exc)[](#l2.12)
self.assertIn('tuple', exc)[](#l2.13)
+ @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
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -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
- 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]).
--- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1280,7 +1280,7 @@ PyDoc_STRVAR(startswith__doc__, Return True if B starts with the specified prefix, False otherwise.\n[](#l4.4) With optional start, test B beginning at that position.\n[](#l4.5) With optional end, stop comparing B at that position.\n[](#l4.6) -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) @@ -1307,8 +1307,12 @@ bytearray_startswith(PyByteArrayObject * Py_RETURN_FALSE; } result = _bytearray_tailmatch(self, subobj, start, end, -1);
- if (result == -1) {
if (PyErr_ExceptionMatches(PyExc_TypeError))[](#l4.18)
PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes "[](#l4.19)
"or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);[](#l4.20) return NULL;[](#l4.21)
- } else return PyBool_FromLong(result);
} @@ -1319,7 +1323,7 @@ PyDoc_STRVAR(endswith__doc__, Return True if B ends with the specified suffix, False otherwise.\n[](#l4.27) With optional start, test B beginning at that position.\n[](#l4.28) With optional end, stop comparing B at that position.\n[](#l4.29) -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) @@ -1346,8 +1350,12 @@ bytearray_endswith(PyByteArrayObject *se Py_RETURN_FALSE; } result = _bytearray_tailmatch(self, subobj, start, end, +1);
- if (result == -1) {
if (PyErr_ExceptionMatches(PyExc_TypeError))[](#l4.41)
PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "[](#l4.42)
"a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);[](#l4.43) return NULL;[](#l4.44)
- } else return PyBool_FromLong(result);
--- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -2228,8 +2228,12 @@ bytes_startswith(PyBytesObject *self, Py Py_RETURN_FALSE; } result = _bytes_tailmatch(self, subobj, start, end, -1);
- if (result == -1) {
if (PyErr_ExceptionMatches(PyExc_TypeError))[](#l5.9)
PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes "[](#l5.10)
"or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);[](#l5.11) return NULL;[](#l5.12)
- } else return PyBool_FromLong(result);
} @@ -2268,8 +2272,12 @@ bytes_endswith(PyBytesObject *self, PyOb Py_RETURN_FALSE; } result = _bytes_tailmatch(self, subobj, start, end, +1);
- if (result == -1) {
if (PyErr_ExceptionMatches(PyExc_TypeError))[](#l5.23)
PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "[](#l5.24)
"a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);[](#l5.25) return NULL;[](#l5.26)
- } else return PyBool_FromLong(result);
--- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -9029,8 +9029,12 @@ unicode_startswith(PyUnicodeObject *self Py_RETURN_FALSE; } substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj);
- if (substring == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError))[](#l6.10)
PyErr_Format(PyExc_TypeError, "startswith first arg must be str or "[](#l6.11)
"a tuple of str, not %s", Py_TYPE(subobj)->tp_name);[](#l6.12)
return NULL;[](#l6.13)
- } result = tailmatch(self, substring, start, end, -1); Py_DECREF(substring); return PyBool_FromLong(result); @@ -9073,9 +9077,12 @@ unicode_endswith(PyUnicodeObject *self, Py_RETURN_FALSE; } substring = (PyUnicodeObject *)PyUnicode_FromObject(subobj);
- if (substring == NULL) {
if (PyErr_ExceptionMatches(PyExc_TypeError))[](#l6.26)
PyErr_Format(PyExc_TypeError, "endswith first arg must be str or "[](#l6.27)
"a tuple of str, not %s", Py_TYPE(subobj)->tp_name);[](#l6.28)
return NULL;[](#l6.29)
- } result = tailmatch(self, substring, start, end, +1); Py_DECREF(substring); return PyBool_FromLong(result);