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


Groups > comp.lang.python > #38152

Re: autoflush on/off

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jabba.laci@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'context': 0.05; "'w',": 0.09; 'exit.': 0.09; 'stdout': 0.09; 'sys.stderr': 0.09; 'sys.stdout': 0.09; 'terry': 0.09; 'def': 0.10; 'library': 0.15; 'value.': 0.15; 'buffering': 0.16; 'exc_type,': 0.16; 'reedy': 0.16; 'pointed': 0.17; 'jan': 0.18; 'work.': 0.23; 'class.': 0.23; 'idea': 0.24; 'header:In-Reply- To:1': 0.25; 'setting': 0.26; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'subject:/': 0.28; 'types.': 0.29; 'url:mailman': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "skip:' 10": 0.30; 'that.': 0.30; 'error': 0.30; 'url:python': 0.32; 'file': 0.32; 'url:listinfo': 0.32; 'problem': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'received:209.85': 0.35; 'except': 0.36; 'but': 0.36; 'url:org': 0.36; 'method': 0.36; 'should': 0.36; 'does': 0.37; 'drop': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'your': 0.60; 'lost': 0.60; 'skip:u 10': 0.60; 'save': 0.61; 'back': 0.62; 'subject:off': 0.65; 'restore': 0.69; 'saving': 0.72; 'manual,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=+IEMk1JFql+b+PgyjyYOdNs/zO79KqHv95J90jGAT/M=; b=GVL0mlbZOgpMEvxh49Hl679HAGYio8uJM+ZKAMRVMff2FIZCb79GxVb4B+8dtijoCh Q76HVq491rlYGLxR9ku9ko261lkPXsB54Uu2E6BUDnpBWxVR8uuuQ/de1L2oy/MzlBS6 n8AQzBWunCIq1dMeAyC8v2oaUpfgbC2GPmwrn2NnD3D4Y9EJn+FguokjaW5x7iUAxRn8 E4bsrHn3GrGT4SK0LcLgOBTD86fp6uzUT16730Btwr8A8V8Ej/r/d5foYM9QVBONuNVk anH7xx1vamdhEscYKqUda1iiiwYbEM8AWyEGIK2eFzeDBpm7AKvUROCukQvWxCCnNNhB FpMg==
X-Received by 10.60.12.226 with SMTP id b2mr3967273oec.32.1360023008900; Mon, 04 Feb 2013 16:10:08 -0800 (PST)
MIME-Version 1.0
In-Reply-To <kep8as$c12$1@ger.gmane.org>
References <CAOuJsMmQ55vjG_TYU2iPNHeA=b-kGX+cWa2xFDXp4j6WfQAp3Q@mail.gmail.com> <kep8as$c12$1@ger.gmane.org>
From Jabba Laci <jabba.laci@gmail.com>
Date Tue, 5 Feb 2013 01:09:48 +0100
Subject Re: autoflush on/off
To Python mailing list <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1342.1360023018.2939.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360023018 news.xs4all.nl 6978 [2001:888:2000:d::a6]:43068
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38152

Show key headers only | View raw


Hi,

Thanks for the answers. I like the context manager idea but setting
the sys.stdout back to the original value doesn't work.

Example:

class Unbuff(object):
    def __init__(self):
        self.stdout_bak = sys.stdout

    def __enter__(self):
        sys.stdout.flush()
        sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout = self.stdout_bak

####

with Unbuff():
        for i in range(5):
            sys.stdout.write('.')
            sleep(.5)
#
sys.stdout.write('EXIT')    # provokes an error


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

Laszlo


> Write a context manager class. See Library manual, 4.11. Context Manager
> Types. The __enter__ method would be much like the above except that is
> should save the old stdout object 'oldstdout = sys.stdout' instead of
> fiddling with 'autoflush_on'. Then __exit__ would simply be 'sys.stdout =
> oldstdout'. Drop autoflush_on.  Your context manager should not care about
> the existing buffering other than to restore it on exit. Saving and
> restoring the existing stdout object does that.
>
> --
> Terry Jan Reedy
>
> --
> http://mail.python.org/mailman/listinfo/python-list

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


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