[Python-3000] Refactoring tool available (work in progress)

Nick Coghlan ncoghlan at gmail.com
Wed Dec 20 14:08:55 CET 2006

Guido van Rossum wrote:
> In the sandbox I've been working on a refactoring tool, which could
> form the basis for a Python 2.x -> 3.0 conversion tool.  I'd like to
> invite folks here to give it a try and give me a hand. It certainly
> needs more work, but I think that the basic infrastructure is sound.
> Check out sandbox/2to3/:
> http://svn.python.org/view/sandbox/trunk/2to3/.
> This message is to invite feedback, and to encourage contributions. It
> would be great if people tried their hands at writing new
> transformations!

Looking at a fix Georg just checked in for intern() - is there a way for the 
fixer to indicate to the refactoring tool that a particular module needs to be 
imported if the module being refactored triggers a match on the fix?

In the intern() case, an entire module could be fixed just by putting "from 
sys import intern" at the start of the module, instead of fixing it at each 
invocation point.

This would also have the virtue of not breaking code that overrides intern() - 
because the new import would be at the start of the module, it would still be 
overridden, just like the current builtin.

For example, if a fix could provide a 'module_prefix' method which returned 
something to be inserted at the beginning of the module if the fix was 
triggered at least once, then the intern fix might look like:

class FixIntern(object):
     # No change to __init__ or match from Georg's checked in version

     def module_prefix(self):
         # Add 'from sys import intern' to the top of the module
         return pytree.Node(syms.import_from,
                           (pytree.Leaf(token.NAME, "sys"),
                            pytree.Leaf(token.NAME, "intern")
                           )) # Is that the right pytree incantation?

     def transform(self, node):
         # Actual occurences are left alone
         return node


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-3000 mailing list