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

查看:142
本文介绍了打开任何(未捕获)异常的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 =(\\\
** 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,是一个例外,

我尝试过:

  def excepthook(type,value,traceback):
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((异常,),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天全站免登陆