[Python-porting] A few questions about the psycopg2 porting to Python 3

Daniele Varrazzo daniele.varrazzo at gmail.com
Thu Jan 13 12:23:09 CET 2011


On Wed, Jan 12, 2011 at 8:27 PM, "Martin v. Löwis" <martin at v.loewis.de> wrote:

>> As emerged from the discussion in this ML back in 2008, there is
>> somewhere the need for a python function b('literal') that would
>> evaluate to 'literal' in Py2 and to b'literal' in py3 (we want to keep
>> compatibility with Python 2.4). Currently there is an encode()
>> involved in Py3, so it would be great to have the transformation
>> b('literal') -> b'literal' performed by 2to3 instead. Looking at the
>> other fixers it seems easy, but I haven't found how to register a
>> custom fixer for the use of build_py_2to3 in setup.py. Is there any
>> reference?
>
> See the source of distutils.util.Mixin2to3. Overriding fixer_names
> should do the trick. The default list is computed as
>
>    get_fixers_from_package('lib2to3.fixes')
>
> and you'll have to append to this list.

Yes, configuring the mixin by setting the class variable works
perfectly. As a reference here it is:

    try:
        from distutils.command.build_py import build_py_2to3 as build_py
    except ImportError:
        from distutils.command.build_py import build_py
    else:
        # Configure distutils to run our custom 2to3 fixers as well
        from lib2to3.refactor import get_fixers_from_package
        build_py.fixer_names = get_fixers_from_package('lib2to3.fixes')
        build_py.fixer_names.append('fix_b')

    setup(name="yourmodule",
          cmdclass={ 'build_py': build_py, },
          [...] )


-- Daniele


More information about the Python-porting mailing list