[IPython-dev] excepthook-like behavior in ipython

MinRK benjaminrk@gmail....
Mon Apr 9 21:09:28 CDT 2012


On Mon, Apr 9, 2012 at 18:39, Erik Tollerud <erik.tollerud@gmail.com> wrote:

> Hello all,
>
> I noticed some odd (although I now realize quite sensible) behavior in
> IPython, and I'm curious if there's a workaround.  I have an
> application log that I want to use to capture unhandled exceptions.
> When not using IPython, it's straightforward to just override
> sys.excepthook to first sent the exception to our logging system, and
> then continue the normal excepthook behavior.  However, IPython seems
> to always use only its own excepthook in interactive mode
> (TerminalInteractiveShell.excepthook, at least in the standard
> terminal).
>
> So my question is: Is there any way to plug anything into the IPython
> exception handling system, either with sys.excepthook-style syntax, or
> some other way?  As I said, it doesn't have to actually catch the
> exceptions - we just want entries placed in a log if an interactive
> session hits an unhandled exception, and it could then pass normally
> into the IPython formatting/pdb running system.
>

You use shell.set_custom_exc<http://ipython.org/ipython-doc/dev/api/generated/IPython.core.interactiveshell.html#IPython.core.interactiveshell.InteractiveShell.set_custom_exc>for
this:

# define custom exception handler:

def custom_exc(shell, etype, evalue, tb, tb_offset=None):
    # do you own thing:
    print 'hi', etype
    # also do what IPython would have done, if you want:
    shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset)

# Tell IPython to use it for any/all Exceptions:
get_ipython().set_custom_exc((Exception,), custom_exc)


> --
> Erik Tollerud
> _______________________________________________
> IPython-dev mailing list
> IPython-dev@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-dev/attachments/20120409/29d1f4f6/attachment.html 


More information about the IPython-dev mailing list