Skip to content

Commit

Permalink
string prefixes: don't normalise capital R-strings, normalise order
Browse files Browse the repository at this point in the history
Reverse sort since that causes fewer changes on black's codebase.

Resolves psf#1244
  • Loading branch information
hauntsaninja committed Feb 11, 2020
1 parent ce14fa8 commit 0fe326e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion black.py
Expand Up @@ -2831,9 +2831,10 @@ def normalize_string_prefix(leaf: Leaf, remove_u_prefix: bool = False) -> None:
match = re.match(r"^([furbFURB]*)(.*)$", leaf.value, re.DOTALL)
assert match is not None, f"failed to match string {leaf.value!r}"
orig_prefix = match.group(1)
new_prefix = orig_prefix.lower()
new_prefix = orig_prefix.replace("F", "f").replace("B", "b").replace("U", "u")
if remove_u_prefix:
new_prefix = new_prefix.replace("u", "")
new_prefix = "".join(sorted(new_prefix, reverse=True))
leaf.value = f"{new_prefix}{match.group(2)}"


Expand Down
2 changes: 1 addition & 1 deletion blib2to3/pgen2/tokenize.py
Expand Up @@ -284,7 +284,7 @@ def compat(self, token: Tuple[int, Text], iterable: Iterable[TokenInfo]) -> None


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


def _get_normal_name(orig_enc: str) -> str:
Expand Down
6 changes: 5 additions & 1 deletion tests/data/string_prefixes.py
Expand Up @@ -3,12 +3,16 @@
name = R"Łukasz"
F"hello {name}"
B"hello"
r"hello"
fr"hello"

# output


#!/usr/bin/env python3.6

name = r"Łukasz"
name = R"Łukasz"
f"hello {name}"
b"hello"
r"hello"
rf"hello"

0 comments on commit 0fe326e

Please sign in to comment.