test_embed.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. """Test embedding of IPython"""
  2. #-----------------------------------------------------------------------------
  3. # Copyright (C) 2013 The IPython Development Team
  4. #
  5. # Distributed under the terms of the BSD License. The full license is in
  6. # the file COPYING, distributed as part of this software.
  7. #-----------------------------------------------------------------------------
  8. #-----------------------------------------------------------------------------
  9. # Imports
  10. #-----------------------------------------------------------------------------
  11. import os
  12. import subprocess
  13. import sys
  14. import nose.tools as nt
  15. from IPython.utils.tempdir import NamedFileInTemporaryDirectory
  16. from IPython.testing.decorators import skip_win32
  17. #-----------------------------------------------------------------------------
  18. # Tests
  19. #-----------------------------------------------------------------------------
  20. _sample_embed = b"""
  21. from __future__ import print_function
  22. import IPython
  23. a = 3
  24. b = 14
  25. print(a, '.', b)
  26. IPython.embed()
  27. print('bye!')
  28. """
  29. _exit = b"exit\r"
  30. def test_ipython_embed():
  31. """test that `IPython.embed()` works"""
  32. with NamedFileInTemporaryDirectory('file_with_embed.py') as f:
  33. f.write(_sample_embed)
  34. f.flush()
  35. f.close() # otherwise msft won't be able to read the file
  36. # run `python file_with_embed.py`
  37. cmd = [sys.executable, f.name]
  38. env = os.environ.copy()
  39. env['IPY_TEST_SIMPLE_PROMPT'] = '1'
  40. p = subprocess.Popen(cmd, env=env, stdin=subprocess.PIPE,
  41. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  42. out, err = p.communicate(_exit)
  43. std = out.decode('UTF-8')
  44. nt.assert_equal(p.returncode, 0)
  45. nt.assert_in('3 . 14', std)
  46. if os.name != 'nt':
  47. # TODO: Fix up our different stdout references, see issue gh-14
  48. nt.assert_in('IPython', std)
  49. nt.assert_in('bye!', std)
  50. @skip_win32
  51. def test_nest_embed():
  52. """test that `IPython.embed()` is nestable"""
  53. import pexpect
  54. ipy_prompt = r']:' #ansi color codes give problems matching beyond this
  55. env = os.environ.copy()
  56. env['IPY_TEST_SIMPLE_PROMPT'] = '1'
  57. child = pexpect.spawn(sys.executable, ['-m', 'IPython', '--colors=nocolor'],
  58. env=env)
  59. child.expect(ipy_prompt)
  60. child.sendline("from __future__ import print_function")
  61. child.expect(ipy_prompt)
  62. child.sendline("import IPython")
  63. child.expect(ipy_prompt)
  64. child.sendline("ip0 = get_ipython()")
  65. #enter first nested embed
  66. child.sendline("IPython.embed()")
  67. #skip the banner until we get to a prompt
  68. try:
  69. prompted = -1
  70. while prompted != 0:
  71. prompted = child.expect([ipy_prompt, '\r\n'])
  72. except pexpect.TIMEOUT as e:
  73. print(e)
  74. #child.interact()
  75. child.sendline("embed1 = get_ipython()"); child.expect(ipy_prompt)
  76. child.sendline("print('true' if embed1 is not ip0 else 'false')")
  77. assert(child.expect(['true\r\n', 'false\r\n']) == 0)
  78. child.expect(ipy_prompt)
  79. child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')")
  80. assert(child.expect(['true\r\n', 'false\r\n']) == 0)
  81. child.expect(ipy_prompt)
  82. #enter second nested embed
  83. child.sendline("IPython.embed()")
  84. #skip the banner until we get to a prompt
  85. try:
  86. prompted = -1
  87. while prompted != 0:
  88. prompted = child.expect([ipy_prompt, '\r\n'])
  89. except pexpect.TIMEOUT as e:
  90. print(e)
  91. #child.interact()
  92. child.sendline("embed2 = get_ipython()"); child.expect(ipy_prompt)
  93. child.sendline("print('true' if embed2 is not embed1 else 'false')")
  94. assert(child.expect(['true\r\n', 'false\r\n']) == 0)
  95. child.expect(ipy_prompt)
  96. child.sendline("print('true' if embed2 is IPython.get_ipython() else 'false')")
  97. assert(child.expect(['true\r\n', 'false\r\n']) == 0)
  98. child.expect(ipy_prompt)
  99. child.sendline('exit')
  100. #back at first embed
  101. child.expect(ipy_prompt)
  102. child.sendline("print('true' if get_ipython() is embed1 else 'false')")
  103. assert(child.expect(['true\r\n', 'false\r\n']) == 0)
  104. child.expect(ipy_prompt)
  105. child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')")
  106. assert(child.expect(['true\r\n', 'false\r\n']) == 0)
  107. child.expect(ipy_prompt)
  108. child.sendline('exit')
  109. #back at launching scope
  110. child.expect(ipy_prompt)
  111. child.sendline("print('true' if get_ipython() is ip0 else 'false')")
  112. assert(child.expect(['true\r\n', 'false\r\n']) == 0)
  113. child.expect(ipy_prompt)
  114. child.sendline("print('true' if IPython.get_ipython() is ip0 else 'false')")
  115. assert(child.expect(['true\r\n', 'false\r\n']) == 0)
  116. child.expect(ipy_prompt)
  117. child.sendline('exit')
  118. child.close()