Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove __del__ method
  • Loading branch information
larrybradley committed Jan 23, 2026
commit b60db73f4de57246e855fb7f676217be4109f084
7 changes: 4 additions & 3 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,10 @@ def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):


def test_axeswidget_del_on_failed_init():
"""Test that __del__ works correctly when __init__ fails."""
# __del__ is called implicitly as part of a failed initialization
# here, which could create an unraisable exception in __del__.
"""
Test that an unraisable exception is not created when initialization
fails.
"""
# Pytest would fail the test if such an exception occurred.
fig, ax = plt.subplots()
with pytest.raises(TypeError, match="unexpected keyword argument 'undefined'"):
Expand Down
15 changes: 4 additions & 11 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""
GUI neutral widgets
===================
Expand All @@ -14,6 +14,7 @@
import enum
import functools
import itertools
import weakref
from numbers import Integral, Number

from cycler import cycler
Expand Down Expand Up @@ -120,16 +121,6 @@
self._cids = []
self._blit_background_id = None

def __del__(self):
blit_background_id = getattr(self, '_blit_background_id', None)
# __del__ may be called on a partially initialized object, e.g.,
# when __init__ raises. Therefore, we handle missing attributes
# gracefully.
if blit_background_id is not None:
canvas = getattr(self, 'canvas', None)
if canvas is not None:
canvas._release_blit_background_id(blit_background_id)

canvas = property(
lambda self: getattr(self.ax.get_figure(root=True), 'canvas', None)
)
Expand Down Expand Up @@ -170,7 +161,9 @@
good enough for all existing widgets.
"""
if self._blit_background_id is None:
self._blit_background_id = self.canvas._get_blit_background_id()
bbid = self.canvas._get_blit_background_id()
weakref.finalize(self, self.canvas._release_blit_background_id, bbid)
self._blit_background_id = bbid
self.canvas._blit_backgrounds[self._blit_background_id] = background

def _load_blit_background(self):
Expand Down
Loading