[Python-ideas] Replacing the standard IO streams (was Re: changing sys.stdout encoding)

Nick Coghlan ncoghlan at gmail.com
Sat Jun 9 11:55:09 CEST 2012


So, after much digging, it appears the *right* way to replace a
standard stream in Python 3 after application start is to do the
following:

    sys.stdin = open(sys.stdin.fileno(), 'r', <new settings>)
    sys.stdout = open(sys.stdout.fileno(), 'w', <new settings>)
    sys.stderr = open(sys.stderr.fileno(), 'w', <new settings>)

Ditto for the other standard streams. It seems it already *is* as
simple as with any other file, we just collectively forgot about:

1. The fact open() accepts file descriptors directly in Python 3
2. The fact that text streams still report the underlying file
descriptor correctly

*That* is something we can happily advertise in the standard library
docs. If you could check to make sure it works properly for your use
case and then file a docs bug at bugs.python.org to get it added to
the std streams documentation, that would be very helpful.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list