Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'broken': 0.03; 'value,': 0.03; 'explicitly': 0.04; 'output': 0.04; 'sys': 0.05; "(i'd": 0.09; '*args):': 0.09; 'stdout': 0.09; 'sys.stdout': 0.09; 'def': 0.10; 'stack': 0.15; 'flush()': 0.16; 'wrote:': 0.17; 'replacing': 0.17; '(or': 0.18; 'preferred': 0.20; 'import': 0.21; 'object.': 0.22; 'subject:problem': 0.22; "i'd": 0.22; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'leave': 0.26; 'am,': 0.27; 'actual': 0.28; 'quoting': 0.29; 'objects': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'function': 0.30; 'url:python': 0.32; 'file': 0.32; 'print': 0.32; 'problem': 0.33; 'to:addr:python- list': 0.33; 'saved': 0.35; 'similar': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'why': 0.37; 'previous': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'some': 0.38; 'url:docs': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'think': 0.40; 'your': 0.60; 'further': 0.61; 'save': 0.61; 'solve': 0.62; 'worth': 0.63; 'header:Reply-To:1': 0.68; 'restore': 0.69; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; "it'd": 0.84; 'technique.': 0.84; 'comment.': 0.91; 'reasoning': 0.91 Date: Fri, 21 Dec 2012 09:36:44 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: redirect standard output problem References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:qfz5apFNUR781rPON3doG43/1M2+F2F/VkAYCseXppK QKV15XMkgmbqiWbiJdbJf0RXwtcdgYA/JLpK1qCmsOZ+bSUdrz n9TVB8Kd8avY9+vMnq4/4KTrGltFQ4sK7ukKbyWZVIT8aTPplz ylTh/DnMQQe37LFT6piF716PyDd67t0MsU2fg2pA1j9C+EP8XI AeqH1Nbr847WA3HdQEbEiXbuPiAXgbXMIl7wqSKr+DBq/NDQA3 /zNv64HS+4A06ZeXMh7dKJrCEG9SQUVDRw7XTGLjnzexRua2Jo UFhL06rLPYAcFq/ZC5MDfoss9XEI8vXlH3pBDg5w3hliRhonQ= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356100624 news.xs4all.nl 6867 [2001:888:2000:d::a6]:39042 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35293 On 12/21/2012 12:23 AM, iMath wrote: > redirect standard output problem > > why the result only print A but leave out 888 ? > > import sys > class RedirectStdoutTo: > > def __init__(self, out_new): > self.out_new = out_new > def __enter__(self): > sys.stdout = self.out_new > def __exit__(self, *args): > sys.stdout = sys.__stdout__ Just a comment. It'd be better to save and restore the stdout value, rather than restoring it to what it was at begin of process. Quoting from http://docs.python.org/3.3/library/sys.html?highlight=__stdout__#sys.__stdout__ It can also be used to restore the actual files to known working file objects in case they have been overwritten with a broken object. However, the preferred way to do this is to explicitly save the previous stream before replacing it, and restore the saved object. My reasoning is that some function further up the stack may also be using a similar (or identical) redirection technique. Anyway, instead of using __stdout__, I'd use a saved value inside your object. def __init__(self, out_new): self.out_new = out_new def __enter__(self): self.savedstate = sys.stdout sys.stdout = self.out_new def __exit__(self, *args): sys.stdout = self.savedstate This may or may not solve your problem (I'd suspect that flush() is the right answer), but I still think it's worth doing. -- DaveA