Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35268 > unrolled thread

redirect standard output problem

Started byiMath <redstone-cold@163.com>
First post2012-12-20 21:23 -0800
Last post2012-12-21 09:36 -0500
Articles 8 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  redirect standard output problem iMath <redstone-cold@163.com> - 2012-12-20 21:23 -0800
    Re: redirect standard output problem Chris Angelico <rosuav@gmail.com> - 2012-12-21 18:24 +1100
      Re: redirect standard output problem iMath <redstone-cold@163.com> - 2012-12-22 07:07 -0800
        Re: redirect standard output problem Chris Angelico <rosuav@gmail.com> - 2012-12-23 02:15 +1100
        Re: redirect standard output problem Terry Reedy <tjreedy@udel.edu> - 2012-12-22 16:02 -0500
      Re: redirect standard output problem iMath <redstone-cold@163.com> - 2012-12-22 07:07 -0800
    Re: redirect standard output problem Hans Mulder <hansmu@xs4all.nl> - 2012-12-21 14:42 +0100
    Re: redirect standard output problem Dave Angel <d@davea.name> - 2012-12-21 09:36 -0500

#35268 — redirect standard output problem

FromiMath <redstone-cold@163.com>
Date2012-12-20 21:23 -0800
Subjectredirect standard output problem
Message-ID<cf470745-6ee6-464c-b936-154a869af997@googlegroups.com>
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__


print('A')
with open('out.log', mode='w', encoding='utf-8') as a_file, RedirectStdoutTo(a_file):

    print('B')
    print('C')

print(888)

[toc] | [next] | [standalone]


#35275

FromChris Angelico <rosuav@gmail.com>
Date2012-12-21 18:24 +1100
Message-ID<mailman.1141.1356074653.29569.python-list@python.org>
In reply to#35268
On Fri, Dec 21, 2012 at 4:23 PM, iMath <redstone-cold@163.com> wrote:
> redirect standard output problem
>
> why the result only print A but leave out 888 ?

No idea, because when I paste your code into the Python 3.3
interpreter or save it to a file and run it, it does exactly what I
would expect. A and 888 get sent to the screen, B and C go to the
file.

What environment are you working in? Python version, operating system,
any little details that just might help us help you.

ChrisA

[toc] | [prev] | [next] | [standalone]


#35368

FromiMath <redstone-cold@163.com>
Date2012-12-22 07:07 -0800
Message-ID<0d7c7686-8b06-4dc2-8b57-3f22d1ef93ae@googlegroups.com>
In reply to#35275
在 2012年12月21日星期五UTC+8下午3时24分10秒,Chris Angelico写道:
> On Fri, Dec 21, 2012 at 4:23 PM, iMath <redstone-cold@163.com> wrote:
> 
> > redirect standard output problem
> 
> >
> 
> > why the result only print A but leave out 888 ?
> 
> 
> 
> No idea, because when I paste your code into the Python 3.3
> 
> interpreter or save it to a file and run it, it does exactly what I
> 
> would expect. A and 888 get sent to the screen, B and C go to the
> 
> file.
> 
> 
> 
> What environment are you working in? Python version, operating system,
> 
> any little details that just might help us help you.
> 
> 
> 
> ChrisA


 on WinXP + python32  

when I run it through command line ,it works ok ,but when I run it through IDLE , only print A but leave out 888
so why ?

[toc] | [prev] | [next] | [standalone]


#35371

FromChris Angelico <rosuav@gmail.com>
Date2012-12-23 02:15 +1100
Message-ID<mailman.1203.1356189305.29569.python-list@python.org>
In reply to#35368
On Sun, Dec 23, 2012 at 2:07 AM, iMath <redstone-cold@163.com> wrote:
> when I run it through command line ,it works ok ,but when I run it through IDLE , only print A but leave out 888
> so why ?

Because IDLE has to fiddle with stdin/stdout a bit to function. Try
adopting Dave's recommendation - you'll likely find that it then
works.

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600
32 bit (Intel)] on win32
>>> sys.stdout is sys.__stdout__
True

The same in IDLE:
>>> sys.stdout is sys.__stdout__
False

Whenever you work in IDLE, expect that some things will be slightly
different, mostly to do with standard I/O streams. So when you post
issues that you've come across, please state that you're using IDLE -
it likely makes a difference!

ChrisA

