From a685781763763f0e50cae966f316bb73bf6c18bf Mon Sep 17 00:00:00 2001 From: "Christopher L. Farrow" Date: Thu, 28 Jul 2011 23:26:03 -0500 Subject: [PATCH] Both the shell and magic functionalities work --- pudb/shell.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/pudb/shell.py b/pudb/shell.py index 345989d..d19b257 100644 --- a/pudb/shell.py +++ b/pudb/shell.py @@ -82,7 +82,6 @@ def run_ipython_shell_v10(locals, globals, first_time): IPShell(argv=[], user_ns=ns, user_global_ns=globals) \ .mainloop(banner=banner) - def run_ipython_shell_v11(locals, globals, first_time): '''IPython shell from IPython version 0.11''' if first_time: @@ -90,9 +89,6 @@ def run_ipython_shell_v11(locals, globals, first_time): else: banner = "" - # avoid IPython's namespace litter - ns = locals.copy() - from IPython.frontend.terminal.interactiveshell import \ TerminalInteractiveShell from IPython.frontend.terminal.ipapp import load_default_config @@ -100,16 +96,29 @@ def run_ipython_shell_v11(locals, globals, first_time): # user (if it exists) that could contain the user's macros and other # niceities. config = load_default_config() - shell = TerminalInteractiveShell.instance(config=config, banner2=banner) - # XXX: there's got to be a better way to do this - shell.user_ns = ns + shell = TerminalInteractiveShell.instance(config=config, + banner2=banner) + # XXX This avoids a warning about not having unique session/line numbers. + # See the HistoryManager.writeout_cache method in IPython.core.history. + shell.history_manager.new_session() + # Save the originating namespace + old_locals = shell.user_ns + old_globals = shell.user_global_ns + # Update shell with current namespace + _update_ns(shell, locals, globals) + shell.mainloop(banner) + # Restore originating namespace + _update_ns(shell, old_locals, old_globals) + +def _update_ns(shell, locals, globals): + '''Update the IPython 0.11 namespace at every visit''' + shell.user_ns = locals.copy() shell.user_global_ns = globals shell.init_user_ns() shell.init_completer() - shell.mainloop(banner) + return - -# Set the proper ipython shel +# Set the proper ipython shell if HAVE_IPYTHON and hasattr(IPython, 'Shell'): run_ipython_shell = run_ipython_shell_v10 else: