escaped_str_to_bytes: support unicode on python 2

This commit is contained in:
Maximilian Hils 2016-05-25 19:16:02 -07:00
parent f7e77d543b
commit 7a8da48a30
2 changed files with 11 additions and 4 deletions

View File

@ -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

View File

@ -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'