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

Add Python 3 support #4

Closed
wants to merge 1 commit into from

Conversation

hfern
Copy link

@hfern hfern commented Apr 16, 2018

This adds python 3 support -- mostly around the string/bytes changes wrt digests.

@hfern hfern changed the title Add Python 3 support #3: Add Python 3 support Apr 16, 2018
@hfern hfern changed the title #3: Add Python 3 support Add Python 3 support Apr 16, 2018
@hfern
Copy link
Author

hfern commented Apr 16, 2018

This is a fix for #3

@hfern
Copy link
Author

hfern commented Apr 16, 2018

Tox output:

~/dev/rsign(hgf/fix-python-3) » tox
GLOB sdist-make: /Users/hufernan/dev/rsign/setup.py
py27 inst-nodeps: /Users/hufernan/dev/rsign/.tox/dist/rsign-0.1.3.zip
py27 installed: astroid==1.6.3,backports.functools-lru-cache==1.5,configparser==3.5.0,enum34==1.1.6,flake8==3.5.0,futures==3.2.0,isort==4.3.4,lazy-object-proxy==1.3.1,mccabe==0.6.1,pycodestyle==2.3.1,pyflakes==1.6.0,pylint==1.8.4,rsign==0.1.3,singledispatch==3.4.0.3,six==1.11.0,wrapt==1.10.11
py27 runtests: PYTHONHASHSEED='3210264798'
py27 runtests: commands[0] | python -m unittest discover
.......
----------------------------------------------------------------------
Ran 7 tests in 0.001s

OK
py27 runtests: commands[1] | flake8
py27 runtests: commands[2] | pylint --rcfile=rsign/tests/config/pylint.cfg rsign
Using config file /Users/hufernan/dev/rsign/rsign/tests/config/pylint.cfg

------------------------------------
Your code has been rated at 10.00/10

py36 inst-nodeps: /Users/hufernan/dev/rsign/.tox/dist/rsign-0.1.3.zip
py36 installed: astroid==1.6.3,flake8==3.5.0,isort==4.3.4,lazy-object-proxy==1.3.1,mccabe==0.6.1,pycodestyle==2.3.1,pyflakes==1.6.0,pylint==1.8.4,rsign==0.1.3,six==1.11.0,wrapt==1.10.11
py36 runtests: PYTHONHASHSEED='3210264798'
py36 runtests: commands[0] | python -m unittest discover
.......
----------------------------------------------------------------------
Ran 7 tests in 0.001s

OK
py36 runtests: commands[1] | flake8
py36 runtests: commands[2] | pylint --rcfile=rsign/tests/config/pylint.cfg rsign
Using config file /Users/hufernan/dev/rsign/rsign/tests/config/pylint.cfg

------------------------------------
Your code has been rated at 10.00/10

________________________________________________________________________________________________ summary _________________________________________________________________________________________________
  py27: commands succeeded
  py36: commands succeeded
  congratulations :)

Copy link

@TheOtherDude TheOtherDude left a comment

Choose a reason for hiding this comment

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

LGTM

@markcole99
Copy link

@gobolt FYI

@@ -70,7 +72,10 @@ def compare(self, s1, s2):
def sign_string(self, key, text):
Copy link
Contributor

Choose a reason for hiding this comment

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

What type does sign_string return? Add to the comment and for corresponding sign_request method in request.py.

Copy link
Author

Choose a reason for hiding this comment

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

Would you prefer if we returned either string or bytes depending on your python version, or just force the result to always be string?

Returning bytes in py3 would be the correct result imo, but it would complicate using the library.

@@ -70,7 +72,10 @@ def compare(self, s1, s2):
def sign_string(self, key, text):
""" Return the signing method's digest """
key, text = _clean(key), _clean(text)
return hmac.new(key, text, self.hash_fn).digest()
# Py3 hmac.new expects key as bytes. py2 expects str

Choose a reason for hiding this comment

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

_clean function suppose to convert Unicode to bytes. It is not correctly implemented. I think it is better we fix _clean instead of adding the second conversion level.

return binascii.b2a_base64(binary).replace('\n', '')
# Py2 binascii.b2a_base64 gives us str, so str replacement
# Py3 gives us bytes, so we can only replace with bytes
newline = '\n'.encode() if PY3 else '\n'

Choose a reason for hiding this comment

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

newline = b'\n'

# Py2 binascii.b2a_base64 gives us str, so str replacement
# Py3 gives us bytes, so we can only replace with bytes
newline = '\n'.encode() if PY3 else '\n'
empty = ''.encode() if PY3 else ''

Choose a reason for hiding this comment

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

empty = b''

@@ -14,7 +16,7 @@ def normalize(timestamp, nonce, method, path, host, port): # pylint: disable=R0
""" Accepts args as strings and returns the normalized form for signing """
normalized = '\n'.join([

Choose a reason for hiding this comment

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

Here we make a string and decode nonce (which might be bytes, can it?). I think the only usage of normalized is in sign_string which requires bytes. If my understanding is correct, should not we just produce bytes here?

@alex8080
Copy link

One of the recommendations on porting from Python 2 to 3 is to:

Consider using optional static type checking to make sure your type usage works in both Python 2 & 3 (e.g. use mypy to check your typing under both Python 2 & Python 3).

I found the advice very helpful in other projects I had to port.

@gobolt
Copy link
Contributor

gobolt commented May 10, 2018

Closed in favor of #6

@gobolt gobolt closed this May 10, 2018
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.

5 participants