Skip to content

Commit

Permalink
Fix echo-content-escaped.py again (#26645)
Browse files Browse the repository at this point in the history
The fix in #26615 only works in Py3.

This time we make it compatible with Py2 & Py3.
  • Loading branch information
Hexcles committed Nov 25, 2020
1 parent cfcd8eb commit c52e4b0
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions FileAPI/file/resources/echo-content-escaped.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
# As a convenience, CRLF newlines are left as is.

def escape_byte(byte):
# Iterating over a 'bytes' type gives ints, so convert to bytes.
byte = bytes([byte])
if b"\0" <= byte <= b"\x1F" or byte >= b"\x7F":
return b"\\x%02x" % ord(byte)
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:
return b"\\x%02x" % code
if code == ord(b"\\"):
return b"\\\\"
return byte

Expand Down

0 comments on commit c52e4b0

Please sign in to comment.