File tree Expand file tree Collapse file tree 2 files changed +51
-1
lines changed
Filter options
Expand file tree Collapse file tree 2 files changed +51
-1
lines changed
Original file line number Diff line number Diff line change @@ -232,10 +232,26 @@ def switch_backend(newbackend):
232
232
close ("all" )
233
233
234
234
if newbackend is rcsetup ._auto_backend_sentinel :
235
+ current_framework = cbook ._get_running_interactive_framework ()
236
+ mapping = {'qt5' : 'qt5agg' ,
237
+ 'qt4' : 'qt4agg' ,
238
+ 'gtk3' : 'gtk3agg' ,
239
+ 'wx' : 'wxagg' ,
240
+ 'tk' : 'tkagg' ,
241
+ 'macosx' : 'macosx' ,
242
+ 'headless' : 'agg' }
243
+
244
+ best_guess = mapping .get (current_framework , None )
245
+ if best_guess is not None :
246
+ candidates = [best_guess ]
247
+ else :
248
+ candidates = []
249
+ candidates += ["macosx" , "qt5agg" , "gtk3agg" , "tkagg" , "wxagg" ]
250
+
235
251
# Don't try to fallback on the cairo-based backends as they each have
236
252
# an additional dependency (pycairo) over the agg-based backend, and
237
253
# are of worse quality.
238
- for candidate in [ "macosx" , "qt5agg" , "gtk3agg" , "tkagg" , "wxagg" ] :
254
+ for candidate in candidates :
239
255
try :
240
256
switch_backend (candidate )
241
257
except ImportError :
Original file line number Diff line number Diff line change @@ -228,3 +228,37 @@ def test_never_update(monkeypatch, capsys):
228
228
# test framework doesn't see tkinter callback exceptions normally
229
229
# see tkinter.Misc.report_callback_exception
230
230
assert "Exception in Tkinter callback" not in capsys .readouterr ().err
231
+
232
+
233
+ @pytest .mark .skipif (sys .platform != "linux" , reason = "this a linux-only test" )
234
+ @pytest .mark .backend ('Qt5Agg' , skip_on_importerror = True )
235
+ def test_lazy_linux_headless ():
236
+ test_script = """
237
+ import os
238
+ import sys
239
+
240
+ # make it look headless
241
+ del os.environ['DISPLAY']
242
+
243
+ # we should fast-track to Agg
244
+ import matplotlib.pyplot as plt
245
+ plt.get_backend() == 'agg'
246
+ assert 'PyQt5' not in sys.modules
247
+
248
+ # make sure we really have pyqt installed
249
+ import PyQt5
250
+ assert 'PyQt5' in sys.modules
251
+
252
+ # try to switch and make sure we fail with ImportError
253
+ try:
254
+ plt.switch_backend('qt5agg')
255
+ except ImportError:
256
+ ...
257
+ else:
258
+ sys.exit(1)
259
+
260
+ """
261
+ proc = subprocess .run ([sys .executable , "-c" , test_script ])
262
+ if proc .returncode :
263
+ pytest .fail ("The subprocess returned with non-zero exit status "
264
+ f"{ proc .returncode } ." )
You can’t perform that action at this time.
0 commit comments