Skip to content

Commit

Permalink
Fix critical bug with pylab support inadvertently introduced in ipyth…
Browse files Browse the repository at this point in the history
…on#648.

code used it as a dict.  Updated that code to handle a dict correctly,
and added tests to catch this issue in the future (also increases test
coverage of pylab code).
  • Loading branch information
fperez committed Nov 27, 2011
1 parent a1e4911 commit cd84e0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
19 changes: 12 additions & 7 deletions IPython/lib/pylabtools.py
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions IPython/lib/tests/test_pylabtools.py
Expand Up @@ -20,6 +20,7 @@
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
Expand Down Expand Up @@ -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)

0 comments on commit cd84e0e

Please sign in to comment.