From 9645c43c5e309afda117e9178694cc3575644d96 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 (cherry picked from commit 67d5e1d3e7cfb57de85131ac0bc703cd89a07c7f) --- CHANGELOG.md | 1 + lib/ansible/modules/net_tools/cloudflare_dns.py | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d7497afd5..12415e5348 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/ansible/modules/net_tools/cloudflare_dns.py b/lib/ansible/modules/net_tools/cloudflare_dns.py index 34fbae5010..a5c9475ec8 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):