Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98180

Re: Unbuffered stderr in Python 3

From George Trojan <george.trojan@noaa.gov>
Newsgroups comp.lang.python
Subject Re: Unbuffered stderr in Python 3
Date 2015-11-03 18:03 +0000
Message-ID <mailman.41.1446573837.8789.python-list@python.org> (permalink)
References <mailman.30.1446570002.25765.python-list@python.org>

Show all headers | View raw


On 11/03/2015 05:00 PM, python-list-request@python.org wrote:
> On Mon, 02 Nov 2015 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.
>
>
This does set line buffering, but does not change the behaviour:

(devenv-3.4.1) dilbert@gtrojan> cat x.py
import sys
import time
if sys.version>'3':
     import io
     sys.stderr = io.TextIOWrapper(sys.stderr.detach().detach())
     #sys.stderr = io.TextIOWrapper(sys.stderr.buffer.raw)
     print(sys.stderr.line_buffering)
sys.stderr.write('a')
time.sleep(10)

This is python2.7.5. a is printed before ^C.

(devenv-3.4.1) dilbert@gtrojan> /bin/python x.py
a^CTraceback (most recent call last):

Here buffer is flushed on close, after typing ^C.

(devenv-3.4.1) dilbert@gtrojan> python x.py
False
^CaTraceback (most recent call last):

George

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Unbuffered stderr in Python 3 George Trojan <george.trojan@noaa.gov> - 2015-11-03 18:03 +0000

csiph-web