[pypy-svn] r59565 - in pypy/trunk/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Thu Oct 30 15:39:08 CET 2008


Author: arigo
Date: Thu Oct 30 15:39:06 2008
New Revision: 59565

Modified:
   pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py
   pypy/trunk/pypy/objspace/std/unicodetype.py
Log:
Test and fix.  The test comes from CPython.


Modified: pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py	(original)
+++ pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py	Thu Oct 30 15:39:06 2008
@@ -702,3 +702,9 @@
         b = B()
         s = '%s %s' % (b, a)
         assert s == u'foo baz'
+
+    def test_str_subclass(self):
+        class Foo9(str):
+            def __unicode__(self):
+                return u"world"
+        assert unicode(Foo9("hello")) == u"world"

Modified: pypy/trunk/pypy/objspace/std/unicodetype.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/unicodetype.py	(original)
+++ pypy/trunk/pypy/objspace/std/unicodetype.py	Thu Oct 30 15:39:06 2008
@@ -212,22 +212,16 @@
     return w_retval
 
 def unicode_from_object(space, w_obj):
-    if space.is_true(space.isinstance(w_obj, space.w_str)):
+    if space.is_w(space.type(w_obj), space.w_str):
         w_res = w_obj
     else:
-        try:
-            # XXX should we have a space.unicode so we can go through
-            # descroperation?
-            w_unicode_method = space.getattr(w_obj, space.wrap("__unicode__"))
-        except OperationError, e:
-            if e.match(space, space.w_AttributeError):
-                w_res = space.str(w_obj)
-            else:
-                raise
+        w_unicode_method = space.lookup(w_obj, "__unicode__")
+        if w_unicode_method is not None:
+            w_res = space.get_and_call_function(w_unicode_method, w_obj)
         else:
-            w_res = space.call_function(w_unicode_method)
-    if space.is_true(space.isinstance(w_res, space.w_unicode)):
-        return w_res
+            w_res = space.str(w_obj)
+        if space.is_true(space.isinstance(w_res, space.w_unicode)):
+            return w_res
     return unicode_from_encoded_object(space, w_res, None, "strict")
 
 def unicode_from_string(space, w_str):
@@ -267,10 +261,7 @@
         w_value = w_obj
     else:
         if encoding is None and errors is None:
-            if space.is_true(space.isinstance(w_obj, space.w_str)):
-                w_value = unicode_from_string(space, w_obj)
-            else:
-                w_value = unicode_from_object(space, w_obj)
+            w_value = unicode_from_object(space, w_obj)
         else:
             w_value = unicode_from_encoded_object(space, w_obj,
                                                   encoding, errors)



More information about the Pypy-commit mailing list