diff options
author | Batuhan Taskaya <batuhanosmantaskaya@gmail.com> | 2020-05-16 22:49:07 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-16 22:49:07 (GMT) |
commit | e966af7cff78e14e1d289db587433504b4b53533 (patch) | |
tree | 372fc4aed9cf42fb0a05062738abe16892c35246 /Lib | |
parent | d5a980a60790571ec88aba4e011c91e099e31e98 (diff) | |
download | cpython-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')
-rw-r--r-- | Lib/ast.py | 13 | ||||
-rw-r--r-- | Lib/test/test_unparse.py | 15 |
2 files changed, 21 insertions, 7 deletions
@@ -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}"""') diff --git a/Lib/test/test_unparse.py b/Lib/test/test_unparse.py index 410df7d..4f57428 100644 --- a/Lib/test/test_unparse.py +++ b/Lib/test/test_unparse.py @@ -313,11 +313,18 @@ class UnparseTestCase(ASTTestCase): 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,)") @@ -390,6 +397,10 @@ class CosmeticTestCase(ASTTestCase): empty newline"""''', '"""With some \t"""', '"""Foo "bar" baz """', + '"""\\r"""', + '""""""', + '"""\'\'\'"""', + '"""\'\'\'\'\'\'"""', ) for prefix in docstring_prefixes: |