3

組み込みの ipython コンソールを doctest 内で実行するのを手伝ってください。サンプル コードは問題を示しており、端末がハングします。bash シェルでは、ctrl-C が機能しないため、ctrl-Z を入力してから %1 を殺して、抜け出して殺します。

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 を使用することIPython.Shell.IPShellEmbedです。このトリックは、私が試したすべての場所 (django manage.py runserver、単体テスト内) で機能しますが、doctests 内では機能しません。stdin/stdout を制御する doctest に関係していると思います。

よろしくお願いします。- フィリップ

4

1 に答える 1

0

私は 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()
于 2010-01-06T08:12:53.257 に答える