Skip to content

Backport PR #29721 on branch v3.10.x (FIX: pyplot auto-backend detection case-sensitivity fixup) #29754

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
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
15 changes: 9 additions & 6 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2715,12 +2715,15 @@ def polar(*args, **kwargs) -> list[Line2D]:
# If rcParams['backend_fallback'] is true, and an interactive backend is
# requested, ignore rcParams['backend'] and force selection of a backend that
# is compatible with the current running interactive framework.
if (rcParams["backend_fallback"]
and rcParams._get_backend_or_none() in ( # type: ignore[attr-defined]
set(backend_registry.list_builtin(BackendFilter.INTERACTIVE)) -
{'webagg', 'nbagg'})
and cbook._get_running_interactive_framework()):
rcParams._set("backend", rcsetup._auto_backend_sentinel)
if rcParams["backend_fallback"]:
requested_backend = rcParams._get_backend_or_none() # type: ignore[attr-defined]
requested_backend = None if requested_backend is None else requested_backend.lower()
available_backends = backend_registry.list_builtin(BackendFilter.INTERACTIVE)
if (
requested_backend in (set(available_backends) - {'webagg', 'nbagg'})
and cbook._get_running_interactive_framework()
):
rcParams._set("backend", rcsetup._auto_backend_sentinel)

# fmt: on

Expand Down
25 changes: 24 additions & 1 deletion lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,11 @@ def test_rcparams_reset_after_fail():


@pytest.mark.skipif(sys.platform != "linux", reason="Linux only")
def test_backend_fallback_headless(tmp_path):
def test_backend_fallback_headless_invalid_backend(tmp_path):
env = {**os.environ,
"DISPLAY": "", "WAYLAND_DISPLAY": "",
"MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
# plotting should fail with the tkagg backend selected in a headless environment
with pytest.raises(subprocess.CalledProcessError):
subprocess_run_for_testing(
[sys.executable, "-c",
Expand All @@ -536,6 +537,28 @@ def test_backend_fallback_headless(tmp_path):
env=env, check=True, stderr=subprocess.DEVNULL)


@pytest.mark.skipif(sys.platform != "linux", reason="Linux only")
def test_backend_fallback_headless_auto_backend(tmp_path):
# specify a headless mpl environment, but request a graphical (tk) backend
env = {**os.environ,
"DISPLAY": "", "WAYLAND_DISPLAY": "",
"MPLBACKEND": "TkAgg", "MPLCONFIGDIR": str(tmp_path)}

# allow fallback to an available interactive backend explicitly in configuration
rc_path = tmp_path / "matplotlibrc"
rc_path.write_text("backend_fallback: true")

# plotting should succeed, by falling back to use the generic agg backend
backend = subprocess_run_for_testing(
[sys.executable, "-c",
"import matplotlib.pyplot;"
"matplotlib.pyplot.plot(42);"
"print(matplotlib.get_backend());"
],
env=env, text=True, check=True, capture_output=True).stdout
assert backend.strip().lower() == "agg"


@pytest.mark.skipif(
sys.platform == "linux" and not _c_internal_utils.xdisplay_is_valid(),
reason="headless")
Expand Down
Loading