mirror of
https://github.com/python/cpython.git
synced 2025-11-24 20:30:18 +00:00
gh-110259: Fix f-strings with multiline expressions and format specs (#110271)
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
This commit is contained in:
parent
af29282fce
commit
cc389ef627
5 changed files with 128 additions and 10 deletions
11
Lib/ast.py
11
Lib/ast.py
|
|
@ -1270,13 +1270,15 @@ class _Unparser(NodeVisitor):
|
|||
quote_type = quote_types[0]
|
||||
self.write(f"{quote_type}{value}{quote_type}")
|
||||
|
||||
def _write_fstring_inner(self, node):
|
||||
def _write_fstring_inner(self, node, scape_newlines=False):
|
||||
if isinstance(node, JoinedStr):
|
||||
# for both the f-string itself, and format_spec
|
||||
for value in node.values:
|
||||
self._write_fstring_inner(value)
|
||||
self._write_fstring_inner(value, scape_newlines=scape_newlines)
|
||||
elif isinstance(node, Constant) and isinstance(node.value, str):
|
||||
value = node.value.replace("{", "{{").replace("}", "}}")
|
||||
if scape_newlines:
|
||||
value = value.replace("\n", "\\n")
|
||||
self.write(value)
|
||||
elif isinstance(node, FormattedValue):
|
||||
self.visit_FormattedValue(node)
|
||||
|
|
@ -1299,7 +1301,10 @@ class _Unparser(NodeVisitor):
|
|||
self.write(f"!{chr(node.conversion)}")
|
||||
if node.format_spec:
|
||||
self.write(":")
|
||||
self._write_fstring_inner(node.format_spec)
|
||||
self._write_fstring_inner(
|
||||
node.format_spec,
|
||||
scape_newlines=True
|
||||
)
|
||||
|
||||
def visit_Name(self, node):
|
||||
self.write(node.id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue