From d2da83f2d0851dd6a855f7f43564c9fbab6e3fe0 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 23 May 2022 15:46:47 +0200 Subject: [PATCH 1/3] Backport PR #23057: FIX: ensure switching the backend installs repl hook Merge pull request #23057 from tacaswell/fix_pylab (cherry picked from commit b31c5ae782876386006a544a5cc833ddddb4b877) --- lib/matplotlib/pyplot.py | 10 ++++------ lib/matplotlib/tests/test_pyplot.py | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 2120a0120749..31719b458807 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -215,12 +215,6 @@ def _get_backend_mod(): # will (re)import pyplot and then call switch_backend if we need to # resolve the auto sentinel) switch_backend(dict.__getitem__(rcParams, "backend")) - # Just to be safe. Interactive mode can be turned on without calling - # `plt.ion()` so register it again here. This is safe because multiple - # calls to `install_repl_displayhook` are no-ops and the registered - # function respects `mpl.is_interactive()` to determine if it should - # trigger a draw. - install_repl_displayhook() return _backend_mod @@ -311,6 +305,10 @@ class backend_mod(matplotlib.backend_bases._Backend): # See https://github.com/matplotlib/matplotlib/issues/6092 matplotlib.backends.backend = newbackend + # make sure the repl display hook is installed in case we become + # interactive + install_repl_displayhook() + def _warn_if_gui_out_of_main_thread(): if (_get_required_interactive_framework(_get_backend_mod()) diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index 0105c6f22d47..d6069944d7e3 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -1,5 +1,6 @@ import difflib import numpy as np +import os import subprocess import sys from pathlib import Path @@ -335,3 +336,26 @@ def test_fallback_position(): axtest = plt.axes([0.2, 0.2, 0.5, 0.5], position=[0.1, 0.1, 0.8, 0.8]) np.testing.assert_allclose(axtest.bbox.get_points(), axref.bbox.get_points()) + + +def test_pylab_integration(): + pytest.importorskip("IPython") + subprocess.run( + [ + sys.executable, + "-m", + "IPython", + "--pylab", + "-c", + ";".join(( + "import matplotlib.pyplot as plt", + "assert plt._REPL_DISPLAYHOOK == plt._ReplDisplayHook.IPYTHON", + )), + ], + env={**os.environ, "SOURCE_DATE_EPOCH": "0"}, + timeout=60, + check=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) From 829b699858c8a25016e23701d4600b193a6ae378 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 23 May 2022 18:06:17 -0400 Subject: [PATCH 2/3] Merge pull request #23106 from anntzer/tpi MNT: Reuse subprocess_run_helper in test_pylab_integration. (cherry picked from commit 51624b6e7d5df970a19e5eac5ff6928ffc035654) --- lib/matplotlib/tests/test_pyplot.py | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index d6069944d7e3..a6d16ff065be 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -1,6 +1,5 @@ import difflib import numpy as np -import os import subprocess import sys from pathlib import Path @@ -339,23 +338,14 @@ def test_fallback_position(): def test_pylab_integration(): - pytest.importorskip("IPython") - subprocess.run( - [ - sys.executable, - "-m", - "IPython", - "--pylab", - "-c", - ";".join(( - "import matplotlib.pyplot as plt", - "assert plt._REPL_DISPLAYHOOK == plt._ReplDisplayHook.IPYTHON", - )), - ], - env={**os.environ, "SOURCE_DATE_EPOCH": "0"}, + IPython = pytest.importorskip("IPython") + mpl.testing.subprocess_run_helper( + IPython.start_ipython, + "--pylab", + "-c", + ";".join(( + "import matplotlib.pyplot as plt", + "assert plt._REPL_DISPLAYHOOK == plt._ReplDisplayHook.IPYTHON", + )), timeout=60, - check=True, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True, ) From d4879e57e24f9021898aab7e52705c9d7f12c8cc Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 26 May 2022 19:52:09 -0400 Subject: [PATCH 3/3] TST: Adjust to state tracking on 3.5.x branch The implementation of tracking if the repl displayhook is installed changed on main. Adjust the backported test to work with the old way. --- lib/matplotlib/tests/test_pyplot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_pyplot.py b/lib/matplotlib/tests/test_pyplot.py index a6d16ff065be..b376c1b54af3 100644 --- a/lib/matplotlib/tests/test_pyplot.py +++ b/lib/matplotlib/tests/test_pyplot.py @@ -345,7 +345,7 @@ def test_pylab_integration(): "-c", ";".join(( "import matplotlib.pyplot as plt", - "assert plt._REPL_DISPLAYHOOK == plt._ReplDisplayHook.IPYTHON", + "assert plt._IP_REGISTERED is not None", )), timeout=60, )