2to3 and maketrans

Dan Stromberg drsalists at gmail.com
Thu Mar 3 16:12:54 EST 2011


On Wed, Mar 2, 2011 at 10:58 PM, Gregory Ewing
<greg.ewing at canterbury.ac.nz>wrote:

> What is the recommended way to write code for 2.7 using
> maketrans() on text strings in such a way that it will
> convert correctly using 2to3?
>
> There seems to be two versions of maketrans in 3.x, one
> for text and one for bytes. Code that references
> string.maketrans ends up with the one for bytes, which
> is not what I want. But I can't write str.maketrans,
> because that doesn't exist in 2.x.
>
> So what am I supposed to do?
>

I've not tried maketrans, but I've been having some luck writing Python that
works, unchanged (not even run through 2to3), on both CPython 2.x (2.5, 2.6,
2.7), CPython 3.x (3.1, 3.2) and pypy (1.4.1 which does Python 2.5 and a
recent trunk that pretty much does Python 2.7).

For strings vs bytes, I've been finding that just using os.read and os.write
helps; on 2.x they do str, on 3.x they do bytes.

You can usually then just pass things around as str or bytes, unchanged,
depending on what your runtime prefers.  If you need an ordinal, you can use
isinstance and take the ord when you have a str of length 1 - otherwise
you've already got an ordinal.

The other main sticky point for str vs bytes this way is sometimes I need a
null string to express things nicely, but that can be done with the
following in 3.x:

''.encode('utf-8')

...without choking a 2.x parser (b'' blows up most, if not all, 2.x
parsers), but it's probably best to wrap that up in a scope (module?) of its
own rather than scattering it throughout your code.

Also, and this was a little surprising to me, if you build your modules with
Cython, you may find that you conveniently get C extension modules for 2.x
and 3.x from the same Cython code, with just a pair of recompiles (one from
Cython to C, one from C to binary) for each CPython you need to support.
And the performance benefit of Cython appears to be even greater on 3.x than
it was on 2.x (at least in my minimal testing).  There's a branch of Cython
in testing now that does generators, BTW, which has been working well for
me.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110303/5f26cbfd/attachment.html>


More information about the Python-list mailing list