redirecting sys.stdout in wx.py.shell.Shell(...)?

Hi all,
I'd like to ask about redirecting the output in the wxpython shell. It
seems, its own proxy is somehow protected and cannot be replaced.

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import sys
sys.stdout

<wx.py.pseudo.PseudoFileOut instance at 0x03637D00>

from cStringIO import StringIO
old_stdout = sys.stdout
sys.stdout = my_stdout = StringIO()
sys.stdout

<wx.py.pseudo.PseudoFileOut instance at 0x03637D00>

old_stdout == sys.stdout

True

my_stdout

<cStringIO.StringO object at 0x03635F80>

my_stdout == sys.stdout

False

(py 2.7.2, wxPython 2.9.3.1, WinXP)

E.g. in tkinter python shell - Idle - the above works, one can replace
stdout from within the shell.

Is there some way to achieve this in the wx Shell?

Thanks in advance,
     Vlastimil Brom

Hi,

···

On Wednesday, July 11, 2012 7:27:59 AM UTC-5, vbr wrote:

Hi all,

I’d like to ask about redirecting the output in the wxpython shell. It

seems, its own proxy is somehow protected and cannot be replaced.

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit

(Intel)] on win32

Type “help”, “copyright”, “credits” or “license” for more information.

import sys

sys.stdout

<wx.py.pseudo.PseudoFileOut instance at 0x03637D00>

from cStringIO import StringIO

old_stdout = sys.stdout

sys.stdout = my_stdout = StringIO()

sys.stdout

<wx.py.pseudo.PseudoFileOut instance at 0x03637D00>

old_stdout == sys.stdout

True

my_stdout

<cStringIO.StringO object at 0x03635F80>

my_stdout == sys.stdout

False

(py 2.7.2, wxPython 2.9.3.1, WinXP)

E.g. in tkinter python shell - Idle - the above works, one can replace

stdout from within the shell.

Is there some way to achieve this in the wx Shell?

Thanks in advance,

 Vlastimil Brom

When you create the wx.App object, you can pass it True instead of False and it will redirect automatically. Or you can do your own custom thing, like in the example in my tutorial:

  • Mike

Hi,

Hi all,
I'd like to ask about redirecting the output in the wxpython shell. It
seems, its own proxy is somehow protected and cannot be replaced.

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout
<wx.py.pseudo.PseudoFileOut instance at 0x03637D00>
>>> from cStringIO import StringIO
>>> old_stdout = sys.stdout
>>> sys.stdout = my_stdout = StringIO()
>>> sys.stdout
<wx.py.pseudo.PseudoFileOut instance at 0x03637D00>
>>> old_stdout == sys.stdout
True
>>> my_stdout
<cStringIO.StringO object at 0x03635F80>
>>> my_stdout == sys.stdout
False
>>>

(py 2.7.2, wxPython 2.9.3.1, WinXP)

E.g. in tkinter python shell - Idle - the above works, one can replace
stdout from within the shell.

Is there some way to achieve this in the wx Shell?

Thanks in advance,
     Vlastimil Brom

When you create the wx.App object, you can pass it True instead of False and
it will redirect automatically. Or you can do your own custom thing, like in
the example in my tutorial:

wxPython - Redirecting stdout / stderr - Mouse Vs Python

- Mike

Hi,
thanks for the info and the reference link,
I am using a similar redirecting class,
however, I'd like to know, how to activate it from within the wxpython
shell (if possible?), as there seem to be some
specialcasing/protection:
sys.stdout = my_stdout = StringIO()
doesn't seem to have any effect, the stdout remains unchanged.

sys.stdout

<wx.py.pseudo.PseudoFileOut instance at 0x03637D00>

Thanks and regards,
    vbr

···

2012/7/11 Mike Driscoll <kyosohma@gmail.com>:

On Wednesday, July 11, 2012 7:27:59 AM UTC-5, vbr wrote:

Correct. It is setting it to the PseudoFile every time a line or block of code is executed and then being reset back to the original sys values after that is done. However it looks like it was intended to not do the restore if the sys.std* values were changed while the code is executing, (see various comments in wx.py.interpreter) but it appears that something else is still changing them back. There is also the redirectStdout and etc. functions that are available via the shell object, (see wx.py.shell) but they do not seem to be working as expected either.

If you can dig in and solve these issues and provide a patch it would be appreciated.

···

On 7/11/12 5:27 AM, Vlastimil Brom wrote:

