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

(cherry picked from commit 67d5e1d3e7)
This commit is contained in:
andy-pi 2017-11-18 03:51:13 +08:00 committed by Sam Doran
parent 2f6e03956f
commit 9645c43c5e
2 changed files with 6 additions and 4 deletions

View file

@ -317,6 +317,7 @@ Ansible Changes By Release
* Fix for hostname module on RHEL 7.5 (https://github.com/ansible/ansible/issues/31811)
* Fix provider password leak in logs for asa modules (https://github.com/ansible/ansible/issues/32343)
* Fix tagging for dynamodb_table if region is not explicitly passed to the module (https://github.com/ansible/ansible/pull/32557)
* Fix Python 3 decode error in `cloudflare_dns` (https://github.com/ansible/ansible/pull/32065)
### Known Bugs
* Implicit localhost is getting ansible_connection from all:vars instead of

View file

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