Skip to content

Commit 3098988

Browse files
committed
Only check X11 when running Tkinter tests
Tkinter only supports X11, not Wayland, so if running in an environment with only the latter, the tests should not run.
1 parent 0aecbcf commit 3098988

6 files changed

+36
-8
lines changed

lib/matplotlib/_c_internal_utils.pyi

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
def display_is_valid() -> bool: ...
2+
def xdisplay_is_valid() -> bool: ...
23

34
def Win32_GetForegroundWindow() -> int | None: ...
45
def Win32_SetForegroundWindow(hwnd: int) -> None: ...

lib/matplotlib/cbook.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def _get_running_interactive_framework():
7272
if frame.f_code in codes:
7373
return "tk"
7474
frame = frame.f_back
75-
# premetively break reference cycle between locals and the frame
75+
# Preemptively break reference cycle between locals and the frame.
7676
del frame
7777
macosx = sys.modules.get("matplotlib.backends._macosx")
7878
if macosx and macosx.event_loop_is_running():

lib/matplotlib/tests/test_backend_tk.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def _isolated_tk_test(success_count, func=None):
3535
reason="missing tkinter"
3636
)
3737
@pytest.mark.skipif(
38-
sys.platform == "linux" and not _c_internal_utils.display_is_valid(),
39-
reason="$DISPLAY and $WAYLAND_DISPLAY are unset"
38+
sys.platform == "linux" and not _c_internal_utils.xdisplay_is_valid(),
39+
reason="$DISPLAY is unset"
4040
)
4141
@pytest.mark.xfail( # https://github.com/actions/setup-python/issues/649
4242
('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and

lib/matplotlib/tests/test_backends_interactive.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def wait_for(self, terminator):
5757
def _get_available_interactive_backends():
5858
_is_linux_and_display_invalid = (sys.platform == "linux" and
5959
not _c_internal_utils.display_is_valid())
60+
_is_linux_and_xdisplay_invalid = (sys.platform == "linux" and
61+
not _c_internal_utils.xdisplay_is_valid())
6062
envs = []
6163
for deps, env in [
6264
*[([qt_api],
@@ -74,10 +76,12 @@ def _get_available_interactive_backends():
7476
]:
7577
reason = None
7678
missing = [dep for dep in deps if not importlib.util.find_spec(dep)]
77-
if _is_linux_and_display_invalid:
78-
reason = "$DISPLAY and $WAYLAND_DISPLAY are unset"
79-
elif missing:
79+
if missing:
8080
reason = "{} cannot be imported".format(", ".join(missing))
81+
elif env["MPLBACKEND"] == "tkagg" and _is_linux_and_xdisplay_invalid:
82+
reason = "$DISPLAY is unset"
83+
elif _is_linux_and_display_invalid:
84+
reason = "$DISPLAY and $WAYLAND_DISPLAY are unset"
8185
elif env["MPLBACKEND"] == 'macosx' and os.environ.get('TF_BUILD'):
8286
reason = "macosx backend fails on Azure"
8387
elif env["MPLBACKEND"].startswith('gtk'):

lib/matplotlib/tests/test_rcparams.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ def test_backend_fallback_headless(tmp_path):
536536

537537

538538
@pytest.mark.skipif(
539-
sys.platform == "linux" and not _c_internal_utils.display_is_valid(),
539+
sys.platform == "linux" and not _c_internal_utils.xdisplay_is_valid(),
540540
reason="headless")
541541
def test_backend_fallback_headful(tmp_path):
542542
pytest.importorskip("tkinter")

src/_c_internal_utils.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace py = pybind11;
2626
using namespace pybind11::literals;
2727

2828
static bool
29-
mpl_display_is_valid(void)
29+
mpl_xdisplay_is_valid(void)
3030
{
3131
#ifdef __linux__
3232
void* libX11;
@@ -50,6 +50,19 @@ mpl_display_is_valid(void)
5050
return true;
5151
}
5252
}
53+
return false;
54+
#else
55+
return true;
56+
#endif
57+
}
58+
59+
static bool
60+
mpl_display_is_valid(void)
61+
{
62+
#ifdef __linux__
63+
if (mpl_xdisplay_is_valid()) {
64+
return true;
65+
}
5366
void* libwayland_client;
5467
if (getenv("WAYLAND_DISPLAY")
5568
&& (libwayland_client = dlopen("libwayland-client.so.0", RTLD_LAZY))) {
@@ -183,6 +196,16 @@ PYBIND11_MODULE(_c_internal_utils, m)
183196
succeeds, or $WAYLAND_DISPLAY is set and wl_display_connect(NULL)
184197
succeeds.
185198
199+
On other platforms, always returns True.)""");
200+
m.def(
201+
"xdisplay_is_valid", &mpl_xdisplay_is_valid,
202+
R"""( --
203+
Check whether the current X11 display is valid.
204+
205+
On Linux, returns True if either $DISPLAY is set and XOpenDisplay(NULL)
206+
succeeds. Use this function if you need to specifically check for X11
207+
only (e.g., for Tkinter).
208+
186209
On other platforms, always returns True.)""");
187210
m.def(
188211
"Win32_GetCurrentProcessExplicitAppUserModelID",

0 commit comments

Comments
 (0)