views:

265

answers:

2

I have a small project I want to try porting to Python 3 - how do I go about this?

I have made made the code run without warnings using python2.6 -3 (mostly removing .has_key() calls), but I am not sure of the best way to use the 2to3 tool.

Use the 2to3 tool to convert this source code to 3.0 syntax. Do not manually edit the output!

Running 2to3 something.py outputs a diff, which isn't useful on it's own. Using the --write flag overwrites something.py and creates a backup.. It seems like I have to do..

2to3 something.py
python3.0 something.py
mv something.py.bak something.py
vim something.py
# repeat

..which is a bit round-a-bout - ideally I could do something like..

mv something.py py2.6_something.py # once

2to3 py2.6_something.py --write-file something.py
vim py2.6_something.py
# repeat
+2  A: 

Aha, you can pipe the 2to3 output to the patch command, which can write the modified file to a new file:

mv something.py py2.6_something.py
2to3 py2.6_something.py | patch -o something.py
dbr
use cp instead of mv for it to work.
nosklo
A: 

2.x should be your codebase of active development, so 2to3 should really be run in a branch or temporary directory. I'm not sure why you'd want to have the 2.x and 3.x versions lying around in the same directory. distutils has a build_2to3 script that will run 2to3 on a 3.0 install.

Benjamin Peterson
So I can quickly/easily edit the 2.x version, run 2to3, run the tests on 3.x (and repeat). If I have a branch for 3.x, how do I quickly make changes to the 2.x branch and regenerate the 2to3'd code?
dbr
Generally, you'd make all your changes on the 2.x branch, and just use 2to3 when you were distributing or trying to port your library.
Benjamin Peterson
I've made the question a bit clearer. I made the py3 branch to contain the changes I had to make to the py2.6 code, so it would 2to3 without errors
dbr