We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent cfcd8eb commit c52e4b0Copy full SHA for c52e4b0
FileAPI/file/resources/echo-content-escaped.py
@@ -5,11 +5,13 @@
5
# As a convenience, CRLF newlines are left as is.
6
7
def escape_byte(byte):
8
- # Iterating over a 'bytes' type gives ints, so convert to bytes.
9
- byte = bytes([byte])
10
- if b"\0" <= byte <= b"\x1F" or byte >= b"\x7F":
11
- return b"\\x%02x" % ord(byte)
12
- if byte == b"\\":
+ # Iterating over a binary string gives different types in Py2 & Py3.
+ # Py3: bytes -> int
+ # Py2: str -> str (of length 1), so we convert it to int
+ code = byte if type(byte) is int else ord(byte)
+ if 0 <= code <= 0x1F or code >= 0x7F:
13
+ return b"\\x%02x" % code
14
+ if code == ord(b"\\"):
15
return b"\\\\"
16
return byte
17
0 commit comments