Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r95089:baf31e0bca1e
Date: 2018-09-11 15:26 +0200
http://bitbucket.org/pypy/pypy/changeset/baf31e0bca1e/

Log:    Add more tests inspired from issue #2866.

        Fix them by simplifying a bit descr_new().

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
@@ -1146,3 +1146,26 @@
         raises(TypeError, "unicode('', encoding=None)")
         raises(TypeError, 'u"".encode("utf-8", None)')
 
+    def test_unicode_constructor_misc(self):
+        x = u'foo'
+        x += u'bar'
+        assert unicode(x) is x
+        #
+        class U(unicode):
+            def __unicode__(self):
+                return u'BOK'
+        u = U(x)
+        assert unicode(u) == u'BOK'
+        #
+        class U2(unicode):
+            pass
+        z = U2(u'foobaz')
+        assert type(unicode(z)) is unicode
+        assert unicode(z) == u'foobaz'
+        #
+        e = raises(TypeError, unicode, u'text', 'supposedly_the_encoding')
+        assert str(e.value) == 'decoding Unicode is not supported'
+        e = raises(TypeError, unicode, u, 'supposedly_the_encoding')
+        assert str(e.value) == 'decoding Unicode is not supported'
+        e = raises(TypeError, unicode, z, 'supposedly_the_encoding')
+        assert str(e.value) == 'decoding Unicode is not supported'
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
@@ -201,27 +201,17 @@
 
         encoding, errors = _get_encoding_and_errors(space, w_encoding,
                                                     w_errors)
-        # convoluted logic for the case when unicode subclass has a __unicode__
-        # method, we need to call this method
-        is_precisely_unicode = space.is_w(space.type(w_obj), space.w_unicode)
-        if (is_precisely_unicode or
-            (space.isinstance_w(w_obj, space.w_unicode) and
-             space.findattr(w_obj, space.newtext('__unicode__')) is None)):
-            if encoding is not None or errors is not None:
+        if encoding is None and errors is None:
+            # this is very quick if w_obj is already a w_unicode
+            w_value = unicode_from_object(space, w_obj)
+        else:
+            if space.isinstance_w(w_obj, space.w_unicode):
                 raise oefmt(space.w_TypeError,
                             "decoding Unicode is not supported")
-            if (is_precisely_unicode and
-                space.is_w(w_unicodetype, space.w_unicode)):
-                return w_obj
-            w_value = w_obj
-        else:
-            if encoding is None and errors is None:
-                w_value = unicode_from_object(space, w_obj)
-            else:
-                w_value = unicode_from_encoded_object(space, w_obj,
-                                                      encoding, errors)
-            if space.is_w(w_unicodetype, space.w_unicode):
-                return w_value
+            w_value = unicode_from_encoded_object(space, w_obj,
+                                                  encoding, errors)
+        if space.is_w(w_unicodetype, space.w_unicode):
+            return w_value
 
         assert isinstance(w_value, W_UnicodeObject)
         w_newobj = space.allocate_instance(W_UnicodeObject, w_unicodetype)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to