Skip to content

Commit

Permalink
Fix crash on docstrings ending with "\ " (#2142)
Browse files Browse the repository at this point in the history
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
  • Loading branch information
JelleZijlstra and ambv committed Apr 26, 2021
1 parent 97c2466 commit 557b54a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -4,6 +4,8 @@

#### _Black_

- Fix crash on docstrings ending with "\ ". (#2142)

- Reflect the `--skip-magic-trailing-comma` and `--experimental-string-processing` flags
in the name of the cache file. Without this fix, changes in these flags would not take
effect if the cache had already been populated. (#2131)
Expand Down
8 changes: 7 additions & 1 deletion src/black/__init__.py
Expand Up @@ -2186,7 +2186,13 @@ def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
if docstring[0] == quote_char:
docstring = " " + docstring
if docstring[-1] == quote_char:
docstring = docstring + " "
docstring += " "
if docstring[-1] == "\\":
backslash_count = len(docstring) - len(docstring.rstrip("\\"))
if backslash_count % 2:
# Odd number of tailing backslashes, add some padding to
# avoid escaping the closing string quote.
docstring += " "
else:
# Add some padding if the docstring is empty.
docstring = " "
Expand Down
31 changes: 31 additions & 0 deletions tests/data/docstring.py
Expand Up @@ -158,6 +158,22 @@ def docstring_with_inline_tabs_and_tab_indentation():
"""
pass


def backslash_space():
"""\ """


def multiline_backslash_1():
'''
hey\there\
\ '''


def multiline_backslash_2():
'''
hey there \ '''


# output

class MyClass:
Expand Down Expand Up @@ -316,3 +332,18 @@ def docstring_with_inline_tabs_and_tab_indentation():
line ends with some tabs
"""
pass


def backslash_space():
"""\ """


def multiline_backslash_1():
"""
hey\there\
\ """


def multiline_backslash_2():
"""
hey there \ """

0 comments on commit 557b54a

Please sign in to comment.