[Tutor] lib2to3 fixers

Steven D'Aprano steve at pearwood.info
Sun Jun 13 05:03:32 CEST 2010

On Sat, 12 Jun 2010 05:27:39 am Zubin Mithra wrote:
> Hey everyone,
> I just discovered that the following construct does not work in Py3k.
> >>> string.maketrans('-', '_')

Like the error message says, you have to use bytes, which I grant is 
counter-intuitive, but it does work:

>>> table = string.maketrans(b'-', b'_')
>>> 'abc-def'.translate(table)

(Perhaps it shouldn't though... it seems to be an undocumented feature.)

> However, the following works,
> >>> str.maketrans('-', '_')
> When i try to convert a python module containing the above construct,
> it does not get modified in a way such that it could run on Python
> 3.0

I think this should be reported as a bug/feature request on the bug 
tracker. Having 2to3 be as extensive as possible is a high priority to 
the Python development team.


> 1. replace string.maketrans() with a function which is compatible in
> both Python 2.x and 3.0 ; as of now I`m unaware of such a construct.

That at least is easy.

def maketrans(a, b):
        return string.translate(a, b)
    except TypeError:
        return str.translate(a, b)

> 2. write a fixer for doing this which I could use. I could`nt find
> any good tutorials out there on writing fixers, i`d be grateful if
> you could point me to any.

For that you probably need to ask on the main Python mailing list or 


Steven D'Aprano

More information about the Tutor mailing list