mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
bpo-38870: Correctly handle empty docstrings in ast.unparse (GH-18768)
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
This commit is contained in:
parent
d5a980a607
commit
e966af7cff
2 changed files with 21 additions and 7 deletions
13
Lib/ast.py
13
Lib/ast.py
|
|
@ -1075,11 +1075,14 @@ class _Unparser(NodeVisitor):
|
|||
if node.kind == "u":
|
||||
self.write("u")
|
||||
|
||||
# Preserve quotes in the docstring by escaping them
|
||||
value = node.value.replace("\\", "\\\\")
|
||||
value = value.replace('"""', '""\"')
|
||||
if value[-1] == '"':
|
||||
value = value.replace('"', '\\"', -1)
|
||||
value = node.value
|
||||
if value:
|
||||
# Preserve quotes in the docstring by escaping them
|
||||
value = value.replace("\\", "\\\\")
|
||||
value = value.replace('"""', '""\"')
|
||||
value = value.replace("\r", "\\r")
|
||||
if value[-1] == '"':
|
||||
value = value.replace('"', '\\"', -1)
|
||||
|
||||
self.write(f'"""{value}"""')
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue