Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #38183
| From | Piet van Oostrum <piet@vanoostrum.org> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: autoflush on/off |
| Date | 2013-02-05 12:59 +0100 |
| Message-ID | <m2zjzjas3o.fsf@cochabamba.vanoostrum.org> (permalink) |
| References | <CAOuJsMmQ55vjG_TYU2iPNHeA=b-kGX+cWa2xFDXp4j6WfQAp3Q@mail.gmail.com> <kep8as$c12$1@ger.gmane.org> <mailman.1342.1360023018.2939.python-list@python.org> <a1r6u9-78o.ln1@satorlaser.homedns.org> |
Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> writes:
> Am 05.02.2013 01:09, schrieb Jabba Laci:
>> setting the sys.stdout back to the original value doesn't work.
> [...]
>> The problem is in __exit__ when sys.stdout is pointed to the old
>> value. sys.stdout.write doesn't work from then on. Output:
>>
>> .....close failed in file object destructor:
>> sys.excepthook is missing
>> lost sys.stderr
>
> Argh! Yes, the newly-created file object takes ownership of the
> filedescriptor. Once done with it, it invokes close() on it, making it
> unusable for the original sys.stdout.
That can be solved with a dup.
import os, sys
class Unbuff(object):
def __enter__(self):
sys.stdout.flush()
self.stdout_bak = sys.stdout
sys.stdout = os.fdopen(os.dup(sys.stdout.fileno()), 'w', 0)
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout = self.stdout_bak
--
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: autoflush on/off Jabba Laci <jabba.laci@gmail.com> - 2013-02-05 01:09 +0100
Re: autoflush on/off Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2013-02-05 09:04 +0100
Re: autoflush on/off Piet van Oostrum <piet@vanoostrum.org> - 2013-02-05 12:59 +0100
csiph-web