-
Notifications
You must be signed in to change notification settings - Fork 23.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixed .loads error for non decoded json in Python 3 #32065
Conversation
The test
The test
The test
The test
The test
The test
The test
The test
The test
The test
The test
The test
The test
The test
The test
The test
The test
|
Tried to submit a new module, but rolled back to previous commit. Will wait until this pull request accepted before adding another. |
bot_status |
@samdoran thanks for taking on the review for this. It's my first open source contribution, so please let me know if there are any mistakes - hoping to submit a new module after this. |
waiting_on: ansible |
@@ -369,7 +369,8 @@ def _cf_simple_api_call(self,api_call,method='GET',payload=None): | |||
|
|||
if content: | |||
try: | |||
result = json.loads(content) | |||
resp_json = content.decode('utf-8') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than using separate variables, you can accomplish this in one line:
result = json.loads(content.decode('utf-8'))
Thanks for the contribution! You did everything correctly as far as contributions go. Please see my comment on your code. |
@samdoran thankyou for your review - code refactored now as per your suggestion! |
@@ -369,7 +369,7 @@ def _cf_simple_api_call(self,api_call,method='GET',payload=None): | |||
|
|||
if content: | |||
try: | |||
result = json.loads(content) | |||
result = json.loads(content.decode('utf-8')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use to_text here instead of .decode. to_text is resistant to unicode errors in a way that .decode is not.
from ansible.module_utils._text import to_native, to_text
[...]
try:
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)
@andy-pi Sorry to give you bad advice. I asked for a wiser opinion and got it. Toshio's recommendation is the most resilient way to fix this issue. |
… as per reviewer comment
@andy-pi |
* 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 67d5e1d)
Cherry-picked to |
@samdoran thanks!!! |
SUMMARY
Using Python 3 this module throws an error when parsing the JSON repsonse, as it has not been decoded from its native utf-8.
ISSUE TYPE
COMPONENT NAME
Cloudflare DNS
ANSIBLE VERSION
ADDITIONAL INFORMATION
Ansible command used:
Before:
After: