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

Skip to content

Commit c745e17

Browse files
committed
Add detection of OSX NSApp event loop.
1 parent c14a754 commit c745e17

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

lib/matplotlib/backends/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def _get_current_event_loop():
2525
Returns
2626
-------
2727
Optional[str]
28-
A value in {"qt5", "qt4", "gtk3", "gtk2", "tk", "headless", None}
28+
One of the following values: "qt5", "qt4", "gtk3", "gtk2", "tk",
29+
"macosx", "headless", ``None``.
2930
"""
30-
# FIXME Add detection of OSX event loop.
3131
QtWidgets = sys.modules.get("PyQt5.QtWidgets")
3232
if QtWidgets and QtWidgets.QApplication.instance():
3333
return "qt5"
@@ -46,6 +46,16 @@ def _get_current_event_loop():
4646
and frame.f_code.co_name == "mainloop"
4747
for frame in sys._current_frames().values()):
4848
return "tk"
49+
try:
50+
from matplotlib.backends import _macosx
51+
except ImportError:
52+
pass
53+
else:
54+
# Note that the NSApp event loop is also running when a non-native
55+
# toolkit (e.g. Qt5) is active, but in that case we want to report the
56+
# other toolkit; thus, this check comes after the other toolkits.
57+
if _macosx.event_loop_is_running():
58+
return "macosx"
4959
if sys.platform.startswith("linux") and not os.environ.get("DISPLAY"):
5060
return "headless"
5161

src/_macosx.m

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,16 @@ - (int)index
28182818
}
28192819
@end
28202820

2821+
static PyObject*
2822+
event_loop_is_running(PyObject* self)
2823+
{
2824+
if ([NSApp isRunning]) {
2825+
Py_RETURN_TRUE;
2826+
} else {
2827+
Py_RETURN_FALSE;
2828+
}
2829+
}
2830+
28212831
static PyObject*
28222832
show(PyObject* self)
28232833
{
@@ -3072,10 +3082,17 @@ static bool verify_framework(void)
30723082
}
30733083

30743084
static struct PyMethodDef methods[] = {
3085+
{"event_loop_is_running",
3086+
(PyCFunction)event_loop_is_running,
3087+
METH_NOARGS,
3088+
"Return whether the NSApp main event loop is currently running."
3089+
},
30753090
{"show",
30763091
(PyCFunction)show,
30773092
METH_NOARGS,
3078-
"Show all the figures and enter the main loop.\nThis function does not return until all Matplotlib windows are closed,\nand is normally not needed in interactive sessions."
3093+
"Show all the figures and enter the main loop.\n"
3094+
"This function does not return until all Matplotlib windows are closed,\n"
3095+
"and is normally not needed in interactive sessions."
30793096
},
30803097
{"choose_save_file",
30813098
(PyCFunction)choose_save_file,
@@ -3087,7 +3104,7 @@ static bool verify_framework(void)
30873104
METH_VARARGS,
30883105
"Sets the active cursor."
30893106
},
3090-
{NULL, NULL, 0, NULL}/* sentinel */
3107+
{NULL, NULL, 0, NULL} /* sentinel */
30913108
};
30923109

30933110
#if PY3K

0 commit comments

Comments
 (0)