sys.excepthack...

David C. Ullrich dullrich at sprynet.com
Wed May 14 16:47:18 EDT 2008


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



More information about the Python-list mailing list