Backport of 0ed7d15563 from master
0ed7d15563
Should be unneeded with Python 2.7 and up. Added some unicode_literals along the way.