Commit cf8e998f authored by Łukasz Langa's avatar Łukasz Langa
Browse files

Don't omit escaping the second consecutive quote

This would produce invalid code for strings like `"x = ''; y = \"\""`.
parent bc6b912f
Loading
Loading
Loading
Loading
+3 −0
Changes for black.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1998,6 +1998,9 @@ def normalize_string_quotes(leaf: Leaf) -> None:
    else:
        new_body = escaped_orig_quote.sub(rf"\1{orig_quote}", body)
        new_body = unescaped_new_quote.sub(rf"\1\\{new_quote}", new_body)
        # Add escapes again for consecutive occurences of new_quote (sub
        # doesn't match overlapping substrings).
        new_body = unescaped_new_quote.sub(rf"\1\\{new_quote}", new_body)
    if new_quote == '"""' and new_body[-1] == '"':
        # edge case:
        new_body = new_body[:-1] + '\\"'
+16 −0
Changes for tests/string_quotes.py: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,14 @@
the \'lazy\' dog.\n\
'
re.compile(r'[\\"]')
"x = ''; y = \"\""
"x = '''; y = \"\""
"x = ''''; y = \"\""
"x = '' ''; y = \"\""
"x = ''; y = \"\"\""
"x = '''; y = \"\"\"\""
"x = ''''; y = \"\"\"\"\""
"x = '' ''; y = \"\"\"\"\""

# output

@@ -65,3 +73,11 @@
the 'lazy' dog.\n\
"
re.compile(r'[\\"]')
"x = ''; y = \"\""
"x = '''; y = \"\""
"x = ''''; y = \"\""
"x = '' ''; y = \"\""
'x = \'\'; y = """'
'x = \'\'\'; y = """"'
'x = \'\'\'\'; y = """""'
'x = \'\' \'\'; y = """""'