Skip to content

Commit 0fe326e

Browse files
author
hauntsaninja
committed
string prefixes: don't normalise capital R-strings, normalise order
Reverse sort since that causes fewer changes on black's codebase. Resolves psf#1244
1 parent ce14fa8 commit 0fe326e

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

black.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2831,9 +2831,10 @@ def normalize_string_prefix(leaf: Leaf, remove_u_prefix: bool = False) -> None:
28312831
match = re.match(r"^([furbFURB]*)(.*)$", leaf.value, re.DOTALL)
28322832
assert match is not None, f"failed to match string {leaf.value!r}"
28332833
orig_prefix = match.group(1)
2834-
new_prefix = orig_prefix.lower()
2834+
new_prefix = orig_prefix.replace("F", "f").replace("B", "b").replace("U", "u")
28352835
if remove_u_prefix:
28362836
new_prefix = new_prefix.replace("u", "")
2837+
new_prefix = "".join(sorted(new_prefix, reverse=True))
28372838
leaf.value = f"{new_prefix}{match.group(2)}"
28382839

28392840

blib2to3/pgen2/tokenize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def compat(self, token: Tuple[int, Text], iterable: Iterable[TokenInfo]) -> None
284284

285285

286286
cookie_re = re.compile(r"^[ \t\f]*#.*?coding[:=][ \t]*([-\w.]+)", re.ASCII)
287-
blank_re = re.compile(br"^[ \t\f]*(?:[#\r\n]|$)", re.ASCII)
287+
blank_re = re.compile(rb"^[ \t\f]*(?:[#\r\n]|$)", re.ASCII)
288288

289289

290290
def _get_normal_name(orig_enc: str) -> str:

tests/data/string_prefixes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
name = R"Łukasz"
44
F"hello {name}"
55
B"hello"
6+
r"hello"
7+
fr"hello"
68

79
# output
810

911

1012
#!/usr/bin/env python3.6
1113

12-
name = r"Łukasz"
14+
name = R"Łukasz"
1315
f"hello {name}"
1416
b"hello"
17+
r"hello"
18+
rf"hello"

0 commit comments

Comments
 (0)