Skip to content

Commit c7c727d

Browse files
committed
Make plt.close() call thread safe to pass interactive backend tests
Prevent multiple attempts to close the frame by setting to FigureManagerWx.frame to None when closed by GUI. When closed by plt.close() call, the frame is closed in a threadsafe way using wx.CallAfter( frame.Close )
1 parent d165b0a commit c7c727d

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/matplotlib/backends/backend_wx.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,14 @@ def _onClose(self, event):
971971
_log.debug("%s - onClose()", type(self))
972972
self.canvas.close_event()
973973
self.canvas.stop_event_loop()
974-
Gcf.destroy(self.figmgr)
974+
# set FigureManagerWx.frame to None to prevent repeated attempts to close
975+
# this frame from with the FigureManagerWx.destroy()
976+
if self.figmgr is not None:
977+
self.figmgr.frame = None
978+
# remove figure manager from Gcf.figs
979+
Gcf.destroy(self.figmgr)
980+
#carry on with wx close event propagation, frame & children destruction
981+
event.Skip()
975982

976983
def GetToolBar(self):
977984
"""Override wxFrame::GetToolBar as we don't have managed toolbar"""
@@ -988,8 +995,9 @@ def Destroy(self, *args, **kwargs):
988995
# MPLBACKEND=wxagg python -c 'from pylab import *; plot()'.
989996
if self and not self.IsBeingDeleted():
990997
super().Destroy(*args, **kwargs)
991-
if self.toolbar is not None:
992-
self.toolbar.Destroy()
998+
# This should not be necessary if the close event is allowed to propagate.
999+
#if self.toolbar is not None:
1000+
# self.toolbar.Destroy()
9931001
return True
9941002

9951003

@@ -1038,7 +1046,9 @@ def destroy(self, *args):
10381046
_log.debug("%s - destroy()", type(self))
10391047
frame = self.frame
10401048
if frame: # Else, may have been already deleted, e.g. when closing.
1041-
frame.Destroy()
1049+
# as this can be called from non gui thread from plt.close use
1050+
# wx.CallAfter to esnure thread safety.
1051+
wx.CallAfter( frame.Close )
10421052

10431053
def full_screen_toggle(self):
10441054
# docstring inherited

0 commit comments

Comments
 (0)