#!/usr/bin/env python

import sys

sys.stdout.write(b"a line with bad encoding: \xc2.")
sys.stdout.flush()

#!/usr/bin/env python

import sys

getattr(sys.stdout, "buffer", sys.stdout).write(b"a line with bad encoding: \xc2.")
sys.stdout.flush()

Make write-bad-encoding.py py3-aware.