[pypy-commit] pypy py3k: add a maybe helpful error message

pjenvey noreply at buildbot.pypy.org
Wed May 1 22:22:01 CEST 2013


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3k
Changeset: r63791:fa91fc59b6da
Date: 2013-05-01 13:20 -0700
http://bitbucket.org/pypy/pypy/changeset/fa91fc59b6da/

Log:	add a maybe helpful error message

diff --git a/pypy/objspace/std/test/test_unicodeobject.py b/pypy/objspace/std/test/test_unicodeobject.py
--- a/pypy/objspace/std/test/test_unicodeobject.py
+++ b/pypy/objspace/std/test/test_unicodeobject.py
@@ -355,6 +355,14 @@
         assert 'ab'.startswith('b', 1) is True
         assert 'abc'.startswith('bc', 1, 2) is False
         assert 'abc'.startswith('c', -1, 4) is True
+        try:
+            'hello'.startswith(['o'])
+        except TypeError as e:
+            msg = str(e)
+            assert 'str' in msg
+            assert 'tuple' in msg
+        else:
+            assert False, 'Expected TypeError'
 
     def test_startswith_tuples(self):
         assert 'hello'.startswith(('he', 'ha'))
@@ -393,6 +401,14 @@
         assert 'abc'.endswith('bc', 1) is True
         assert 'abc'.endswith('bc', 2) is False
         assert 'abc'.endswith('b', -3, -1) is True
+        try:
+            'hello'.endswith(['o'])
+        except TypeError as e:
+            msg = str(e)
+            assert 'str' in msg
+            assert 'tuple' in msg
+        else:
+            assert False, 'Expected TypeError'
 
     def test_endswith_tuple(self):
         assert not 'hello'.endswith(('he', 'ha'))
diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -454,11 +454,22 @@
             space, len(self), w_start, w_end, upper_bound)
     return (self, start, end)
 
+def unicode_endswith__Unicode_ANY_ANY_ANY(space, w_self, w_substr, w_start, w_end):
+    typename = space.type(w_substr).getname(space)
+    msg = "endswith first arg must be str or a tuple of str, not %s" % typename
+    raise OperationError(space.w_TypeError, space.wrap(msg))
+
 def unicode_endswith__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
     self, start, end = _convert_idx_params(space, w_self,
                                                    w_start, w_end, True)
     return space.newbool(stringendswith(self, w_substr._value, start, end))
 
+def unicode_startswith__Unicode_ANY_ANY_ANY(space, w_self, w_substr, w_start, w_end):
+    typename = space.type(w_substr).getname(space)
+    msg = ("startswith first arg must be str or a tuple of str, not %s" %
+           typename)
+    raise OperationError(space.w_TypeError, space.wrap(msg))
+
 def unicode_startswith__Unicode_Unicode_ANY_ANY(space, w_self, w_substr, w_start, w_end):
     self, start, end = _convert_idx_params(space, w_self, w_start, w_end, True)
     # XXX this stuff can be waaay better for ootypebased backends if


More information about the pypy-commit mailing list