Bob Ippolito (@etrepum) on Haskell, Python, Erlang, JavaScript, etc.
«

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 site.py: 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:

/Library/Python/2.3/2.3_FIXES.pth

import _2_3_FIXES

/Library/Python/2.3/_2_3_FIXES.py

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 _strptime.py from 2.3.3 right away ;)