From cc90c152927714705cfcac92f93faf0ff6769f0f Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 9 Aug 2019 15:38:15 +0200 Subject: [PATCH] Backport PR #14979: FIX: Don't enable IPython integration if not entering REPL. FIX: Don't enable IPython integration if not entering REPL. (#14979) FIX: Don't enable IPython integration if not entering REPL. --- lib/matplotlib/backend_bases.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 08357ab0b99a..0be54ae14fd7 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -1532,6 +1532,20 @@ def __init__(self, name, canvas, key, x=0, y=0, guiEvent=None): self.key = key +def _is_non_interactive_terminal_ipython(ip): + """ + Return whether we are in a a terminal IPython, but non interactive. + + When in _terminal_ IPython, ip.parent will have and `interact` attribute, + if this attribute is False we do not setup eventloop integration as the + user will _not_ interact with IPython. In all other case (ZMQKernel, or is + interactive), we do. + """ + return (hasattr(ip, 'parent') + and (ip.parent is not None) + and getattr(ip.parent, 'interact', None) is False) + + class FigureCanvasBase(object): """ The canvas the figure renders into. @@ -1620,15 +1634,8 @@ def _fix_ipython_backend2gui(cls): backend2gui_rif = {"qt5": "qt", "qt4": "qt", "gtk3": "gtk3", "wx": "wx", "macosx": "osx"}.get(rif) if backend2gui_rif: - pt.backend2gui[get_backend()] = backend2gui_rif - # Work around pylabtools.find_gui_and_backend always reading from - # rcParamsOrig. - orig_origbackend = mpl.rcParamsOrig["backend"] - try: - mpl.rcParamsOrig["backend"] = mpl.rcParams["backend"] - ip.enable_matplotlib() - finally: - mpl.rcParamsOrig["backend"] = orig_origbackend + if _is_non_interactive_terminal_ipython(ip): + ip.enable_gui(backend2gui_rif) @contextmanager def _idle_draw_cntx(self):