Author: Armin Rigo <[email protected]>
Branch: py3.6
Changeset: r97860:82e53786ebea
Date: 2019-10-25 14:03 +0200
http://bitbucket.org/pypy/pypy/changeset/82e53786ebea/

Log:    Issue #3100

        Test and fix

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
@@ -117,6 +117,7 @@
                     nested = 1
                     field_start = i
                     recursive = False
+                    in_second_part = False
                     while i < end:
                         c = s[i]
                         if c == "{":
@@ -126,11 +127,13 @@
                             nested -= 1
                             if not nested:
                                 break
-                        elif c == "[":
+                        elif c == "[" and not in_second_part:
                             i += 1
                             while i < end and s[i] != "]":
                                 i += 1
                             continue
+                        elif c == ':' or c == '!':
+                            in_second_part = True
                         i += 1
                     if nested:
                         raise oefmt(space.w_ValueError, "Unmatched '{'")
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
@@ -203,6 +203,13 @@
         raises(ValueError, self.s("{a{b}").format, 42)
         raises(ValueError, self.s("{[}").format, 42)
 
+    def test_issue3100(self):
+        class Foo:
+            def __format__(self, f):
+                return '<<%r>>' % (f,)
+        fmtstr = self.s("{:[XYZ}")
+        assert fmtstr.format(Foo()) == "<<%r>>" % (self.s("[XYZ"),)
+
 
 class AppTestUnicodeFormat(BaseStringFormatTests):
     def setup_class(cls):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to