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 compat #26

Closed
wants to merge 8 commits into from
Closed

Python3 compat #26

wants to merge 8 commits into from

Conversation

keis
Copy link

@keis keis commented Jul 30, 2012

These patches should make it possible to use rauth with python3 without breaking python2 support.

Not all unittests are working yet for python3. Most importantly not the unicode ones as I have to comment these out as the u'' stuff is a syntax error in 3.

@maxcountryman
Copy link
Contributor

This is great! Really happy to see someone working on Python 3 support. A couple of thoughts:

Maybe it's worth borrowing from Request's and maintaining a compat module for Python 3 compatibility purposes?

I'm a little concerned about some of the encoding changes, specifically where ASCII is explicitly called. Probably not a big deal but again, maybe this is best managed by breaking out Python 3 idioms into their own module where it makes sense?

Anyway, again, very much appreciate the effort here!

@keis
Copy link
Author

keis commented Jul 31, 2012

Maybe it's worth borrowing from Request's and maintaining a compat module for Python 3 compatibility purposes?

the request compat module looks nice, should be possible to reduce the imports to a single from request.compat import line using that. Not sure how public/stable that api is, but maybe rauth will follow request close enough so it wouldnt be a problem.

I'm a little concerned about some of the encoding changes, specifically where ASCII is explicitly called.

I, maybe falsely, assumed the tokens/keys would all be ascii. utf-8 is another option.

maybe this is best managed by breaking out Python 3 idioms into their own module where it makes sense?

In my experience having common code for both is to prefer when possible. But encodings tend to be a bit tricky so maybe its better to leave them out in python2 where they are not needed.

Anyway, again, very much appreciate the effort here!

Thanks and np

@maxcountryman
Copy link
Contributor

the request compat module looks nice, should be possible to reduce the imports to a single from request.compat import line using that. Not sure how public/stable that api is, but maybe rauth will follow request close enough so it wouldnt be a problem.

Absolutely. Perhaps it's best to just add a new module to rauth that essentially replicates the functionality?

In my experience having common code for both is to prefer when possible. But encodings tend to be a bit tricky so maybe its better to leave them out in python2 where they are not needed.

This sounds good to me.

@maxcountryman
Copy link
Contributor

This looks awesome! Thanks for updating the pull request. Are tests passing for Python 3 as well?

@maxcountryman
Copy link
Contributor

I think this is getting close:

There's a syntax error on line 150 of oauth.py. Also I think it's worth cross checking everything by porting the example applications and making sure they work on both 2.x and 3.

I tried with the Twitter example but there are a few outstanding changes that break the rauth API and probably need to be patched up before we can merge this.

Otherwise this is looking really good. Thanks again!

@keis
Copy link
Author

keis commented Aug 2, 2012

oops, messed up that last commit.

there's a few tests not passing yet, and I'm trying to get a working py3.3 install where unicode literals are reintroduced to run the unicode tests.

it's bound to be a few other issues but I hope to find those when I get a around to dog food the code.

@maxcountryman
Copy link
Contributor

Sounds good. Let me know when you think it's ready to be merged.


