On Fri, Feb 15, 2008 at 10:24 AM, Guido van Rossum <[EMAIL PROTECTED]> wrote: > On Thu, Feb 14, 2008 at 5:02 PM, <[EMAIL PROTECTED]> wrote: > > Guido> You can hack around this for now by doing (before printing > > Guido> anything) > > > > Guido> sys.stdout._encoding = 'utf-8' > > > > Guido> but that ought to be a temporary hack until we've figured out > the > > Guido> right way to set it. > > > > How about being able to reopen() an open file object, e.g.: > > > > print(..., file=sys.stdout.reopen(encoding="utf-8") > > > > ? > > I think you're on to something. There's an API to access the > underlying (buffered) binary stream: sys.stdout.buffer. It's possible > to create a new TextIOWrapper on top of that: > > sys.stdout = io.TextIOWrapper(sys.stdout.buffer, "utf-8") > > The only problem is that if the original TextIOWrapper is deleted or > closed, its close() method will close the underlying buffer object, > rendering sys.stdout useless. (Well, actually, the lowest-level raw > I/O object refuses to be closed if the file descriptor is 0, 1 or 2, > but is issues a warning and this is a close call. It would be better > if TextIOWrapper didn't explicitly close() when deleted. That requires > some redesign of the interaction between __del__() and close() in the > IOBase class. Maybe __del__() should just flush() instead of close()?
That'd rely on prompt deletion for correct behaviour. Why not an explicit detach method? buf = sys.stdout.buffer sys.stdout.detach() sys.stdout = io.TextIOWrapper(buf, "utf-8") -- Adam Olsen, aka Rhamphoryncus _______________________________________________ Python-3000 mailing list Python-3000@python.org http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com