user376403
user376403

Reputation: 1175

Force UTF-8 output (mostly when not talking to a tty)

I am doing this:

import sys, codecs
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)

But I know there is something missing. I had a huge collection of code before an HD crash, and my snippet in there had something in it which prevented:

reload(sys)

From undoing the codec changes. Does anyone know what that might have been?

Upvotes: 2

Views: 426

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 882791

Quite apart from UTF8 (and thus entirely apart from your Q's title), reload(sys) does not reopen stdandard input, output, and error files. Try a simpler case to see that:

>>> import sys
>>> print>>sys.stderr,'ciao'
ciao
>>> sys.stderr.close()
>>> print>>sys.stderr,'ciao'
>>> reload(sys)
<module 'sys' (built-in)>
>>> print>>sys.stderr,'ciao'

If you want to restore sys.stdout and/or sys.stderr after rebinding them, just stash away the old references and just rebind them to the "stashed away" refs to restore. (You could use sys.__stdout__ and sys.__stderr__, where Python itself stashes them away at startup, but that might run athwart advanced shells or debuggers you might be running, as the latter may be doing their own similar operations).

If you also want to survive file or file-descriptor closures, you'll have to go deeper (reopening e.g. with os.fdopen the standard descriptors 0, 1, and 2 -- if the descriptors themselves have been closer, that gets even hairier;-) and living peacefully with advanced shells or debuggers in general may become a lost cause. But for simpler cases, just rebinding things back and forth can work fine.

Upvotes: 3

Related Questions