From 3ba761f325f4c6ecc4f12ef324fb1d74d7aa5f6d Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 6 Sep 2019 23:25:22 +0100 Subject: [PATCH] Backport PR #15206: FIX: be more forgiving about expecting internal state in draw_idle Merge pull request #15206 from tacaswell/fix_python_ion_second_qtcanvas_init FIX: be more forgiving about expecting internal state in draw_idle Conflicts: lib/matplotlib/tests/test_backend_qt.py - two tests were added on master, only kept the relevant one --- lib/matplotlib/backends/backend_qt5.py | 3 ++- lib/matplotlib/tests/test_backend_qt.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index 1e60ec14dab9..d7a4fd0f0ca0 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -492,7 +492,8 @@ def draw_idle(self): # current event loop in order to ensure thread affinity and to # accumulate multiple draw requests from event handling. # TODO: queued signal connection might be safer than singleShot - if not (self._draw_pending or self._is_drawing): + if not (getattr(self, '_draw_pending', False) or + getattr(self, '._is_drawing', False)): self._draw_pending = True QtCore.QTimer.singleShot(0, self._draw_idle) diff --git a/lib/matplotlib/tests/test_backend_qt.py b/lib/matplotlib/tests/test_backend_qt.py index f6493cfb9c38..0d4f1bec56a6 100644 --- a/lib/matplotlib/tests/test_backend_qt.py +++ b/lib/matplotlib/tests/test_backend_qt.py @@ -307,3 +307,23 @@ def test_figureoptions(): "matplotlib.backends.qt_editor._formlayout.FormDialog.exec_", lambda self: None): fig.canvas.manager.toolbar.edit_parameters() + + +@pytest.mark.backend("Qt5Agg") +def test_canvas_reinit(): + import matplotlib.pyplot as plt + from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg + from functools import partial + + called = False + + def crashing_callback(fig, stale): + nonlocal called + fig.canvas.draw_idle() + called = True + + fig, ax = plt.subplots() + fig.stale_callback = crashing_callback + # this should not raise + canvas = FigureCanvasQTAgg(fig) + assert called