From 7a8da48a306dfc8e43239d7f2a141c465e40ab77 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 25 May 2016 19:16:02 -0700 Subject: escaped_str_to_bytes: support unicode on python 2 --- netlib/utils.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'netlib/utils.py') diff --git a/netlib/utils.py b/netlib/utils.py index 7499f71f..fe11cb5b 100644 --- a/netlib/utils.py +++ b/netlib/utils.py @@ -439,10 +439,14 @@ def escaped_str_to_bytes(data): """ 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") if six.PY2: + if isinstance(data, unicode): + data = data.encode("utf8") return data.decode("string-escape") # This one is difficult - we use an undocumented Python API here -- cgit v1.2.3 From d3477e27fa4ffbcfa9c1b9aa937d1d54448cc597 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 25 May 2016 20:11:34 -0700 Subject: bytes_to_escaped_str: always escape single quotes --- netlib/utils.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'netlib/utils.py') diff --git a/netlib/utils.py b/netlib/utils.py index fe11cb5b..73b2adb3 100644 --- a/netlib/utils.py +++ b/netlib/utils.py @@ -425,6 +425,10 @@ def safe_subn(pattern, repl, target, *args, **kwargs): def bytes_to_escaped_str(data): """ Take bytes and return a safe string that can be displayed to the user. + + Single quotes are always escaped, double quotes are never escaped: + "'" + bytes_to_escaped_str(...) + "'" + gives a valid Python string. """ # TODO: We may want to support multi-byte characters without escaping them. # One way to do would be calling .decode("utf8", "backslashreplace") first @@ -432,7 +436,9 @@ def bytes_to_escaped_str(data): if not isinstance(data, bytes): raise ValueError("data must be bytes") - return repr(data).lstrip("b")[1:-1] + # We always insert a double-quote here so that we get a single-quoted string back + # https://stackoverflow.com/questions/29019340/why-does-python-use-different-quotes-for-representing-strings-depending-on-their + return repr('"' + data).lstrip("b")[2:-1] def escaped_str_to_bytes(data): -- cgit v1.2.3 From d149c447fe9d3ec359271270ed1c32c2c7da6aad Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Wed, 25 May 2016 20:31:32 -0700 Subject: fix py3 tests --- netlib/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'netlib/utils.py') diff --git a/netlib/utils.py b/netlib/utils.py index 73b2adb3..648915fa 100644 --- a/netlib/utils.py +++ b/netlib/utils.py @@ -438,7 +438,7 @@ def bytes_to_escaped_str(data): raise ValueError("data must be bytes") # We always insert a double-quote here so that we get a single-quoted string back # https://stackoverflow.com/questions/29019340/why-does-python-use-different-quotes-for-representing-strings-depending-on-their - return repr('"' + data).lstrip("b")[2:-1] + return repr(b'"' + data).lstrip("b")[2:-1] def escaped_str_to_bytes(data): -- cgit v1.2.3