Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6a40954

Browse filesBrowse files
authored
Merge pull request #18245 from tacaswell/lazier_headless
MNT: do a better job guessing the GUI framework in use
2 parents d9fd7f9 + 448779a commit 6a40954
Copy full SHA for 6a40954

File tree

Expand file treeCollapse file tree

2 files changed

+51
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+51
-1
lines changed

‎lib/matplotlib/pyplot.py

Copy file name to clipboardExpand all lines: lib/matplotlib/pyplot.py
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,26 @@ def switch_backend(newbackend):
232232
close("all")
233233

234234
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+
235251
# Don't try to fallback on the cairo-based backends as they each have
236252
# an additional dependency (pycairo) over the agg-based backend, and
237253
# are of worse quality.
238-
for candidate in ["macosx", "qt5agg", "gtk3agg", "tkagg", "wxagg"]:
254+
for candidate in candidates:
239255
try:
240256
switch_backend(candidate)
241257
except ImportError:

‎lib/matplotlib/tests/test_backends_interactive.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_backends_interactive.py
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,37 @@ def test_never_update(monkeypatch, capsys):
228228
# test framework doesn't see tkinter callback exceptions normally
229229
# see tkinter.Misc.report_callback_exception
230230
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}.")

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.