from __future__ import * 21.2.2004


How to patch Python's standard library without touching vendor files

I've been using 2.3.0 for quite some time because it's the version Apple provides with OS X 10.3. This works out just fine most of the time, even though 2.3.3 is the latest version. However, it's definitely possible to hit a nasty stardard library bug or performance issue that has already been fixed.

For example, the _strptime module in 2.3.0 didn't cache locale settings. This misfeature makes it (in my experience) about 30 times slower than it should be (~20 dates/sec here); completely unacceptable.

Short of using PYTHONPATH (a total kludge, IMHO), it's not obvious how one would go about overriding (without overwriting) bits of the standard library; site-packages and any pth files are appended to sys.path long afterwards. Fortunately, someone already thought of this and placed an obscure feature into pth files can import arbitrary python code!

So now all you have to do is create a folder for standard library fixes, a python module to inject it into sys.path, and a pth file to make that happen whenever you start an interpreter. Here's mine:


import _2_3_FIXES


import sys
sys.path.insert(0, "/Library/Python/2.3_FIXES")

Now create /Library/Python/2.3_FIXES and drop all the standard library updates you need right in there. I suggest fetching from 2.3.3 right away ;)

posted at 21:29:20    #    comment []    trackback []
2 3 4 5 6 7 8
Jan Mar

Bob's Rants

XML-Image Letterimage

© 2004, Bob Ippolito