[IPython-dev] excepthook-like behavior in ipython

Erik Tollerud erik.tollerud at gmail.com
Mon May 14 05:27:06 EDT 2012


Further odd behavior that I'm not sure to classify as a bug or a
feature.  If I make a *script* to do this, the exact same thing
happens.

However, if I make a script that's identical, but raises ValueError
(or any other subclass of Exception), it does *not* trigger the
recursion loop, but also, the "hi" message does *not* appear - it
seems to just use the standard traceback report and ignore the custom
one.  Is this intended?

On Mon, Apr 9, 2012 at 7:09 PM, MinRK <benjaminrk at gmail.com> wrote:
>
>
> On Mon, Apr 9, 2012 at 18:39, Erik Tollerud <erik.tollerud at 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 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 at scipy.org
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>



-- 
Erik Tollerud



More information about the IPython-dev mailing list