mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 00:01:36 +00:00
escaped_str_to_bytes: support unicode on python 2
This commit is contained in:
parent
f7e77d543b
commit
7a8da48a30
@ -439,10 +439,14 @@ def escaped_str_to_bytes(data):
|
|||||||
"""
|
"""
|
||||||
Take an escaped string and return the unescaped bytes equivalent.
|
Take an escaped string and return the unescaped bytes equivalent.
|
||||||
"""
|
"""
|
||||||
if not isinstance(data, str):
|
if not isinstance(data, six.string_types):
|
||||||
|
if six.PY2:
|
||||||
|
raise ValueError("data must be str or unicode")
|
||||||
raise ValueError("data must be str")
|
raise ValueError("data must be str")
|
||||||
|
|
||||||
if six.PY2:
|
if six.PY2:
|
||||||
|
if isinstance(data, unicode):
|
||||||
|
data = data.encode("utf8")
|
||||||
return data.decode("string-escape")
|
return data.decode("string-escape")
|
||||||
|
|
||||||
# This one is difficult - we use an undocumented Python API here
|
# This one is difficult - we use an undocumented Python API here
|
||||||
|
@ -182,6 +182,9 @@ def test_bytes_to_escaped_str():
|
|||||||
|
|
||||||
def test_escaped_str_to_bytes():
|
def test_escaped_str_to_bytes():
|
||||||
assert utils.escaped_str_to_bytes("foo") == b"foo"
|
assert utils.escaped_str_to_bytes("foo") == b"foo"
|
||||||
assert utils.escaped_str_to_bytes(r"\x08") == b"\b"
|
assert utils.escaped_str_to_bytes("\x08") == b"\b"
|
||||||
assert utils.escaped_str_to_bytes(r"&!?=\\)") == br"&!?=\)"
|
assert utils.escaped_str_to_bytes("&!?=\\\\)") == br"&!?=\)"
|
||||||
assert utils.escaped_str_to_bytes(r"ü") == b'\xc3\xbc'
|
assert utils.escaped_str_to_bytes("ü") == b'\xc3\xbc'
|
||||||
|
assert utils.escaped_str_to_bytes(u"\\x08") == b"\b"
|
||||||
|
assert utils.escaped_str_to_bytes(u"&!?=\\\\)") == br"&!?=\)"
|
||||||
|
assert utils.escaped_str_to_bytes(u"ü") == b'\xc3\xbc'
|
Loading…
Reference in New Issue
Block a user