Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2fced7

Browse files
authored
[3.12] gh-112364: Correct unparsing of backslashes and quotes in ast.… (#115782)
[3.12] gh-112364: Correct unparsing of backslashes and quotes in ast.unparse (GH-115696) (cherry picked from commit 69ab930)
1 parent 3651c27 commit d2fced7

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

Lib/ast.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,14 +1268,18 @@ def visit_JoinedStr(self, node):
12681268
quote_type = quote_types[0]
12691269
self.write(f"{quote_type}{value}{quote_type}")
12701270

1271-
def _write_fstring_inner(self, node, scape_newlines=False):
1271+
def _write_fstring_inner(self, node, is_format_spec=False):
12721272
if isinstance(node, JoinedStr):
12731273
# for both the f-string itself, and format_spec
12741274
for value in node.values:
1275-
self._write_fstring_inner(value, scape_newlines=scape_newlines)
1275+
self._write_fstring_inner(value, is_format_spec=is_format_spec)
12761276
elif isinstance(node, Constant) and isinstance(node.value, str):
12771277
value = node.value.replace("{", "{{").replace("}", "}}")
1278-
if scape_newlines:
1278+
1279+
if is_format_spec:
1280+
value = value.replace("\\", "\\\\")
1281+
value = value.replace("'", "\\'")
1282+
value = value.replace('"', '\\"')
12791283
value = value.replace("\n", "\\n")
12801284
self.write(value)
12811285
elif isinstance(node, FormattedValue):
@@ -1299,10 +1303,7 @@ def unparse_inner(inner):
12991303
self.write(f"!{chr(node.conversion)}")
13001304
if node.format_spec:
13011305
self.write(":")
1302-
self._write_fstring_inner(
1303-
node.format_spec,
1304-
scape_newlines=True
1305-
)
1306+
self._write_fstring_inner(node.format_spec, is_format_spec=True)
13061307

13071308
def visit_Name(self, node):
13081309
self.write(node.id)

Lib/test/test_unparse.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,21 @@ def test_multiquote_joined_string(self):
649649
self.check_ast_roundtrip("""f'''""\"''\\'{"\\n\\"'"}''' """)
650650
self.check_ast_roundtrip("""f'''""\"''\\'{""\"\\n\\"'''""\" '''\\n'''}''' """)
651651

652+
def test_backslash_in_format_spec(self):
653+
self.check_ast_roundtrip("""f"{x:\\ }" """)
654+
self.check_ast_roundtrip("""f"{x:\\\\ }" """)
655+
self.check_ast_roundtrip("""f"{x:\\\\\\ }" """)
656+
self.check_ast_roundtrip("""f"{x:\\\\\\\\ }" """)
657+
658+
def test_quote_in_format_spec(self):
659+
self.check_ast_roundtrip("""f"{x:'}" """)
660+
self.check_ast_roundtrip("""f"{x:\\'}" """)
661+
self.check_ast_roundtrip("""f"{x:\\\\'}" """)
662+
663+
self.check_ast_roundtrip("""f'\\'{x:"}' """)
664+
self.check_ast_roundtrip("""f'\\'{x:\\"}' """)
665+
self.check_ast_roundtrip("""f'\\'{x:\\\\"}' """)
666+
652667

653668
class ManualASTCreationTestCase(unittest.TestCase):
654669
"""Test that AST nodes created without a type_params field unparse correctly."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed :func:`ast.unparse` to handle format_spec with ``"``, ``'`` or ``\\``. Patched by Frank Hoffmann.

0 commit comments

Comments
 (0)