From 7d027a031a3649ac04965a09cd26563ac9d760fd Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 6 Nov 2012 14:13:30 -0800 Subject: Fix the case where a unknown type is seen and it has contents which are in unicode which seems to cause python to blow-up when this happens since 'string-escape' doesn't work on unicode (at least in 2.6). LP: #1075756 --- cloudinit/handlers/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cloudinit/handlers/__init__.py b/cloudinit/handlers/__init__.py index 99caed1f..e843eb75 100644 --- a/cloudinit/handlers/__init__.py +++ b/cloudinit/handlers/__init__.py @@ -171,7 +171,11 @@ def walker_callback(pdata, ctype, filename, payload): elif payload: # Extract the first line or 24 bytes for displaying in the log start = _extract_first_or_bytes(payload, 24) - details = "'%s...'" % (start.encode("string-escape")) + try: + details = "'%s...'" % (start.encode("string-escape")) + except TypeError: + # Unicode doesn't support string-escape... + details = "'%s...'" % (start) if ctype == NOT_MULTIPART_TYPE: LOG.warning("Unhandled non-multipart (%s) userdata: %s", ctype, details) -- cgit v1.2.3 From 7ec0ef04b975eb5b4c40f7ae746d706585c73a02 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 6 Nov 2012 14:24:19 -0800 Subject: Use a method instead + at least attempt the unicode-escape path. --- cloudinit/handlers/__init__.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/cloudinit/handlers/__init__.py b/cloudinit/handlers/__init__.py index e843eb75..d847f331 100644 --- a/cloudinit/handlers/__init__.py +++ b/cloudinit/handlers/__init__.py @@ -160,6 +160,19 @@ def _extract_first_or_bytes(blob, size): return start +def _escape_string(text): + try: + text = "'%s...'" % (text.encode("string-escape")) + except TypeError: + try: + # Unicode doesn't support string-escape... + text = "'%s...'" % (text.encode('unicode-escape')) + except TypeError: + # Give up... + text = "'%s...'" % (text) + return text + + def walker_callback(pdata, ctype, filename, payload): if ctype in PART_CONTENT_TYPES: walker_handle_handler(pdata, ctype, filename, payload) @@ -171,11 +184,7 @@ def walker_callback(pdata, ctype, filename, payload): elif payload: # Extract the first line or 24 bytes for displaying in the log start = _extract_first_or_bytes(payload, 24) - try: - details = "'%s...'" % (start.encode("string-escape")) - except TypeError: - # Unicode doesn't support string-escape... - details = "'%s...'" % (start) + details = _escape_string(start) if ctype == NOT_MULTIPART_TYPE: LOG.warning("Unhandled non-multipart (%s) userdata: %s", ctype, details) -- cgit v1.2.3 From 9850895442afe55079cecf4fd96fe8430ed960ea Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 6 Nov 2012 14:27:56 -0800 Subject: Do the append after escape. --- cloudinit/handlers/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cloudinit/handlers/__init__.py b/cloudinit/handlers/__init__.py index d847f331..8d6dcd4d 100644 --- a/cloudinit/handlers/__init__.py +++ b/cloudinit/handlers/__init__.py @@ -162,14 +162,14 @@ def _extract_first_or_bytes(blob, size): def _escape_string(text): try: - text = "'%s...'" % (text.encode("string-escape")) + return text.encode("string-escape") except TypeError: try: # Unicode doesn't support string-escape... - text = "'%s...'" % (text.encode('unicode-escape')) + return text.encode('unicode-escape') except TypeError: # Give up... - text = "'%s...'" % (text) + pass return text @@ -184,7 +184,7 @@ def walker_callback(pdata, ctype, filename, payload): elif payload: # Extract the first line or 24 bytes for displaying in the log start = _extract_first_or_bytes(payload, 24) - details = _escape_string(start) + details = "'%s...'" % (_escape_string(start)) if ctype == NOT_MULTIPART_TYPE: LOG.warning("Unhandled non-multipart (%s) userdata: %s", ctype, details) -- cgit v1.2.3