I have defined the following (embedded) shell in Python:
from IPython.config.loader import Config
cfg = Config()
prompt_config = cfg.PromptManager
prompt_config.in_template = 'N.In <\\#>: '
prompt_config.in2_template = ' .\\D.: '
prompt_config.out_template = 'N.Out<\\#>: '
banner_msg = ("\n**Nested Interpreter:\n"
"Hit Ctrl-D to exit interpreter and continue program.\n"
"Note that if you use %kill_embedded, you can fully deactivate\n"
"This embedded instance so it will never turn on again")
exit_msg = '**Leaving Nested interpreter'
from IPython.frontend.terminal.embed import InteractiveShellEmbed
ipshell = InteractiveShellEmbed(config=cfg, banner1=banner_msg, exit_msg=exit_msg)
I would like my program to automatically drop me into that shell whenever there is an exception, in the scope where the exception occurs.
I have tried with:
def excepthook(type, value, traceback):
ipshell(msg)
sys.excepthook = excepthook
But this doesn't seem to work (I just get the standard traceback). Also, ideally it would be best if :
- IPython prints the regular traceback
- and then drops me into an IPython shell
If the code I wrote above worked, it would merely drop me into an IPython shell, and not print the traceback.
Update 1:
I have read here that when running code from IPython, IPython is in charge of catching all exceptions, and that it may no be possible to directly override sys.excepthook(). If this is the case, how can I have IPython execute other exception hooks (e.g. my ipshell()) in the scope where the exception occurs and after it prints the traceback?
Update 2:
There seems to be a thread in the dev list discussing this: excepthook-like behavior in ipython. There is apparently a function set_custom_exc that takes care of this, but I am not sure how I could plug that in to my ipshell().