Skip to content

Commit f9301e4

Browse files
namaevaeasottile
authored andcommitted
this reduces execution time from ~5700 ns to ~3400 ns
1 parent c50ba15 commit f9301e4

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
@@ -1627,6 +1627,29 @@ def ambiguous_identifier(logical_line, tokens):
16271627
prev_start = start
16281628

16291629

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

1640-
# https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
1641-
valid = [
1642-
'\n',
1643-
'\\',
1644-
'\'',
1645-
'"',
1646-
'a',
1647-
'b',
1648-
'f',
1649-
'n',
1650-
'r',
1651-
't',
1652-
'v',
1653-
'0', '1', '2', '3', '4', '5', '6', '7',
1654-
'x',
1655-
1656-
# Escape sequences only recognized in string literals
1657-
'N',
1658-
'u',
1659-
'U',
1660-
]
1661-
16621663
prefixes = []
16631664
for token_type, text, start, _, _ in tokens:
1664-
if token_type in {tokenize.STRING, FSTRING_START, TSTRING_START}:
1665+
if (
1666+
token_type == tokenize.STRING or
1667+
token_type == FSTRING_START or
1668+
token_type == TSTRING_START
1669+
):
16651670
# Extract string modifiers (e.g. u or r)
16661671
prefixes.append(text[:text.index(text[-1])].lower())
16671672

1668-
if token_type in {tokenize.STRING, FSTRING_MIDDLE, TSTRING_MIDDLE}:
1673+
if (
1674+
token_type == tokenize.STRING or
1675+
token_type == FSTRING_MIDDLE or
1676+
token_type == TSTRING_MIDDLE
1677+
):
16691678
if 'r' not in prefixes[-1]:
16701679
start_line, start_col = start
16711680
pos = text.find('\\')
16721681
while pos >= 0:
16731682
pos += 1
1674-
if text[pos] not in valid:
1683+
if text[pos] not in _PYTHON_3000_VALID_ESC:
16751684
line = start_line + text.count('\n', 0, pos)
16761685
if line == start_line:
16771686
col = start_col + pos
@@ -1683,7 +1692,11 @@ def python_3000_invalid_escape_sequence(logical_line, tokens, noqa):
16831692
)
16841693
pos = text.find('\\', pos + 1)
16851694

1686-
if token_type in {tokenize.STRING, FSTRING_END, TSTRING_END}:
1695+
if (
1696+
token_type == tokenize.STRING or
1697+
token_type == FSTRING_END or
1698+
token_type == TSTRING_END
1699+
):
16871700
prefixes.pop()
16881701

16891702

0 commit comments

Comments
 (0)