Skip to content

ast.unparse is not working correctly for strings with multiple quotes #108843

Closed
@sunmy2019

Description

@sunmy2019

Bug report

CPython versions tested on:

3.11

Operating systems tested on:

macOS

A clear and concise description of the bug:

During the implementation of #108553, I discovered this bug in the original implementation.

Due to the new syntax, the fix for 3.12 above cannot be reused for 3.11 and below.

import ast

s = "f\"'''{1}\\\"\\\"\\\"\" "
print(s)
eval(s)

s = ast.parse(s)
s1 = ast.unparse(s)
print(s1)
eval(s1)

output:

f"'''{1}\"\"\"" 
f''''{1}"""'
Traceback (most recent call last):
  File "/Users/xxx/work/cpython/a.py", line 10, in <module>
    eval(s1)
  File "<string>", line 1
    f''''{1}"""'
    ^
SyntaxError: unterminated triple-quoted string literal (detected at line 1)

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions