Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit060fa00

Browse files
committed
BUG: raise when creating a MacOS FigureManager outside the main thread
1 parente0f0ded commit060fa00

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

‎lib/matplotlib/tests/test_backend_macosx.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
importos
2+
importthreading
23
frompathlibimportPath
34

45
importpytest
@@ -84,3 +85,18 @@ def _test_save_figure_return():
8485
deftest_save_figure_return():
8586
subprocess_run_helper(_test_save_figure_return,timeout=_test_timeout,
8687
extra_env={"MPLBACKEND":"macosx"})
88+
89+
90+
@pytest.mark.backend('macosx',skip_on_importerror=True)
91+
deftest_create_figure_on_worker_thread_fails():
92+
93+
defcreate_figure():
94+
warn_msg="Matplotlib GUI outside of the main thread will likely fail."
95+
err_msg="Cannot create a GUI FigureManager outside the main thread"
96+
withpytest.warns(UserWarning,match=warn_msg):
97+
withpytest.raises(RuntimeError,match=err_msg):
98+
plt.gcf()
99+
100+
worker=threading.Thread(target=create_figure)
101+
worker.start()
102+
worker.join()

‎src/_macosx.m‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,16 @@ bool mpl_check_modifier(bool present, PyObject* list, char const* name)
582582
static PyObject*
583583
FigureManager_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
584584
{
585+
if (![NSThreadisMainThread]) {
586+
PyErr_SetString(
587+
PyExc_RuntimeError,
588+
"Cannot create a GUI FigureManager outside the main thread"
589+
"using the MacOS backend. Use a non-interactive"
590+
"backend like 'agg' to make plots on worker threads."
591+
);
592+
returnNULL;
593+
}
594+
585595
lazy_init();
586596
Window* window = [Windowalloc];
587597
if (!window) {returnNULL; }

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp