How to propagate exceptions across ProcessEvent

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to propagate exceptions across ProcessEvent

Eric Fahlgren
​Is there any clean method for propagating Python exceptions across the ProcessEvent boundary?  Here's an extremely simplified scenario:

1) Click the "animate" button (or something that takes a while).
2) While animation is playing inside the button's event handler, click the "stop" button.
3) Stop button raises "UserAbort" exception.
4) The stack is two (or more) deep in ProcessEvent calls, but Python events aren't passed back across the boundary, so my "try...except" is dead code.

Example code attached (behaves the same in Py2+classic, Py2+Phoenix, probably Py3), I just want some way to get to the "except" clause at line 25 to fire...

Current solution: Set globals or status on event.EventObject in every event handler, then poll the status after every ProcessEvent and re-raise.  Really ugly.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

noexceptions.py (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to propagate exceptions across ProcessEvent

GadgetSteve


On 04/05/2017 23:43, Eric Fahlgren wrote:

> ​Is there any clean method for propagating Python exceptions across the
> ProcessEvent boundary?  Here's an extremely simplified scenario:
>
> 1) Click the "animate" button (or something that takes a while).
> 2) While animation is playing inside the button's event handler, click
> the "stop" button.
> 3) Stop button raises "UserAbort" exception.
> 4) The stack is two (or more) deep in ProcessEvent calls, but Python
> events aren't passed back across the boundary, so my "try...except" is
> dead code.
>
> Example code attached (behaves the same in Py2+classic, Py2+Phoenix,
> probably Py3), I just want some way to get to the "except" clause at
> line 25 to fire...
>
> Current solution: Set globals or status on event.EventObject in every
> event handler, then poll the status after every ProcessEvent and
> re-raise.  Really ugly.
>
> --
> You received this message because you are subscribed to the Google
> Groups "wxPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [hidden email]
> <mailto:[hidden email]>.
> For more options, visit https://groups.google.com/d/optout.

First using Exception to propagate normal program flow is a BAD habit to
get into - Exceptions should always mean "Something Went WRONG" not "the
user clicked a button" - try to get out of it.

The tricks are to:
  - bind your event handlers at the appropriate levels
  - Events have control IDs that can be used to decide what to do
  - remember that in python you can add custom information into events
and pass them on
  - Read up on the Pub-Sub model e.g.
http://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/pubsub.html

--
Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect
those of my employer.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...