Show More
@@ -14,6 +14,7 b' import warnings' | |||||
14 | from IPython.core import ultratb, compilerop |
|
14 | from IPython.core import ultratb, compilerop | |
15 | from IPython.core.magic import Magics, magics_class, line_magic |
|
15 | from IPython.core.magic import Magics, magics_class, line_magic | |
16 | from IPython.core.interactiveshell import DummyMod |
|
16 | from IPython.core.interactiveshell import DummyMod | |
|
17 | from IPython.core.interactiveshell import InteractiveShell | |||
17 | from IPython.terminal.interactiveshell import TerminalInteractiveShell |
|
18 | from IPython.terminal.interactiveshell import TerminalInteractiveShell | |
18 | from IPython.terminal.ipapp import load_default_config |
|
19 | from IPython.terminal.ipapp import load_default_config | |
19 |
|
20 | |||
@@ -192,7 +193,8 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||||
192 | # like _ih and get_ipython() into the local namespace, but delete them |
|
193 | # like _ih and get_ipython() into the local namespace, but delete them | |
193 | # later. |
|
194 | # later. | |
194 | if local_ns is not None: |
|
195 | if local_ns is not None: | |
195 | self.user_ns = local_ns |
|
196 | reentrant_local_ns = {k: v for (k, v) in local_ns.items() if k not in self.user_ns_hidden.keys()} | |
|
197 | self.user_ns = reentrant_local_ns | |||
196 | self.init_user_ns() |
|
198 | self.init_user_ns() | |
197 |
|
199 | |||
198 | # Compiler flags |
|
200 | # Compiler flags | |
@@ -208,8 +210,8 b' class InteractiveShellEmbed(TerminalInteractiveShell):' | |||||
208 |
|
210 | |||
209 | # now, purge out the local namespace of IPython's hidden variables. |
|
211 | # now, purge out the local namespace of IPython's hidden variables. | |
210 | if local_ns is not None: |
|
212 | if local_ns is not None: | |
211 | for name in self.user_ns_hidden: |
|
213 | local_ns.update({k: v for (k, v) in self.user_ns.items() if k not in self.user_ns_hidden.keys()}) | |
212 | local_ns.pop(name, None) |
|
214 | ||
213 |
|
215 | |||
214 | # Restore original namespace so shell can shut down when we exit. |
|
216 | # Restore original namespace so shell can shut down when we exit. | |
215 | self.user_module = orig_user_module |
|
217 | self.user_module = orig_user_module | |
@@ -249,5 +251,28 b' def embed(**kwargs):' | |||||
249 | config = load_default_config() |
|
251 | config = load_default_config() | |
250 | config.InteractiveShellEmbed = config.TerminalInteractiveShell |
|
252 | config.InteractiveShellEmbed = config.TerminalInteractiveShell | |
251 | kwargs['config'] = config |
|
253 | kwargs['config'] = config | |
|
254 | #save ps1/ps2 if defined | |||
|
255 | ps1 = None | |||
|
256 | ps2 = None | |||
|
257 | try: | |||
|
258 | ps1 = sys.ps1 | |||
|
259 | ps2 = sys.ps2 | |||
|
260 | except AttributeError: | |||
|
261 | pass | |||
|
262 | #save previous instance | |||
|
263 | saved_shell_instance = InteractiveShell._instance | |||
|
264 | if saved_shell_instance is not None: | |||
|
265 | cls = type(saved_shell_instance) | |||
|
266 | cls.clear_instance() | |||
252 | shell = InteractiveShellEmbed.instance(**kwargs) |
|
267 | shell = InteractiveShellEmbed.instance(**kwargs) | |
253 | shell(header=header, stack_depth=2, compile_flags=compile_flags) |
|
268 | shell(header=header, stack_depth=2, compile_flags=compile_flags) | |
|
269 | InteractiveShellEmbed.clear_instance() | |||
|
270 | #restore previous instance | |||
|
271 | if saved_shell_instance is not None: | |||
|
272 | cls = type(saved_shell_instance) | |||
|
273 | cls.clear_instance() | |||
|
274 | for subclass in cls._walk_mro(): | |||
|
275 | subclass._instance = saved_shell_instance | |||
|
276 | if ps1 is not None: | |||
|
277 | sys.ps1 = ps1 | |||
|
278 | sys.ps2 = ps2 |
@@ -16,11 +16,13 b' import sys' | |||||
16 | import nose.tools as nt |
|
16 | import nose.tools as nt | |
17 | from IPython.utils.process import process_handler |
|
17 | from IPython.utils.process import process_handler | |
18 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory |
|
18 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory | |
|
19 | from IPython.testing.decorators import skip_win32 | |||
19 |
|
20 | |||
20 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
21 | # Tests |
|
22 | # Tests | |
22 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
23 |
|
24 | |||
|
25 | ||||
24 | _sample_embed = b""" |
|
26 | _sample_embed = b""" | |
25 | from __future__ import print_function |
|
27 | from __future__ import print_function | |
26 | import IPython |
|
28 | import IPython | |
@@ -55,3 +57,69 b' def test_ipython_embed():' | |||||
55 | nt.assert_in('IPython', std) |
|
57 | nt.assert_in('IPython', std) | |
56 | nt.assert_in('bye!', std) |
|
58 | nt.assert_in('bye!', std) | |
57 |
|
59 | |||
|
60 | @skip_win32 | |||
|
61 | def test_nest_embed(): | |||
|
62 | """test that `IPython.embed()` is nestable""" | |||
|
63 | from IPython.external import pexpect | |||
|
64 | ipy_prompt = r']:' #ansi color codes give problems matching beyond this | |||
|
65 | ||||
|
66 | ||||
|
67 | child = pexpect.spawn('%s -m IPython'%(sys.executable, )) | |||
|
68 | child.expect(ipy_prompt) | |||
|
69 | child.sendline("from __future__ import print_function") | |||
|
70 | child.expect(ipy_prompt) | |||
|
71 | child.sendline("import IPython") | |||
|
72 | child.expect(ipy_prompt) | |||
|
73 | child.sendline("ip0 = get_ipython()") | |||
|
74 | #enter first nested embed | |||
|
75 | child.sendline("IPython.embed()") | |||
|
76 | #skip the banner until we get to a prompt | |||
|
77 | try: | |||
|
78 | prompted = -1 | |||
|
79 | while prompted != 0: | |||
|
80 | prompted = child.expect([ipy_prompt, '\r\n']) | |||
|
81 | except pexpect.TIMEOUT as e: | |||
|
82 | print(e) | |||
|
83 | #child.interact() | |||
|
84 | child.sendline("embed1 = get_ipython()"); child.expect(ipy_prompt) | |||
|
85 | child.sendline("print('true' if embed1 is not ip0 else 'false')") | |||
|
86 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |||
|
87 | child.expect(ipy_prompt) | |||
|
88 | child.sendline("print('true' if IPython.get_ipython() is embed1 else 'false')") | |||
|
89 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |||
|
90 | child.expect(ipy_prompt) | |||
|
91 | #enter second nested embed | |||
|
92 | child.sendline("IPython.embed()") | |||
|
93 | #skip the banner until we get to a prompt | |||
|
94 | try: | |||
|
95 | prompted = -1 | |||
|
96 | while prompted != 0: | |||
|
97 | prompted = child.expect([ipy_prompt, '\r\n']) | |||
|
98 | except pexpect.TIMEOUT as e: | |||
|
99 | print(e) | |||
|
100 | #child.interact() | |||
|
101 | child.sendline("embed2 = get_ipython()"); child.expect(ipy_prompt) | |||
|
102 | child.sendline("print('true' if embed2 is not 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 embed2 is IPython.get_ipython() 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 first embed | |||
|
110 | child.expect(ipy_prompt) | |||
|
111 | child.sendline("print('true' if get_ipython() is 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 IPython.get_ipython() is embed1 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 launching scope | |||
|
119 | child.expect(ipy_prompt) | |||
|
120 | child.sendline("print('true' if get_ipython() is ip0 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 ip0 else 'false')") | |||
|
124 | assert(child.expect(['true\r\n', 'false\r\n']) == 0) | |||
|
125 | child.expect(ipy_prompt) |
General Comments 0
You need to be logged in to leave comments.
Login now