Skip to content
Merged
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
68 changes: 55 additions & 13 deletions lib/matplotlib/tests/test_backend_macosx.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import os
import threading
from pathlib import Path

import pytest
from unittest import mock

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


@pytest.mark.backend('macosx')
def test_cached_renderer():
_test_timeout = 60


def _test_cached_renderer():
# Make sure that figures have an associated renderer after
# a fig.canvas.draw() call
fig = plt.figure(1)
Expand All @@ -24,8 +25,14 @@ def test_cached_renderer():
assert fig.canvas.get_renderer()._renderer is not None


@pytest.mark.backend('macosx')
def test_savefig_rcparam(monkeypatch, tmp_path):
@pytest.mark.backend('macosx', skip_on_importerror=True)
def test_cached_renderer():
subprocess_run_helper(_test_cached_renderer, timeout=_test_timeout,
extra_env={"MPLBACKEND": "macosx"})


def _test_savefig_rcparam():
tmp_path = Path(os.environ["TEST_SAVEFIG_PATH"])

def new_choose_save_file(title, directory, filename):
# Replacement function instead of opening a GUI window
Expand All @@ -34,9 +41,10 @@ def new_choose_save_file(title, directory, filename):
os.makedirs(f"{directory}/test")
return f"{directory}/test/{filename}"

monkeypatch.setattr(_macosx, "choose_save_file", new_choose_save_file)
fig = plt.figure()
with mpl.rc_context({"savefig.directory": tmp_path}):
with (mock.patch("matplotlib.backends._macosx.choose_save_file",
new_choose_save_file),
mpl.rc_context({"savefig.directory": tmp_path})):
fig.canvas.toolbar.save_figure()
# Check the saved location got created
save_file = f"{tmp_path}/test/{fig.canvas.get_default_filename()}"
Expand All @@ -47,14 +55,20 @@ def new_choose_save_file(title, directory, filename):
assert mpl.rcParams["savefig.directory"] == f"{tmp_path}/test"


@pytest.mark.backend('macosx')
@pytest.mark.backend('macosx', skip_on_importerror=True)
def test_savefig_rcparam(tmp_path):
subprocess_run_helper(
_test_savefig_rcparam, timeout=_test_timeout,
extra_env={"MPLBACKEND": "macosx", "TEST_SAVEFIG_PATH": tmp_path})


@pytest.mark.backend('macosx', skip_on_importerror=True)
def test_ipython():
from matplotlib.testing import ipython_in_subprocess
ipython_in_subprocess("osx", {(8, 24): "macosx", (7, 0): "MacOSX"})


@pytest.mark.backend('macosx')
def test_save_figure_return():
def _test_save_figure_return():
fig, ax = plt.subplots()
ax.imshow([[1]])
prop = "matplotlib.backends._macosx.choose_save_file"
Expand All @@ -65,3 +79,31 @@ def test_save_figure_return():
with mock.patch(prop, return_value=None):
fname = fig.canvas.manager.toolbar.save_figure()
assert fname is None


@pytest.mark.backend('macosx', skip_on_importerror=True)
def test_save_figure_return():
subprocess_run_helper(_test_save_figure_return, timeout=_test_timeout,
extra_env={"MPLBACKEND": "macosx"})


def _test_create_figure_on_worker_thread_fails():
def create_figure():
warn_msg = "Matplotlib GUI outside of the main thread will likely fail."
err_msg = "Cannot create a GUI FigureManager outside the main thread"
with pytest.warns(UserWarning, match=warn_msg):
with pytest.raises(RuntimeError, match=err_msg):
plt.gcf()

worker = threading.Thread(target=create_figure)
worker.start()
worker.join()


@pytest.mark.backend('macosx', skip_on_importerror=True)
def test_create_figure_on_worker_thread_fails():
subprocess_run_helper(
_test_create_figure_on_worker_thread_fails,
timeout=_test_timeout,
extra_env={"MPLBACKEND": "macosx"}
)
10 changes: 10 additions & 0 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,16 @@ bool mpl_check_modifier(bool present, PyObject* list, char const* name)
static PyObject*
FigureManager_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
if (![NSThread isMainThread]) {
PyErr_SetString(
PyExc_RuntimeError,
"Cannot create a GUI FigureManager outside the main thread "
"using the MacOS backend. Use a non-interactive "
"backend like 'agg' to make plots on worker threads."
);
return NULL;
}

lazy_init();
Window* window = [Window alloc];
if (!window) { return NULL; }
Expand Down
Loading