在任何(未捕获的)异常上打开IPython shell [英] Opening an IPython shell on any (uncaught) exception

查看:113
本文介绍了在任何(未捕获的)异常上打开IPython shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中定义了以下(嵌入式)shell:

 来自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 **嵌套解释器:\ n
按Ctrl-D退出解释器并继续program.\ n
注意,如果你使用%kill_embedded,你可以完全停用\ n
这个嵌入式实例所以它会永远不要再打开)$​​ b $ b exit_msg ='**从IPython.frontend.terminal.embed导出嵌套的解释器'

导入InteractiveShellEmbed
ipshell = InteractiveShellEmbed(config = cfg,banner1 = banner_msg,exit_msg = exit_msg)

我希望我的程序能够自动将我放入该shell中是一个例外,在发生例外的范围内。



我尝试过:

  def excepthook(类型,值,追溯):
ipshell(msg)

sys.excepthook = excepthook

但这似乎不起作用(我只是得到标准的追溯)。另外,理想情况下最好是:


  1. IPython打印常规追溯

  2. 然后将我带入一个IPython shell

如果我上面编写的代码有效,那只会让我陷入困境IPython shell,而不是打印回溯。



更新1:



我读过其他,当从IPython运行代码时,IPython负责捕获所有异常,并且可能无法直接覆盖 sys.excepthook()。如果是这种情况,我怎样才能让IPython在发生异常的范围内以及打印回溯后执行其他异常挂钩(例如我的 ipshell())? / p>

更新2:



开发人员列表中似乎有一个讨论这个问题的线程: ipython中的类似行为。显然有一个函数 set_custom_exc 负责这一点,但我不知道如何将其插入我的 ipshell()

解决方案

在我的ipython shell中定义以下内容似乎可以满足您的需求:

  def custom_exc(shell,etype,evalue,tb,tb_offset = None):
shell.showtraceback((etype,evalue,tb) ),tb_offset = tb_offset)
ipshell()

get_ipython()。set_custom_exc((Exception,),custom_exc)


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 :

  1. IPython prints the regular traceback
  2. 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().

解决方案

Defining the following in my ipython shell seems to do what you want:

def custom_exc(shell, etype, evalue, tb, tb_offset=None):
    shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset)
    ipshell()

get_ipython().set_custom_exc((Exception,), custom_exc)

这篇关于在任何(未捕获的)异常上打开IPython shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