Skip to content

Commit a315899

Browse files
author
namaevae
committed
this reduces execution time from ~5700 ns to ~3400 ns
1 parent c50ba15 commit a315899

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

pycodestyle.py

Lines changed: 33 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,23 @@ 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 token_type == tokenize.STRING or \
1666+
token_type == FSTRING_START or \
1667+
token_type == TSTRING_START:
16651668
# Extract string modifiers (e.g. u or r)
16661669
prefixes.append(text[:text.index(text[-1])].lower())
16671670

1668-
if token_type in {tokenize.STRING, FSTRING_MIDDLE, TSTRING_MIDDLE}:
1671+
if token_type == tokenize.STRING or \
1672+
token_type == FSTRING_MIDDLE or \
1673+
token_type == TSTRING_MIDDLE:
16691674
if 'r' not in prefixes[-1]:
16701675
start_line, start_col = start
16711676
pos = text.find('\\')
16721677
while pos >= 0:
16731678
pos += 1
1674-
if text[pos] not in valid:
1679+
if text[pos] not in _PYTHON_3000_VALID_ESC:
16751680
line = start_line + text.count('\n', 0, pos)
16761681
if line == start_line:
16771682
col = start_col + pos
@@ -1683,7 +1688,9 @@ def python_3000_invalid_escape_sequence(logical_line, tokens, noqa):
16831688
)
16841689
pos = text.find('\\', pos + 1)
16851690

1686-
if token_type in {tokenize.STRING, FSTRING_END, TSTRING_END}:
1691+
if token_type == tokenize.STRING or \
1692+
token_type == FSTRING_END or \
1693+
token_type == TSTRING_END:
16871694
prefixes.pop()
16881695

16891696

0 commit comments

Comments
 (0)