如何在运行的 doctest 中使用 ipython 的 IPShellEmbed

发布于 2024-08-16 03:56:59 字数 719 浏览 3 评论 0原文

请帮助我获得一个嵌入式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清泪尽 2024-08-23 03:56:59

我给 ipython 用户组发了电子邮件并得到了一些帮助。现在有一个支持票,可以在未来版本的 ipython 中修复此功能。这是带有解决方法的代码片段:

import sys

from IPython.Shell import IPShellEmbed

class IPShellDoctest(IPShellEmbed):
   def __call__(self, *a, **kw):
       sys_stdout_saved = sys.stdout
       sys.stdout = sys.stderr
       try:
           IPShellEmbed.__call__(self, *a, **kw)
       finally:
           sys.stdout = sys_stdout_saved


def some_function():
  """
  >>> some_function()
  'someoutput'
  """
  # now try to drop into an ipython shell to help
  # with development
  IPShellDoctest()(local_ns=locals())
  return 'someoutput'

if __name__ == '__main__':
  import doctest
  print "Running doctest . . ."
  doctest.testmod()

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:

import sys

from IPython.Shell import IPShellEmbed

class IPShellDoctest(IPShellEmbed):
   def __call__(self, *a, **kw):
       sys_stdout_saved = sys.stdout
       sys.stdout = sys.stderr
       try:
           IPShellEmbed.__call__(self, *a, **kw)
       finally:
           sys.stdout = sys_stdout_saved


def some_function():
  """
  >>> some_function()
  'someoutput'
  """
  # now try to drop into an ipython shell to help
  # with development
  IPShellDoctest()(local_ns=locals())
  return 'someoutput'

if __name__ == '__main__':
  import doctest
  print "Running doctest . . ."
  doctest.testmod()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文