8000 FIX/API: Do not reset rcParams['backend'] in rc_context · matplotlib/matplotlib@7fce03e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7fce03e

Browse files
committed
FIX/API: Do not reset rcParams['backend'] in rc_context
The motivating issue is that if the backend was the auto backend sentinel and the backend was first fully resolved in an `rc_context` block, then we would reset back to the sentinel an exit which would lead to strange behavior with figures being erroneously closed. This special cases the 'backend' key to not be reset on `__exit__` Closes #23298
1 parent d58e966 commit 7fce03e

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mpl.rc_context no longer resests the `'backend'` key
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
If you want to rest :rc:`backend` after the context, you must use
5+
`matplotlib.use` to change the backend. Previously the key would be reset, but
6+
it would have any of the expected side-effects because internally
7+
`matplotlib.rc_context` side-steps the custom logic in the
8+
`~matplotlib.RcParams` class (because we copied the values from ``rcParamms``
9+
so it does not need to be re-validated.

lib/matplotlib/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,8 @@ def rc_context(rc=None, fname=None):
10591059
"""
10601060
Return a context manager for temporarily changing rcParams.
10611061
1062+
The :rc:`backend` will not be reset by the context manager.
1063+
10621064
Parameters
10631065
----------
10641066
rc : dict
@@ -1087,7 +1089,8 @@ def rc_context(rc=None, fname=None):
10871089
plt.plot(x, y) # uses 'print.rc'
10881090
10891091
"""
1090-
orig = rcParams.copy()
1092+
orig = dict(rcParams.copy())
1093+
del orig['backend']
10911094
try:
10921095
if fname:
10931096
rc_file(fname)

lib/matplotlib/tests/test_rcparams.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,13 @@ def test_keymaps():
496496
assert isinstance(mpl.rcParams[k], list)
497497

498498

499+
def test_no_backend_reset_rccontext():
500+
assert mpl.rcParams['backend'] != 'module://aardvark'
501+
with mpl.rc_context():
502+
mpl.rcParams['backend'] = 'module://aardvark'
503+
assert mpl.rcParams['backend'] == 'module://aardvark'
504+
505+
499506
def test_rcparams_reset_after_fail():
500507
# There was previously a bug that meant that if rc_context failed and
501508
# raised an exception due to issues in the supplied rc parameters, the

0 commit comments

Comments
 (0)
0