Fix a unicode-related crash in output buffering (-B) on Python 2.6

Python 2.6 does not honour the encoding attribute of sys.std{out|err},
when calling .write(), causing it to fail with a UnicodeEncodeError
if a unicode object containing non-ASCII characters is passed in.

See http://bugs.python.org/issue4947 for more details.

This patch works around the problem by always encoding according to
.encoding, optionally falling back to UTF-8 if it is None.