Montag, 12. September 2011

Python and Linux kernel 3.0: sys.platform != 'linux2'

It's getting more and more challenging to compile Python. Half a year ago Python 2.x's build system broke caused by multiarch support in Ubuntu Natty. Now Linux kernel 3.0 is going to reveal yet another issue in Python's configure script.

If you compile Python under kernel 3.0, sys.platform changes to 'linux3'. The altered platform string introduces bugs in several libraries and in our softwares stack, too. We and a lot of other people check for Linux with sys.platform == "linux2".
>>> import sys
>>> sys.platform
'linux3'
It turns out the 'configure' script causes the problem. It takes the lower case kernel name (uname -s) and first digit of the kernel release (uname -r) to fill the variable MACHDEP. The issue is discussed in http://bugs.python.org/issue12326 to a create length and addressed in upcoming releases 2.7.3, 3.2.2 and 3.3. The 2.7 and 3.2 series announce a 3.0 Linux kernel as linux2 platform. Starting with Python 3.3 sys.platform will be 'linux' for Kernel 2.x and 3.x.

However 2.7.3 isn't out yet. Worse older versions of Python are in maintenance mode and will only see security fixes. I'm going to show you, how you can work around the issue.

Change your software

I recommend that you replace all code like sys.platform == "linux2" with sys.platform.startswith("linux"). It causes the least trouble and is future compatible with Python 3.3 as well.

Alter the configure script

If you compile your own version of Python on Linux, you can alter the configure script before running it.

       case $MACHDEP in
cygwin*) MACHDEP="cygwin";;
darwin*) MACHDEP="darwin";;
atheos*) MACHDEP="atheos";;
irix646) MACHDEP="irix6";;
linux*) MACHDEP="linux2";; # add this line
'') MACHDEP="unknown";;
esac

Run make with MACHDEP=linux2

I find it easier to run make a different MACHDEP variable. It requires no patching.
    ./configure
make MACHDEP=linux2
make altinstall
Good luck!