class RedirectText: def __init__(self,aWxTextCtrl): self.out=aWxTextCtrl
def write(self,string): wx.CallAfter(self.out.WriteText,string) #CallAfter def flush(self): pass [david] [david] wrote: > What am I doing wrong? > I'm trying to capture stdErr in a multi-threaded program. > > This code crashes wxPython with > /Py Assertion Error: C++ assertion "m_count=-1 || m_count=-2" failed/ > > What I'm trying to do is redirect stderr and stdout to a wxPython text > control. > In an ideal world, when the worker thread crashes, I should get a stderr > message, with no effect on the main thread. > > In an alternate world, I should get an asynchronous exception in the > main thread when there is an unhandled exception in the worker thread. > > In the sample below, the program crashes on the following print statement. > > Oddly enough, I do catch the unhandled worker exception in the main > thread IF THE WORKER THREAD HAS NO INIT PARAMETER. Note: the init > parameter is the only subtle thing in the code. > > Since this is a crash, the exact behaviour will depend on your > environment. You may have difficulty seeing stdErr in some environments. > > Python 2.5, wxPython 2.7.2.0, Windows XP. > (david) > > -------- > import wx > import sys > import threading > > class ExceptionThread(threading.Thread): > def __init__(self,initvar): > threading.Thread.__init__(self); > > def run(self): > raise "ET run" > > > class RedirectText: > def __init__(self,aWxTextCtrl): > self.out=aWxTextCtrl > > def write(self,string): > self.out.WriteText(string) > > def flush(self): > pass > > class Frame(wx.Frame): > def __init__(self, image=None, parent=None): > > wx.Frame.__init__(self, parent) > self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE|wx.TE_RICH2) > > #---- redirection window ---- > redir=RedirectText(self.text) > sys.stdout=redir > sys.stderr=redir > > > class App(wx.App): > def OnInit(self): > self.frame=Frame() > self.frame.Show() > self.Thread=ExceptionThread(1) > self.Thread.start(); > return True > > > app = App() > print 'trigger print failure' > app.MainLoop() > > -- http://mail.python.org/mailman/listinfo/python-list