Hacker News new | ask | show | jobs
by tyingq 3347 days ago
Great idea.

A writeup on how you handled mapping bytes, bytearrays, unicode, etc, to 2.7 constructs would be interesting.

In code that supports both 2 and 3, I end up with ugly stuff testing sys.hexversion to deal with 3rd party libraries, like pyserial, that expect str in v2, bytearrays in v3.

2 comments

I've scanned the source code and it doesn't look like it handles the bytes/unicode thing. Which, while the project is cool, will mean it won't work for a ton of Python 3 code.
The implicit coercion between bytes and unicode was one of the biggest hurtles I ran into when porting a relatively large (30k+ lines) code base from Python 2 to 3. Also challenging was the change in comparisons (None no longer smaller than all types, TypeError when trying to order disjoint types). While this looks like an interesting hack, I can't see anyone seriously using it to backport code to Python 2.7.

As I mentioned elsewhere, I created "ppython" to handle these two porting problems. The source code is on github: https://github.com/nascheme/ppython . If someone needs a Windows binary, I could build it if you ask politely. ;-) For my project, it has been a useful tool to speed up porting.

Porting code is not trivial and there is huge amount of Python 2 out there. I wish more effort had been spent building tools to help porting of code. Even simple things like disallowing the 'u' prefix on strings in Python 3 was a big mistake. Mostly those things have been corrected but it is still going to take a very long time to move the majority of the community to Python 3. There will be Python 2 running for the next 50 years easily, I'm sure, maybe 100 years. At least Python is open source and you will not be stranded like VB developers where when MS drastically changed Visual Basic.

You probably fixed lots of bugs in the process. How long did it take?
sys.version_info gives you a tuple, which is way more convenient for comparisons than sys.hexversion:

    if sys.version_info >= (3,):
        ...  # Python 3.X code
Or you could check whether str is bytes:

    if str is bytes:
        ...  # Python 2.X code
That is why I used sys.hexversion. Since there are 3 paths, has real bytes, fake str bytes, and no bytes.
bytes exists since Python 2.6. Do you really need to support anything older?
Maybe not, but the incremental work was less than making a single codebase work for both 2 and 3. It's code that runs on openwrt, so forcing my ideas on the users is painful for them...they would have to build a whole new image.