Use BytesIO rather than StringIO; support python3