Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Backporting from Python 2.3 to Python 2.2

We have a home-grown templating system at work, which I intend to dedicate an entry to some time in the future. We originally wrote it in Python 2.2, but upgraded to Python 2.3 a while ago and have since been evolving our code in that environment. Today I found a need to load the most recent version of our templating system on to a small, long neglected application that had been running the original version ever since it had enough features to be usable.

Unfortunately, this application was running on a server that only had Python 2.2. Installing Python 2.3 would have been somewhat more painful here than on other servers we run for reasons I won’t go in to, so I decided to have a go at getting our current code to run under the older Python version.

In the end, I only had to make three minor changes, all at the top of the file in question.

  1. I added from __future__ import generators as the very first line of the file. We use generators (with the yield statement) in a few places—this feature was only properly added in Python 2.3, but was made available in Python 2.2 as a “future enhancement” through the aforementioned obscure import.

  2. I added True, False = 1, 0 on the next line down. Surprisingly, Python 2.2 had no support for a boolean type and instead used a test for non-zero. The above line defines constants that behave enough like Python 2.3’s True and False to avoid any problems.

  3. I defined an enumerate function, which was introduced for real in Python 2.3. Here’s the code I used:

    
    def enumerate(obj):
        for i, item in zip(range(len(obj)), obj):
            yield i, item 
    

All in all it only took around ten minutes to put the above together, after which the script worked just fine. It was interesting to see how our code had grown to rely on Python 2.3 features without us realising it.

Update: Check this entry’s comments for improvements to the above code snippets.

This is Backporting from Python 2.3 to Python 2.2 by Simon Willison, posted on 9th June 2004.

View blog reactions

Next: Embracing Best Practice

Previous: OS X Tip: Remapping keyboard shortcuts

4 comments

  1. Didn't Python 2.2.1 add True and False as builtins (but no bool type, of course)? I know it wasn't in 2.2.0, but I think they added them shortly thereafter.

    Your enumerate isn't strictly correct. The iterable protocol doesn't imply len, so you should simply maintain your own index (a la i += 1) of the objects. I'd paste code, but I hate your comment system.

    Bob Ippolito - 9th June 2004 13:00 - #

  2. Bob: you're absolutely right on the enumerate thing. I was just trying to get a small script working and the incorrect code I knocked out worked fine, but my enumerate function wouldn't work with "real" iterators as opposed to lists. I guess a more correct implementation would be:

    
    def enumerate(obj):
        i = -1
        for item in obj:
            i += 1
            yield i, item
    

    Simon Willison - 9th June 2004 15:27 - #

  3. It would be slightly better to use this:
    True, False = 1==1, 0==1
    
    This is more forward compatible, as in Python 2.3 True and False will still be properly bound (since 1==1 returns True in Python 2.3, and 1 in Python 2.2).

    Ian Bicking - 9th June 2004 18:13 - #

  4. Better yet would be:
    try:
        True, False
    except NameError:
        True, False = 1, 0
    
    True and False were added in a bugfix release of 2.2 :-(

    Anthony Baxter - 16th June 2004 10:03 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2004/06/09/backporting

A django site