Reputation: 662
I'm using telnetlib.expect()
to interface with a device that responds in bytestrings, apparently. Unless I use bytestrings in the regex passed to expect()
(either precompiled, or literals), an exception is generated: TypeError: cannot use a string pattern on a bytes-like object
. However, pycodestyle
complains this is W605 invalid escape sequence '\d'
, and further reading makes me think this will become a Python syntax error in the future.
In summary:
telnetlib.expect([b'\d']) # OK, but W065
telnetlib.expect(['\d'] # TypeError
telnetlib.expect([r'\d'] # TypeError
Is there a way through this, or is pycodestyle simply wrong?
(BTW, can't seem to suppress the W065 in pycodestyle, other than suppressing all warnings.)
Upvotes: 2
Views: 879
Reputation: 8413
Bytestring literals use \
as an escape character the same way string literals do. So similar to them you have to either use a raw bytestring literal rb'\d'
or use double backslash b'\\d'
.
From https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
In plain English: Both types of literals can be enclosed in matching single quotes (
'
) or double quotes ("
). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash (\
) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.
Upvotes: 2