Skip to content

Commit 321b769

Browse files
turnipseasonQuLogic
authored andcommitted
Add error handling around install_repl_displayhook
# This is a combination of 3 commits. Fixes the issue 23770 and adds a test in test_backends_interactive.py Changed the test to skip when IPython can't be imported and use _run_helper for the subprocess. Removed the redundant assert statement.
1 parent 183b04f commit 321b769

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/matplotlib/pyplot.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,11 @@ def draw_if_interactive() -> None:
518518
matplotlib.backends.backend = newbackend # type: ignore[attr-defined]
519519

520520
# Make sure the repl display hook is installed in case we become interactive.
521-
install_repl_displayhook()
521+
try:
522+
install_repl_displayhook()
523+
except NotImplementedError as err:
524+
_log.warning("Fallback to a different backend")
525+
raise ImportError from err
522526

523527

524528
def _warn_if_gui_out_of_main_thread() -> None:

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,21 @@ def test_blitting_events(env):
623623
assert 0 < ndraws < 5
624624

625625

626+
def _fallback_check():
627+
import IPython.core.interactiveshell as ipsh
628+
import matplotlib.pyplot
629+
ipsh.InteractiveShell.instance()
630+
matplotlib.pyplot.figure()
631+
632+
633+
def test_fallback_to_different_backend():
634+
pytest.importorskip("IPython")
635+
# Runs the process that caused the GH issue 23770
636+
# making sure that this doesn't crash
637+
# since we're supposed to be switching to a different backend instead.
638+
response = _run_helper(_fallback_check, timeout=_test_timeout)
639+
640+
626641
def _impl_test_interactive_timers():
627642
# A timer with <1 millisecond gets converted to int and therefore 0
628643
# milliseconds, which the mac framework interprets as singleshot.

0 commit comments

Comments
 (0)