Skip to content

Commit c236e5c

Browse files
authored
Merge pull request #28887 from meeseeksmachine/auto-backport-of-pr-28883-on-v3.9.x
Backport PR #28883 on branch v3.9.x (Only check X11 when running Tkinter tests)
2 parents 55c38af + ff3e448 commit c236e5c

File tree

6 files changed

+36
-8
lines changed

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
@@ -33,7 +33,7 @@ namespace py = pybind11;
3333
using namespace pybind11::literals;
3434

3535
static bool
36-
mpl_display_is_valid(void)
36+
mpl_xdisplay_is_valid(void)
3737
{
3838
#ifdef __linux__
3939
void* libX11;
@@ -57,6 +57,19 @@ mpl_display_is_valid(void)
5757
return true;
5858
}
5959
}
60+
return false;
61+
#else
62+
return true;
63+
#endif
64+
}
65+
66+
static bool
67+
mpl_display_is_valid(void)
68+
{
69+
#ifdef __linux__
70+
if (mpl_xdisplay_is_valid()) {
71+
return true;
72+
}
6073
void* libwayland_client;
6174
if (getenv("WAYLAND_DISPLAY")
6275
&& (libwayland_client = dlopen("libwayland-client.so.0", RTLD_LAZY))) {
@@ -194,6 +207,16 @@ PYBIND11_MODULE(_c_internal_utils, m)
194207
succeeds, or $WAYLAND_DISPLAY is set and wl_display_connect(NULL)
195208
succeeds.
196209
210+
On other platforms, always returns True.)""");
211+
m.def(
212+
"xdisplay_is_valid", &mpl_xdisplay_is_valid,
213+
R"""( --
214+
Check whether the current X11 display is valid.
215+
216+
On Linux, returns True if either $DISPLAY is set and XOpenDisplay(NULL)
217+
succeeds. Use this function if you need to specifically check for X11
218+
only (e.g., for Tkinter).
219+
197220
On other platforms, always returns True.)""");
198221
m.def(
199222
"Win32_GetCurrentProcessExplicitAppUserModelID",

0 commit comments

Comments
 (0)