diff --git a/IPython/lib/pylabtools.py b/IPython/lib/pylabtools.py index 0b6feb1..1187d55 100644 --- a/IPython/lib/pylabtools.py +++ b/IPython/lib/pylabtools.py @@ -247,7 +247,11 @@ def import_pylab(user_ns, backend, import_all=True, shell=None): exec s in user_ns if shell is not None: - exec s in shell.user_ns_hidden + # All local executions are done in a fresh namespace and we then update + # the set of 'hidden' keys so these variables don't show up in %who + # (which is meant to show only what the user has manually defined). + ns = {} + exec s in ns # If using our svg payload backend, register the post-execution # function that will pick up the results for display. This can only be # done with access to the real shell object. @@ -268,7 +272,7 @@ def import_pylab(user_ns, backend, import_all=True, shell=None): # Add 'figsize' to pyplot and to the user's namespace user_ns['figsize'] = pyplot.figsize = figsize - shell.user_ns_hidden['figsize'] = figsize + ns['figsize'] = figsize # Setup the default figure format fmt = cfg.figure_format @@ -277,17 +281,18 @@ def import_pylab(user_ns, backend, import_all=True, shell=None): # The old pastefig function has been replaced by display from IPython.core.display import display # Add display and display_png to the user's namespace - user_ns['display'] = display - shell.user_ns_hidden['display'] = display - user_ns['getfigs'] = getfigs - shell.user_ns_hidden['getfigs'] = getfigs + ns['display'] = user_ns['display'] = display + ns['getfigs'] = user_ns['getfigs'] = getfigs if import_all: s = ("from matplotlib.pylab import *\n" "from numpy import *\n") exec s in user_ns if shell is not None: - exec s in shell.user_ns_hidden + exec s in ns + + # Update the set of hidden variables with anything we've done here. + shell.user_ns_hidden.update(ns) def pylab_activate(user_ns, gui=None, import_all=True, shell=None): diff --git a/IPython/lib/tests/test_pylabtools.py b/IPython/lib/tests/test_pylabtools.py index 02a7dd7..c64e01f 100644 --- a/IPython/lib/tests/test_pylabtools.py +++ b/IPython/lib/tests/test_pylabtools.py @@ -20,6 +20,7 @@ import matplotlib; matplotlib.use('Agg') import nose.tools as nt from matplotlib import pyplot as plt +import numpy as np # Our own imports from IPython.testing import decorators as dec @@ -52,3 +53,10 @@ def test_figure_to_svg(): plt.draw() svg = pt.print_figure(fig, 'svg')[:100].lower() yield nt.assert_true('doctype svg' in svg) + + +def test_import_pylab(): + ip = get_ipython() + pt.import_pylab(ip.user_ns, 'inline', import_all=False, shell=ip) + nt.assert_true('plt' in ip.user_ns) + nt.assert_equal(ip.user_ns['np'], np)