|
2 | 2 | from pathlib import Path |
3 | 3 | import subprocess |
4 | 4 | import sys |
| 5 | +import weakref |
5 | 6 |
|
6 | 7 | import numpy as np |
7 | 8 | from pathlib import Path |
@@ -258,3 +259,51 @@ def test_failing_ffmpeg(tmpdir, monkeypatch): |
258 | 259 | make_animation().save("test.mpeg") |
259 | 260 | finally: |
260 | 261 | animation.writers.reset_available_writers() |
| 262 | + |
| 263 | + |
| 264 | +@pytest.mark.parametrize("cache_frame_data, weakref_assertion_fn", [ |
| 265 | + pytest.param( |
| 266 | + False, lambda ref: ref is None, id='cache_frame_data_is_disabled'), |
| 267 | + pytest.param( |
| 268 | + True, lambda ref: ref is not None, id='cache_frame_data_is_enabled'), |
| 269 | +]) |
| 270 | +def test_funcanimation_holding_frames(cache_frame_data, weakref_assertion_fn): |
| 271 | + fig, ax = plt.subplots() |
| 272 | + line, = ax.plot([], []) |
| 273 | + |
| 274 | + class Frame(dict): |
| 275 | + # this subclassing enables to use weakref.ref() |
| 276 | + pass |
| 277 | + |
| 278 | + def init(): |
| 279 | + line.set_data([], []) |
| 280 | + return line, |
| 281 | + |
| 282 | + def animate(frame): |
| 283 | + line.set_data(frame['x'], frame['y']) |
| 284 | + return line, |
| 285 | + |
| 286 | + frames_generated = [] |
| 287 | + |
| 288 | + def frames_generator(): |
| 289 | + for _ in range(5): |
| 290 | + x = np.linspace(0, 10, 100) |
| 291 | + y = np.random.rand(100) |
| 292 | + |
| 293 | + frame = Frame(x=x, y=y) |
| 294 | + |
| 295 | + # collect weak references to frames |
| 296 | + # to validate their references later |
| 297 | + frames_generated.append(weakref.ref(frame)) |
| 298 | + |
| 299 | + yield frame |
| 300 | + |
| 301 | + anim = animation.FuncAnimation(fig, animate, init_func=init, |
| 302 | + frames=frames_generator, |
| 303 | + cache_frame_data=cache_frame_data) |
| 304 | + |
| 305 | + writer = NullMovieWriter() |
| 306 | + anim.save('unused.null', writer=writer) |
| 307 | + assert len(frames_generated) == 5 |
| 308 | + for f in frames_generated: |
| 309 | + assert weakref_assertion_fn(f()) |
0 commit comments