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}"""')
def test_docstrings(self):
docstrings = (
'this ends with double quote"',
- 'this includes a """triple quote"""'
+ 'this includes a """triple quote"""',
+ '\r',
+ '\\r',
+ '\t',
+ '\\t',
+ '\n',
+ '\\n',
+ '\r\\r\t\\t\n\\n'
)
for docstring in docstrings:
# check as Module docstrings for easy testing
- self.check_ast_roundtrip(f"'{docstring}'")
+ self.check_ast_roundtrip(f"'''{docstring}'''")
def test_constant_tuples(self):
self.check_src_roundtrip(ast.Constant(value=(1,), kind=None), "(1,)")
empty newline"""''',
'"""With some \t"""',
'"""Foo "bar" baz """',
+ '"""\\r"""',
+ '""""""',
+ '"""\'\'\'"""',
+ '"""\'\'\'\'\'\'"""',
)
for prefix in docstring_prefixes: