Skip to content

Commit e6a9e4d

Browse files
committed
MNT: simplify detecting when to do the initial backend switch
ht @anntzer for the implementation suggestion.
1 parent fe6fd2c commit e6a9e4d

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

lib/matplotlib/pyplot.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,20 @@ def _get_required_interactive_framework(backend_mod):
211211
return getattr(
212212
backend_mod.FigureCanvas, "required_interactive_framework", None)
213213

214+
_backend_mod = None
215+
216+
217+
def _get_backend_mod():
218+
"""
219+
Ensure that a backend is selected and return it.
220+
221+
This is currently private, but may be made public in the future.
222+
"""
223+
if _backend_mod is None:
224+
# this will set the global!
225+
_initial_switch_backend()
226+
return _backend_mod
227+
214228

215229
def switch_backend(newbackend):
216230
"""
@@ -298,14 +312,10 @@ class backend_mod(matplotlib.backend_bases._Backend):
298312
# Need to keep a global reference to the backend for compatibility reasons.
299313
# See https://github.com/matplotlib/matplotlib/issues/6092
300314
matplotlib.backends.backend = newbackend
301-
switch_backend._ever_called = True
302-
switch_backend._ever_called = False
303315

304316

305317
def _warn_if_gui_out_of_main_thread():
306-
if not switch_backend._ever_called:
307-
_initial_switch_backend()
308-
if (_get_required_interactive_framework(_backend_mod)
318+
if (_get_required_interactive_framework(_get_backend_mod())
309319
and threading.current_thread() is not threading.main_thread()):
310320
_api.warn_external(
311321
"Starting a Matplotlib GUI outside of the main thread will likely "
@@ -316,7 +326,7 @@ def _warn_if_gui_out_of_main_thread():
316326
def new_figure_manager(*args, **kwargs):
317327
"""Create a new figure manager instance."""
318328
_warn_if_gui_out_of_main_thread()
319-
return _backend_mod.new_figure_manager(*args, **kwargs)
329+
return _get_backend_mod().new_figure_manager(*args, **kwargs)
320330

321331

322332
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -329,9 +339,7 @@ def draw_if_interactive(*args, **kwargs):
329339
End users will typically not have to call this function because the
330340
the interactive mode takes care of this.
331341
"""
332-
if not switch_backend._ever_called:
333-
_initial_switch_backend()
334-
return _backend_mod.draw_if_interactive(*args, **kwargs)
342+
return _get_backend_mod().draw_if_interactive(*args, **kwargs)
335343

336344

337345
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -380,7 +388,7 @@ def show(*args, **kwargs):
380388
explicitly there.
381389
"""
382390
_warn_if_gui_out_of_main_thread()
383-
return _backend_mod.show(*args, **kwargs)
391+
return _get_backend_mod().show(*args, **kwargs)
384392

385393

386394
def isinteractive():

lib/matplotlib/tests/test_backends_interactive.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,10 @@ def _impl():
271271
# just importing pyplot should not be enough to trigger resolution
272272
bk = dict.__getitem__(matplotlib.rcParams, 'backend')
273273
assert not isinstance(bk, str)
274-
assert not plt.switch_backend._ever_called
274+
assert plt._backend_mod is None
275275
# but actually plotting should
276276
plt.plot(5)
277-
assert plt.switch_backend._ever_called
277+
assert plt._backend_mod is not None
278278
bk = dict.__getitem__(matplotlib.rcParams, 'backend')
279279
assert isinstance(bk, str)
280280

0 commit comments

Comments
 (0)