Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

sys.excepthack...

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


dullrich at sprynet

May 14, 2008, 1:47 PM

Post #1 of 4 (176 views)
Permalink
sys.excepthack...

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
--
http://mail.python.org/mailman/listinfo/python-list


exarkun at divmod

May 14, 2008, 2:09 PM

Post #2 of 4 (160 views)
Permalink
Re: sys.excepthack... [In reply to]

On Wed, 14 May 2008 15:47:18 -0500, "David C. Ullrich" <dullrich[at]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
--
http://mail.python.org/mailman/listinfo/python-list


dullrich at sprynet

May 14, 2008, 2:39 PM

Post #3 of 4 (158 views)
Permalink
Re: sys.excepthack... [In reply to]

In article <mailman.1136.1210799421.12834.python-list[at]python.org>,
Jean-Paul Calderone <exarkun[at]divmod.com> wrote:

> On Wed, 14 May 2008 15:47:18 -0500, "David C. Ullrich" <dullrich[at]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.

Thanks.

> Jean-Paul

--
David C. Ullrich
--
http://mail.python.org/mailman/listinfo/python-list


nick at craig-wood

May 15, 2008, 1:30 AM

Post #4 of 4 (154 views)
Permalink
Re: sys.excepthack... [In reply to]

David C. Ullrich <dullrich[at]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 <nick[at]craig-wood.com> -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.