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

antocuni at codespeak.net antocuni at codespeak.net
Mon Jan 26 16:52:29 CET 2009


Author: antocuni
Date: Mon Jan 26 16:52:28 2009
New Revision: 61358

Modified:
   pypy/trunk/pypy/objspace/std/test/test_unicodeobject.py
   pypy/trunk/pypy/objspace/std/unicodetype.py
Log:
make sure that unicode(x) calls __unicode__ if x is an instance of subclass of unicode



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	Mon Jan 26 16:52:28 2009
@@ -678,6 +678,19 @@
         u = unicode(buf, 'utf-8', 'strict')
         assert u == u'character buffers are decoded to unicode'
 
+    def test_unicode_conversion_with__unicode__(self):
+        class A(unicode):
+            def __unicode__(self):
+                return "foo"
+        class B(unicode):
+            pass
+        a = A('bar')
+        assert a == 'bar'
+        assert unicode(a) == 'foo'
+        b = B('bar')
+        assert b == 'bar'
+        assert unicode(b) == 'bar'
+
     def test_unicode_conversion_with__str__(self):
         # new-style classes
         class A(object):

Modified: pypy/trunk/pypy/objspace/std/unicodetype.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/unicodetype.py	(original)
+++ pypy/trunk/pypy/objspace/std/unicodetype.py	Mon Jan 26 16:52:28 2009
@@ -256,9 +256,9 @@
     from pypy.objspace.std.unicodeobject import W_UnicodeObject
     from pypy.objspace.std.ropeunicodeobject import W_RopeUnicodeObject
     w_obj = w_string
-    
+
     encoding, errors = _get_encoding_and_errors(space, w_encoding, w_errors) 
-    if space.is_true(space.isinstance(w_obj, space.w_unicode)):
+    if space.is_w(space.type(w_obj), space.w_unicode):
         if encoding is not None or errors is not None:
             raise OperationError(space.w_TypeError,
                                  space.wrap('decoding Unicode is not supported'))



More information about the Pypy-commit mailing list