File tree Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change
1
+ from six import PY3
1
2
from wptserve .utils import isomorphic_encode
2
3
3
4
# Outputs the request body, with controls and non-ASCII bytes escaped
4
5
# (b"\n" becomes b"\\x0a"), and with backslashes doubled.
5
6
# As a convenience, CRLF newlines are left as is.
6
7
7
8
def escape_byte (byte ):
8
- # Iterating over a binary string gives different types in Py2 & Py3.
9
- # Py3: bytes -> int
10
- # Py2: str -> str (of length 1), so we convert it to int
11
- code = byte if type ( byte ) is int else ord ( byte )
12
- if 0 <= code <= 0x1F or code >= 0x7F :
13
- return b"\\ x%02x" % code
14
- if code == ord ( b"\\ " ) :
9
+ # We want a single-char binary string, but in Python 3 we may get an int
10
+ # from iterating over bytes.
11
+ if PY3 :
12
+ byte = bytes ([ byte ] )
13
+ if b" \0 " <= byte <= b" \x1F " or byte >= b" \x7F " :
14
+ return b"\\ x%02x" % ord ( byte )
15
+ if byte == b"\\ " :
15
16
return b"\\ \\ "
16
17
return byte
17
18
You can’t perform that action at this time.
0 commit comments