[pypy-commit] pypy py3.5: '%b' in unicode strings is not supported

arigo pypy.commits at gmail.com
Mon Nov 28 12:50:44 EST 2016


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

Log:	'%b' in unicode strings is not supported

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
@@ -503,6 +503,8 @@
                 raise oefmt(space.w_TypeError, "%c requires int or char")
 
         def fmt_b(self, w_value):
+            if do_unicode:
+                self.unknown_fmtchar()
             space = self.space
             # cpython explicitly checks for bytes & bytearray
             if space.isinstance_w(w_value, space.w_bytes):
@@ -524,7 +526,7 @@
                 return
 
             raise oefmt(space.w_TypeError,
-                    "requires bytes, or an object that" \
+                    "requires bytes, or an object that "
                     "implements __bytes__, not '%T'", w_value)
 
     return StringFormatter
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
@@ -321,6 +321,10 @@
         f = 4
         raises(ValueError, '"%\u1234" % (f,)')
 
+    def test_invalid_b_with_unicode(self):
+        raises(ValueError, '"%b" % b"A"')
+        raises(ValueError, '"%b" % 42')
+
     def test_formatting_huge_precision(self):
         prec = 2**31
         format_string = u"%.{}f".format(prec)
@@ -370,6 +374,10 @@
         assert b"<%c>" % b"?" == b"<?>"
         raises(TypeError, 'b"<%c>" % "?"')
         assert b"<%c>" % bytearray(b"?") == b"<?>"
+        class X:
+            def __bytes__(self):
+                return b'5'
+        raises(TypeError, 'b"<%c>" % X()')
 
     def test_bytes_bytes(self):
         assert b"<%b>" % b"123" == b"<123>"


More information about the pypy-commit mailing list