Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sys.excepthack...

0 views
Skip to first unread message

David C. Ullrich

unread,
May 14, 2008, 4:47:18 PM5/14/08
to
Becoming a fan of wxPython, but I can't stand
what it does with error messsages (I can't find
a way to dismiss that window with the error message
from the keyboard. Seems to be anti-modal - the
key strokes that normally kill the active window
kill the main window (sitting behind the window
with the error message) instead. If the window
only had an OK button...)

So I want to pop up a modal dialog on error.

Tried setting sys.stderr to an object with a
write method that pops up a dialog. The problem
with that is write() gets called several times
per exception - I don't see how to get my new
sys.stderr object to figure out when it's time
to show the message.

So then I found sys.excepthook. The problem with
that was that I was too stoopid to figure out how
to get a readable error message from the parameters
passed to excepthook.

Came up with a ridiculous hack involving both sys.stderr
and sys.excepthook. Works exactly the way I want.
Seems ridiculous - what's the right way to do this?

Ridiculous_hack.py:

import sys
import wx

def hook(*args):
try:
sys.__excepthook__(*args)
finally:
printer.Show()

class ErrorDisplay:
def __init__(self):
self.buffer = ''
def write(self, text):
self.buffer = self.buffer + text

def Show(self):
wx.MessageDialog(None, str(self.buffer),
'Error:',wx.OK).ShowModal()
self.buffer = ''

printer = ErrorDisplay()
sys.stderr = printer
sys.excepthook = hook

--
David C. Ullrich

Jean-Paul Calderone

unread,
May 14, 2008, 5:09:45 PM5/14/08
to pytho...@python.org
On Wed, 14 May 2008 15:47:18 -0500, "David C. Ullrich" <dull...@sprynet.com> wrote:
> [snip]

>
>Came up with a ridiculous hack involving both sys.stderr
>and sys.excepthook. Works exactly the way I want.
>Seems ridiculous - what's the right way to do this?
>
> [snip]

Hi David,

Take a look at the traceback module. The excepthook gets called with
an exception type, instance, and traceback object. The traceback module
is good at turning these things into strings.

Jean-Paul

David C. Ullrich

unread,
May 14, 2008, 5:39:13 PM5/14/08
to
In article <mailman.1136.1210799...@python.org>,
Jean-Paul Calderone <exa...@divmod.com> wrote:

Thanks.

> Jean-Paul

--
David C. Ullrich

Nick Craig-Wood

unread,
May 15, 2008, 4:30:03 AM5/15/08
to
David C. Ullrich <dull...@sprynet.com> wrote:
> Becoming a fan of wxPython, but I can't stand
> what it does with error messsages
[snip]

> So I want to pop up a modal dialog on error.
[snip]

> Came up with a ridiculous hack involving both sys.stderr
> and sys.excepthook. Works exactly the way I want.
> Seems ridiculous - what's the right way to do this?
>
> Ridiculous_hack.py:
>
> import sys
> import wx
>
> def hook(*args):
> try:
> sys.__excepthook__(*args)
> finally:
> printer.Show()
>
> class ErrorDisplay:
> def __init__(self):
> self.buffer = ''
> def write(self, text):
> self.buffer = self.buffer + text
>
> def Show(self):
> wx.MessageDialog(None, str(self.buffer),
> 'Error:',wx.OK).ShowModal()
> self.buffer = ''
>
> printer = ErrorDisplay()
> sys.stderr = printer
> sys.excepthook = hook

Here is how I've done it in the past. It is a complete runnable
example :-

------------------------------------------------------------
#!/usr/bin/python

import wx
import sys
from traceback import format_exception

class TestApp(wx.Frame):
"""Test error handling"""

def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, -1, title, size=(200,200))
sys.excepthook = self.OnException
button = wx.Button(self, wx.NewId(), "Produce Error")
self.Bind(wx.EVT_BUTTON, self.OnClick)
self.Show(True)

def OnException(self, type, value, traceback):
"""
Called on OnException
"""
error_text = "".join(format_exception(type, value, traceback))
wx.MessageDialog(None, error_text, 'Custom Error:', wx.OK).ShowModal()

def OnClick(self, evt):
"Click with a deliberate mistake"
adsfsfsdf

if __name__ == "__main__":
app = wx.PySimpleApp()
frame = TestApp(None, -1, "Test")
app.MainLoop()
------------------------------------------------------------

--
Nick Craig-Wood <ni...@craig-wood.com> -- http://www.craig-wood.com/nick

0 new messages