From b57c6dc0b154c52b641540ec20e3eb0938be03ae Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sat, 30 Nov 2013 13:15:03 -0600 Subject: [PATCH 1/4] Check if iterator has length If an iterable is passed into `FuncAnimation` check if it has `__len__` before trying to take it's length closes #1769 --- lib/matplotlib/animation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index 9854ca2527f6..6cb593f00614 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -1000,7 +1000,8 @@ def __init__(self, fig, func, frames=None, init_func=None, fargs=None, self._iter_gen = frames elif iterable(frames): self._iter_gen = lambda: iter(frames) - self.save_count = len(frames) + if hasattr(frames, '__len__'): + self.save_count = len(frames) else: self._iter_gen = lambda: xrange(frames).__iter__() self.save_count = frames From 03c71f53cd8d1d39f4ef16687800ce07a3bea06b Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Dec 2013 14:33:17 -0600 Subject: [PATCH 2/4] added test for code path where the object passed into `frames` is does not have a length. --- lib/matplotlib/tests/test_animation.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/matplotlib/tests/test_animation.py b/lib/matplotlib/tests/test_animation.py index 04fb65291b5b..532f2ca15184 100644 --- a/lib/matplotlib/tests/test_animation.py +++ b/lib/matplotlib/tests/test_animation.py @@ -52,6 +52,25 @@ def animate(i): anim.save(F.name, fps=30, writer=writer) +@cleanup +def test_(): + fig, ax = plt.subplots() + line, = ax.plot([], []) + + def init(): + line.set_data([], []) + return line, + + def animate(i): + x = np.linspace(0, 10, 100) + y = np.sin(x + i) + line.set_data(x, y) + return line, + + anim = animation.FuncAnimation(fig, animate, init_func=init, + frames=iter(range(5))) + + if __name__ == "__main__": import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False) From 8ab1f0ab24d089f82db076c1d0284186dea80608 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Dec 2013 14:36:19 -0600 Subject: [PATCH 3/4] corrected test function name --- lib/matplotlib/tests/test_animation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_animation.py b/lib/matplotlib/tests/test_animation.py index 532f2ca15184..2f5495643c03 100644 --- a/lib/matplotlib/tests/test_animation.py +++ b/lib/matplotlib/tests/test_animation.py @@ -53,7 +53,7 @@ def animate(i): @cleanup -def test_(): +def test_no_length_frames(): fig, ax = plt.subplots() line, = ax.plot([], []) From 1b144e879455f3e6168c9c6e5899860cf2eca185 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 12 Dec 2013 17:45:16 -0600 Subject: [PATCH 4/4] added test_animation back in to default_test_modules so that the iterator test actually runs --- lib/matplotlib/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index b61f183b767b..633f69b22d8b 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1288,6 +1288,7 @@ def tk_window_focus(): default_test_modules = [ 'matplotlib.tests.test_agg', + 'matplotlib.tests.test_animation', 'matplotlib.tests.test_arrow_patches', 'matplotlib.tests.test_artist', 'matplotlib.tests.test_axes',