Skip to content

Commit

Permalink
Also update the backend when doing rcParams["backend"] = "foo".
Browse files Browse the repository at this point in the history
  • Loading branch information
anntzer committed Dec 22, 2017
1 parent a477c8e commit 6725bf4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
21 changes: 18 additions & 3 deletions lib/matplotlib/rcsetup.py
Expand Up @@ -260,13 +260,28 @@ def validate_backend(s):
lambda s:
s if s.startswith("module://")
else ValidateInStrings('backend', all_backends, ignorecase=True)(s))(s)
pyplot = sys.modules.get("matplotlib.pyplot")
if len(candidates) == 1:
return candidates[0]
backend, = candidates
if pyplot:
# This import needs to be delayed (below too) because it is not
# available at first import.
from matplotlib import rcParams
# Don't recurse.
old_backend = rcParams["backend"]
if old_backend == backend:
return backend
dict.__setitem__(rcParams, "backend", backend)
try:
pyplot.switch_backend(backend)
except Exception:
dict.__setitem__(rcParams, "backend", old_backend)
raise
return backend
else:
pyplot = sys.modules.get("matplotlib.pyplot")
if pyplot:
pyplot.switch_backend(candidates) # Actually resolves the backend.
from matplotlib import rcParams
pyplot.switch_backend(candidates) # Actually resolves the backend.
return rcParams["backend"]
else:
return candidates
Expand Down
6 changes: 2 additions & 4 deletions lib/matplotlib/tests/test_backend_qt4.py
Expand Up @@ -12,10 +12,8 @@
except ImportError:
import mock

with matplotlib.rc_context(rc={'backend': 'Qt4Agg'}):
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')
from matplotlib.backends.backend_qt4 import (
MODIFIER_KEYS, SUPER, ALT, CTRL, SHIFT) # noqa
pytest.importorskip('PyQt4')
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat')

QtCore = qt_compat.QtCore
_, ControlModifier, ControlKey = MODIFIER_KEYS[CTRL]
Expand Down
7 changes: 4 additions & 3 deletions lib/matplotlib/tests/test_backend_qt5.py
Expand Up @@ -15,9 +15,10 @@
except ImportError:
import mock

with matplotlib.rc_context(rc={'backend': 'Qt5Agg'}):
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat',
minversion='5')
pytest.importorskip('PyQt5')
qt_compat = pytest.importorskip('matplotlib.backends.qt_compat',
minversion='5')

from matplotlib.backends.backend_qt5 import (
MODIFIER_KEYS, SUPER, ALT, CTRL, SHIFT) # noqa

Expand Down

0 comments on commit 6725bf4

Please sign in to comment.