From 67d5e1d3e7cfb57de85131ac0bc703cd89a07c7f Mon Sep 17 00:00:00 2001 From: andy-pi Date: Sat, 18 Nov 2017 03:51:13 +0800 Subject: [PATCH] fixed .loads error for non decoded json in Python 3 (#32065) * fixed .loads error for non decoded json in Python 3 * fixed .loads error Python 3.5 - refactor code to one line * fixed .loads error python 3.5 - mod to use to_text instead of .decode as per reviewer comment --- lib/ansible/modules/net_tools/cloudflare_dns.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/net_tools/cloudflare_dns.py b/lib/ansible/modules/net_tools/cloudflare_dns.py index 34fbae5010b..a5c9475ec8d 100644 --- a/lib/ansible/modules/net_tools/cloudflare_dns.py +++ b/lib/ansible/modules/net_tools/cloudflare_dns.py @@ -274,10 +274,11 @@ import json from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.six.moves.urllib.parse import urlencode -from ansible.module_utils._text import to_native +from ansible.module_utils._text import to_native, to_text from ansible.module_utils.urls import fetch_url + class CloudflareAPI(object): cf_api_endpoint = 'https://api.cloudflare.com/client/v4' @@ -369,9 +370,9 @@ class CloudflareAPI(object): if content: try: - result = json.loads(content) - except json.JSONDecodeError: - error_msg += "; Failed to parse API response: {0}".format(content) + result = json.loads(to_text(content, errors='surrogate_then_strict')) + except (json.JSONDecodeError, UnicodeError) as e: + error_msg += "; Failed to parse API response with error {0}: {1}".format(to_native(e), content) # received an error status but no data with details on what failed if (info['status'] not in [200,304]) and (result is None):