[Python-porting] Script for automatic 2to3-ing during develooment?

Mark Hammond skippy.hammond at gmail.com
Thu Feb 19 10:11:43 CET 2009


On 19/02/2009 10:29 AM, Lennart Regebro wrote:
> On Wed, Feb 18, 2009 at 22:19, "Martin v. Löwis"<martin at v.loewis.de>  wrote:
>> Lennart Regebro wrote:
>>> When porting you constantly need two copies of the code, one that has
>>> been 2to3'd and one not. Before I start writing a script that syncs
>>> the two and runs 2to3 on the updates scripts I thought I better check
>>> if somebody else already have done this.
>> In distutils' build_py command, there is support for running 2to3 at
>> installation time.
>
> Yeah, but I don't want to *install* it. I want to run the tests under
> Python 3. :)

pywin32 takes the approach outlined by Martin - so *does* actually 
install from the source dir, and the tests are run in the install dir.

If the tests are simple, I use the following "run2.py" script a'la:

% python30 run2.py some_script_in_py2_syntax.py etc...

It is far from perfect, but works-for-me...

Cheers,

Mark

-- run2.py --
# This is a Python 3.x script to execute a python 2.x script by
# 2to3'ing it.
import sys
from lib2to3.refactor import RefactoringTool, get_fixers_from_package

fixers = get_fixers_from_package('lib2to3.fixes')
options = dict(doctests_only=False, fix=[], list_fixes=[],
                print_function=False, verbose=False,
                write=True)
r = RefactoringTool(fixers, options)
script = sys.argv[1]
data = open(script).read()
print("Converting...")
got = r.refactor_string(data, script)
print("Executing...")
# nuke ourselves from argv
del sys.argv[0]
# patch our env
__file__ = sys.argv[0]
# and go
exec(str(got))



More information about the Python-porting mailing list