Skip to content

Destroy figures by manager instance, not by number. #16819

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 1 commit into from
Mar 19, 2020
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
20 changes: 14 additions & 6 deletions lib/matplotlib/_pylab_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,23 @@ def get_fig_manager(cls, num):
@classmethod
def destroy(cls, num):
"""
Destroy figure number *num*.
Destroy manager *num* -- either a manager instance or a manager number.

In the interactive backends, this is bound to the window "destroy" and
"delete" events.

It is recommended to pass a manager instance, to avoid confusion when
two managers share the same number.
"""
if not cls.has_fignum(num):
return
manager = cls.figs.pop(num)
if all(hasattr(num, attr) for attr in ["num", "_cidgcf", "destroy"]):
manager = num
if cls.figs.get(manager.num) is manager:
cls.figs.pop(manager.num)
else:
try:
manager = cls.figs.pop(num)
except KeyError:
return
manager.canvas.mpl_disconnect(manager._cidgcf)
manager.destroy()
gc.collect(1)
Expand All @@ -62,8 +71,7 @@ def destroy_fig(cls, fig):
"""Destroy figure *fig*."""
canvas = getattr(fig, "canvas", None)
manager = getattr(canvas, "manager", None)
num = getattr(manager, "num", None)
cls.destroy(num)
cls.destroy(manager)

@classmethod
def destroy_all(cls):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ def show(self):
if not self._shown:
def destroy(*args):
self.window = None
Gcf.destroy(self.num)
Gcf.destroy(self)
self.canvas._tkcanvas.bind("<Destroy>", destroy)
self.window.deiconify()
else:
Expand Down
6 changes: 2 additions & 4 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,8 @@ def add_widget(child):

self.window.set_default_size(w, h)

def destroy(*args):
Gcf.destroy(num)
self.window.connect("destroy", destroy)
self.window.connect("delete_event", destroy)
self.window.connect("destroy", lambda *args: Gcf.destroy(self))
self.window.connect("delete_event", lambda *args: Gcf.destroy(self))
if mpl.is_interactive():
self.window.show()
self.canvas.draw_idle()
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(self, canvas, num):
self.canvas.draw_idle()

def close(self):
Gcf.destroy(self.num)
Gcf.destroy(self)


class NavigationToolbar2Mac(_macosx.NavigationToolbar2, NavigationToolbar2):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_nbagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def new_figure_manager_given_figure(num, figure):
if is_interactive():
manager.show()
figure.canvas.draw_idle()
canvas.mpl_connect('close_event', lambda event: Gcf.destroy(num))
canvas.mpl_connect('close_event', lambda event: Gcf.destroy(manager))
return manager

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def _widgetclosed(self):
return
self.window._destroying = True
try:
Gcf.destroy(self.num)
Gcf.destroy(self)
except AttributeError:
pass
# It seems that when the python session is killed,
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_wx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ def _onClose(self, event):
_log.debug("%s - onClose()", type(self))
self.canvas.close_event()
self.canvas.stop_event_loop()
Gcf.destroy(self.num)
Gcf.destroy(self)
# self.Destroy()

def GetToolBar(self):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ def close(fig=None):
if figManager is None:
return
else:
_pylab_helpers.Gcf.destroy(figManager.num)
_pylab_helpers.Gcf.destroy(figManager)
elif fig == 'all':
_pylab_helpers.Gcf.destroy_all()
elif isinstance(fig, int):
Expand Down