Skip to content

Commit

Permalink
Merge pull request matplotlib#28883 from QuLogic/no-x-tk
Browse files Browse the repository at this point in the history
Only check X11 when running Tkinter tests
  • Loading branch information
timhoffm authored Sep 25, 2024
2 parents e43e0bd + f496323 commit 69f09d1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/matplotlib/_c_internal_utils.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
def display_is_valid() -> bool: ...
def xdisplay_is_valid() -> bool: ...

def Win32_GetForegroundWindow() -> int | None: ...
def Win32_SetForegroundWindow(hwnd: int) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _get_running_interactive_framework():
if frame.f_code in codes:
return "tk"
frame = frame.f_back
# premetively break reference cycle between locals and the frame
# Preemptively break reference cycle between locals and the frame.
del frame
macosx = sys.modules.get("matplotlib.backends._macosx")
if macosx and macosx.event_loop_is_running():
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def _isolated_tk_test(success_count, func=None):
reason="missing tkinter"
)
@pytest.mark.skipif(
sys.platform == "linux" and not _c_internal_utils.display_is_valid(),
reason="$DISPLAY and $WAYLAND_DISPLAY are unset"
sys.platform == "linux" and not _c_internal_utils.xdisplay_is_valid(),
reason="$DISPLAY is unset"
)
@pytest.mark.xfail( # https://github.com/actions/setup-python/issues/649
('TF_BUILD' in os.environ or 'GITHUB_ACTION' in os.environ) and
Expand Down
10 changes: 7 additions & 3 deletions lib/matplotlib/tests/test_backends_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def wait_for(self, terminator):
def _get_available_interactive_backends():
_is_linux_and_display_invalid = (sys.platform == "linux" and
not _c_internal_utils.display_is_valid())
_is_linux_and_xdisplay_invalid = (sys.platform == "linux" and
not _c_internal_utils.xdisplay_is_valid())
envs = []
for deps, env in [
*[([qt_api],
Expand All @@ -74,10 +76,12 @@ def _get_available_interactive_backends():
]:
reason = None
missing = [dep for dep in deps if not importlib.util.find_spec(dep)]
if _is_linux_and_display_invalid:
reason = "$DISPLAY and $WAYLAND_DISPLAY are unset"
elif missing:
if missing:
reason = "{} cannot be imported".format(", ".join(missing))
elif env["MPLBACKEND"] == "tkagg" and _is_linux_and_xdisplay_invalid:
reason = "$DISPLAY is unset"
elif _is_linux_and_display_invalid:
reason = "$DISPLAY and $WAYLAND_DISPLAY are unset"
elif env["MPLBACKEND"] == 'macosx' and os.environ.get('TF_BUILD'):
reason = "macosx backend fails on Azure"
elif env["MPLBACKEND"].startswith('gtk'):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def test_backend_fallback_headless(tmp_path):


@pytest.mark.skipif(
sys.platform == "linux" and not _c_internal_utils.display_is_valid(),
sys.platform == "linux" and not _c_internal_utils.xdisplay_is_valid(),
reason="headless")
def test_backend_fallback_headful(tmp_path):
pytest.importorskip("tkinter")
Expand Down
25 changes: 24 additions & 1 deletion src/_c_internal_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace py = pybind11;
using namespace pybind11::literals;

static bool
mpl_display_is_valid(void)
mpl_xdisplay_is_valid(void)
{
#ifdef __linux__
void* libX11;
Expand All @@ -57,6 +57,19 @@ mpl_display_is_valid(void)
return true;
}
}
return false;
#else
return true;
#endif
}

static bool
mpl_display_is_valid(void)
{
#ifdef __linux__
if (mpl_xdisplay_is_valid()) {
return true;
}
void* libwayland_client;
if (getenv("WAYLAND_DISPLAY")
&& (libwayland_client = dlopen("libwayland-client.so.0", RTLD_LAZY))) {
Expand Down Expand Up @@ -194,6 +207,16 @@ PYBIND11_MODULE(_c_internal_utils, m)
succeeds, or $WAYLAND_DISPLAY is set and wl_display_connect(NULL)
succeeds.
On other platforms, always returns True.)""");
m.def(
"xdisplay_is_valid", &mpl_xdisplay_is_valid,
R"""( --
Check whether the current X11 display is valid.
On Linux, returns True if either $DISPLAY is set and XOpenDisplay(NULL)
succeeds. Use this function if you need to specifically check for X11
only (e.g., for Tkinter).
On other platforms, always returns True.)""");
m.def(
"Win32_GetCurrentProcessExplicitAppUserModelID",
Expand Down

0 comments on commit 69f09d1

Please sign in to comment.