[Python-Dev] PEP 340 -- concept clarification

Nick Coghlan ncoghlan at gmail.com
Wed May 4 12:25:32 CEST 2005


James Y Knight wrote:
> On May 3, 2005, at 12:53 PM, Guido van Rossum wrote:
> 
>>def saving_stdout(f):
>>    save_stdout = sys.stdout
>>    try:
>>        sys.stdout = f
>>        yield
>>    finally:
>>        sys.stdout = save_stdout
> 
> 
> I hope you aren't going to be using that in any threaded program. 

sys.stdout is a global - threading issues are inherent in monkeying with it. At 
least this approach allows all code that redirects stdout to be easily serialised:

def redirect_stdout(f, the_lock=Lock()):
     locking(the_lock):
         save_stdout = sys.stdout
         try:
             sys.stdout = f
             yield
         finally:
             sys.stdout = save_stdout

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net


More information about the Python-Dev mailing list