[toc] | [prev] | [next] | [standalone]


#35389

FromTerry Reedy <tjreedy@udel.edu>
Date2012-12-22 16:02 -0500
Message-ID<mailman.1212.1356210160.29569.python-list@python.org>
In reply to#35368
On 12/22/2012 10:15 AM, Chris Angelico wrote:
> On Sun, Dec 23, 2012 at 2:07 AM, iMath <redstone-cold@163.com> wrote:
>> when I run it through command line ,it works ok ,but when I run it through IDLE , only print A but leave out 888
>> so why ?
>
> Because IDLE has to fiddle with stdin/stdout a bit to function. Try
> adopting Dave's recommendation - you'll likely find that it then
> works.
>
> Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600
> 32 bit (Intel)] on win32
>>>> sys.stdout is sys.__stdout__
> True
>
> The same in IDLE:
>>>> sys.stdout is sys.__stdout__
> False

In particular, on Windows,
 >>> import sys
 >>> sys.stdout
<idlelib.run._RPCOutputFile object at 0x0000000002D852E8>
 >>> sys.__stdout__
 >>>

In other words, sys.__stdout__ is None!

To explain: IDLE runs in two processes connected by sockets. There is 
one process for user code and one for the gui. The user code process 
should be invisible as the gui process handles all user interaction. On 
Windows, this mean a process with no window and no input/output, which 
is what the pythonw (windowless) exe is for. (I believe IDLE on *nix 
uses an invisible window instead.) In any case, sys.stdout is set to a 
remote procedure call object that sends output to the socket connection. 
The gui process reads the socket and call the tkinter method to all text 
to a tk text box.

> Whenever you work in IDLE, expect that some things will be slightly
> different, mostly to do with standard I/O streams. So when you post
> issues that you've come across, please state that you're using IDLE -
> it likely makes a difference!

Indeed! Especially for i/o issues, retest in the command window;-).

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#35369

FromiMath <redstone-cold@163.com>
Date2012-12-22 07:07 -0800
Message-ID<mailman.1202.1356188844.29569.python-list@python.org>
In reply to#35275
在 2012年12月21日星期五UTC+8下午3时24分10秒,Chris Angelico写道:
> On Fri, Dec 21, 2012 at 4:23 PM, iMath <redstone-cold@163.com> wrote:
> 
> > redirect standard output problem
> 
> >
> 
> > why the result only print A but leave out 888 ?
> 
> 
> 
> No idea, because when I paste your code into the Python 3.3
> 
> interpreter or save it to a file and run it, it does exactly what I
> 
> would expect. A and 888 get sent to the screen, B and C go to the
> 
> file.
> 
> 
> 
> What environment are you working in? Python version, operating system,
> 
> any little details that just might help us help you.
> 
> 
> 
> ChrisA


 on WinXP + python32  

when I run it through command line ,it works ok ,but when I run it through IDLE , only print A but leave out 888
so why ?

[toc] | [prev] | [next] | [standalone]


#35290

FromHans Mulder <hansmu@xs4all.nl>
Date2012-12-21 14:42 +0100
Message-ID<50d46731$0$6883$e4fe514c@news2.news.xs4all.nl>
In reply to#35268
On 21/12/12 06:23:18, 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__
> 
> 
> print('A')
> with open('out.log', mode='w', encoding='utf-8') as a_file, RedirectStdoutTo(a_file):
> 
>     print('B')
>     print('C')
> 
> print(888)

On my machine it works as you'd expect.

If it doesn't work on your system, it might help to flush
sys.stdout in a few strategic places:

class RedirectStdoutTo:

    def __init__(self, out_new):
        self.out_new = out_new
    def __enter__(self):
        sys.stdout.flush()
        sys.stdout = self.out_new
    def __exit__(self, *args):
        sys.stdout.flush()
        sys.stdout = sys.__stdout__


print('A')
with open('out.log', mode='w', encoding='utf-8') as a_file,
RedirectStdoutTo(a_file):

    print('B')
    print('C')

print(888)
sys.stdout.flush()


Hope this helps,

-- HansM

[toc] | [prev] | [next] | [standalone]


#35293

FromDave Angel <d@davea.name>
Date2012-12-21 09:36 -0500
Message-ID<mailman.1150.1356100624.29569.python-list@python.org>
In reply to#35268
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web