Unbuffered stderr in Python 3

Nobody nobody at nowhere.invalid
Tue Nov 3 02:10:58 EST 2015


On 2015年11月02日 18:52:55 +1100, Steven D'Aprano wrote:
> In Python 2, stderr is unbuffered.
>> In most other environments (the shell, C...) stderr is unbuffered.
>> It is usually considered a bad, bad thing for stderr to be buffered. What
> happens if your application is killed before the buffer fills up? The
> errors in the buffer will be lost.
>> So how come Python 3 has line buffered stderr? And more importantly, how
> can I turn buffering off?

It's probably related to the fact that std{in,out,err} are Unicode
streams. 
	> type(sys.stderr)
	<class '_io.TextIOWrapper'>
	> type(sys.stderr.buffer)
	<class '_io.BufferedWriter'>
	> type(sys.stderr.buffer.raw)
	<class '_io.FileIO'>
It appears that you can turn it off with:
	sys.stderr = io.TextIOWrapper(sys.stderr.buffer.raw)
or:
	sys.stderr = io.TextIOWrapper(sys.stderr.detach().detach())
This results in a sys.stderr which appears to work and whose
.line_buffering property is False.


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /