Thanks to visit codestin.com
Credit goes to github.com

Skip to content

macOS: Check for display availability when looking for backends #27761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/matplotlib/tests/test_backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
import pytest

import matplotlib as mpl
from matplotlib import _c_internal_utils
import matplotlib.pyplot as plt
try:
from matplotlib.backends import _macosx
except ImportError:
pytest.skip("These are mac only tests", allow_module_level=True)


pytestmark = [
pytest.mark.skipif(not _c_internal_utils.display_is_valid(),
reason="Display is unavailable")
]


@pytest.mark.backend('macosx')
def test_cached_renderer():
# Make sure that figures have an associated renderer after
Expand Down
5 changes: 2 additions & 3 deletions lib/matplotlib/tests/test_backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,8 @@
]:
reason = None
missing = [dep for dep in deps if not importlib.util.find_spec(dep)]
if (sys.platform == "linux" and
not _c_internal_utils.display_is_valid()):
reason = "$DISPLAY and $WAYLAND_DISPLAY are unset"
if not _c_internal_utils.display_is_valid():
reason = "Display is unavailable"

Check warning on line 309 in lib/matplotlib/tests/test_backend_qt.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_backend_qt.py#L309

Added line #L309 was not covered by tests
elif missing:
reason = "{} cannot be imported".format(", ".join(missing))
elif env["MPLBACKEND"] == 'macosx' and os.environ.get('TF_BUILD'):
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"
not _c_internal_utils.display_is_valid(),
reason="Display is unavailable"
)
@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
7 changes: 3 additions & 4 deletions lib/matplotlib/tests/test_backends_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def wait_for(self, terminator):

@functools.lru_cache
def _get_available_interactive_backends():
_is_linux_and_display_invalid = (sys.platform == "linux" and
not _c_internal_utils.display_is_valid())
_is_display_invalid = not _c_internal_utils.display_is_valid()
envs = []
for deps, env in [
*[([qt_api],
Expand All @@ -74,8 +73,8 @@ 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"
if _is_display_invalid:
reason = "Display is unavailable"
elif missing:
reason = "{} cannot be imported".format(", ".join(missing))
elif env["MPLBACKEND"] == 'macosx' and os.environ.get('TF_BUILD'):
Expand Down
4 changes: 1 addition & 3 deletions lib/matplotlib/tests/test_rcparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,7 @@ def test_backend_fallback_headless(tmp_path):
env=env, check=True, stderr=subprocess.DEVNULL)


@pytest.mark.skipif(
sys.platform == "linux" and not _c_internal_utils.display_is_valid(),
reason="headless")
@pytest.mark.skipif(not _c_internal_utils.display_is_valid(), reason="headless")
def test_backend_fallback_headful(tmp_path):
pytest.importorskip("tkinter")
env = {**os.environ, "MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
Expand Down
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ project(

cc = meson.get_compiler('c')
cpp = meson.get_compiler('cpp')
# Objective C is needed for the _c_internal_utils and macosx extension.
if host_machine.system() == 'darwin'
add_languages('objc', native: false)
endif

# https://mesonbuild.com/Python-module.html
py_mod = import('python')
Expand Down
10 changes: 10 additions & 0 deletions src/_c_internal_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
#else
#define UNUSED_ON_NON_WINDOWS Py_UNUSED
#endif
#ifdef __APPLE__
// Defined in _objc_internal_utils.m.
extern "C" {
int _macos_display_is_valid(void);
}
#endif

namespace py = pybind11;
using namespace pybind11::literals;
Expand Down Expand Up @@ -69,6 +75,8 @@ mpl_display_is_valid(void)
}
}
return false;
#elif defined(__APPLE__)
return _macos_display_is_valid() == 1;
#else
return true;
#endif
Expand Down Expand Up @@ -180,6 +188,8 @@ PYBIND11_MODULE(_c_internal_utils, m)
succeeds, or $WAYLAND_DISPLAY is set and wl_display_connect(NULL)
succeeds.

On macOS, returns True if NSScreen::mainScreen is not nil.

On other platforms, always returns True.)""");
m.def(
"Win32_GetCurrentProcessExplicitAppUserModelID",
Expand Down
13 changes: 13 additions & 0 deletions src/_objc_internal_utils.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <Cocoa/Cocoa.h>

int
_macos_display_is_valid(void)
{
NSApplicationLoad();
NSScreen *main = [NSScreen mainScreen];
if (main != nil) {
return 1;
} else {
return 0;
}
}
18 changes: 12 additions & 6 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ else
user32 = []
endif

if host_machine.system() == 'darwin'
cocoa = dependency('appleframeworks', modules: 'Cocoa')
else
cocoa = []
endif

extension_data = {
'_backend_agg': {
'subdir': 'matplotlib/backends',
Expand All @@ -81,10 +87,11 @@ extension_data = {
},
'_c_internal_utils': {
'subdir': 'matplotlib',
'sources': files(
'_c_internal_utils.cpp',
),
'dependencies': [pybind11_dep, dl, ole32, shell32, user32],
'sources': [
files('_c_internal_utils.cpp'),
(host_machine.system() == 'darwin') ? files('_objc_internal_utils.m') : [],
],
'dependencies': [pybind11_dep, dl, ole32, shell32, user32, cocoa],
},
'ft2font': {
'subdir': 'matplotlib',
Expand Down Expand Up @@ -182,14 +189,13 @@ foreach ext, kwargs : extension_data
endforeach

if get_option('macosx') and host_machine.system() == 'darwin'
add_languages('objc', native: false)
py3.extension_module(
'_macosx',
subdir: 'matplotlib/backends',
sources: files(
'_macosx.m',
),
dependencies: dependency('appleframeworks', modules: 'Cocoa'),
dependencies: [cocoa],
override_options: ['werror=true'],
install: true,
)
Expand Down