如何在运行的 doctest 中使用 ipython 的 IPShellEmbed
请帮助我获得一个嵌入式 ipython 控制台以在 doctest 中运行。示例代码演示了该问题并将挂起您的终端。在 bash shell 上,我输入 ctrl-Z 然后杀死 %1 来突破并杀死,因为 ctrl-C 不起作用。
def some_function():
"""
>>> some_function()
'someoutput'
"""
# now try to drop into an ipython shell to help
# with development
import IPython.Shell; IPython.Shell.IPShellEmbed(argv=[])()
return 'someoutput'
if __name__ == '__main__':
import doctest
print "Running doctest . . ."
doctest.testmod()
我喜欢使用 ipython 来帮助编写代码。一个常见的技巧是通过调用 IPython.Shell.IPShellEmbed 来使用 ipython 作为代码中的断点。这个技巧在我尝试过的任何地方都有效(在 django manage.py runserver 中,单元测试中),但它在 doctests 中不起作用。我认为这与 doctest 控制标准输入/标准输出有关。
预先感谢您的帮助。 - 菲利普
Please help me get an embedded ipython console to run inside a doctest. The example code demonstrates the problem and will hang your terminal. On bash shell I type ctrl-Z and then kill %1 to break out and kill, since ctrl-C won't work.
def some_function():
"""
>>> some_function()
'someoutput'
"""
# now try to drop into an ipython shell to help
# with development
import IPython.Shell; IPython.Shell.IPShellEmbed(argv=[])()
return 'someoutput'
if __name__ == '__main__':
import doctest
print "Running doctest . . ."
doctest.testmod()
I like to use ipython to help write code. A common trick is to use ipython as a breakpoint in my code by calling IPython.Shell.IPShellEmbed
. This trick works everywhere I've tried (inside a django manage.py runserver, unit tests), but it doesn't work within doctests. I think it has to do with doctest controlling stdin/stdout.
Thanks in advance for your help.
- Philip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我给 ipython 用户组发了电子邮件并得到了一些帮助。现在有一个支持票,可以在未来版本的 ipython 中修复此功能。这是带有解决方法的代码片段:
I emailed the ipython user group and got some help. There is now a support ticket to get this feature fixed in future versions of ipython. Here is a code snippet with a workaround: