[pypy-commit] pypy py3k: ensure __repr/str__ results are unicode

pjenvey noreply at buildbot.pypy.org
Wed Oct 10 02:56:23 CEST 2012


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3k
Changeset: r57959:0543550e6088
Date: 2012-10-09 11:24 -0700
http://bitbucket.org/pypy/pypy/changeset/0543550e6088/

Log:	ensure __repr/str__ results are unicode

diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -673,20 +673,12 @@
                       "unsupported operand type for %(targetname)s(): '%%s'",
                                       typename)
             w_result = space.get_and_call_function(w_impl, w_obj)
+            if space.isinstance_w(w_result, space.w_unicode):
+                return w_result
 
-            if space.is_true(space.isinstance(w_result, space.w_str)):
-                return w_result
-            try:
-                result = space.unicode_w(w_result)
-            except OperationError, e:
-                if not e.match(space, space.w_TypeError):
-                    raise
-                typename = space.type(w_result).getname(space)
-                msg = "%(specialname)s returned non-%(targetname)s (type '%%s')"
-                raise operationerrfmt(space.w_TypeError, msg, typename)
-            else:
-                # re-wrap the result as a real string
-                return space.wrap(result)
+            typename = space.type(w_result).getname(space)
+            msg = "%(specialname)s returned non-%(targetname)s (type '%%s')"
+            raise operationerrfmt(space.w_TypeError, msg, typename)
         assert not hasattr(DescrOperation, %(targetname)r)
         DescrOperation.%(targetname)s = %(targetname)s
         del %(targetname)s
diff --git a/pypy/objspace/test/test_descroperation.py b/pypy/objspace/test/test_descroperation.py
--- a/pypy/objspace/test/test_descroperation.py
+++ b/pypy/objspace/test/test_descroperation.py
@@ -300,6 +300,17 @@
             assert x == 'àèì'
             assert type(x) is str
             
+
+    def test_byte_results_unicode(self):
+        class A(object):
+            def __str__(self):
+                return b'foo'
+            def __repr__(self):
+                return b'bar'
+
+        for operate in (str, repr):
+            raises(TypeError, operate, A())
+
     def test_missing_getattribute(self):
         class X(object): pass
 


More information about the pypy-commit mailing list