Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Nobody Newsgroups: comp.lang.python Subject: Re: Unbuffered stderr in Python 3 Date: Tue, 03 Nov 2015 07:10:58 +0000 Organization: A noiseless patient Spider Lines: 32 Message-ID: References: <5637165b$0$1505$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="152d4dee6f95183c956ee6fa63d7e69f"; logging-data="21757"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mKCW5mYk5UX9T/2bcobH5" User-Agent: Pan/0.14.2 (This is not a psychotic episode. It's a cleansing moment of clarity.) Cancel-Lock: sha1:mhn5APOiOThJYqpJ2lIUc0k8Gq4= Xref: csiph.com comp.lang.python:98135 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) > type(sys.stderr.buffer) > type(sys.stderr.buffer.raw) 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.