COMPAT: Backport python 3 compatibility fixes.

Cherry-picked from #1057. Not positive this is the best way to do this. Might want to do a local merge first and check it out.