[wxPython-users] Re: EVT_PAINT handler raising excption, gets called repeatedly

Robin Dunn robin at alldunn.com
Tue Jul 3 14:16:18 PDT 2007


Charl P. Botha wrote:
> On 7/3/07, Christian K. <ckkart at hoc.net> wrote:
>> Charl P. Botha wrote:
>> > Dear all,
>> >
>> > I'm running on Windows XP SP2 and wxPython 2.6.3.3.  If an EVT_PAINT
>> > handler is raising an exception (and thus not returning normally), is
>> > there any reason why it would then be repeatedly called?  If I catch
>> > the exception inside the event handler, it behaves as expected.
>>
>> EVT_PAINT handlers are obviously called by the wxPython event system, so
>> there is no way to put try: except around the call. Where in your code
>> have you hoped that exception handling could catch exceptions occuring
>> inside the handler?
> 
> I have a custom EVT_PAINT handler, let's call it OnPaint.  OnPaint
> makes a call to another method, which could raise an exception.  I'm
> curious as to what wxPython's event handling does with this handler,
> as it does not exit normally, due to the exception raised by the
> method it calls.  I've solved the problem by setting event.Skip()
> _before_ calling the exception-raising method, but I'm still curious.

When control returns from Python to C++ (such as when an event handler 
or other callback exits) then it checks if there was an exception and if 
so it calls PyErr_Print() and clears the error.  So essentially the 
exception is stopped at that point and doesn't raise any further up the 
stack.  (This is something I'd like to change someday, maybe for 3.0...) 
  The bottom line of this is that if you expect an exception may happen 
in a particular layer of the Python/C++ sandwich then you need to catch 
it in the same layer as it won't propagate up to the next Python layer 
on the stack.  If you don't catch it then it will just be printed to 
sys.stderr and then lost.



-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list