Python 3 Compatibility

Needed Python 3 support for a project. Not sure when the canonical branch will add support but the code is here if it's useful.