:param s: object to be converted
'''
return bytes(str(s), 'ascii')
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that this doesn't quite do what you'd expect if s is already bytes; I don't know if that's a problem:

In [2]: s
Out[2]: b'abc'

In [3]: bytes(str(s), 'ascii')
Out[3]: b"b'abc'"

Something like this would handle that case:

if isinstance(s, str):
    return s.encode('ascii')
return s

Copy link
Contributor

Choose a reason for hiding this comment

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

And I've just seen that it's used on a float below. If you need to handle bytes as well, I guess this would work:

if isinstance(s, bytes):
    return s
return bytes(str(s), 'ascii')

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for pointing this out. Certainly something that should be taken into consideration.

Copy link
Author

Choose a reason for hiding this comment

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

good catch. I think I rather dumb it down a bit to only accept str and let the py2 version be a lambda x: x
to much magic will only hurt

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree about magic. :)

@maxcountryman
Copy link
Contributor

Also wanted to add that I'll incorporate Python 3 as a testing environment for our Travis configuration once this is finalized.

@@ -78,17 +77,20 @@ def __init__(self, response):

@property
def content(self):
content = self.response.content
if not isinstance(content, basestring) and hasattr(content, 'decode'):
content = content.decode(self.response.encoding)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think requests has a response.text attribute that does the same as this. I don't know what version of requests we require, though - it was added some time in the last few months.

Choose a reason for hiding this comment

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

it's seems to have been introduced here, in january
kennethreitz/requests@49a38ac

Is there any reason to not use this for python2?

Copy link
Contributor

Choose a reason for hiding this comment

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

We should be using response.text for Python 2. This was an oversight of my part.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also there's nothing stopping us from increasing the required version of Requests. So if there are other features that should be taken advantage of that's an option too.

@maxcountryman
Copy link
Contributor

Just a note that this will have to be rebased before it can be merged. This could happen after the implementation details are finalized or before.

@maxcountryman
Copy link
Contributor

Hey great, looks like this is coming along nicely! Seems the Twitter example is now functional?

@keis
Copy link
Author

keis commented Sep 10, 2012

Yup, it's working. finally got some time to look into this again.

the unittests should work now with python3 after running 2to3, except for test_parse_utf8_qsl_non_unicode which becomes a bit weird when using response.text

@maxcountryman
Copy link
Contributor

So is this essentially fully-functional at this point? Is it possible to fix that remaining unit test?

@maxcountryman
Copy link
Contributor

Oh by the way, if you rebase off of master so that this can be merged, consider updating the .travis.yml config to include Python 3.x. This way Travis will test the unit test suite against Python 3. (I'd also like to ensure that Travis agrees this is good to pull.)

@maxcountryman
Copy link
Contributor

How are things going with this? Do you think it's about ready to merge into master?

@keis
Copy link
Author

keis commented Sep 22, 2012

Yeah, it all seems to work now. Not really sure how to get it to run on travis, it needs a 2to3 on the tests before running.

@maxcountryman
Copy link
Contributor

Hm okay. Do the tests pass for 2.x after 2to3 or is it a mutually exclusive conversion?

@maxcountryman
Copy link
Contributor

Also as a side note, it looks like Travis is not failing on coverage like it should be. So I'll need to update that.

@keis
Copy link
Author

keis commented Sep 23, 2012

it's to bad, but after 2to3 the tests no longer work in py2. The change is in the unicode literals.

@takluyver
Copy link
Contributor

What Python versions do you need to support? From Python 2.6, you can use from __future__ import unicode_literals to make 'foo' unicode.

Alternatively, there's the approach six offers, where you define a u() function, and write unicode literals like u('foo').

@maxcountryman
Copy link
Contributor

2.6 is the minimum version we need to support.

@keis
Copy link
Author

keis commented Sep 26, 2012

@takluyver That worked nicely

tests are now passing in both 2.7 and 3.2 without any modification

@maxcountryman
Copy link
Contributor

How about 2.6?

@keis
Copy link
Author

keis commented Sep 26, 2012

I don't have a 2.6 on this laptop, but the travis build was green.

@maxcountryman
Copy link
Contributor

Unfortunately Travis is not failing (as it should be) for coverage. If you run the tests locally you'll see there's an issue with PEP8. Either we should ignore the compat module or fix those in some way.

@maxcountryman
Copy link
Contributor

Here's the log for the curious (and those without 2.6): http://travis-ci.org/#!/litl/rauth/jobs/2573204

@maxcountryman
Copy link
Contributor

Any updates here?

@maxcountryman
Copy link
Contributor

Checking in, wondering where things sit with this commit set?

@keis
Copy link
Author

keis commented Nov 12, 2012

Sorry I didn't get back to you sooner, things been hectic.

seems to be pyflakes (and not pep8) complaining about the unused imports, it's a quite easy workaround to add dummy statements using the names.

complete coverage of the compat module I don't think we can get unless the results of a 2.x and 3.x run is combined

@maxcountryman
Copy link
Contributor

complete coverage of the compat module I don't think we can get unless the results of a 2.x and 3.x run is combined

Totally understand: can we just explicitly exclude it from coverage in the tests in that case?

@keis
Copy link
Author

keis commented Nov 19, 2012

seems like it's crashing from a broken requests build.

@maxcountryman
Copy link
Contributor

I see that none of the tests are running properly on Travis. Maybe we need to require a specific version of Requests if that's what you think seems to be causing the issue? I also see that on the 3.2 build pyflakes is not invoked properly... Coverage is missing on 2.7 and 2.6.

@maxcountryman
Copy link
Contributor

Is there anything I can do to help out with this? Would love to get 3.x support in before the end of the year. Also working on doing a new release that will incorporate some breaking API changes, might be a good milestone to include this set of changes as well.

@keis
Copy link
Author

keis commented Dec 3, 2012

If you have any idea on how to fix the travis issues that would be great. And the pyflakes thing.
I hope I find the time to take a longer look at getting the coverage up this weekend.

@maxcountryman
Copy link
Contributor

Sure I'll see what I can do.

@maxcountryman
Copy link
Contributor

Just a heads up: due to the API changes in requests v1.x, I've had to completely rework the OAuth1 architecture. Hopefully this won't make Python 3 support much more difficult than it already is but it probably does mean this patch has to be rewritten at least in part.

@maxcountryman
Copy link
Contributor

Hi again, I'm going to close this pull as it won't be compatible with 0.5.0. However if you have time to do a rewrite against the new release of rauth, please feel free to reopen this or make another pull.

@maxcountryman
Copy link
Contributor

I think there's some renewed interest in porting rauth to Python 3. Is anyone who had worked on this willing to pick things back up and try to see how well they mesh with the changes in rauth 0.5.0?

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.

4 participants