diff --git a/qutebrowser/misc/wmname.py b/qutebrowser/misc/wmname.py index 04d267d14..e27cca394 100644 --- a/qutebrowser/misc/wmname.py +++ b/qutebrowser/misc/wmname.py @@ -26,12 +26,15 @@ class _WaylandDisplayStruct(ctypes.Structure): _WaylandDisplay = NewType("_WaylandDisplay", "ctypes._Pointer[_WaylandDisplayStruct]") -def _load_libwayland_client() -> ctypes.CDLL: - """Load the Wayland client library.""" +def _load_library(name: str) -> ctypes.CDLL: + lib = ctypes.util.find_library(name) + if lib is None: + raise Error(f"{name} library not found") + try: - return ctypes.CDLL("libwayland-client.so") + return ctypes.CDLL(lib) except OSError as e: - raise Error(f"Failed to load libwayland-client: {e}") + raise Error(f"Failed to load {name} library: {e}") def _pid_from_fd(fd: int) -> int: @@ -113,7 +116,7 @@ def wayland_compositor_name() -> str: Approach based on: https://stackoverflow.com/questions/69302630/wayland-client-get-compositor-name """ - wayland_client = _load_libwayland_client() + wayland_client = _load_library("wayland-client") with _wayland_display(wayland_client) as display: fd = _wayland_get_fd(wayland_client, display) pid = _pid_from_fd(fd) @@ -136,18 +139,6 @@ _X11Display = NewType("_X11Display", "ctypes._Pointer[_X11DisplayStruct]") _X11Window = NewType("_X11Window", int) -def _x11_load_lib() -> ctypes.CDLL: - """Load the X11 library.""" - lib = ctypes.util.find_library("X11") - if lib is None: - raise Error("X11 library not found") - - try: - return ctypes.CDLL(lib) - except OSError as e: - raise Error(f"Failed to load X11 library: {e}") - - @contextlib.contextmanager def _x11_open_display(xlib: ctypes.CDLL) -> Iterator[_X11Display]: """Open a connection to the X11 display.""" @@ -307,7 +298,7 @@ def _x11_get_wm_name( def x11_wm_name() -> str: """Get the name of the running X11 window manager.""" - xlib = _x11_load_lib() + xlib = _load_library("X11") with _x11_open_display(xlib) as display: atoms = _X11Atoms( NET_SUPPORTING_WM_CHECK=_x11_intern_atom( diff --git a/tests/unit/misc/test_wmname.py b/tests/unit/misc/test_wmname.py index 4c4b6c50c..bc0e1daea 100644 --- a/tests/unit/misc/test_wmname.py +++ b/tests/unit/misc/test_wmname.py @@ -21,17 +21,18 @@ from qutebrowser.misc import wmname def test_load_libwayland_client(): """Test loading the Wayland client library, which might or might not exist.""" try: - wmname._load_libwayland_client() + wmname._load_library("wayland-client") except wmname.Error: pass def test_load_libwayland_client_error(mocker: pytest_mock.MockerFixture): """Test that an error in loading the Wayland client library raises an error.""" + mocker.patch.object(ctypes.util, "find_library", return_value="libwayland-client.so.6") mocker.patch("ctypes.CDLL", side_effect=OSError("Library not found")) - with pytest.raises(wmname.Error, match="Failed to load libwayland-client"): - wmname._load_libwayland_client() + with pytest.raises(wmname.Error, match="Failed to load wayland-client"): + wmname._load_library("wayland-client") @pytest.fixture @@ -177,7 +178,7 @@ def test_wayland_real(): def test_load_xlib(): """Test loading Xlib, which might or might not exist.""" try: - wmname._x11_load_lib() + wmname._load_library("X11") except wmname.Error: pass @@ -187,7 +188,7 @@ def test_load_xlib_not_found(monkeypatch: pytest.MonkeyPatch): monkeypatch.setattr(ctypes.util, "find_library", lambda x: None) with pytest.raises(wmname.Error, match="X11 library not found"): - wmname._x11_load_lib() + wmname._load_library("X11") def test_load_xlib_error(mocker: pytest_mock.MockerFixture): @@ -198,7 +199,7 @@ def test_load_xlib_error(mocker: pytest_mock.MockerFixture): with pytest.raises( wmname.Error, match="Failed to load X11 library: Failed to load library" ): - wmname._x11_load_lib() + wmname._load_library("X11") @pytest.fixture @@ -289,7 +290,7 @@ def test_x11_get_wm_name( qtbot.add_widget(w) w.setWindowTitle("Test Window") - xlib = wmname._x11_load_lib() + xlib = wmname._load_library("X11") with wmname._x11_open_display(xlib) as display: atoms = wmname._X11Atoms( NET_SUPPORTING_WM_CHECK=-1,