##// END OF EJS Templates
Make embed nestable...
Jason Newton -
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,17 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 previous instance
255 saved_shell_instance = InteractiveShell._instance
256 if saved_shell_instance is not None:
257 cls = type(saved_shell_instance)
258 cls.clear_instance()
252 shell = InteractiveShellEmbed.instance(**kwargs)
259 shell = InteractiveShellEmbed.instance(**kwargs)
253 shell(header=header, stack_depth=2, compile_flags=compile_flags)
260 shell(header=header, stack_depth=2, compile_flags=compile_flags)
261 InteractiveShellEmbed.clear_instance()
262 #restore previous instance
263 if saved_shell_instance is not None:
264 cls = type(saved_shell_instance)
265 cls.clear_instance()
266 for subclass in cls._walk_mro():
267 subclass._instance = saved_shell_instance
General Comments 0
You need to be logged in to leave comments. Login now