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