fix writing to sys.stdout on Python 2