pycrust & sys.exit

I am importing a module which for some reasons does a sys.exit ... and pycrust
exits while idle traps it and print the output.

Is there some way to have pycrust do the same ?

Is this module explicitly calling sys.exit(), or is it just displaying
the function? If its the latter, then pycrust shouldn't be exiting.

PyCrust 0.9.5 - The Flakiest Python Shell
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import sys
sys.exit

<built-in function exit>

If some module is replacing sys.exit with something that quits the
interpreter when it is displayed, then you can replace that yourself by
doing sys.exit = <something else>

- Josiah

···

"Philippe C. Martin" <pmartin@snakecard.com> wrote:

I agree that sys.exit is hugly and that I should change that ... I still would
like to know how idle catches it.

···

On Thursday 14 December 2006 13:48, Josiah Carlson wrote:

"Philippe C. Martin" <pmartin@snakecard.com> wrote:
> On Thursday 14 December 2006 12:51, Josiah Carlson wrote:
> > "Philippe C. Martin" <pmartin@snakecard.com> wrote:
> > > I am importing a module which for some reasons does a sys.exit ...
> > > and pycrust exits while idle traps it and print the output.
> > >
> > > Is there some way to have pycrust do the same ?
> >
> > Is this module explicitly calling sys.exit(), or is it just displaying
> > the function? If its the latter, then pycrust shouldn't be exiting.
>
> It is calling it.

Replace sys.exit with something that raises an exception that no code
will capture except for your custom wx.py.Shell subclass.
KeyboardInterrupt would work fine.

Possibly subclass wx.py.interpreter.Interpreter and
code.InteractiveInterpreter to override runsource() to capture
KeyboardInterrupt exceptions and print out a message like
code.InteractiveConsole does.

You don't want to capture the SystemExit, otherwise it may become
a bit more difficult to kill the process if it got out of control.

- Josiah

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

--
_________________________
Philippe C. Martin
www.snakecard.com
_________________________

> > > > I am importing a module which for some reasons does a sys.exit ...
> > > > and pycrust exits while idle traps it and print the output.
> > > >
> > > > Is there some way to have pycrust do the same ?
> > >
> > > Is this module explicitly calling sys.exit(), or is it just displaying
> > > the function? If its the latter, then pycrust shouldn't be exiting.
> >
> > It is calling it.
>
> Replace sys.exit with something that raises an exception that no code
> will capture except for your custom wx.py.Shell subclass.
> KeyboardInterrupt would work fine.
>
>
> Possibly subclass wx.py.interpreter.Interpreter and
> code.InteractiveInterpreter to override runsource() to capture
> KeyboardInterrupt exceptions and print out a message like
> code.InteractiveConsole does.
>
> You don't want to capture the SystemExit, otherwise it may become
> a bit more difficult to kill the process if it got out of control.
>

I agree that sys.exit is hugly and that I should change that ... I still would
like to know how idle catches it.

Use the source! Or use the console!

import sys
sys.exit()

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    sys.exit()
SystemExit

raise SystemExit

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    raise SystemExit
SystemExit

Note the "<pyshell#1>" and "<pyshell#2>"? Idle is creating a new shell
every time the old one exits. So it is not so much that it is capturing
the SystemExit exception as much as it is starting a new one when the
old one quits. How does it do it? The actual python interpreter that
is the shell is running in a process separate from the GUI (wx.py runs
in-process), and uses sockets to communicate. If you open up
idlelib.rpc or idlelib.RemoteDebugger, you can discover that Idle runs a
host RPC server, which the interpreter connects to. When the client
disconnects, the server assumes that it has exited, and starts up a new
one.

The communications happen in the client with a secondary (daemonic) thread,
so that it can use thread.interrupt_main() to cause a KeyboardInterrupt
if necessary.

PyPE does a similar thing, only with stdin/stdout pipes, and doesn't add
any debugging or introspection capabilities. Most everything from Idle
could probably be borrowed, and the stuff you really need to change for
wx.py is lower-level calls from wx.py.shell.Shell.processline() .

- Josiah

···

"Philippe C. Martin" <pmartin@snakecard.com> wrote:

On Thursday 14 December 2006 13:48, Josiah Carlson wrote:
> "Philippe C. Martin" <pmartin@snakecard.com> wrote:
> > On Thursday 14 December 2006 12:51, Josiah Carlson wrote:
> > > "Philippe C. Martin" <pmartin@snakecard.com> wrote:

Indeed from pycrust directly I do see the trace ... but when I import a module
that sys.exits .... then pycrust quits.

···

On Thursday 14 December 2006 14:51, Josiah Carlson wrote:

"Philippe C. Martin" <pmartin@snakecard.com> wrote:
> On Thursday 14 December 2006 13:48, Josiah Carlson wrote:
> > "Philippe C. Martin" <pmartin@snakecard.com> wrote:
> > > On Thursday 14 December 2006 12:51, Josiah Carlson wrote:
> > > > "Philippe C. Martin" <pmartin@snakecard.com> wrote:
> > > > > I am importing a module which for some reasons does a sys.exit
> > > > > ... and pycrust exits while idle traps it and print the output.
> > > > >
> > > > > Is there some way to have pycrust do the same ?
> > > >
> > > > Is this module explicitly calling sys.exit(), or is it just
> > > > displaying the function? If its the latter, then pycrust shouldn't
> > > > be exiting.
> > >
> > > It is calling it.
> >
> > Replace sys.exit with something that raises an exception that no code
> > will capture except for your custom wx.py.Shell subclass.
> > KeyboardInterrupt would work fine.
> >
> >
> > Possibly subclass wx.py.interpreter.Interpreter and
> > code.InteractiveInterpreter to override runsource() to capture
> > KeyboardInterrupt exceptions and print out a message like
> > code.InteractiveConsole does.
> >
> > You don't want to capture the SystemExit, otherwise it may become
> > a bit more difficult to kill the process if it got out of control.
>
> I agree that sys.exit is hugly and that I should change that ... I still
> would like to know how idle catches it.

Use the source! Or use the console!

>>> import sys
>>> sys.exit()

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    sys.exit()
SystemExit

>>> raise SystemExit

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    raise SystemExit
SystemExit

Note the "<pyshell#1>" and "<pyshell#2>"? Idle is creating a new shell
every time the old one exits. So it is not so much that it is capturing
the SystemExit exception as much as it is starting a new one when the
old one quits. How does it do it? The actual python interpreter that
is the shell is running in a process separate from the GUI (wx.py runs
in-process), and uses sockets to communicate. If you open up
idlelib.rpc or idlelib.RemoteDebugger, you can discover that Idle runs a
host RPC server, which the interpreter connects to. When the client
disconnects, the server assumes that it has exited, and starts up a new
one.

The communications happen in the client with a secondary (daemonic) thread,
so that it can use thread.interrupt_main() to cause a KeyboardInterrupt
if necessary.

PyPE does a similar thing, only with stdin/stdout pipes, and doesn't add
any debugging or introspection capabilities. Most everything from Idle
could probably be borrowed, and the stuff you really need to change for
wx.py is lower-level calls from wx.py.shell.Shell.processline() .

- Josiah

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

--
_________________________
Philippe C. Martin
www.snakecard.com
_________________________