Monday, August 21, 2006

Py3k Sprint Project

Guido suggested a Python 3000 project for me, albeit one that will be done mostly in Python 2.5. I'm going to work on tools for analyzing and refactoring Python 2.x code so that it runs in Python 3.x. To pick one example: Uses of mydict.has_key(akey) needs to be changes to "akey in mydict" (or mydict.__contains__(akey) in some odd cases).

I'm not sure that there is a lot of existing code to work from. I believe Bicycle Repair Man may handle some of these issues, but the SourceForge site is unreachable right now. My plan is to start with a simple tool to parse a Python source file, build an AST annotated with token information, and rewrite the same code (preserving whitespaces, comments, &c). This minimal functionality will be useful for general source-to-source transformations. It will probably take a day or two to get it working.


John Reese said...

> or mydict.__contains__(akey) in some odd cases

I'm curious as to what the odd cases are.

Jeremy Hylton said...

The example was not very clear. The real problem is a case where you use mydict.has_key as a first-class object. For example:

map(mydict.has_key, range(10))

This code would need to be translated to

map(mydict.__contains__, range(10))


map(lambda k: k in mydict, range(10))

I'm not sure which version I prefer.