Skip to content

PRF: only try IPython if it is already imported #5038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def _backend_selection():
_IP_REGISTERED = None
_INSTALL_FIG_OBSERVER = False


def install_repl_displayhook():
"""
Install a repl display hook so that any stale figure are automatically
Expand All @@ -128,27 +129,30 @@ class _NotIPython(Exception):
# see if we have IPython hooks around, if use them

try:
from IPython import get_ipython
ip = get_ipython()
if ip is None:
raise _NotIPython()

if _IP_REGISTERED:
return

def post_execute():
if matplotlib.is_interactive():
draw_all()

# IPython >= 2
try:
ip.events.register('post_execute', post_execute)
except AttributeError:
# IPython 1.x
ip.register_post_execute(post_execute)

_IP_REGISTERED = post_execute
_INSTALL_FIG_OBSERVER = False
if 'IPython' in sys.modules:
from IPython import get_ipython
ip = get_ipython()
if ip is None:
raise _NotIPython()

if _IP_REGISTERED:
return

def post_execute():
if matplotlib.is_interactive():
draw_all()

# IPython >= 2
try:
ip.events.register('post_execute', post_execute)
except AttributeError:
# IPython 1.x
ip.register_post_execute(post_execute)

_IP_REGISTERED = post_execute
_INSTALL_FIG_OBSERVER = False
else:
_INSTALL_FIG_OBSERVER = True

# import failed or ipython is not running
except (ImportError, _NotIPython):
Expand Down