Re: autoflush on/off

Discussion in 'Python' started by Terry Reedy, Feb 5, 2013.

  1. Terry Reedy

    Terry Reedy Guest

    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
    Terry Reedy, Feb 5, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Herman Chan

    CGI autoflush in Window

    Herman Chan, Oct 9, 2003, in forum: Perl
    Replies:
    0
    Views:
    571
    Herman Chan
    Oct 9, 2003
  2. Jabba Laci

    autoflush on/off

    Jabba Laci, Feb 4, 2013, in forum: Python
    Replies:
    2
    Views:
    96
  3. Terry Reedy

    Re: autoflush on/off

    Terry Reedy, Feb 4, 2013, in forum: Python
    Replies:
    0
    Views:
    89
    Terry Reedy
    Feb 4, 2013
  4. Jabba Laci

    Re: autoflush on/off

    Jabba Laci, Feb 5, 2013, in forum: Python
    Replies:
    2
    Views:
    122
    Piet van Oostrum
    Feb 5, 2013
  5. Lele Gaifax

    Re: autoflush on/off

    Lele Gaifax, Feb 5, 2013, in forum: Python
    Replies:
    0
    Views:
    94
    Lele Gaifax
    Feb 5, 2013
Loading...

Share This Page