How to use ipython IPShellEmbed from a working doctrine

Please help me get the ipython built-in console to run inside the doctrine. Sample code demonstrates the problem and hangs your terminal. In bash shell I type ctrl-Z and then kill% 1 to break free and kill since ctrl-C will not 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 write code. A common trick is using ipython as a breakpoint in my code, calling IPython.Shell.IPShellEmbed. This trick works wherever I tried (inside django manage.py runningerver, unit tests), but it does not work as part of a doctrine. I think this is related to stdin / stdout doctrine management.

Thanks in advance for your help. - Philip

+3
1

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()
0

Source: https://habr.com/ru/post/1726997/


All Articles