Skip to content
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

fix python3 incompatibility with json.loads #41

Closed
wants to merge 1 commit into from

Conversation

JanKoppe
Copy link
Contributor

if using python3, decode bytes to str befor using json.loads

fixes #39

if using python3, decode bytes to str befor using json.loads
Copy link
Member

@lkiesow lkiesow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some tests and now fixed the particular problem in both Python 2 and Python 3 in pull request #44. I would suggest declining this if favor of that patch. What do you think, @JanKoppe?

if sys.version_info[0] == 2:
return json.loads(self.data)
else:
return json.loads(self.data.decode('utf8'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do hate the Python 2 and 3 differences in this way and more of, it confuses me. But I think in Python 2 it works because of it's default fallback to ascii encoding which will probably break if there are actually special characters. Hence, I think we might always need to encode this. Let me do a test…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that it fails in Python 2 as well:

2016-11-20 23:25:02 ERROR    [ca.py:395:safe_start_capture()] Start capture failed
2016-11-20 23:25:02 ERROR    [ca.py:396:safe_start_capture()] Traceback (most recent call last):
  File "pyca/ca.py", line 393, in safe_start_capture
    return start_capture(event)
  File "pyca/ca.py", line 264, in start_capture
    f.write(value)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 506-510: ordinal not in range(128)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screw that, I should start actually reading error message -_-'. It fails even before that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To get all this fixed, I think we should set-up a unittest framework first. I've created a pullrequest for that. What do you think: #43

That should enable us to easily add Unicode tests :-)

Copy link
Contributor Author

@JanKoppe JanKoppe Nov 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. You can never have enough tests. :) Will look over your PR in a minute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Let's take your version, obviously much better.

@JanKoppe JanKoppe closed this Nov 21, 2016
@JanKoppe JanKoppe deleted the t/39-json.loads-error branch February 28, 2017 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

json.loads Error - related to Python 3?
2 participants