8000 Backend switching by anntzer · Pull Request #9795 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Backend switching #9795

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add detection of OSX NSApp event loop.
  • Loading branch information
anntzer committed Jan 9, 2018
commit 1083c3c079048e5679536c3c0e50081f9af60045
13 changes: 12 additions & 1 deletion lib/matplotlib/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def _get_current_event_loop():
Returns
-------
Optional[str]
A value in {"qt5", "qt4", "gtk3", "gtk2", "tk", "headless", None}
One of the following values: "qt5", "qt4", "gtk3", "gtk2", "tk",
"macosx", "headless", ``None``.
"""
QtWidgets = (sys.modules.get("PyQt5.QtWidgets")
or sys.modules.get("PySide2.QtWidgets"))
Expand All @@ -48,6 +49,16 @@ def _get_current_event_loop():
and frame.f_code.co_name == "mainloop"
for frame in sys._current_frames().values()):
return "tk"
try:
from matplotlib.backends import _macosx
except ImportError:
pass
else:
# Note that the NSApp event loop is also running when a non-native
# toolkit (e.g. Qt5) is active, but in that case we want to report the
# other toolkit; thus, this check comes after the other toolkits.
if _macosx.event_loop_is_running():
return "macosx"
if sys.platform.startswith("linux") and not os.environ.get("DISPLAY"):
return "headless"

Expand Down
21 changes: 19 additions & 2 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -2818,6 +2818,16 @@ - (int)index
}
@end

static PyObject*
event_loop_is_running(PyObject* self)
{
if ([NSApp isRunning]) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}

static PyObject*
show(PyObject* self)
{
Expand Down Expand Up @@ -3072,10 +3082,17 @@ static bool verify_framework(void)
}

static struct PyMethodDef methods[] = {
{"event_loop_is_running",
(PyCFunction)event_loop_is_running,
METH_NOARGS,
"Return whether the NSApp main event loop is currently running."
},
{"show",
(PyCFunction)show,
METH_NOARGS,
"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."
"Show all the figures and enter the main loop.\n"
"This function does not return until all Matplotlib windows are closed,\n"
"and is normally not needed in interactive sessions."
},
{"choose_save_file",
(PyCFunction)choose_save_file,
Expand All @@ -3087,7 +3104,7 @@ static bool verify_framework(void)
METH_VARARGS,
"Sets the active cursor."
},
{NULL, NULL, 0, NULL}/* sentinel */
{NULL, NULL, 0, NULL} /* sentinel */
};

#if PY3K
Expand Down
0