Debian Codegraph @

tests/test_embed.py

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