[pypy-commit] pypy py3.5: b"%c" % bytearray(b"X")

arigo pypy.commits at gmail.com
Mon Nov 28 12:45:24 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r88713:a9aa848c5cb2
Date: 2016-11-28 18:44 +0100
http://bitbucket.org/pypy/pypy/changeset/a9aa848c5cb2/

Log:	b"%c" % bytearray(b"X")

diff --git a/pypy/objspace/std/formatting.py b/pypy/objspace/std/formatting.py
--- a/pypy/objspace/std/formatting.py
+++ b/pypy/objspace/std/formatting.py
@@ -483,22 +483,24 @@
                                     "character code not in range(256)")
                     self.std_wp(s)
                 return
-            if space.isinstance_w(w_value, space.w_str):
-                s = space.str_w(w_value)
+            if not do_unicode:
+                if space.isinstance_w(w_value, space.w_str):
+                    s = space.str_w(w_value)
+                elif space.isinstance_w(w_value, space.w_bytearray):
+                    s = w_value.buffer_w(space, 0).as_str()
+                else:
+                    s = ''
                 if len(s) == 1:
                     self.std_wp(s)
                     return
-            elif space.isinstance_w(w_value, space.w_unicode):
-                if not do_unicode:
-                    raise NeedUnicodeFormattingError
-                ustr = space.unicode_w(w_value)
-                if len(ustr) == 1:
-                    self.std_wp(ustr)
-                    return
-            if do_unicode:
+                raise oefmt(space.w_TypeError, "%c requires int or single byte")
+            else:
+                if space.isinstance_w(w_value, space.w_unicode):
+                    ustr = space.unicode_w(w_value)
+                    if len(ustr) == 1:
+                        self.std_wp(ustr)
+                        return
                 raise oefmt(space.w_TypeError, "%c requires int or char")
-            else:
-                raise oefmt(space.w_TypeError, "%c requires int or single byte")
 
         def fmt_b(self, w_value):
             space = self.space
diff --git a/pypy/objspace/std/test/test_stringformat.py b/pypy/objspace/std/test/test_stringformat.py
--- a/pypy/objspace/std/test/test_stringformat.py
+++ b/pypy/objspace/std/test/test_stringformat.py
@@ -178,6 +178,7 @@
         raises(TypeError, '%c'.__mod__, ("bla",))
         raises(TypeError, '%c'.__mod__, ("",))
         raises(TypeError, '%c'.__mod__, (['c'],))
+        raises(TypeError, '%c'.__mod__, b'A')
 
     def test___int__index__(self):
         class MyInt(object):
@@ -368,6 +369,7 @@
         assert b"<%c>" % 48 == b"<0>"
         assert b"<%c>" % b"?" == b"<?>"
         raises(TypeError, 'b"<%c>" % "?"')
+        assert b"<%c>" % bytearray(b"?") == b"<?>"
 
     def test_bytes_bytes(self):
         assert b"<%b>" % b"123" == b"<123>"
@@ -421,6 +423,8 @@
         assert bytearray(b"<%c>") % 48 == bytearray(b"<0>")
         assert bytearray(b"<%c>") % b"?" == bytearray(b"<?>")
         raises(TypeError, 'bytearray(b"<%c>") % "?"')
+        assert bytearray(b"<%c>") % bytearray(b"?") == bytearray(b"<?>")
+        raises(TypeError, 'bytearray(b"<%c>") % memoryview(b"X")')
 
     def test_bytes_bytes(self):
         assert bytearray(b"<%b>") % b"123" == bytearray(b"<123>")


More information about the pypy-commit mailing list