Skip to content

Commit 09bd5bd

Browse files
authored
Merge pull request #1293 from abebus/speedups/python_3000_invalid_escape_sequence
reduce "python_3000_invalid_escape_sequence" execution time by ~60%
2 parents db26deb + f9301e4 commit 09bd5bd

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

pycodestyle.py

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,29 @@ def ambiguous_identifier(logical_line, tokens):
16341634
prev_start = start
16351635

16361636

1637+
# https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
1638+
_PYTHON_3000_VALID_ESC = frozenset([
1639+
'\n',
1640+
'\\',
1641+
'\'',
1642+
'"',
1643+
'a',
1644+
'b',
1645+
'f',
1646+
'n',
1647+
'r',
1648+
't',
1649+
'v',
1650+
'0', '1', '2', '3', '4', '5', '6', '7',
1651+
'x',
1652+
1653+
# Escape sequences only recognized in string literals
1654+
'N',
1655+
'u',
1656+
'U',
1657+
])
1658+
1659+
16371660
@register_check
16381661
def python_3000_invalid_escape_sequence(logical_line, tokens, noqa):
16391662
r"""Invalid escape sequences are deprecated in Python 3.6.
@@ -1644,41 +1667,27 @@ def python_3000_invalid_escape_sequence(logical_line, tokens, noqa):
16441667
if noqa:
16451668
return
16461669

1647-
# https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
1648-
valid = [
1649-
'\n',
1650-
'\\',
1651-
'\'',
1652-
'"',
1653-
'a',
1654-
'b',
1655-
'f',
1656-
'n',
1657-
'r',
1658-
't',
1659-
'v',
1660-
'0', '1', '2', '3', '4', '5', '6', '7',
1661-
'x',
1662-
1663-
# Escape sequences only recognized in string literals
1664-
'N',
1665-
'u',
1666-
'U',
1667-
]
1668-
16691670
prefixes = []
16701671
for token_type, text, start, _, _ in tokens:
1671-
if token_type in {tokenize.STRING, FSTRING_START, TSTRING_START}:
1672+
if (
1673+
token_type == tokenize.STRING or
1674+
token_type == FSTRING_START or
1675+
token_type == TSTRING_START
1676+
):
16721677
# Extract string modifiers (e.g. u or r)
16731678
prefixes.append(text[:text.index(text[-1])].lower())
16741679

1675-
if token_type in {tokenize.STRING, FSTRING_MIDDLE, TSTRING_MIDDLE}:
1680+
if (
1681+
token_type == tokenize.STRING or
1682+
token_type == FSTRING_MIDDLE or
1683+
token_type == TSTRING_MIDDLE
1684+
):
16761685
if 'r' not in prefixes[-1]:
16771686
start_line, start_col = start
16781687
pos = text.find('\\')
16791688
while pos >= 0:
16801689
pos += 1
1681-
if text[pos] not in valid:
1690+
if text[pos] not in _PYTHON_3000_VALID_ESC:
16821691
line = start_line + text.count('\n', 0, pos)
16831692
if line == start_line:
16841693
col = start_col + pos
@@ -1690,7 +1699,11 @@ def python_3000_invalid_escape_sequence(logical_line, tokens, noqa):
16901699
)
16911700
pos = text.find('\\', pos + 1)
16921701

1693-
if token_type in {tokenize.STRING, FSTRING_END, TSTRING_END}:
1702+
if (
1703+
token_type == tokenize.STRING or
1704+
token_type == FSTRING_END or
1705+
token_type == TSTRING_END
1706+
):
16941707
prefixes.pop()
16951708

16961709

0 commit comments

Comments
 (0)