mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[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 69ab93082d
)
This commit is contained in:
parent
3651c2729a
commit
d2fced7128
3 changed files with 24 additions and 7 deletions
15
Lib/ast.py
15
Lib/ast.py
|
@ -1268,14 +1268,18 @@ class _Unparser(NodeVisitor):
|
||||||
quote_type = quote_types[0]
|
quote_type = quote_types[0]
|
||||||
self.write(f"{quote_type}{value}{quote_type}")
|
self.write(f"{quote_type}{value}{quote_type}")
|
||||||
|
|
||||||
def _write_fstring_inner(self, node, scape_newlines=False):
|
def _write_fstring_inner(self, node, is_format_spec=False):
|
||||||
if isinstance(node, JoinedStr):
|
if isinstance(node, JoinedStr):
|
||||||
# for both the f-string itself, and format_spec
|
# for both the f-string itself, and format_spec
|
||||||
for value in node.values:
|
for value in node.values:
|
||||||
self._write_fstring_inner(value, scape_newlines=scape_newlines)
|
self._write_fstring_inner(value, is_format_spec=is_format_spec)
|
||||||
elif isinstance(node, Constant) and isinstance(node.value, str):
|
elif isinstance(node, Constant) and isinstance(node.value, str):
|
||||||
value = node.value.replace("{", "{{").replace("}", "}}")
|
value = node.value.replace("{", "{{").replace("}", "}}")
|
||||||
if scape_newlines:
|
|
||||||
|
if is_format_spec:
|
||||||
|
value = value.replace("\\", "\\\\")
|
||||||
|
value = value.replace("'", "\\'")
|
||||||
|
value = value.replace('"', '\\"')
|
||||||
value = value.replace("\n", "\\n")
|
value = value.replace("\n", "\\n")
|
||||||
self.write(value)
|
self.write(value)
|
||||||
elif isinstance(node, FormattedValue):
|
elif isinstance(node, FormattedValue):
|
||||||
|
@ -1299,10 +1303,7 @@ class _Unparser(NodeVisitor):
|
||||||
self.write(f"!{chr(node.conversion)}")
|
self.write(f"!{chr(node.conversion)}")
|
||||||
if node.format_spec:
|
if node.format_spec:
|
||||||
self.write(":")
|
self.write(":")
|
||||||
self._write_fstring_inner(
|
self._write_fstring_inner(node.format_spec, is_format_spec=True)
|
||||||
node.format_spec,
|
|
||||||
scape_newlines=True
|
|
||||||
)
|
|
||||||
|
|
||||||
def visit_Name(self, node):
|
def visit_Name(self, node):
|
||||||
self.write(node.id)
|
self.write(node.id)
|
||||||
|
|
|
@ -649,6 +649,21 @@ class CosmeticTestCase(ASTTestCase):
|
||||||
self.check_ast_roundtrip("""f'''""\"''\\'{"\\n\\"'"}''' """)
|
self.check_ast_roundtrip("""f'''""\"''\\'{"\\n\\"'"}''' """)
|
||||||
self.check_ast_roundtrip("""f'''""\"''\\'{""\"\\n\\"'''""\" '''\\n'''}''' """)
|
self.check_ast_roundtrip("""f'''""\"''\\'{""\"\\n\\"'''""\" '''\\n'''}''' """)
|
||||||
|
|
||||||
|
def test_backslash_in_format_spec(self):
|
||||||
|
self.check_ast_roundtrip("""f"{x:\\ }" """)
|
||||||
|
self.check_ast_roundtrip("""f"{x:\\\\ }" """)
|
||||||
|
self.check_ast_roundtrip("""f"{x:\\\\\\ }" """)
|
||||||
|
self.check_ast_roundtrip("""f"{x:\\\\\\\\ }" """)
|
||||||
|
|
||||||
|
def test_quote_in_format_spec(self):
|
||||||
|
self.check_ast_roundtrip("""f"{x:'}" """)
|
||||||
|
self.check_ast_roundtrip("""f"{x:\\'}" """)
|
||||||
|
self.check_ast_roundtrip("""f"{x:\\\\'}" """)
|
||||||
|
|
||||||
|
self.check_ast_roundtrip("""f'\\'{x:"}' """)
|
||||||
|
self.check_ast_roundtrip("""f'\\'{x:\\"}' """)
|
||||||
|
self.check_ast_roundtrip("""f'\\'{x:\\\\"}' """)
|
||||||
|
|
||||||
|
|
||||||
class ManualASTCreationTestCase(unittest.TestCase):
|
class ManualASTCreationTestCase(unittest.TestCase):
|
||||||
"""Test that AST nodes created without a type_params field unparse correctly."""
|
"""Test that AST nodes created without a type_params field unparse correctly."""
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fixed :func:`ast.unparse` to handle format_spec with ``"``, ``'`` or ``\\``. Patched by Frank Hoffmann.
|
Loading…
Add table
Add a link
Reference in a new issue