Skip to content

Commit 3a826b8

Browse files
committed
bpo-43417: Better buffer handling for ast.unparse
1 parent 73a85c4 commit 3a826b8

File tree

2 files changed

+86
-65
lines changed

2 files changed

+86
-65
lines changed

Lib/ast.py

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,6 @@ class _Unparser(NodeVisitor):
677677

678678
def __init__(self, *, _avoid_backslashes=False):
679679
self._source = []
680-
self._buffer = []
681680
self._precedences = {}
682681
self._type_ignores = {}
683682
self._indent = 0
@@ -720,14 +719,15 @@ def write(self, text):
720719
"""Append a piece of text"""
721720
self._source.append(text)
722721

723-
def buffer_writer(self, text):
724-
self._buffer.append(text)
722+
@contextmanager
723+
def buffered(self, buffer = None):
724+
if buffer is None:
725+
buffer = []
725726

726-
@property
727-
def buffer(self):
728-
value = "".join(self._buffer)
729-
self._buffer.clear()
730-
return value
727+
original_source = self._source
728+
self._source = buffer
729+
yield buffer
730+
self._source = original_source
731731

732732
@contextmanager
733733
def block(self, *, extra = None):
@@ -1123,70 +1123,72 @@ def _write_str_avoiding_backslashes(self, string, *, quote_types=_ALL_QUOTES):
11231123
def visit_JoinedStr(self, node):
11241124
self.write("f")
11251125
if self._avoid_backslashes:
1126-
self._fstring_JoinedStr(node, self.buffer_writer)
1127-
self._write_str_avoiding_backslashes(self.buffer)
1128-
return
1126+
with self.buffered() as buffer:
1127+
self._write_fstring_inner(node)
1128+
return self._write_str_avoiding_backslashes("".join(buffer))
11291129

11301130
# If we don't need to avoid backslashes globally (i.e., we only need
11311131
# to avoid them inside FormattedValues), it's cosmetically preferred
11321132
# to use escaped whitespace. That is, it's preferred to use backslashes
11331133
# for cases like: f"{x}\n". To accomplish this, we keep track of what
11341134
# in our buffer corresponds to FormattedValues and what corresponds to
11351135
# Constant parts of the f-string, and allow escapes accordingly.
1136-
buffer = []
1136+
fstring_parts = []
11371137
for value in node.values:
1138-
meth = getattr(self, "_fstring_" + type(value).__name__)
1139-
meth(value, self.buffer_writer)
1140-
buffer.append((self.buffer, isinstance(value, Constant)))
1141-
new_buffer = []
1142-
quote_types = _ALL_QUOTES
1143-
for value, is_constant in buffer:
1144-
# Repeatedly narrow down the list of possible quote_types
1138+
with self.buffered() as buffer:
1139+
self._write_fstring_inner(value)
1140+
fstring_parts.append(
1141+
("".join(buffer), isinstance(value, Constant))
1142+
)
1143+
1144+
new_fstring_parts = []
1145+
quote_types = list(_ALL_QUOTES)
1146+
for value, is_constant in fstring_parts:
11451147
value, quote_types = self._str_literal_helper(
1146-
value, quote_types=quote_types,
1147-
escape_special_whitespace=is_constant
1148+
value,
1149+
quote_types=quote_types,
1150+
escape_special_whitespace=is_constant,
11481151
)
1149-
new_buffer.append(value)
1150-
value = "".join(new_buffer)
1152+
new_fstring_parts.append(value)
1153+
1154+
value = "".join(new_fstring_parts)
11511155
quote_type = quote_types[0]
11521156
self.write(f"{quote_type}{value}{quote_type}")
11531157

1158+
def _write_fstring_inner(self, node):
1159+
if isinstance(node, JoinedStr):
1160+
# for both the f-string itself, and format_spec
1161+
for value in node.values:
1162+
self._write_fstring_inner(value)
1163+
elif isinstance(node, Constant) and isinstance(node.value, str):
1164+
value = node.value.replace("{", "{{").replace("}", "}}")
1165+
self.write(value)
1166+
elif isinstance(node, FormattedValue):
1167+
self.visit_FormattedValue(node)
1168+
else:
1169+
raise ValueError(f"Unexpected node inside JoinedStr, {node!r}")
1170+
11541171
def visit_FormattedValue(self, node):
1155-
self.write("f")
1156-
self._fstring_FormattedValue(node, self.buffer_writer)
1157-
self._write_str_avoiding_backslashes(self.buffer)
1172+
def unparse_inner(inner):
1173+
unparser = type(self)(_avoid_backslashes=True)
1174+
unparser.set_precedence(_Precedence.TEST.next(), inner)
1175+
return unparser.visit(inner)
11581176

