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

Skip to content

Commit 2c15ab4

Browse files
committed
raise RuntimeError appropriately for animation update func
1 parent 09d8da2 commit 2c15ab4

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

lib/matplotlib/animation.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,9 +1716,19 @@ def _draw_frame(self, framedata):
17161716
# func needs to return a sequence of any artists that were modified.
17171717
self._drawn_artists = self._func(framedata, *self._args)
17181718
if self._blit:
1719-
if self._drawn_artists is None:
1720-
raise RuntimeError('The animation function must return a '
1719+
err = RuntimeError('The animation function must return a '
17211720
'sequence of Artist objects.')
1721+
try:
1722+
# check if a sequence
1723+
iter(self._drawn_artists)
1724+
except TypeError:
1725+
raise err
1726+
1727+
# check each item if is artist
1728+
for i in self._drawn_artists:
1729+
if not isinstance(i, mpl.artist.Artist):
1730+
raise err
1731+
17221732
self._drawn_artists = sorted(self._drawn_artists,
17231733
key=lambda x: x.get_zorder())
17241734

lib/matplotlib/tests/test_animation.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,40 @@ def frames_generator():
272272
# If cache_frame_data is True, then the weakref should be alive;
273273
# if cache_frame_data is False, then the weakref should be dead (None).
274274
assert (f() is None) != cache_frame_data
275+
276+
277+
def test_draw_frame():
278+
# test _draw_frame method
279+
280+
fig, ax = plt.subplots()
281+
line, = ax.plot([])
282+
283+
def init():
284+
pass
285+
286+
def animate_case_1(i):
287+
# user forgot to return (returns None)
288+
line.set_data([0, 1], [0, i])
289+
# return line
290+
291+
def animate_case_2(i):
292+
# user forgot to put comma or return a sequence
293+
# TypeError will be raised (same with returning a number or bool)
294+
line.set_data([0, 1], [0, i])
295+
return line
296+
297+
def animate_case_3(i):
298+
# user (for some reason) returned a string...AttributeError is raised
299+
line.set_data([0, 1], [0, i])
300+
return 'a string'
301+
302+
def animate_case_4(i):
303+
# user returns a sequence other objects instead of Artist.
304+
line.set_data([0, 1], [0, i])
305+
return 'a string',
306+
307+
with pytest.raises(RuntimeError) as context:
308+
animation.FuncAnimation(fig, animate_case_1, blit=True)
309+
animation.FuncAnimation(fig, animate_case_2, blit=True)
310+
animation.FuncAnimation(fig, animate_case_3, blit=True)
311+
animation.FuncAnimation(fig, animate_case_4, blit=True)

0 commit comments

Comments
 (0)