[pypy-commit] pypy default: Whack whack whack

arigo pypy.commits at gmail.com
Mon Apr 29 05:12:37 EDT 2019


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r96556:265eccc9831f
Date: 2019-04-29 11:11 +0200
http://bitbucket.org/pypy/pypy/changeset/265eccc9831f/

Log:	Whack whack whack

diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -350,6 +350,9 @@
             raise oefmt(space.w_TypeError,
                         "%T.__format__ must return string or unicode, not %T",
                         w_obj, w_res)
+        if (space.isinstance_w(w_format_spec, space.w_unicode) and
+                not space.isinstance_w(w_res, space.w_unicode)):
+            w_res = space.unicode_from_object(w_res)
         return w_res
 
     def pow(space, w_obj1, w_obj2, w_obj3):
diff --git a/pypy/objspace/std/newformat.py b/pypy/objspace/std/newformat.py
--- a/pypy/objspace/std/newformat.py
+++ b/pypy/objspace/std/newformat.py
@@ -349,9 +349,11 @@
             if recursive:
                 spec = self._build_string(spec_start, end, level)
             w_rendered = self.space.format(w_obj, self.wrap(spec))
-            unwrapper = "utf8_w" if self.is_unicode else "bytes_w"
-            to_interp = getattr(self.space, unwrapper)
-            return to_interp(w_rendered)
+            if self.is_unicode:
+                w_rendered = self.space.unicode_from_object(w_rendered)
+                return self.space.utf8_w(w_rendered)
+            else:
+                return self.space.bytes_w(w_rendered)
 
         def formatter_parser(self):
             self.parser_list_w = []
@@ -779,6 +781,7 @@
             return out.build()
 
         def _format_int_or_long(self, w_num, kind):
+            # note: 'self.is_unicode' cannot be True any more here
             space = self.space
             if self._precision != -1:
                 raise oefmt(space.w_ValueError,
@@ -903,6 +906,7 @@
             return "".join(buf[i:])
 
         def format_int_or_long(self, w_num, kind):
+            # note: 'self.is_unicode' cannot be True any more here
             space = self.space
             if self._parse_spec("d", ">"):
                 if self.is_unicode:
@@ -992,6 +996,7 @@
                              fill, to_remainder, False))
 
         def format_float(self, w_float):
+            # note: 'self.is_unicode' cannot be True any more here
             space = self.space
             if self._parse_spec("\0", ">"):
                 if self.is_unicode:
@@ -1155,6 +1160,7 @@
 
         def format_complex(self, w_complex):
             """return the string representation of a complex number"""
+            # note: 'self.is_unicode' cannot be True any more here
             space = self.space
             #parse format specification, set associated variables
             if self._parse_spec("\0", ">"):
@@ -1178,9 +1184,5 @@
 
 @specialize.arg(2)
 def run_formatter(space, w_format_spec, meth, *args):
-    if space.isinstance_w(w_format_spec, space.w_unicode):
-        formatter = unicode_formatter(space, space.utf8_w(w_format_spec))
-        return getattr(formatter, meth)(*args)
-    else:
-        formatter = str_formatter(space, space.bytes_w(w_format_spec))
-        return getattr(formatter, meth)(*args)
+    formatter = str_formatter(space, space.bytes_w(w_format_spec))
+    return getattr(formatter, meth)(*args)
diff --git a/pypy/objspace/std/test/test_newformat.py b/pypy/objspace/std/test/test_newformat.py
--- a/pypy/objspace/std/test/test_newformat.py
+++ b/pypy/objspace/std/test/test_newformat.py
@@ -258,6 +258,7 @@
     def test_simple(self):
         assert format(self.i(2)) == "2"
         assert isinstance(format(self.i(2), u""), unicode)
+        assert isinstance(self.i(2).__format__(u""), str)
 
     def test_invalid(self):
         raises(ValueError, format, self.i(8), "s")
@@ -471,3 +472,15 @@
         assert isinstance(first, unicode)
         for x, y in l:
             assert isinstance(y, unicode)
+
+    def test_format_char(self):
+        import sys
+        assert '{0:c}'.format(42) == '*'
+        raises(OverflowError, '{0:c}'.format, 1234)
+        # the rest looks unexpected, but that's also what CPython does
+        raises(UnicodeDecodeError, u'{0:c}'.format, 255)
+        raises(OverflowError, u'{0:c}'.format, 1234)
+        raises(OverflowError, u'{0:c}'.format, -1)
+
+    def test_format_unicode_nonutf8_bytestring(self):
+        raises(UnicodeDecodeError, u'{0}'.format, '\xff')


More information about the pypy-commit mailing list