Skip to content

When no gui event loop is running, propagate callback exceptions. #15270

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 3 commits into from
Oct 20, 2019
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
8 changes: 8 additions & 0 deletions doc/api/next_api_changes/behaviour.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ Previously, it did not necessarily have such an attribute. A check for
``hasattr(figure.canvas, "manager")`` should now be replaced by
``figure.canvas.manager is not None`` (or ``getattr(figure.canvas, "manager", None) is not None``
for back-compatibility).

`.cbook.CallbackRegistry` now propagates exceptions when no GUI event loop is running
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`.cbook.CallbackRegistry` now defaults to propagating exceptions thrown by
callbacks when no interactive GUI event loop is running. If a GUI event loop
*is* running, `.cbook.CallbackRegistry` still defaults to just printing a
traceback, as unhandled exceptions can make the program completely ``abort()``
in that case.
3 changes: 1 addition & 2 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

import numpy as np

import matplotlib as mpl
from matplotlib import (
backend_tools as tools, cbook, colors, textpath, tight_bbox, transforms,
widgets, get_backend, is_interactive, rcParams)
Expand Down Expand Up @@ -2552,7 +2551,7 @@ def show(self):
warning in `.Figure.show`.
"""
# This should be overridden in GUI backends.
if mpl.backends._get_running_interactive_framework() != "headless":
if cbook._get_running_interactive_framework() != "headless":
raise NonGuiException(
f"Matplotlib is currently using {get_backend()}, which is "
f"a non-GUI backend, so cannot show the figure.")
Expand Down
47 changes: 0 additions & 47 deletions lib/matplotlib/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,2 @@
import logging
import os
import sys

_log = logging.getLogger(__name__)


# NOTE: plt.switch_backend() (called at import time) will add a "backend"
# attribute here for backcompat.


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")
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
if 'matplotlib.backends._macosx' in sys.modules:
if sys.modules["matplotlib.backends._macosx"].event_loop_is_running():
return "macosx"
if sys.platform.startswith("linux") and not os.environ.get("DISPLAY"):
return "headless"
return None
45 changes: 44 additions & 1 deletion lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,51 @@
MatplotlibDeprecationWarning, mplDeprecation)


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")
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
if 'matplotlib.backends._macosx' in sys.modules:
if sys.modules["matplotlib.backends._macosx"].event_loop_is_running():
return "macosx"
if sys.platform.startswith("linux") and not os.environ.get("DISPLAY"):
return "headless"
return None


def _exception_printer(exc):
traceback.print_exc()
if _get_running_interactive_framework() in ["headless", None]:
raise exc
else:
traceback.print_exc()


class _StrongRef:
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np

from matplotlib import rcParams
from matplotlib import backends, docstring, projections
from matplotlib import docstring, projections
from matplotlib import __version__ as _mpl_version
from matplotlib import get_backend

Expand Down
6 changes: 2 additions & 4 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
FormatStrFormatter, ScalarFormatter, LogFormatter, LogFormatterExponent,
LogFormatterMathtext, Locator, IndexLocator, FixedLocator, NullLocator,
LinearLocator, LogLocator, AutoLocator, MultipleLocator, MaxNLocator)
from matplotlib.backends import _get_running_interactive_framework

_log = logging.getLogger(__name__)

Expand Down Expand Up @@ -226,8 +225,7 @@ def switch_backend(newbackend):
required_framework = getattr(
Backend.FigureCanvas, "required_interactive_framework", None)
if required_framework is not None:
current_framework = \
matplotlib.backends._get_running_interactive_framework()
current_framework = cbook._get_running_interactive_framework()
if (current_framework and required_framework
and current_framework != required_framework):
raise ImportError(
Expand Down Expand Up @@ -2250,7 +2248,7 @@ def getname_val(identifier):
# is compatible with the current running interactive framework.
if (rcParams["backend_fallback"]
and dict.__getitem__(rcParams, "backend") in _interactive_bk
and _get_running_interactive_framework()):
and cbook._get_running_interactive_framework()):
dict.__setitem__(rcParams, "backend", rcsetup._auto_backend_sentinel)
# Set up the backend.
switch_backend(rcParams["backend"])
Expand Down
38 changes: 26 additions & 12 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,22 +233,33 @@ def test_pickling(self):
"callbacks")


def test_callbackregistry_default_exception_handler(monkeypatch):
cb = cbook.CallbackRegistry()
cb.connect("foo", lambda: None)
monkeypatch.setattr(
cbook, "_get_running_interactive_framework", lambda: None)
with pytest.raises(TypeError):
cb.process("foo", "argument mismatch")
monkeypatch.setattr(
cbook, "_get_running_interactive_framework", lambda: "not-none")
cb.process("foo", "argument mismatch") # No error in that case.


def raising_cb_reg(func):
class TestException(Exception):
pass

def raising_function():
raise RuntimeError

def raising_function_VE():
raise ValueError

def transformer(excp):
if isinstance(excp, RuntimeError):
raise TestException
raise excp

# default behavior
cb = cbook.CallbackRegistry()
cb.connect('foo', raising_function)

# old default
cb_old = cbook.CallbackRegistry(exception_handler=None)
cb_old.connect('foo', raising_function)
Expand All @@ -257,18 +268,21 @@ def transformer(excp):
cb_filt = cbook.CallbackRegistry(exception_handler=transformer)
cb_filt.connect('foo', raising_function)

# filter
cb_filt_pass = cbook.CallbackRegistry(exception_handler=transformer)
cb_filt_pass.connect('foo', raising_function_VE)

return pytest.mark.parametrize('cb, excp',
[[cb, None],
[cb_old, RuntimeError],
[cb_filt, TestException]])(func)
[[cb_old, RuntimeError],
[cb_filt, TestException],
[cb_filt_pass, ValueError]])(func)


@raising_cb_reg
def test_callbackregistry_process_exception(cb, excp):
if excp is not None:
with pytest.raises(excp):
cb.process('foo')
else:
def test_callbackregistry_custom_exception_handler(monkeypatch, cb, excp):
monkeypatch.setattr(
cbook, "_get_running_interactive_framework", lambda: None)
with pytest.raises(excp):
cb.process('foo')


Expand Down