Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: autoflush on/off

Reply
Thread Tools

Re: autoflush on/off

 
 
Jabba Laci
Guest
Posts: n/a
 
      02-05-2013
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

 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      02-05-2013
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

 
Reply With Quote
 
 
 
 
Piet van Oostrum
Guest
Posts: n/a
 
      02-05-2013
Ulrich Eckhardt <(E-Mail Removed)> 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 <(E-Mail Removed)>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with autoflush on Semaphore-threads for Windows dede Perl 0 07-28-2004 03:48 PM
Autoflush in python cgi Gianluca Trombetta Python 1 06-02-2004 01:13 AM
Autoflush for DB_FILE to share a hash among cooperating processes? Axel Boldt Perl Misc 1 04-07-2004 03:33 PM
Yet Another Autoflush problem -- What's wrong with this code? John Chambers Perl Misc 2 01-23-2004 08:54 PM
CGI autoflush in Window Herman Chan Perl 0 10-09-2003 05:08 PM



Advertisments