test that `IPython.embed()` is nestable
()
| 64 | |
| 65 | @skip_win32 |
| 66 | def test_nest_embed(): |
| 67 | """test that `IPython.embed()` is nestable""" |
| 68 | import pexpect |
| 69 | ipy_prompt = r']:' #ansi color codes give problems matching beyond this |
| 70 | env = os.environ.copy() |
| 71 | env['IPY_TEST_SIMPLE_PROMPT'] = '1' |
| 72 | |
| 73 | |
| 74 | child = pexpect.spawn(sys.executable, ['-m', 'IPython', '--colors=nocolor'], |
| 75 | env=env) |
| 76 | child.timeout = 5 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 77 | child.expect(ipy_prompt) |
| 78 | child.sendline("import IPython") |
| 79 | child.expect(ipy_prompt) |
| 80 | child.sendline("ip0 = get_ipython()") |
| 81 | #enter first nested embed |
| 82 | child.sendline("IPython.embed()") |
| 83 | #skip the banner until we get to a prompt |
| 84 | try: |
| 85 | prompted = -1 |
| 86 | while prompted != 0: |
| 87 | prompted = child.expect([ipy_prompt, '\r\n']) |
| 88 | except pexpect.TIMEOUT as e: |
| 89 | print(e) |
| 90 | #child.interact() |
| 91 | child.sendline("embed1 = get_ipython()") |
| 92 | child.expect(ipy_prompt) |
| 93 | child.sendline("print('true' if embed1 is not ip0 else 'false')") |
| 94 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
| 95 | child.expect(ipy_prompt) |
| 96 | child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')") |
| 97 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
| 98 | child.expect(ipy_prompt) |
| 99 | #enter second nested embed |
| 100 | child.sendline("IPython.embed()") |
| 101 | #skip the banner until we get to a prompt |
| 102 | try: |
| 103 | prompted = -1 |
| 104 | while prompted != 0: |
| 105 | prompted = child.expect([ipy_prompt, '\r\n']) |
| 106 | except pexpect.TIMEOUT as e: |
| 107 | print(e) |
| 108 | #child.interact() |
| 109 | child.sendline("embed2 = get_ipython()") |
| 110 | child.expect(ipy_prompt) |
| 111 | child.sendline("print('true' if embed2 is not embed1 else 'false')") |
| 112 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
| 113 | child.expect(ipy_prompt) |
| 114 | child.sendline("print('true' if embed2 is IPython.get_ipython() else 'false')") |
| 115 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
| 116 | child.expect(ipy_prompt) |
| 117 | child.sendline('exit') |
| 118 | #back at first embed |
| 119 | child.expect(ipy_prompt) |
| 120 | child.sendline("print('true' if get_ipython() is embed1 else 'false')") |
| 121 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) |
| 122 | child.expect(ipy_prompt) |
| 123 | child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')") |