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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pyca/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ class Event(Base):
def get_data(self):
'''Load JSON data from event.
'''
return json.loads(self.data)
# Python 3 wants str
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.


def set_data(self, data):
'''Store data as JSON.
Expand Down