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


Groups > comp.lang.python > #38168

Re: autoflush on/off

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'context': 0.05; 'method.': 0.05; '*args,': 0.07; 'sys,': 0.07; 'valueerror:': 0.07; "'w',": 0.09; '**kwargs)': 0.09; '__init__': 0.09; 'descriptor': 0.09; 'interpreter,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sys.stderr': 0.09; 'sys.stdout': 0.09; 'terry': 0.09; 'def': 0.10; 'value.': 0.15; '3.3,': 0.16; 'exc_type,': 0.16; 'fileno': 0.16; 'idle,': 0.16; 'idle.': 0.16; 'oserror:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'pointed': 0.17; 'string,': 0.17; 'creates': 0.18; 'jan': 0.18; '>>>': 0.18; 'module': 0.19; 'parameters': 0.20; 'skip:" 30': 0.20; 'import': 0.21; '"",': 0.22; 'work.': 0.23; 'idea': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'setting': 0.26; 'skip:" 20': 0.26; 'regular': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'subject:/': 0.28; 'fine': 0.28; 'i/o': 0.29; 'sleep': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'mode': 0.30; 'function': 0.30; 'error': 0.30; 'file': 0.32; 'problem': 0.33; 'to:addr:python- list': 0.33; 'hi,': 0.33; "can't": 0.34; 'text': 0.34; 'thanks': 0.34; 'pm,': 0.35; 'something': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'should': 0.36; 'bad': 0.37; 'does': 0.37; 'level': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'lost': 0.60; 'skip:u 10': 0.60; 'back': 0.62; 'subject:off': 0.65; 'want,': 0.65; 'received:fios.verizon.net': 0.84; 'wish.': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: autoflush on/off
Date Tue, 05 Feb 2013 01:43:46 -0500
References <CAOuJsMmQ55vjG_TYU2iPNHeA=b-kGX+cWa2xFDXp4j6WfQAp3Q@mail.gmail.com> <kep8as$c12$1@ger.gmane.org> <CAOuJsM=Xv-VH_Jr0jh-1ysnbFAAn8WL+dNd+23Qh+6yZXgO-cQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0
In-Reply-To <CAOuJsM=Xv-VH_Jr0jh-1ysnbFAAn8WL+dNd+23Qh+6yZXgO-cQ@mail.gmail.com>
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.1353.1360046662.2939.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1360046662 news.xs4all.nl 6915 [2001:888:2000:d::a6]:35083
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:38168

Show key headers only | View raw


On 2/4/2013 7:09 PM, Jabba Laci wrote:
> 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

This could/should go in the __enter__ method. You do not need __init__ here.

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

This creates a new *python* object but perhaps it does something at the 
OS level that does not get reversed.

>      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)

from the time module :-?
> #
> 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

In 3.3, Win7, with the regular interpreter, I get
   File "<stdin>", line 5, in __enter__
   File "C:\Programs\Python33\lib\os.py", line 1032, in fdopen
     return io.open(fd, *args, **kwargs)
ValueError: can't have unbuffered text I/O

If I change the mode to 'wb', I get
   File "C:\Programs\Python33\lib\os.py", line 1032, in fdopen
     return io.open(fd, *args, **kwargs)
OSError: [Errno 9] Bad file descriptor

With IDLE, I get
   File "F:\Python\mypy\tem.py", line 7, in __enter__
     sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
AttributeError: fileno

It looks like you should perhaps just forget about reopening and just 
use sys.stdout.flush(). This works fine even on IDLE.

import sys, time

for i in range(10):
     sys.stdout.write('.'); sys.stdout.flush()
     time.sleep(.5)
 >>>
,,,,,,,,,,

Write a write_flush function if you want, and add any of the number, 
string, and sleep time as parameters if you wish.

-- 
Terry Jan Reedy

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


Thread

Re: autoflush on/off Terry Reedy <tjreedy@udel.edu> - 2013-02-05 01:43 -0500

csiph-web