Thanks to Collin Anderson for the suggestion and Tim Graham for reviewing the patch.
Should be unneeded with Python 2.7 and up. Added some unicode_literals along the way.