sys.excepthook and wxpython phoenix (PyShell)

Hi all,
I'd like to ask for advice regarding the usage of excepthook in wx.phoenix;
in my app, which I am trying to port from py27 + wxpython classic to
py3.4 + phoenix, I am using sys.excepthook for displaying remaining
errors in a dialog, as I prefer all messages to stay within the gui.
The hook function is simply displaying the error data in a message dialog.

The problem I am facing regards the PyShell part of my app - for code
run form this shell, I need the error message and trace to be shown in
the shell itself as usual, but in phoenix (3.0.3.dev1641+76cf834 msw
(phoenix) + py 3.4.2) my excepthook somehow overrides this and the
errors are shown in the dialogs too.
In classic wxpython 3.0.0.0 msw (classic) + py 2.7.6 this works as
expected without any extra effort.
(In both versions I use win 7, 32-bit python versions).
Is it possible to get the behaviour in classic? Or is it possibly some
bug in PyShell or some other issue?
Thanks in advance,
    vbr

No replys yet?

···

On Wed, Feb 11, 2015 at 6:43 PM, Vlastimil Brom vlastimil.brom@gmail.com wrote:

Hi all,

I’d like to ask for advice regarding the usage of excepthook in wx.phoenix;

in my app, which I am trying to port from py27 + wxpython classic to

py3.4 + phoenix, I am using sys.excepthook for displaying remaining

errors in a dialog, as I prefer all messages to stay within the gui.

The hook function is simply displaying the error data in a message dialog.

The problem I am facing regards the PyShell part of my app - for code

run form this shell, I need the error message and trace to be shown in

the shell itself as usual, but in phoenix (3.0.3.dev1641+76cf834 msw

(phoenix) + py 3.4.2) my excepthook somehow overrides this and the

errors are shown in the dialogs too.

In classic wxpython 3.0.0.0 msw (classic) + py 2.7.6 this works as

expected without any extra effort.

(In both versions I use win 7, 32-bit python versions).

Is it possible to get the behaviour in classic? Or is it possibly some

bug in PyShell or some other issue?

Thanks in advance,

vbr

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Hi all,
I'd like to attach a simplified sample app demonstrating the mentioned
problems, in case my original description wasn't clear. The code
behaves differently between python versions and wx implementations, as
mentioned in the comment:

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#! Python
# -*- coding: utf-8 -*-

import sys
import wx
import wx.py

# test for error redirecting via sys.excepthook
# - in wxpython classic this doesn't affect/catch the errors
triggered within wx.py.shell - Python 2.7.6; wxpython 3.0.0.0 msw
(classic)
# - in wxpython phoenix these are catched and redirected too - Python
3.4.2; 3.0.3.dev1679+94b6e35 msw (phoenix)

def alert_error(err_type=None, err_value=None, err_trace=None):
    err_dialog = wx.MessageDialog(None, str(err_type) + "\n" +
str(err_value), str(err_type), wx.OK | wx.ICON_ERROR)
    err_dialog.ShowModal()
    err_dialog.Destroy()

def compute_1_divided_by_0(evt=None):
    return 1/0

apl = wx.App(redirect=False)
sys.excepthook = alert_error

frm = wx.Frame(None, -1, "test error redirecting")
py_shell = wx.py.shell.Shell(frm, -1,)
err_button = wx.Button(frm, -1, "compute 1/0")
err_button.Bind(wx.EVT_LEFT_UP, compute_1_divided_by_0)
sizer_frm = wx.BoxSizer(wx.VERTICAL)
sizer_frm.Add(py_shell, 1, wx.EXPAND)
sizer_frm.Add(err_button, 0, wx.EXPAND)
frm.SetSizer(sizer_frm)
frm.Show(True)

apl.MainLoop()

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ## #

The same code is also attached as file.

thanks in advance for any pointers or remarks...
   vbr

···

2015-02-12 0:43 GMT+01:00 Vlastimil Brom <vlastimil.brom@gmail.com>:

Hi all,
I'd like to ask for advice regarding the usage of excepthook in wx.phoenix;
in my app, which I am trying to port from py27 + wxpython classic to
py3.4 + phoenix, I am using sys.excepthook for displaying remaining
errors in a dialog, as I prefer all messages to stay within the gui.
The hook function is simply displaying the error data in a message dialog.

The problem I am facing regards the PyShell part of my app - for code
run form this shell, I need the error message and trace to be shown in
the shell itself as usual, but in phoenix (3.0.3.dev1641+76cf834 msw
(phoenix) + py 3.4.2) my excepthook somehow overrides this and the
errors are shown in the dialogs too.
In classic wxpython 3.0.0.0 msw (classic) + py 2.7.6 this works as
expected without any extra effort.
(In both versions I use win 7, 32-bit python versions).
Is it possible to get the behaviour in classic? Or is it possibly some
bug in PyShell or some other issue?
Thanks in advance,
    vbr

Vlastimil Brom wrote:

Hi all,
I'd like to ask for advice regarding the usage of excepthook in wx.phoenix;
in my app, which I am trying to port from py27 + wxpython classic to
py3.4 + phoenix, I am using sys.excepthook for displaying remaining
errors in a dialog, as I prefer all messages to stay within the gui.
The hook function is simply displaying the error data in a message dialog.

