Skip to content

Commit b60db73

Browse files
committed
Remove __del__ method
1 parent adfd6bb commit b60db73

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

lib/matplotlib/tests/test_widgets.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,10 @@ def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):
518518

519519

520520
def test_axeswidget_del_on_failed_init():
521-
"""Test that __del__ works correctly when __init__ fails."""
522-
# __del__ is called implicitly as part of a failed initialization
523-
# here, which could create an unraisable exception in __del__.
521+
"""
522+
Test that an unraisable exception is not created when initialization
523+
fails.
524+
"""
524525
# Pytest would fail the test if such an exception occurred.
525526
fig, ax = plt.subplots()
526527
with pytest.raises(TypeError, match="unexpected keyword argument 'undefined'"):

lib/matplotlib/widgets.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import enum
1515
import functools
1616
import itertools
17+
import weakref
1718
from numbers import Integral, Number
1819

1920
from cycler import cycler
@@ -120,16 +121,6 @@ def __init__(self, ax):
120121
self._cids = []
121122
self._blit_background_id = None
122123

123-
def __del__(self):
124-
blit_background_id = getattr(self, '_blit_background_id', None)
125-
# __del__ may be called on a partially initialized object, e.g.,
126-
# when __init__ raises. Therefore, we handle missing attributes
127-
# gracefully.
128-
if blit_background_id is not None:
129-
canvas = getattr(self, 'canvas', None)
130-
if canvas is not None:
131-
canvas._release_blit_background_id(blit_background_id)
132-
133124
canvas = property(
134125
lambda self: getattr(self.ax.get_figure(root=True), 'canvas', None)
135126
)
@@ -170,7 +161,9 @@ def _save_blit_background(self, background):
170161
good enough for all existing widgets.
171162
"""
172163
if self._blit_background_id is None:
173-
self._blit_background_id = self.canvas._get_blit_background_id()
164+
bbid = self.canvas._get_blit_background_id()
165+
weakref.finalize(self, self.canvas._release_blit_background_id, bbid)
166+
self._blit_background_id = bbid
174167
self.canvas._blit_backgrounds[self._blit_background_id] = background
175168

176169
def _load_blit_background(self):

0 commit comments

Comments
 (0)