From 5165dfa2dfb1eed28c6a2ea15a8d106a221e8645 Mon Sep 17 00:00:00 2001 From: Alexander Lozovskoy Date: Wed, 6 Aug 2014 12:38:14 +0400 Subject: [PATCH 1/2] Issue with printing unicode non-english characters #8425 patch --- lib/ansible/utils/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 83b80ab885e..2f34c86cdd8 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -205,9 +205,9 @@ def jsonify(result, format=False): if type(value) is str: result2[key] = value.decode('utf-8', 'ignore') if format: - return json.dumps(result2, sort_keys=True, indent=4) + return json.dumps(result2, sort_keys=True, indent=4, ensure_ascii=False) else: - return json.dumps(result2, sort_keys=True) + return json.dumps(result2, sort_keys=True, ensure_ascii=False) def write_tree_file(tree, hostname, buf): ''' write something into treedir/hostname ''' From 4fafd3baa8e8df06f4d8277abb71699157b2e1cb Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Thu, 21 Aug 2014 15:52:36 -0500 Subject: [PATCH 2/2] Add error checking around json.dumps when not forcing ascii mode --- lib/ansible/utils/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 2f34c86cdd8..f5182beca8d 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -204,10 +204,15 @@ def jsonify(result, format=False): for key, value in result2.items(): if type(value) is str: result2[key] = value.decode('utf-8', 'ignore') + + indent = None if format: - return json.dumps(result2, sort_keys=True, indent=4, ensure_ascii=False) - else: - return json.dumps(result2, sort_keys=True, ensure_ascii=False) + indent = 4 + + try: + return json.dumps(result2, sort_keys=True, indent=indent, ensure_ascii=False) + except UnicodeDecodeError: + return json.dumps(result2, sort_keys=True, indent=indent) def write_tree_file(tree, hostname, buf): ''' write something into treedir/hostname '''