Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4723dab

Browse files
authored
Merge pull request #17699 from johnnyEmpires/draw-frame-error-check
raise RuntimeError appropriately for animation update func
2 parents 3b097c6 + a37e61e commit 4723dab

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

lib/matplotlib/animation.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,10 +1716,22 @@ def _draw_frame(self, framedata):
17161716
# Call the func with framedata and args. If blitting is desired,
17171717
# func needs to return a sequence of any artists that were modified.
17181718
self._drawn_artists = self._func(framedata, *self._args)
1719+
17191720
if self._blit:
1720-
if self._drawn_artists is None:
1721-
raise RuntimeError('The animation function must return a '
1721+
1722+
err = RuntimeError('The animation function must return a '
17221723
'sequence of Artist objects.')
1724+
try:
1725+
# check if a sequence
1726+
iter(self._drawn_artists)
1727+
except TypeError:
1728+
raise err
1729+
1730+
# check each item if is artist
1731+
for i in self._drawn_artists:
1732+
if not isinstance(i, mpl.artist.Artist):
1733+
raise err
1734+
17231735
self._drawn_artists = sorted(self._drawn_artists,
17241736
key=lambda x: x.get_zorder())
17251737

lib/matplotlib/tests/test_animation.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import matplotlib as mpl
1111
from matplotlib import pyplot as plt
1212
from matplotlib import animation
13+
from matplotlib import artist
1314

1415

1516
class NullMovieWriter(animation.AbstractMovieWriter):
@@ -272,3 +273,36 @@ def frames_generator():
272273
# If cache_frame_data is True, then the weakref should be alive;
273274
# if cache_frame_data is False, then the weakref should be dead (None).
274275
assert (f() is None) != cache_frame_data
276+
277+
278+
def test_draw_frame():
279+
# test _draw_frame method
280+
281+
fig, ax = plt.subplots()
282+
line, = ax.plot([])
283+
284+
def animate(i, arg):
285+
# general update func
286+
line.set_data([0, 1], [0, i])
287+
if arg:
288+
return arg
289+
290+
with pytest.raises(RuntimeError):
291+
292+
# user forgot to return (returns None)
293+
animation.FuncAnimation(fig, animate, blit=True, fargs=(None,))
294+
295+
# user (for some reason) returned a string...AttributeError is raised
296+
animation.FuncAnimation(fig, animate, blit=True, fargs=('string', ))
297+
298+
# user (for some reason) returned a string...AttributeError is raised
299+
animation.FuncAnimation(fig, animate, blit=True, fargs=(1, ))
300+
301+
# user returns a sequence of other objects
302+
# e.g. a string instead of Artist
303+
animation.FuncAnimation(fig, animate, blit=True, fargs=(('string',), ))
304+
305+
# user forgot to put comma or return a sequence
306+
# TypeError will be raised (same with returning a number or bool)
307+
artist_obj = artist.Artist()
308+
animation.FuncAnimation(fig, animate, blit=True, fargs=(artist_obj, ))

0 commit comments

Comments
 (0)