The problem I am facing regards the PyShell part of my app - for code
run form this shell, I need the error message and trace to be shown in
the shell itself as usual, but in phoenix (3.0.3.dev1641+76cf834 msw
(phoenix) + py 3.4.2) my excepthook somehow overrides this and the
errors are shown in the dialogs too.
In classic wxpython 3.0.0.0 msw (classic) + py 2.7.6 this works as
expected without any extra effort.
(In both versions I use win 7, 32-bit python versions).
Is it possible to get the behaviour in classic? Or is it possibly some
bug in PyShell or some other issue?
Thanks in advance,
     vbr

Hi all,
I'd like to attach a simplified sample app demonstrating the mentioned
problems, in case my original description wasn't clear. The code
behaves differently between python versions and wx implementations, as
mentioned in the comment:

TBH, I'm not sure why this works in any Python. sys.execpthook is called when an exception is not caught, and I thought that PyShell doesn't actually catch the exceptions but just captures the printing of the tracebacks to sys.stderr. It's been a long time since I dug into that code however so I could be misremembering things.

Anyway, it sounds like there needs to be a modification made to PyShell where it clears sys.excepthook before running the code, and restores it after. I'll gladly review a patch that does that if you or anybody else submits one.

···

2015-02-12 0:43 GMT+01:00 Vlastimil Brom<vlastimil.brom@gmail.com>:

--
Robin Dunn
Software Craftsman

Vlastimil Brom wrote:

Hi all,
I'd like to ask for advice regarding the usage of excepthook in
wx.phoenix;
in my app, which I am trying to port from py27 + wxpython classic to
py3.4 + phoenix, I am using sys.excepthook for displaying remaining
errors in a dialog, as I prefer all messages to stay within the gui.
The hook function is simply displaying the error data in a message
dialog.

The problem I am facing regards the PyShell part of my app - for code
run form this shell, I need the error message and trace to be shown in
the shell itself as usual, but in phoenix (3.0.3.dev1641+76cf834 msw
(phoenix) + py 3.4.2) my excepthook somehow overrides this and the
errors are shown in the dialogs too.
In classic wxpython 3.0.0.0 msw (classic) + py 2.7.6 this works as
expected without any extra effort.
(In both versions I use win 7, 32-bit python versions).
Is it possible to get the behaviour in classic? Or is it possibly some
bug in PyShell or some other issue?
Thanks in advance,
     vbr

Hi all,
I'd like to attach a simplified sample app demonstrating the mentioned
problems, in case my original description wasn't clear. The code
behaves differently between python versions and wx implementations, as
mentioned in the comment:

TBH, I'm not sure why this works in any Python. sys.execpthook is called
when an exception is not caught, and I thought that PyShell doesn't actually
catch the exceptions but just captures the printing of the tracebacks to
sys.stderr. It's been a long time since I dug into that code however so I
could be misremembering things.

Anyway, it sounds like there needs to be a modification made to PyShell
where it clears sys.excepthook before running the code, and restores it
after. I'll gladly review a patch that does that if you or anybody else
submits one.

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

--
You received this message because you are subscribed to the Google Groups
"wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thanks for the hint, Robin,
it appears, that the origin of this difference is in the python
standard library, outside of wxpython - I hadn't looked that far
before in my previous debuging attempts.

In python 3.4 the excepthook is explicitly handled and called in code.py
C:\Python34\Lib\code.py
    def showtraceback(self):
[...]
        finally:
            tblist = tb = None
        if sys.excepthook is sys.__excepthook__:
            self.write(''.join(lines))
        else:
            # If someone has set sys.excepthook, we let that take precedence
            # over self.write
            sys.excepthook(type, value, tb)

which wasn't the case in python 2.7:
C:\Python27\Lib\code.py
    def showtraceback(self):
[...]
        finally:
            tblist = tb = None
        map(self.write, list)

Unless I missed some problems or cornercases, it seems, that the
problem can be worked around with the following hack - a special case
in the excepthook function:

def redirect_error(err_type, err_value, err_trace):
    if "wx\\py\\interpreter.py" in "".join(traceback.format_stack()):
# bypass pyshell - TEST!!
        sys.__excepthook__(err_type, err_value, err_trace)
        return False
    ...

the only difference I noticed is another item in the traceback, code.py ...

1/0 # pyshell using redirect_error excethook

Traceback (most recent call last):
  File "C:\Python34\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
ZeroDivisionError: division by zero

1/0 # pyshell without excethook

Traceback (most recent call last):
  File "<input>", line 1, in <module>
ZeroDivisionError: division by zero

Anyway, unless some other problems arise, I am quite happy with the
current solution. I'd be grateful for ideas of other possible
approaches.

thanks and regards,
     vbr

···

2015-03-07 2:32 GMT+01:00 Robin Dunn <robin@alldunn.com>:

2015-02-12 0:43 GMT+01:00 Vlastimil Brom<vlastimil.brom@gmail.com>: