Commit efcd0394 authored by Zsolt Dollenstein's avatar Zsolt Dollenstein
Browse files

Fix string normalization eating all backslashes above 3

parent 2cf125a9
Loading
Loading
Loading
Loading
+2 −0
Changes for README.md: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -816,6 +816,8 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).

* fixed unnecessary slowdown on AST nodes with very many siblings

* fixed cannibalizing backslashes during string normalization


### 18.6b2

+4 −4
Changes for black.py: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -2529,8 +2529,8 @@ def normalize_string_quotes(leaf: Leaf) -> None:

    prefix = leaf.value[:first_quote_pos]
    unescaped_new_quote = re.compile(rf"(([^\\]|^)(\\\\)*){new_quote}")
    escaped_new_quote = re.compile(rf"([^\\]|^)\\(\\\\)*{new_quote}")
    escaped_orig_quote = re.compile(rf"([^\\]|^)\\(\\\\)*{orig_quote}")
    escaped_new_quote = re.compile(rf"([^\\]|^)\\((?:\\\\)*){new_quote}")
    escaped_orig_quote = re.compile(rf"([^\\]|^)\\((?:\\\\)*){orig_quote}")
    body = leaf.value[first_quote_pos + len(orig_quote) : -len(orig_quote)]
    if "r" in prefix.casefold():
        if unescaped_new_quote.search(body):
@@ -2541,10 +2541,10 @@ def normalize_string_quotes(leaf: Leaf) -> None:
        # Do not introduce or remove backslashes in raw strings
        new_body = body
    else:
        # remove unnecessary quotes
        # remove unnecessary escapes
        new_body = sub_twice(escaped_new_quote, rf"\1\2{new_quote}", body)
        if body != new_body:
            # Consider the string without unnecessary quotes as the original
            # Consider the string without unnecessary escapes as the original
            body = new_body
            leaf.value = f"{prefix}{orig_quote}{body}{orig_quote}"
        new_body = sub_twice(escaped_orig_quote, rf"\1\2{orig_quote}", new_body)
+2 −0
Changes for tests/data/string_quotes.py: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@
"unnecessary \'\'escaping"
'\\""'
"\\''"
'Lots of \\\\\\\\\'quotes\''

# output

@@ -91,3 +92,4 @@
"unnecessary ''escaping"
'\\""'
"\\''"
"Lots of \\\\\\\\'quotes'"

tests/data/string_wayne.py

deleted100644 → 0
+0 −1
Changes for tests/data/string_wayne.py: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
u'"Based on the Saturday Night Live sketch of the same name, Wayne\'s World is a wacky, irreverent pop culture comedy about the adventures of two amiably aimless metal head friends, Wayne (Mike Myers) and Garth (Dana Carvey). From Wayne\'s basement, the pair broadcast a talk show called \\\\"Wayne\'s World\\\\" on local public access television. The show comes to the attention of a sleazy network executive (Rob Lowe) who wants to produce a big budget version of \\\\"Wayne\'s World\\\\" and he also wants Wayne\'s girlfriend, a rock singer named Cassandra (Tia Carrere). Wayne and Garth have to battle the executive not only to save their show, but also Cassandra. Director Penelope Spheeris, Myers and Carvey hang a lot of silly, but funny, jokes on this thin plot, and the energy of the cast as well as the wild pop culture references make Wayne\'s World a cut above the average Saturday Night Live spin off movie.\\\\n"'