Author: Amaury Forgeot d'Arc <amaur...@gmail.com> Branch: Changeset: r53303:186605be5a0a Date: 2012-03-11 22:31 +0100 http://bitbucket.org/pypy/pypy/changeset/186605be5a0a/
Log: #1081: string.Formatter did not unescape double braces 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 @@ -91,9 +91,18 @@ 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 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 @@ -11,6 +11,7 @@ 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() @@ -385,6 +386,12 @@ 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 == '' _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit