aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/ast.py
diff options
context:
space:
mode:
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 d6cb334432c..5d0171f1072 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}"""')