[Python-3000] A couple 2to3 questions

Neal Norwitz nnorwitz at gmail.com
Sun Aug 26 05:13:10 CEST 2007


On 8/25/07, skip at pobox.com <skip at pobox.com> wrote:
> I ran 2to3 over the Doc/tools directory.  This left a number of problems
> which I initially began replacing manually.  I then realized that it would
> be better to tweak 2to3.  A couple things I wondered about:
>
>     1. How are we supposed to maintain changes to Doc/tools?  Running svn
>        status doesn't show any changes.

Dunno, Georg will have to answer this one.

>     2. I noticed a couple places where it seems to replace "if isinstance"
>        with "ifinstance".  Seems like an output bug of some sort.

That bug was probably me.  I did some large changes and broke
somethings a while back.  I've since learned my lesson and just use
2to3 to automate the task. :-)

>     3. Here are some obvious transformations (I don't know what to do to
>        make these changes to 2to3):
>
>           * replace uppercase and lowercase from the string module with
>             their "ascii_"-prefixed names.

This should be an easy fixer.  Typically the easiest thing to do is
copy an existing fixer that is similar and replace the pattern and
transform method.  To figure out the pattern, use 2to3/find_pattern.py
.  1) Pass the python expression on the command line.  2) Press return
until it shows you the expression you are interested in.  3) Then type
y<return> and you have your pattern.  Here's an example:

$ python find_pattern.py 'string.letters'
'.letters'

'string.letters'
y
power< 'string' trailer< '.' 'letters' > >

That last line is the pattern to use.  Use that string in the fixer as
the PATTERN.  You may want to add names so you can pull out pieces.
For example, if we want to pull out letters, modify the pattern to add
my_name:

power< 'string' trailer< '.' my_name='letters' > >

Then modify the transform method to get my_name, clone the node, and
set the new node to what you want.  (See another fixer for the
details.)

>           * replace types.StringType and types.UnicodeType with str and
>             unicode.

This one is already done.  I checked in fixer for this a few days ago
(during the sprint).  See 2to3/fixes/fix_types.py .  It also handles
other builtin types that were aliased in the types module.

HTH,
n


More information about the Python-3000 mailing list