Nick Coghlan
2012-06-09 09:55:09 UTC
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.
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-***@public.gmane.org | Brisbane, Australia
Nick Coghlan | ncoghlan-***@public.gmane.org | Brisbane, Australia