summaryrefslogtreecommitdiffstats
path: root/Lib/ast.py
diff options
context:
space:
mode:
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>2020-05-16 22:49:07 (GMT)
committerGitHub <noreply@github.com>2020-05-16 22:49:07 (GMT)
commite966af7cff78e14e1d289db587433504b4b53533 (patch)
tree372fc4aed9cf42fb0a05062738abe16892c35246 /Lib/ast.py
parentd5a980a60790571ec88aba4e011c91e099e31e98 (diff)
downloadcpython-e966af7cff78e14e1d289db587433504b4b53533.zip
cpython-e966af7cff78e14e1d289db587433504b4b53533.tar.gz
cpython-e966af7cff78e14e1d289db587433504b4b53533.tar.bz2
bpo-38870: Correctly handle empty docstrings in ast.unparse (GH-18768)
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Diffstat (limited to 'Lib/ast.py')
-rw-r--r--Lib/ast.py13
1 files changed, 8 insertions, 5 deletions
diff --git a/Lib/ast.py b/Lib/ast.py
index d6cb334..5d0171f 100644
--- a/Lib/ast.py
+++ b/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}"""')