Python 3.0 added a nice bucketload of new features that simply have no syntactical equivalent in Python 2.5, (See Appendix for a list) (table coming soon!). Up to this point, I have not touched anything that requires more than a syntactic change. However, this is an important topic, because developers using 3.0 are encouraged to use all of its new features and syntax, and the goal for any code refactoring tool is (or should be!) to work universally for all cases. (See reasoning why fix_memoryview is required explicitly, even though not using the fixer would result in a SyntaxError in 2.5/2.6 if a memoryview is encountered) Thus, I will address the feature issue here.
This blog post applies to all features present in any py3k branch of Python that have no backwards-compatible syntactic equivalent (“BCSE”), or an equivalent representation using syntax that already existed in Python 2.5 without an unreasonable amount of helper code, e.g., that recreates data types that are built-in to py3k (said features herein named “New Features”). The end-game goal for lib3to2 is to make all code using any built-in functionality of all py3k branches work on python2.5 with minimal (or no) user intervention when 3to2-ified. Obviously, this is a very difficult goal to achieve, given that new features added in py3k are always being added, and there is already enough to occupy a huge chunk of my time, projecting out towards Christmas 2009. For the purposes of having a pretty good tool by the end of GSoC 2009, my goal is to make 3to2 refactor syntax where there is a BCSE, and (case-by-case) raise warnings that tell the user their options. An example warning would look like this (using “memoryview” as the sample case):
Warning: memoryview does not have an exact syntactic equivalent in Python 2.5, and the fixer for memoryview -> buffer is disabled by default. You may either enable the “memoryview” fixer and accept the consequences of using buffer (which only allows a subset of the operations available to memoryview), manually refactor your code to avoid using memoryview, or ignore this message. Ignoring this message limits your code to work on the following Python version (at minimum): Python 2.7
That’s obviously rather verbose. A short version might look like:
Warning: memoryview encountered, fixer disabled. Enable fixer, manually refactor, or ignore (2.7 minimum required).
Comments on this blog post are welcome: I welcome and request alternatives to this structure.
Appendix
Features, comma-delimited, in the format feature(minimum python version supported): bin (2.6), str.format (2.6), multiprocessing (“2.5″)*, io (2.6), abc (2.6), numbers (2.6), memoryview (2.7). 3.1 New Features not included.
* pypi has backports for the multiprocessing package back to 2.4, but it is not built in, so the syntax would require some additional fluff to make it work, which is a future goal, not for now.