cherry pick python 2.6 compatibility fix
Improve the previous 2.6 fix: use bytes rather, than string.
Also, optimize for better speed.