Skip to content
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
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,17 @@ def test_rectangle_resize_square_center_aspect(ax, use_data_coordinates):
46.25, 133.75])


def test_axeswidget_del_on_failed_init():
"""
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'"):
widgets.Button(ax, undefined='bar')


def test_ellipse(ax):
"""For ellipse, test out the key modifiers"""
tool = widgets.EllipseSelector(ax, grab_range=10, interactive=True)
Expand Down
9 changes: 4 additions & 5 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,10 +121,6 @@
self._cids = []
self._blit_background_id = None

def __del__(self):
if self._blit_background_id is not None:
self.canvas._release_blit_background_id(self._blit_background_id)

canvas = property(
lambda self: getattr(self.ax.get_figure(root=True), 'canvas', None)
)
Expand Down Expand Up @@ -164,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
1 change: 0 additions & 1 deletion lib/matplotlib/widgets.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Widget:
class AxesWidget(Widget):
ax: Axes
def __init__(self, ax: Axes) -> None: ...
def __del__(self) -> None: ...
@property
def canvas(self) -> FigureCanvasBase | None: ...
def connect_event(self, event: Event, callback: Callable) -> None: ...
Expand Down
Loading