Fix python 2 compat
Make write_to_tempfile accept native strings