[IronPython] Implementing sys.excepthook

Dan Eloff dan.eloff at gmail.com
Tue Apr 22 23:38:58 CEST 2008

On Tue, Apr 22, 2008 at 4:13 PM, Dino Viehland
<dinov at exchange.microsoft.com> wrote:
> #1 shouldn't be a problem - is this only in the Silverlight builds?  Certainly the current desktop IronPython builds allow assignment to sys (both overwriting existing members as well as creating new members).  But I'd expect that to have worked more or less forever.

I read that somewhere about IronPython, but it must have long since
stopped being an issue. This explains why I can assign to
sys.excepthook without error in Silverlight. That's 1 down.

>  For #2 I believe what we want to do is change Python's PythonContext.FormatException to call excepthook.  That's not 100% correct but it'll get pretty close to the right time when excepthook should be called - especially for Silverlight where we won't have another chance to really do it.  But Silverlight is going to try and replace your page w/ it's own error page so the end result won't be quite what you want.

The trouble with that, if I understand you, is that by the time you're
formatting the exception, it's already reached Silverlight and is
going to continue the rest of the distance (Silverlight popping up
it's error page.) In CPython sys.excepthook bypasses the normal
mechanism of printing the traceback to stderr, unless you do that in
your excepthook function, nothing will be printed. Can we do better
here, and get functionality closer to CPython? It will save headaches
down the road.

I really want to get sys.excepthook in before FormatException, right
about the point where the exception has bubbled to the top python
frame and is about to leave to wherever it goes next.

In a simple world you can simulate sys.excepthook by wrapping the main
entry point in try/except and calling sys.excepthook in the except
clause. In real life, all asynchronus callbacks that are invoked by
the CLR machinery are also entry points in this respect, and they'd
have to be wrapped too.

Thanks for your interest!

More information about the Ironpython-users mailing list