Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r60073:f4f25ac26c4a
Date: 2013-01-14 16:22 -0800
http://bitbucket.org/pypy/pypy/changeset/f4f25ac26c4a/
Log: improve starts/endswith error messages
diff --git a/pypy/objspace/std/stringobject.py
b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -705,11 +705,21 @@
u_self, u_start, u_end = _convert_idx_params(space, w_self, w_start, w_end)
return wrapint(space, u_self.count(w_arg._value, u_start, u_end))
+def _suffix_to_str(space, w_suffix, funcname):
+ try:
+ return space.bufferstr_w(w_suffix)
+ except OperationError as e:
+ if e.match(space, space.w_TypeError):
+ msg = ("%s first arg must be bytes or a tuple of bytes, "
+ "not %s")
+ typename = space.type(w_suffix).getname(space)
+ raise operationerrfmt(space.w_TypeError, msg, funcname, typename)
+
def str_endswith__String_ANY_ANY_ANY(space, w_self, w_suffix, w_start, w_end):
(u_self, start, end) = _convert_idx_params(space, w_self, w_start,
w_end, True)
- return space.newbool(stringendswith(u_self, space.bufferstr_w(w_suffix),
- start, end))
+ return space.newbool(stringendswith(
+ u_self, _suffix_to_str(space, w_suffix, 'endswith'), start, end))
def str_endswith__String_String_ANY_ANY(space, w_self, w_suffix, w_start,
w_end):
(u_self, start, end) = _convert_idx_params(space, w_self, w_start,
@@ -728,8 +738,8 @@
def str_startswith__String_ANY_ANY_ANY(space, w_self, w_prefix, w_start,
w_end):
(u_self, start, end) = _convert_idx_params(space, w_self, w_start,
w_end, True)
- return space.newbool(stringstartswith(u_self, space.bufferstr_w(w_prefix),
- start, end))
+ return space.newbool(stringstartswith(
+ u_self, _suffix_to_str(space, w_prefix, 'startswith'), start, end))
def str_startswith__String_String_ANY_ANY(space, w_self, w_prefix, w_start,
w_end):
(u_self, start, end) = _convert_idx_params(space, w_self, w_start,
diff --git a/pypy/objspace/std/test/test_stringobject.py
b/pypy/objspace/std/test/test_stringobject.py
--- a/pypy/objspace/std/test/test_stringobject.py
+++ b/pypy/objspace/std/test/test_stringobject.py
@@ -321,6 +321,12 @@
assert b'hello'.startswith((bytearray(b'he'), bytearray(b'hel')))
assert b'hello'.startswith((b'he', None, 123))
assert b'y'.startswith(b'xx') is False
+ try:
+ b'hello'.startswith([b'o'])
+ except TypeError as e:
+ assert 'bytes' in str(e)
+ else:
+ assert False, 'Expected TypeError'
def test_startswith_more(self):
assert b'ab'.startswith(b'a', 0) is True
@@ -351,6 +357,12 @@
assert b''.endswith(b'a') is False
assert b'x'.endswith(b'xx') is False
assert b'y'.endswith(b'xx') is False
+ try:
+ b'hello'.endswith([b'o'])
+ except TypeError as e:
+ assert 'bytes' in str(e)
+ else:
+ assert False, 'Expected TypeError'
def test_endswith_more(self):
assert b'abc'.endswith(b'ab', 0, 2) is True
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit