From 6637e609a039de4a1feda83085ccc623607710c0 Mon Sep 17 00:00:00 2001 From: OceanWolf Date: Thu, 4 Jun 2015 02:11:29 +0200 Subject: [PATCH] Handle FigureManager destroy internaly without pyplot. --- lib/matplotlib/_pylab_helpers.py | 2 +- lib/matplotlib/backend_bases.py | 2 +- lib/matplotlib/backend_managers.py | 24 +++++++++++++++--------- lib/matplotlib/backend_tools.py | 2 +- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/_pylab_helpers.py b/lib/matplotlib/_pylab_helpers.py index b6f28c8628d1..cfe5be3a8a81 100644 --- a/lib/matplotlib/_pylab_helpers.py +++ b/lib/matplotlib/_pylab_helpers.py @@ -124,7 +124,7 @@ def destroy(cls, num): cls._activeQue.append(f) del cls.figs[num] - manager.destroy() + manager.destroy() # Unneeded with MEP27 remove later gc.collect(1) @classmethod diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 9dca1337d9fa..32bd07adc307 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2530,7 +2530,7 @@ def key_press_handler(event, canvas, toolbar=None): if isinstance(canvas.manager, FigureManagerBase): # Using old figman. Gcf.destroy_fig(canvas.figure) else: - canvas.manager._destroy('window_destroy_event') + canvas.manager.destroy('window_destroy_event') if toolbar is not None: # home or reset mnemonic (default key 'h', 'home' and 'r') diff --git a/lib/matplotlib/backend_managers.py b/lib/matplotlib/backend_managers.py index 38886a39b2fe..455c4482e558 100644 --- a/lib/matplotlib/backend_managers.py +++ b/lib/matplotlib/backend_managers.py @@ -78,7 +78,7 @@ def __init__(self, figure, num): self._backend = get_backend() self._mainloop = self._backend.MainLoop() self.window = self._backend.Window('Figure %d' % num) - self.window.mpl_connect('window_destroy_event', self._destroy) + self.window.mpl_connect('window_destroy_event', self.destroy) self.canvas = self._backend.FigureCanvas(figure, manager=self) @@ -122,23 +122,29 @@ def key_press(self, event): """ key_press_handler(event, self.canvas, self.canvas.toolbar) - def _destroy(self, event=None): - # Callback from the when the window wants to destroy itself - s = 'window_destroy_event' - event = FigureManagerEvent(s, self) - self._callbacks.process(s, event) - def destroy(self, *args): - """Called to destroy this FigureManager, gets called by Gcf through - event magic. + """Called to destroy this FigureManager. """ + + # Make sure we run this routine only once for the FigureManager + # This ensures the nasty __del__ fix below works. + if getattr(self, '_destroying', False): + return + + self._destroying = True self.canvas.destroy() if self.toolbar: self.toolbar.destroy() self.window.destroy() + # Fix as for some reason we have extra references to this# + # i.e. ``del self._mainloop`` doesn't work self._mainloop.__del__() + s = 'window_destroy_event' + event = FigureManagerEvent(s, self) + self._callbacks.process(s, event) + def show(self): """Shows the figure""" self.window.show() diff --git a/lib/matplotlib/backend_tools.py b/lib/matplotlib/backend_tools.py index 9544719d097c..9346afb041b1 100644 --- a/lib/matplotlib/backend_tools.py +++ b/lib/matplotlib/backend_tools.py @@ -322,7 +322,7 @@ def trigger(self, sender, event, data=None): except: pass else: - manager._destroy('window_destroy_event') + manager.destroy('window_destroy_event') class ToolEnableAllNavigation(ToolBase):