Move Python 2.x / 3.x compatibility functions to separate module