打开任何(未捕获)异常的IPython shell [英] Opening an IPython shell on any (uncatched) exception
问题描述
我已经在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 =(\\\
** Nested Interpreter:\\\
按Ctrl-D退出解释器并继续程序。\\
请注意,如果使用%kill_embedded,则可以完全禁用\\
这个嵌入式实例不再打开)$ b $ b exit_msg ='**离开嵌套解释器'
从IPython.frontend.terminal.embed import InteractiveShellEmbed
ipshell = InteractiveShellEmbed(config = cfg,banner1 = banner_msg,exit_msg = exit_msg)
我希望我的程序自动将我放入那个shell,是一个例外, 我尝试过: 但这似乎不起作用(我只是得到标准的追溯)。此外,理想情况下,最好是: 如果我上面写的代码工作,它只会把我放入IPython shell,而不是打印回溯。 我已经阅读 在IPython运行代码时,IPython负责捕获所有异常,并且可能无法直接覆盖 开发者列表中似乎有一个线程讨论了这一点: ipython中的类似行为。显然有一个函数 在我的ipython shell中定义以下内容似乎是你想要的: I have defined the following (embedded) shell in Python: 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: But this doesn't seem to work (I just get the standard traceback). Also, ideally it would be best if : If the code I wrote above worked, it would merely drop me into an IPython shell, and not print the traceback. 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 There seems to be a thread in the dev list discussing this: excepthook-like behavior in ipython. There is apparently a function Defining the following in my ipython shell seems to do what you want:
这篇关于打开任何(未捕获)异常的IPython shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
def excepthook(type,value,traceback):
ipshell(msg)
sys.excepthook = excepthook
更新1:
sys.excepthook()
。如果是这种情况,那么如何让IPython执行其他异常钩子(例如我的 ipshell()
),在异常发生的范围之后,它打印回溯? / p>
更新2:
set_custom_exc
,但我不知道如何将其插入到我的 ipshell()
。
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((异常,),custom_exc)
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)
def excepthook(type, value, traceback):
ipshell(msg)
sys.excepthook = excepthook
Update 1:
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:
set_custom_exc
that takes care of this, but I am not sure how I could plug that in to my ipshell()
.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)