1159-
def _fstring_JoinedStr(self, node, write):
1160-
for value in node.values:
1161-
meth = getattr(self, "_fstring_" + type(value).__name__)
1162-
meth(value, write)
1163-
1164-
def _fstring_Constant(self, node, write):
1165-
if not isinstance(node.value, str):
1166-
raise ValueError("Constants inside JoinedStr should be a string.")
1167-
value = node.value.replace("{", "{{").replace("}", "}}")
1168-
write(value)
1169-
1170-
def _fstring_FormattedValue(self, node, write):
1171-
write("{")
1172-
unparser = type(self)(_avoid_backslashes=True)
1173-
unparser.set_precedence(_Precedence.TEST.next(), node.value)
1174-
expr = unparser.visit(node.value)
1175-
if expr.startswith("{"):
1176-
write(" ") # Separate pair of opening brackets as "{ {"
1177-
if "\\" in expr:
1178-
raise ValueError("Unable to avoid backslash in f-string expression part")
1179-
write(expr)
1180-
if node.conversion != -1:
1181-
conversion = chr(node.conversion)
1182-
if conversion not in "sra":
1183-
raise ValueError("Unknown f-string conversion.")
1184-
write(f"!{conversion}")
1185-
if node.format_spec:
1186-
write(":")
1187-
meth = getattr(self, "_fstring_" + type(node.format_spec).__name__)
1188-
meth(node.format_spec, write)
1189-
write("}")
1177+
with self.delimit("{", "}"):
1178+
expr = unparse_inner(node.value)
1179+
if "\\" in expr:
1180+
raise ValueError(
1181+
"Unable to avoid backslash in f-string expression part"
1182+
)
1183+
if expr.startswith("{"):
1184+
# Separate pair of opening brackets as "{ {"
1185+
self.write(" ")
1186+
self.write(expr)
1187+
if node.conversion != -1:
1188+
self.write(f"!{chr(node.conversion)}")
1189+
if node.format_spec:
1190+
self.write(":")
1191+
self._write_fstring_inner(node.format_spec)
11901192

11911193
def visit_Name(self, node):
11921194
self.write(node.id)

Lib/test/test_unparse.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,27 @@ class UnparseTestCase(ASTTestCase):
149149
# Tests for specific bugs found in earlier versions of unparse
150150

151151
def test_fstrings(self):
152+
self.check_ast_roundtrip("f'a'")
153+
self.check_ast_roundtrip("f'{{}}'")
154+
self.check_ast_roundtrip("f'{{5}}'")
155+
self.check_ast_roundtrip("f'{{5}}5'")
156+
self.check_ast_roundtrip("f'X{{}}X'")
157+
self.check_ast_roundtrip("f'{a}'")
158+
self.check_ast_roundtrip("f'{ {1:2}}'")
159+
self.check_ast_roundtrip("f'a{a}a'")
160+
self.check_ast_roundtrip("f'a{a}{a}a'")
161+
self.check_ast_roundtrip("f'a{a}a{a}a'")
162+
self.check_ast_roundtrip("f'{a!r}x{a!s}12{{}}{a!a}'")
163+
self.check_ast_roundtrip("f'{a:10}'")
164+
self.check_ast_roundtrip("f'{a:100_000{10}}'")
165+
self.check_ast_roundtrip("f'{a!r:10}'")
166+
self.check_ast_roundtrip("f'{a:a{b}10}'")
167+
self.check_ast_roundtrip(
168+
"f'a{b}{c!s}{d!r}{e!a}{f:a}{g:a{b}}{h!s:a}"
169+
"{j!s:{a}b}{k!s:a{b}c}{l!a:{b}c{d}}{x+y=}'"
170+
)
171+
172+
def test_fstrings_special_chars(self):
152173
# See issue 25180
153174
self.check_ast_roundtrip(r"""f'{f"{0}"*3}'""")
154175
self.check_ast_roundtrip(r"""f'{f"{y}"*3}'""")
@@ -311,15 +332,13 @@ def test_slices(self):
311332
def test_invalid_raise(self):
312333
self.check_invalid(ast.Raise(exc=None, cause=ast.Name(id="X")))
313334

314-
def test_invalid_fstring_constant(self):
315-
self.check_invalid(ast.JoinedStr(values=[ast.Constant(value=100)]))
316-
317-
def test_invalid_fstring_conversion(self):
335+
def test_invalid_fstring_value(self):
318336
self.check_invalid(
319-
ast.FormattedValue(
320-
value=ast.Constant(value="a", kind=None),
321-
conversion=ord("Y"), # random character
322-
format_spec=None,
337+
ast.JoinedStr(
338+
values=[
339+
ast.Name(id="test"),
340+
ast.Constant(value="test")
341+
]
323342
)
324343
)
325344

0 commit comments

Comments
 (0)