Use compatibility layer for python 3.

This is a pretty blind python 3 compatibility conversion that
may well have completely missed the point of any or all of the
code it touches, but does seem to make the tests pass. :)