Skip to content

Add private API retrieving the current event loop and backend GUI info. #11520

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

Merged
merged 1 commit into from
Jul 4, 2018
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
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/2018-06-27-AL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ Changes to backend loading
Failure to load backend modules (``macosx`` on non-framework builds and
``gtk3`` when running headless) now raises `ImportError` (instead of
`RuntimeError` and `TypeError`, respectively.

Third-party backends that integrate with an interactive framework are now
encouraged to define the ``required_interactive_framework`` global value to one
of the following values: "qt5", "qt4", "gtk3", "wx", "tk", or "macosx". This
information will be used to determine whether it is possible to switch from a
backend to another (specifically, whether they use the same interactive
framework).
50 changes: 50 additions & 0 deletions lib/matplotlib/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import importlib
import logging
import os
import sys
import traceback

import matplotlib
Expand All @@ -14,6 +16,54 @@
if not line.startswith(' File "<frozen importlib._bootstrap'))


def _get_running_interactive_framework():
"""
Return the interactive framework whose event loop is currently running, if
any, or "headless" if no event loop can be started, or None.

Returns
-------
Optional[str]
One of the following values: "qt5", "qt4", "gtk3", "wx", "tk",
"macosx", "headless", ``None``.
"""
QtWidgets = (sys.modules.get("PyQt5.QtWidgets")
or sys.modules.get("PySide2.QtWidgets"))
if QtWidgets and QtWidgets.QApplication.instance():
return "qt5"
QtGui = (sys.modules.get("PyQt4.QtGui")
or sys.modules.get("PySide.QtGui"))
if QtGui and QtGui.QApplication.instance():
return "qt4"
Gtk = (sys.modules.get("gi.repository.Gtk")
or sys.modules.get("pgi.repository.Gtk"))
if Gtk and Gtk.main_level():
return "gtk3"
wx = sys.modules.get("wx")
if wx and wx.GetApp():
return "wx"
tkinter = sys.modules.get("tkinter")
if tkinter:
for frame in sys._current_frames().values():
while frame:
if frame.f_code == tkinter.mainloop.__code__:
return "tk"
frame = frame.f_back
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"
return None


def pylab_setup(name=None):
"""
Return new_figure_manager, draw_if_interactive and show for pyplot.
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ def trigger(self, *args):

@_Backend.export
class _BackendTk(_Backend):
required_interactive_framework = "tk"
FigureManager = FigureManagerTk

@classmethod
Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ def error_msg_gtk(msg, parent=None):

@_Backend.export
class _BackendGTK3(_Backend):
required_interactive_framework = "gtk3"
FigureCanvas = FigureCanvasGTK3
FigureManager = FigureManagerGTK3

Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def set_message(self, message):

@_Backend.export
class _BackendMac(_Backend):
required_interactive_framework = "macosx"
FigureCanvas = FigureCanvasMac
FigureManager = FigureManagerMac

Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_qt4.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

@_BackendQT5.export
class _BackendQT4(_BackendQT5):
pass
required_interactive_framework = "qt4"
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@ def exception_handler(type, value, tb):

@_Backend.export
class _BackendQT5(_Backend):
required_interactive_framework = "qt5"
FigureCanvas = FigureCanvasQT
FigureManager = FigureManagerQT

Expand Down
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,7 @@ def OnPrintPage(self, page):

@_Backend.export
class _BackendWx(_Backend):
required_interactive_framework = "wx"
FigureCanvas = FigureCanvasWx
FigureManager = FigureManagerWx
_frame_class = FigureFrameWx
Expand Down
15 changes: 15 additions & 0 deletions src/_macosx.m
Original file line number Diff line number Diff line change
Expand Up @@ -2789,6 +2789,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 @@ -3038,6 +3048,11 @@ 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,
Expand Down