Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r89465:a003fe61afd9
Date: 2017-01-10 10:28 +0100
http://bitbucket.org/pypy/pypy/changeset/a003fe61afd9/

Log:    More strange cases of bytes.__mod__

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
@@ -583,12 +583,22 @@
         # we check directly for dict to avoid obscure checking
         # in simplest case
         if space.isinstance_w(w_values, space.w_dict) or \
-           (space.lookup(w_values, '__getitem__') and
-           not space.isinstance_w(w_values, space.w_unicode)):
+             _looks_like_a_mapping(space, w_values, fmt_type):
             return format(space, w_format, [w_values], w_values, fmt_type)
         else:
             return format(space, w_format, [w_values], None, fmt_type)
 
+def _looks_like_a_mapping(space, w_x, fmt_type):
+    if not space.lookup(w_x, '__getitem__'):
+        return False
+    if space.isinstance_w(w_x, space.w_unicode):
+        return False
+    if fmt_type != FORMAT_UNICODE:  # (S6) in http://bugs.python.org/issue28885
+        if (space.isinstance_w(w_x, space.w_bytes) or
+            space.isinstance_w(w_x, space.w_bytearray)):
+            return False
+    return True
+
 # ____________________________________________________________
 # Formatting helpers
 
diff --git a/pypy/objspace/std/test/test_bytearrayobject.py 
b/pypy/objspace/std/test/test_bytearrayobject.py
--- a/pypy/objspace/std/test/test_bytearrayobject.py
+++ b/pypy/objspace/std/test/test_bytearrayobject.py
@@ -604,6 +604,17 @@
     def test_format_bytes(self):
         assert bytearray(b'<%s>') % b'abc' == b'<abc>'
 
+    def test_formatting_not_tuple(self):
+        class mydict(dict):
+            pass
+        xxx = bytearray(b'xxx')
+        assert xxx % mydict() == xxx
+        assert xxx % [] == xxx       # [] considered as a mapping(!)
+        raises(TypeError, "xxx % 'foo'")
+        raises(TypeError, "xxx % b'foo'")
+        raises(TypeError, "xxx % bytearray()")
+        raises(TypeError, "xxx % 53")
+
     def test___alloc__(self):
         # pypy: always returns len()+1; cpython: may be bigger
         assert bytearray(b'123456').__alloc__() >= 7
diff --git a/pypy/objspace/std/test/test_bytesobject.py 
b/pypy/objspace/std/test/test_bytesobject.py
--- a/pypy/objspace/std/test/test_bytesobject.py
+++ b/pypy/objspace/std/test/test_bytesobject.py
@@ -176,6 +176,16 @@
     def test_format_bytes(self):
         assert b'<%s>' % b'abc' == b'<abc>'
 
+    def test_formatting_not_tuple(self):
+        class mydict(dict):
+            pass
+        assert b'xxx' % mydict() == b'xxx'
+        assert b'xxx' % [] == b'xxx'       # [] considered as a mapping(!)
+        raises(TypeError, "b'xxx' % 'foo'")
+        raises(TypeError, "b'xxx' % b'foo'")
+        raises(TypeError, "b'xxx' % bytearray()")
+        raises(TypeError, "b'xxx' % 53")
+
     def test_split(self):
         assert b"".split() == []
         assert b"".split(b'x') == [b'']
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
@@ -912,6 +912,16 @@
         # Printable character
         assert '%r' % chr(0xe9) == "'\xe9'"
 
+    def test_formatting_not_tuple(self):
+        class mydict(dict):
+            pass
+        assert 'xxx' % mydict() == 'xxx'
+        assert 'xxx' % b'foo' == 'xxx'   # b'foo' considered as a mapping(!)
+        assert 'xxx' % bytearray() == 'xxx'   # same
+        assert 'xxx' % [] == 'xxx'       # [] considered as a mapping(!)
+        raises(TypeError, "'xxx' % 'foo'")
+        raises(TypeError, "'xxx' % 53")
+
     def test_str_subclass(self):
         class Foo9(str):
             def __str__(self):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to