Commits

Amaury Forgeot d'Arc committed 186605b

#1081: string.Formatter did not unescape double braces

Comments (0)

Files changed (2)

pypy/objspace/std/newformat.py

                         if s[i] == "{":
                             i += 1
                             markup_follows = False
-                    # Attach literal data
+                    # Attach literal data, ending with { or }
                     out.append_slice(s, last_literal, i - 1)
                     if not markup_follows:
+                        if self.parser_list_w is not None:
+                            end_literal = i - 1
+                            assert end_literal > last_literal
+                            literal = self.template[last_literal:end_literal]
+                            w_entry = space.newtuple([
+                                space.wrap(literal),
+                                space.w_None, space.w_None, space.w_None])
+                            self.parser_list_w.append(w_entry)
+                            self.last_end = i
                         last_literal = i
                         continue
                     nested = 1

pypy/objspace/std/test/test_newformat.py

         assert self.s("}}").format() == self.s("}")
         assert self.s("{} {{ {}").format(1, 2) == self.s("1 { 2")
         assert self.s("{{}}").format() == self.s("{}")
+        assert self.s("{{{{").format() == self.s("{{")
 
     def test_empty(self):
         assert self.s().format() == self.s()
         for x in l[0]:
             assert isinstance(x, unicode)
 
+    def test_formatter_parser_escape(self):
+        l = list("{{a}}"._formatter_parser())
+        assert l == [('{', None, None, None), ('a}', None, None, None)]
+        l = list("{{{{"._formatter_parser())
+        assert l == [('{', None, None, None), ('{', None, None, None)]
+
     def test_formatter_field_name_split(self):
         first, rest = ''._formatter_field_name_split()
         assert first == ''