diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 8a6f9f62c378..63cc1aedba60 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -652,7 +652,9 @@ def __getitem__(self, key): version, name=key, obj_type="rcparam", alternative=alt_key) return dict.__getitem__(self, alt_key) if alt_key else None - elif key == "backend": + # In theory, this should only ever be used after the global rcParams + # has been set up, but better be safe e.g. in presence of breakpoints. + elif key == "backend" and self is globals().get("rcParams"): val = dict.__getitem__(self, key) if val is rcsetup._auto_backend_sentinel: from matplotlib import pyplot as plt diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index 17f63f737d84..c44bc7bf92df 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -500,8 +500,10 @@ def test_backend_fallback_headful(tmpdir): backend = subprocess.check_output( [sys.executable, "-c", "import matplotlib as mpl; " - "assert dict.__getitem__(mpl.rcParams, 'backend') == " - "mpl.rcsetup._auto_backend_sentinel; " + "sentinel = mpl.rcsetup._auto_backend_sentinel; " + # Check that access on another instance does not resolve the sentinel. + "assert mpl.RcParams({'backend': sentinel})['backend'] == sentinel; " + "assert dict.__getitem__(mpl.rcParams, 'backend') == sentinel; " "import matplotlib.pyplot; " "print(matplotlib.get_backend())"], env=env, universal_newlines=True)