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

Python3 support : another take #16

Merged
merged 4 commits into from
Jun 19, 2016
Merged

Python3 support : another take #16

merged 4 commits into from
Jun 19, 2016

Conversation

jlaine
Copy link
Contributor

@jlaine jlaine commented Jun 17, 2016

This approach does not require any additional dependencies.

@DavidMuller
Copy link
Owner

Thanks for taking a go at this @jlaine -- would you mind adding a test case the covers request.body being empty -- and, thus, us using bytes() instead of ''

@jlaine
Copy link
Contributor Author

jlaine commented Jun 18, 2016

Hi, adding a test sounds like an excellent idea, however it doesn't look as though this method - with or without a body is currently being tested at all, so I'm struggling a bit with what the expected input / output is.

@jlaine
Copy link
Contributor Author

jlaine commented Jun 18, 2016

@DavidMuller I have added the requested unit tests, feel free to change them if you had envisioned them differently. I also altered the README.md to mention python 3 support. It would probably be worth enabling travis tests against python 3.5, but I'll leave that for another PR once this has landed.

@@ -95,7 +100,7 @@ def __call__(self, r):

# Create payload hash (hash of the request body content). For GET
# requests, the payload is an empty string ('').
body = r.body if r.body else ''
body = r.body if r.body else bytes()
payload_hash = hashlib.sha256(body).hexdigest()
Copy link
Owner

@DavidMuller DavidMuller Jun 19, 2016

Choose a reason for hiding this comment

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

python2 vs pytnon3 string compability is still a bit of an unknown for me. Could you explain why we run .encode('utf-8') on hashlib.sha256(canonical_request.encode('utf-8')).hexdigest() but not on payload_hash = hashlib.sha256(body).hexdigest()?

(A separte python3 PR, #14, for example runs a .encode(utf-8') option in this general area)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The key is that PreparedRequest.body is never a string, it is either None or binary data (bytes), encoding has already been applied by the time it becomes a request body. In PR #14 encoding is applied conditionally, but the only case it makes sense is when we replace None by an empty string. Instead of this hack we do the right thing and set body to an empty "bytes".

Copy link
Owner

Choose a reason for hiding this comment

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

Nice, thanks for explaining

@DavidMuller
Copy link
Owner

DavidMuller commented Jun 19, 2016

Thanks for pushing tests @jlaine -- have you had a chance to test these changes on a production system that uses python3?

I ran some cursory tests on a python2 production system, and things seemed OK

@jlaine
Copy link
Contributor Author

jlaine commented Jun 19, 2016

Yes, I am using this with python3 for elasticsearch. I had started by naively applying "encode" in the line you pointed out, which worked for GETs (no body, which got replaced by an empty string) but bombed for POSTs as the body is already bytes and hence does not have an "encode" method.

@jlaine
Copy link
Contributor Author

jlaine commented Jun 19, 2016

By the way you can quite easily run some python3 tests locally by creating a py3 virtualenv (virtualenv -p /usr/bin/python3 myenv) and installing requests + elasticsearch

@DavidMuller
Copy link
Owner

Thanks for all your work here @jlaine -- I appreciate it

Will issue a new pypi release in the next hour or so and post back here

@DavidMuller DavidMuller merged commit 30d1cce into DavidMuller:master Jun 19, 2016
@DavidMuller
Copy link
Owner

Version 0.3.0 is on pypi

@jlaine
Copy link
Contributor Author

jlaine commented Jun 19, 2016 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants