Skip to content

FIX: ensure switching the backend installs repl hook #23057

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 2 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
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
10 changes: 4 additions & 6 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,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


Expand Down Expand Up @@ -302,6 +296,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())
Expand Down
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_pyplot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import difflib
import numpy as np
import os
import subprocess
import sys
from pathlib import Path
Expand Down Expand Up @@ -367,3 +368,26 @@ def test_set_current_axes_on_subfigure():
assert plt.gca() != ax
plt.sca(ax)
assert plt.gca() == ax


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"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't really need this, nor the stdout/stderr capture (pytest does it for you), nor universal_newlines, I think?

timeout=60,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)