At some point I’m meaning to set things up so that any unhandled exception, anywhere in the whole application (from lurking bugs) will result in a “Whoops!” dialog for the user instead of the app just hanging there or crashing. I looked around and found this SE question and answer about it:
I’m wondering what the “canonical” answer is to this. I guess it isn’t sufficient to just wrap the app.MainLoop() in a try/except block (one person there said this doesn’t even work), but are the answers there what people here would recommend, or some other way?
On Tue, Jan 28, 2014 at 11:32:07PM -0500, C M wrote:
At some point I'm meaning to set things up so that any unhandled exception,
anywhere in the whole application (from lurking bugs) will result in a
"Whoops!" dialog for the user instead of the app just hanging there or
crashing. I looked around and found this SE question and answer about it:
I'm wondering what the "canonical" answer is to this. I guess it isn't
sufficient to just wrap the app.MainLoop() in a try/except block (one
person there said this doesn't even work), but are the answers there what
people here would recommend, or some other way?
At some point I’m meaning to set things up so that any unhandled exception, anywhere in the whole application (from lurking bugs) will result in a “Whoops!” dialog for the user instead of the app just hanging there or crashing. I looked around and found this SE question and answer about it:
I’m wondering what the “canonical” answer is to this. I guess it isn’t sufficient to just wrap the app.MainLoop() in a try/except block (one person there said this doesn’t even work), but are the answers there what people here would recommend, or some other way?
The way I normally handle this is by redirecting sys.excepthook to some custom class, like this more or less:
sys.excepthook = MyExceptionHook
def ExceptionHook(exctype, value, trace):
“”"
Handler for all unhandled exceptions.
:param etype: the exception type (SyntaxError, ZeroDivisionError, etc…);
:type etype: Exception
:param string value: the exception error message;
:param string trace: the traceback header, if any (otherwise, it prints the
standard Python header: Traceback (most recent call last).
“”"
frame = wx.GetApp().GetTopWindow()
Format the traceback message in whatever way you like…
Well, it’s looking like sys.excepthook is indeed the winner! Great, that’s very helpful to know there seems to be a consensus (as well as on the SO answers). I’ll get to implementing something like this.
At some point I'm meaning to set things up so that any unhandled
exception, anywhere in the whole application (from lurking bugs) will
result in a "Whoops!" dialog for the user instead of the app just
hanging there or crashing. I looked around and found this SE question
and answer about it:
I'm wondering what the "canonical" answer is to this. I guess it isn't
sufficient to just wrap the app.MainLoop() in a try/except block (one
person there said this doesn't even work),
It would be nice if somebody could update that page with info and examples of using sys.excepthook. Also the current solution on that page would be handy as a decorator, so examples for using it that way would be helpful.
It would be nice if somebody could update that page with info and
examples of using sys.excepthook. Also the current solution on that
page would be handy as a decorator, so examples for using it that way
would be helpful.
I came up with an example using the sys.excepthook method (attached). Can anyone take a look and see if that’s a good example and/or suggest improvements?
The code for the current solution is incomplete as it doesn’t have the actual logging code in it, but I’m going to take a crack at it anyway.
It would be nice if somebody could update that page with info and
examples of using sys.excepthook. Also the current solution on that
page would be handy as a decorator, so examples for using it that way
would be helpful.
I think I figured out how to create a class decorator out of that sample code. I have attached my example. Could you take a look and see if it’s a good example? Thanks!
It would be nice if somebody could update that page with info and
examples of using sys.excepthook. Also the current solution on that
page would be handy as a decorator, so examples for using it that way
would be helpful.
One other thing I forgot to mention. When we do get these examples figured out, I think they should go on the wiki page you mentioned and also on their own page to make them easier to find.
One minor nitpick that's now driving me nuts (I don't yet know enough about wxPython to make other comments). Please label __new__ as the constructor and __init__ as the initialiser as the latter is *NOT* a constructor
···
On 31/01/2014 15:30, Mike Driscoll wrote:
Hi Robin,
It would be nice if somebody could update that page with info and
examples of using sys.excepthook. Also the current solution on that
page would be handy as a decorator, so examples for using it that way
would be helpful.
I think I figured out how to create a class decorator out of that sample
code. I have attached my example. Could you take a look and see if it's
a good example? Thanks!
Mike
--
--
My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language.
On Friday, January 31, 2014 1:11:10 PM UTC-6, Mark Lawrence wrote:
On 31/01/2014 15:30, Mike Driscoll wrote:
Hi Robin,
It would be nice if somebody could update that page with info and
examples of using sys.excepthook. Also the current solution on that
page would be handy as a decorator, so examples for using it that way
would be helpful.
--
Robin Dunn
Software Craftsman
[http://wxPython.org](http://wxPython.org)
I think I figured out how to create a class decorator out of that sample
code. I have attached my example. Could you take a look and see if it’s
a good example? Thanks!
Mike
–
One minor nitpick that’s now driving me nuts (I don’t yet know enough
about wxPython to make other comments). Please label new as the
constructor and init as the initialiser as the latter is NOT a
constructor
–
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
I think the docs are wrong so I've asked for clarification on the main Python mailing list/news group.
···
On 31/01/2014 19:16, Mike Driscoll wrote:
On Friday, January 31, 2014 1:11:10 PM UTC-6, Mark Lawrence wrote:
On 31/01/2014 15:30, Mike Driscoll wrote:
> Hi Robin,
>
> It would be nice if somebody could update that page with info
and
> examples of using sys.excepthook. Also the current solution
on that
> page would be handy as a decorator, so examples for using it
that way
> would be helpful.
>
> --
> Robin Dunn
> Software Craftsman
> http://wxPython.org
>
> I think I figured out how to create a class decorator out of that
sample
> code. I have attached my example. Could you take a look and see
if it's
> a good example? Thanks!
>
> Mike
>
> --
One minor nitpick that's now driving me nuts (I don't yet know enough
about wxPython to make other comments). Please label __new__ as the
constructor and __init__ as the initialiser as the latter is *NOT* a
constructor
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
It would be nice if somebody could update that page with info and
examples of using sys.excepthook. Also the current solution on that
page would be handy as a decorator, so examples for using it that way
would be helpful.
I think I figured out how to create a class decorator out of that sample
code. I have attached my example. Could you take a look and see if it's
a good example? Thanks!
It would be nice to be able to use the decorator on things besides event handlers, so making it more generic (with *args, **kw) would be good.
Mike Driscoll wrote:
> Hi Robin,
>
> It would be nice if somebody could update that page with info and
> examples of using sys.excepthook. Also the current solution on that
> page would be handy as a decorator, so examples for using it that
way
> would be helpful.
>
> --
> Robin Dunn
> Software Craftsman
> http://wxPython.org
>
> I think I figured out how to create a class decorator out of that
sample
> code. I have attached my example. Could you take a look and see
if it's
> a good example? Thanks!
It would be nice to be able to use the decorator on things besides
event
handlers, so making it more generic (with *args, **kw) would be good.
I modified it a bit to use the *a, **kw format. Hopefully the attached
will suffice. It worked on my machine anyway.
The self being passed to the event handler is the ExceptionLogging instance, not the Panel instance. ISTR that there is a simple way around this but I don't remember details at the moment.
···
On Tuesday, February 4, 2014 8:37:06 PM UTC-6, Robin Dunn wrote:
The self being passed to the event handler is the ExceptionLogging
instance, not the Panel instance. ISTR that there is a simple way
around this but I don’t remember details at the moment.
The self being passed to the event handler is the ExceptionLogging
instance, not the Panel instance. ISTR that there is a simple way
around this but I don't remember details at the moment.
Hmmm...I think that might be a bit beyond me at the moment. I can't find
any examples that are similar to what you're talking about anyway.
I don't think that this is what I was trying to remember earlier, but implementing the decorator as a function wrapper instead of a class with __call__ appears to do the right thing. In the attached there are two decorator functions, one which logs to the root logger by default, and another that wraps the first and allows the target logger name to be set.
On Saturday, February 8, 2014 4:38:31 PM UTC-6, Robin Dunn wrote:
Mike Driscoll wrote:
Robin,
The self being passed to the event handler is the ExceptionLogging
instance, not the Panel instance. ISTR that there is a simple way
around this but I don't remember details at the moment.
--
Robin Dunn
Software Craftsman
[http://wxPython.org](http://wxPython.org)
Hmmm…I think that might be a bit beyond me at the moment. I can’t find
any examples that are similar to what you’re talking about anyway.
I don’t think that this is what I was trying to remember earlier, but
implementing the decorator as a function wrapper instead of a class with call appears to do the right thing. In the attached there are two
decorator functions, one which logs to the root logger by default, and
another that wraps the first and allows the target logger name to be set.
I was just thinking I would add your decorator to the wiki page, but I noticed that the C++ and Python Sandwich page is immutable. So I’m going to blog about it instead.
By the way, couldn’t you have used @functools.wrap to retain the original function’s name and docstring instead of setting them explicitly?
Mike
···
On Saturday, February 8, 2014 4:38:31 PM UTC-6, Robin Dunn wrote:
Mike Driscoll wrote:
Robin,
The self being passed to the event handler is the ExceptionLogging
instance, not the Panel instance. ISTR that there is a simple way
around this but I don't remember details at the moment.
--
Robin Dunn
Software Craftsman
[http://wxPython.org](http://wxPython.org)
Hmmm…I think that might be a bit beyond me at the moment. I can’t find
any examples that are similar to what you’re talking about anyway.
I don’t think that this is what I was trying to remember earlier, but
implementing the decorator as a function wrapper instead of a class with call appears to do the right thing. In the attached there are two
decorator functions, one which logs to the root logger by default, and
another that wraps the first and allows the target logger name to be set.
By the way, couldn't you have used @functools.wrap to retain the
original function's name and docstring instead of setting them explicitly?
Yeah, I always forget about that.
I was just thinking I would add your decorator to the wiki page, but I noticed that the C++ and Python Sandwich page is immutable. So I'm going to blog about it instead.
The wiki was getting hit hard with spam so I had to disable editing until I can get it updated to the newest version which I hope will have better spam blocks.