Skip to content

Commit c1f6837

Browse files
authored
Merge pull request #21106 from meeseeksmachine/auto-backport-of-pr-21034-on-v3.5.x
Backport PR #21034 on branch v3.5.x (Make rcParams["backend"] backend fallback check rcParams identity first.)
2 parents 80dad26 + 068a3e3 commit c1f6837

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/matplotlib/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,9 @@ def __getitem__(self, key):
663663
version, name=key, obj_type="rcparam", alternative=alt_key)
664664
return dict.__getitem__(self, alt_key) if alt_key else None
665665

666-
elif key == "backend":
666+
# In theory, this should only ever be used after the global rcParams
667+
# has been set up, but better be safe e.g. in presence of breakpoints.
668+
elif key == "backend" and self is globals().get("rcParams"):
667669
val = dict.__getitem__(self, key)
668670
if val is rcsetup._auto_backend_sentinel:
669671
from matplotlib import pyplot as plt

lib/matplotlib/tests/test_rcparams.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,10 @@ def test_backend_fallback_headful(tmpdir):
500500
backend = subprocess.check_output(
501501
[sys.executable, "-c",
502502
"import matplotlib as mpl; "
503-
"assert dict.__getitem__(mpl.rcParams, 'backend') == "
504-
"mpl.rcsetup._auto_backend_sentinel; "
503+
"sentinel = mpl.rcsetup._auto_backend_sentinel; "
504+
# Check that access on another instance does not resolve the sentinel.
505+
"assert mpl.RcParams({'backend': sentinel})['backend'] == sentinel; "
506+
"assert dict.__getitem__(mpl.rcParams, 'backend') == sentinel; "
505507
"import matplotlib.pyplot; "
506508
"print(matplotlib.get_backend())"],
507509
env=env, universal_newlines=True)

0 commit comments

Comments
 (0)