Re: autoflush on/off

Discussion in 'Python' started by Jabba Laci, Feb 5, 2013.

  1. Jabba Laci

    Jabba Laci Guest

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

  2. Am 05.02.2013 01:09, schrieb Jabba Laci:
    > I like the context manager idea


    There is a helper library for constructing context managers, see
    http://docs.python.org/2/library/contextlib.html. That would have made
    your code even shorter.



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

    Okay, other approach: I believe that the only function regularly called
    on sys.stdout is write(). Just write a replacement that forwards the
    data to the original, followed by a call to flush. If you are ambitious,
    forward any other call to sys.stdout directly by catching attribute
    lookup (__getattribute__) in your class.

    Good luck!

    Uli
     
    Ulrich Eckhardt, Feb 5, 2013
    #2
    1. Advertising

  3. Ulrich Eckhardt <> 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 <>
    WWW: http://pietvanoostrum.com/
    PGP key: [8DAE142BE17999C4]
     
    Piet van Oostrum, Feb 5, 2013
    #3
    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:
    588
    Herman Chan
    Oct 9, 2003
  2. Jabba Laci

    autoflush on/off

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

    Re: autoflush on/off

    Terry Reedy, Feb 4, 2013, in forum: Python
    Replies:
    0
    Views:
    103
    Terry Reedy
    Feb 4, 2013
  4. Terry Reedy

    Re: autoflush on/off

    Terry Reedy, Feb 5, 2013, in forum: Python
    Replies:
    0
    Views:
    140
    Terry Reedy
    Feb 5, 2013
  5. Lele Gaifax

    Re: autoflush on/off

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

Share This Page