Skip to content

Commit 729d021

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

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

lib/matplotlib/pyplot.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ 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+
"""Ensure that a backend is selected and return it.
219+
220+
This is currently private, but may be made public in the future.
221+
"""
222+
if _backend_mod is None:
223+
# this will set the global!
224+
_initial_switch_backend()
225+
return _backend_mod
226+
214227

215228
def switch_backend(newbackend):
216229
"""
@@ -298,14 +311,10 @@ class backend_mod(matplotlib.backend_bases._Backend):
298311
# Need to keep a global reference to the backend for compatibility reasons.
299312
# See https://github.com/matplotlib/matplotlib/issues/6092
300313
matplotlib.backends.backend = newbackend
301-
switch_backend._ever_called = True
302-
switch_backend._ever_called = False
303314

304315

305316
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)
317+
if (_get_required_interactive_framework(_get_backend_mod())
309318
and threading.current_thread() is not threading.main_thread()):
310319
_api.warn_external(
311320
"Starting a Matplotlib GUI outside of the main thread will likely "
@@ -316,7 +325,7 @@ def _warn_if_gui_out_of_main_thread():
316325
def new_figure_manager(*args, **kwargs):
317326
"""Create a new figure manager instance."""
318327
_warn_if_gui_out_of_main_thread()
319-
return _backend_mod.new_figure_manager(*args, **kwargs)
328+
return _get_backend_mod().new_figure_manager(*args, **kwargs)
320329

321330

322331
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -329,9 +338,7 @@ def draw_if_interactive(*args, **kwargs):
329338
End users will typically not have to call this function because the
330339
the interactive mode takes care of this.
331340
"""
332-
if not switch_backend._ever_called:
333-
_initial_switch_backend()
334-
return _backend_mod.draw_if_interactive(*args, **kwargs)
341+
return _get_backend_mod().draw_if_interactive(*args, **kwargs)
335342

336343

337344
# This function's signature is rewritten upon backend-load by switch_backend.
@@ -380,7 +387,7 @@ def show(*args, **kwargs):
380387
explicitly there.
381388
"""
382389
_warn_if_gui_out_of_main_thread()
383-
return _backend_mod.show(*args, **kwargs)
390+
return _get_backend_mod().show(*args, **kwargs)
384391

385392

386393
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)