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

Skip to content

Commit a5a47a5

Browse files
committed
Add test for close_event.
Test that a single close_event is triggered when a window is closed. This caught a bug in the gtk3 backends, which used to emit *two* close_events (see comment there; a better fix would be welcome); and another in the wx backends (which emit *no* close_events when the window is closed by pressing "q") which I didn't see how to fix and is just xfailed for now.
1 parent 54b4263 commit a5a47a5

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ def add_widget(child):
367367

368368
self.window.set_default_size(w, h)
369369

370+
self._destroying = False
370371
self.window.connect("destroy", lambda *args: Gcf.destroy(self))
371372
self.window.connect("delete_event", lambda *args: Gcf.destroy(self))
372373
if mpl.is_interactive():
@@ -376,6 +377,13 @@ def add_widget(child):
376377
self.canvas.grab_focus()
377378

378379
def destroy(self, *args):
380+
if self._destroying:
381+
# Otherwise, this can be called twice when the user presses 'q',
382+
# which calls Gcf.destroy(self), then this destroy(), then triggers
383+
# Gcf.destroy(self) once again via
384+
# `connect("destroy", lambda *args: Gcf.destroy(self))`.
385+
return
386+
self._destroying = True
379387
self.vbox.destroy()
380388
self.window.destroy()
381389
self.canvas.destroy()

lib/matplotlib/tests/test_backends_interactive.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def check_alt_backend(alt_backend):
111111
timer.add_callback(FigureCanvasBase.key_press_event, fig.canvas, "q")
112112
# Trigger quitting upon draw.
113113
fig.canvas.mpl_connect("draw_event", lambda event: timer.start())
114+
fig.canvas.mpl_connect("close_event", print)
114115
115116
plt.show()
116117
"""
@@ -120,12 +121,19 @@ def check_alt_backend(alt_backend):
120121
@pytest.mark.parametrize("backend", _get_testable_interactive_backends())
121122
@pytest.mark.flaky(reruns=3)
122123
def test_interactive_backend(backend):
123-
proc = subprocess.run([sys.executable, "-c", _test_script],
124-
env={**os.environ, "MPLBACKEND": backend},
125-
timeout=_test_timeout)
124+
proc = subprocess.run(
125+
[sys.executable, "-c", _test_script],
126+
env={**os.environ, "MPLBACKEND": backend}, timeout=_test_timeout,
127+
stdout=subprocess.PIPE, universal_newlines=True)
126128
if proc.returncode:
127129
pytest.fail("The subprocess returned with non-zero exit status "
128130
f"{proc.returncode}.")
131+
close_events_count = proc.stdout.count("CloseEvent")
132+
if backend.startswith("wx") and close_events_count != 1:
133+
pytest.xfail(f"{close_events_count} CloseEvent(s) emitted by "
134+
f"{backend}; expected 1")
135+
else:
136+
assert close_events_count == 1
129137

130138

131139
@pytest.mark.skipif('SYSTEM_TEAMFOUNDATIONCOLLECTIONURI' in os.environ,

0 commit comments

Comments
 (0)