Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ccb4e7e

Browse files
committed
Fixes the issue 23770 and adds a test in test_backends_interactive.py
1 parent 3133213 commit ccb4e7e

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/matplotlib/pyplot.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,12 @@ def install_repl_displayhook():
202202

203203
from IPython.core.pylabtools import backend2gui # type: ignore
204204
# trigger IPython's eventloop integration, if available
205-
ipython_gui_name = backend2gui.get(get_backend())
206-
if ipython_gui_name:
207-
ip.enable_gui(ipython_gui_name)
205+
try:
206+
ipython_gui_name = backend2gui.get(get_backend())
207+
if ipython_gui_name:
208+
ip.enable_gui(ipython_gui_name)
209+
except NotImplementedError as err:
210+
raise ImportError('Fallback to a different backend.') from err
208211

209212

210213
def uninstall_repl_displayhook():
@@ -389,7 +392,6 @@ def draw_if_interactive():
389392
for func_name in ["new_figure_manager", "draw_if_interactive", "show"]:
390393
globals()[func_name].__signature__ = inspect.signature(
391394
getattr(backend_mod, func_name))
392-
393395
# Need to keep a global reference to the backend for compatibility reasons.
394396
# See https://github.com/matplotlib/matplotlib/issues/6092
395397
matplotlib.backends.backend = newbackend
@@ -398,7 +400,11 @@ def draw_if_interactive():
398400

399401
# make sure the repl display hook is installed in case we become
400402
# interactive
401-
install_repl_displayhook()
403+
try:
404+
install_repl_displayhook()
405+
except ImportError as err:
406+
_log.warning(str(err))
407+
raise ImportError
402408

403409

404410
def _warn_if_gui_out_of_main_thread():

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,19 @@ def test_blitting_events(env):
536536
assert 0 < ndraws < 5
537537

538538

539+
def test_fallback_to_different_backend():
540+
import subprocess
541+
# Runs the process that caused the GH issue 23770
542+
# making sure that this doesn't crash
543+
# since we're supposed to be switching to a different backend instead.
544+
response = subprocess.run(["python", "-c",
545+
"import IPython.core.interactiveshell as ipsh; "
546+
"ipsh.InteractiveShell.instance(); "
547+
"import matplotlib.pyplot; "
548+
"matplotlib.pyplot.figure()"], check=True)
549+
assert response != subprocess.CalledProcessError
550+
551+
539552
# The source of this function gets extracted and run in another process, so it
540553
# must be fully self-contained.
541554
def _test_figure_leak():

0 commit comments

Comments
 (0)