Skip to content

Commit

Permalink
fix tokenization of tqs with escaped newline
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Oct 13, 2022
1 parent e620ebc commit 3810c9a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions reorder_python_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
COMMENT = r'#[^\r\n]*'
NAME = r'\w+'
PREFIX = r'[RrUu]?'
DOUBLE_3 = r'"""[^"\\]*(?:(?:\\.|"(?!""))[^"\\]*)*"""'
SINGLE_3 = r"'''[^'\\]*(?:(?:\\.|'(?!''))[^'\\]*)*'''"
DOUBLE_3 = r'"""[^"\\]*(?:(?:\\.|\\\n|"(?!""))[^"\\]*)*"""'
SINGLE_3 = r"'''[^'\\]*(?:(?:\\.|\\\n|'(?!''))[^'\\]*)*'''"
DOUBLE_1 = r'"[^"\\]*(?:\\.[^"\\]*)*"'
SINGLE_1 = r"'[^'\\]*(?:\\.[^'\\]*)*'"
# END GENERATED
Expand Down
8 changes: 6 additions & 2 deletions testing/generate-tokenize
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ def raw(s: str) -> str:
return repr(s)


def _escaped_newline(s: str) -> str:
return s.replace(r'\\.|', r'\\.|\\\n|')


def main() -> int:
# only want the prefixes that can be docstrings
string_prefix = '[RrUu]?'

double_3 = f'"""{tokenize.Double3}'
single_3 = f"'''{tokenize.Single3}"
double_3 = f'"""{_escaped_newline(tokenize.Double3)}'
single_3 = f"'''{_escaped_newline(tokenize.Single3)}"
double_1 = f'"{tokenize.Double}'
single_1 = f"'{tokenize.Single}"

Expand Down
12 changes: 11 additions & 1 deletion tests/reorder_python_imports_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ def in_tmpdir(tmpdir):


@pytest.mark.parametrize(
's', ("''", '""', 'r"hi"', 'u"hello"', '"""hello\nworld"""', "'''\n'''"),
's',
(
"''",
'""',
'r"hi"',
'u"hello"',
'"""hello\nworld"""',
"'''\n'''",
"'''\\\n'''",
"'''\\\r\n'''",
),
)
def test_tokenize_can_match_strings(s):
tp, pat = TOKENIZE[-1]
Expand Down

0 comments on commit 3810c9a

Please sign in to comment.