Don't omit escaping the second consecutive quote
This would produce invalid code for strings like `"x = ''; y = \"\""`.
This commit is contained in:
parent
bc6b912fcb
commit
cf8e998f46
3
black.py
3
black.py
@ -1998,6 +1998,9 @@ def normalize_string_quotes(leaf: Leaf) -> None:
|
|||||||
else:
|
else:
|
||||||
new_body = escaped_orig_quote.sub(rf"\1{orig_quote}", body)
|
new_body = escaped_orig_quote.sub(rf"\1{orig_quote}", body)
|
||||||
new_body = unescaped_new_quote.sub(rf"\1\\{new_quote}", new_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] == '"':
|
if new_quote == '"""' and new_body[-1] == '"':
|
||||||
# edge case:
|
# edge case:
|
||||||
new_body = new_body[:-1] + '\\"'
|
new_body = new_body[:-1] + '\\"'
|
||||||
|
@ -30,6 +30,14 @@
|
|||||||
the \'lazy\' dog.\n\
|
the \'lazy\' dog.\n\
|
||||||
'
|
'
|
||||||
re.compile(r'[\\"]')
|
re.compile(r'[\\"]')
|
||||||
|
"x = ''; y = \"\""
|
||||||
|
"x = '''; y = \"\""
|
||||||
|
"x = ''''; y = \"\""
|
||||||
|
"x = '' ''; y = \"\""
|
||||||
|
"x = ''; y = \"\"\""
|
||||||
|
"x = '''; y = \"\"\"\""
|
||||||
|
"x = ''''; y = \"\"\"\"\""
|
||||||
|
"x = '' ''; y = \"\"\"\"\""
|
||||||
|
|
||||||
# output
|
# output
|
||||||
|
|
||||||
@ -65,3 +73,11 @@
|
|||||||
the 'lazy' dog.\n\
|
the 'lazy' dog.\n\
|
||||||
"
|
"
|
||||||
re.compile(r'[\\"]')
|
re.compile(r'[\\"]')
|
||||||
|
"x = ''; y = \"\""
|
||||||
|
"x = '''; y = \"\""
|
||||||
|
"x = ''''; y = \"\""
|
||||||
|
"x = '' ''; y = \"\""
|
||||||
|
'x = \'\'; y = """'
|
||||||
|
'x = \'\'\'; y = """"'
|
||||||
|
'x = \'\'\'\'; y = """""'
|
||||||
|
'x = \'\' \'\'; y = """""'
|
||||||
|
Loading…
Reference in New Issue
Block a user