Hi all,
I'd like to ask about redirecting the output in the wxpython shell. It
seems, its own proxy is somehow protected and cannot be replaced.

--
Robin Dunn
Software Craftsman

Thanks for the hints, Robin!
I somehow manage to make stdout replacement work within wx shell,

however, it looks like too trivial a mistake in the original code, if
it would realy be the only source of the mentioned problems and I
can't claim, I fully understand the code of
C:\Python27\Lib\site-packages\wx-2.9.3-msw\wx\py\shell.py
C:\Python27\Lib\site-packages\wx-2.9.3-msw\wx\py\pseudo.py
C:\Python27\Lib\site-packages\wx-2.9.3-msw\wx\py\interpreter.py
...

However, the only changes I made are the added "else" branches in
interpreter.py - in the functions runsource(...) and runmodule(...)
e.g.:

        # If sys.std* is still what we set it to, then restore it.
        # But, if the executed source changed sys.std*, assume it was
        # meant to be changed and leave it. Power to the people.
        if sys.stdin == self.stdin:
            sys.stdin = stdin
### ADDED PART ####
        else:
            self.stdin = sys.stdin

···

2012/7/11 Robin Dunn <robin@alldunn.com>:

On 7/11/12 5:27 AM, Vlastimil Brom wrote:

Hi all,
I'd like to ask about redirecting the output in the wxpython shell. It
seems, its own proxy is somehow protected and cannot be replaced.

Correct. It is setting it to the PseudoFile every time a line or block of
code is executed and then being reset back to the original sys values after
that is done. However it looks like it was intended to not do the restore
if the sys.std* values were changed while the code is executing, (see
various comments in wx.py.interpreter) but it appears that something else is
still changing them back. There is also the redirectStdout and etc.
functions that are available via the shell object, (see wx.py.shell) but
they do not seem to be working as expected either.

If you can dig in and solve these issues and provide a patch it would be
appreciated.

--
Robin Dunn
Software Craftsman
http://wxPython.org

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

#######
etc.
It seems, that the functions kept reseting sys.std* according to
self.std*, which were set on initialisation of the Interpreter
instance, with this update, the changes made from the code are taken
into account permanently.

The diff would be:

=================================
--- interpreter.py

+++ interpreter.py - updated

@@ -100,0 +101,2 @@

+ else:
+ self.stdin = sys.stdin
@@ -102,0 +105,2 @@

+ else:
+ self.stdout = sys.stdout
@@ -104,0 +109,2 @@

+ else:
+ self.stderr = sys.stderr
@@ -117,0 +124,2 @@

+ else:
+ self.stdin = sys.stdin
@@ -119,0 +128,2 @@

+ else:
+ self.stdout = sys.stdout
@@ -121,0 +132,2 @@

+ else:
+ self.stderr = sys.stderr

======================================

PyShell 0.9.8 - The Flakiest Python Shell
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

"UPDATED SHELL"

'UPDATED SHELL'

import sys
sys.stdout

<wx.py.pseudo.PseudoFileOut instance at 0x0368C418>

from cStringIO import StringIO
old_stdout = sys.stdout
sys.stdout = my_stdout = StringIO()
sys.stdout
2
3
4
sys.stdout = old_stdout
5

5

my_stdout.getvalue()

'<cStringIO.StringO object at 0x036A81C0>\n2\n3\n4\n'

my_stdout

<cStringIO.StringO object at 0x036A81C0>

sys.stdout

<wx.py.pseudo.PseudoFileOut instance at 0x0368C418>

=================================

Although these changes make the wx shell work for me like I'd expect,
I'd like to ask someone more experienced to check the correctness.
Or would it be appropriate to send this directly to the wxpython tracker?

Regarding the shell functions redirectStdout etc., I'm not sure, how
they were supposed to work, but maybe they would be used especially
for reseting the stdout ... to the implicit value (with
redirect=True)?

Thanks,
    vbr

=================================

Although these changes make the wx shell work for me like I’d expect,

I’d like to ask someone more experienced to check the correctness.

Or would it be appropriate to send this directly to the wxpython tracker?

Regarding the shell functions redirectStdout etc., I’m not sure, how

they were supposed to work, but maybe they would be used especially

for reseting the stdout … to the implicit value (with

redirect=True)?

Well, I’m still attempting to see and read msgs with a fixed width font.
(new google group)

jmf