【发布时间】:2013-04-01 21:44:58
【问题描述】:
我在 Python 中定义了以下(嵌入式)shell:
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)
我希望我的程序在出现异常时自动将我放入该 shell,在异常发生的范围内。
我试过了:
def excepthook(type, value, traceback):
ipshell(msg)
sys.excepthook = excepthook
但这似乎不起作用(我只是得到标准的回溯)。此外,理想情况下,最好:
- IPython 打印常规回溯
- 然后让我进入 IPython shell
如果我上面写的代码有效,它只会把我放到一个 IPython shell 中,而不是打印回溯。
更新 1:
我读过here,当从 IPython 运行代码时,IPython 负责捕获所有异常,并且可能无法直接覆盖 sys.excepthook()。如果是这种情况,我如何让 IPython 在发生异常的范围内和打印回溯后执行其他异常挂钩(例如我的ipshell())?
更新 2:
开发列表中似乎有一个帖子讨论这个问题:excepthook-like behavior in ipython。显然有一个函数 set_custom_exc 可以解决这个问题,但我不确定如何将其插入我的 ipshell()。
【问题讨论】:
-
只是为了明确一点:您希望嵌入式 shell 在第一次引发异常的范围内(与第一次捕获异常的位置相反)?
-
这是另一个问题的答案,可以完成您想要的,包括保留发生异常的大部分上下文。比 PDB 有用得多。 stackoverflow.com/a/26707499/2683747