|
1 | 1 | import os
|
2 | 2 | from pathlib import Path
|
3 | 3 | import sys
|
| 4 | +import weakref |
4 | 5 |
|
5 | 6 | import numpy as np
|
6 | 7 | import pytest
|
@@ -260,3 +261,86 @@ def test_failing_ffmpeg(tmpdir, monkeypatch):
|
260 | 261 | make_animation().save("test.mpeg")
|
261 | 262 | finally:
|
262 | 263 | animation.writers.reset_available_writers()
|
| 264 | + |
| 265 | + |
| 266 | +def test_funcanimation_holds_frames_by_default(): |
| 267 | + fig, ax = plt.subplots() |
| 268 | + line, = ax.plot([], []) |
| 269 | + |
| 270 | + class Frame(dict): |
| 271 | + # this subclassing enables to use weakref.ref() |
| 272 | + pass |
| 273 | + |
| 274 | + def init(): |
| 275 | + line.set_data([], []) |
| 276 | + return line, |
| 277 | + |
| 278 | + def animate(frame): |
| 279 | + line.set_data(frame['x'], frame['y']) |
| 280 | + return line, |
| 281 | + |
| 282 | + frames_generated = [] |
| 283 | + |
| 284 | + def frames_generator(): |
| 285 | + for _ in range(5): |
| 286 | + x = np.linspace(0, 10, 100) |
| 287 | + y = np.random.rand(100) |
| 288 | + |
| 289 | + frame = Frame(x=x, y=y) |
| 290 | + |
| 291 | + # collect weak references to frames |
| 292 | + # to validate their references later |
| 293 | + frames_generated.append(weakref.ref(frame)) |
| 294 | + |
| 295 | + yield frame |
| 296 | + |
| 297 | + anim = animation.FuncAnimation(fig, animate, init_func=init, |
| 298 | + frames=frames_generator, save_count=5) |
| 299 | + |
| 300 | + writer = NullMovieWriter() |
| 301 | + anim.save('unused.null', writer=writer) |
| 302 | + |
| 303 | + for f in frames_generated: |
| 304 | + assert f() is not None |
| 305 | + |
| 306 | + |
| 307 | +def test_funcanimation_does_not_hold_frames_when_cache_frame_data_is_False(): |
| 308 | + fig, ax = plt.subplots() |
| 309 | + line, = ax.plot([], []) |
| 310 | + |
| 311 | + class Frame(dict): |
| 312 | + # this subclassing enables to use weakref.ref() |
| 313 | + pass |
| 314 | + |
| 315 | + def init(): |
| 316 | + line.set_data([], []) |
| 317 | + return line, |
| 318 | + |
| 319 | + def animate(frame): |
| 320 | + line.set_data(frame['x'], frame['y']) |
| 321 | + return line, |
| 322 | + |
| 323 | + frames_generated = [] |
| 324 | + |
| 325 | + def frames_generator(): |
| 326 | + for _ in range(5): |
| 327 | + x = np.linspace(0, 10, 100) |
| 328 | + y = np.random.rand(100) |
| 329 | + |
| 330 | + frame = Frame(x=x, y=y) |
| 331 | + |
| 332 | + # collect weak references to frames |
| 333 | + # to validate their references later |
| 334 | + frames_generated.append(weakref.ref(frame)) |
| 335 | + |
| 336 | + yield frame |
| 337 | + |
| 338 | + anim = animation.FuncAnimation(fig, animate, init_func=init, |
| 339 | + frames=frames_generator, save_count=5, |
| 340 | + cache_frame_data=False) |
| 341 | + |
| 342 | + writer = NullMovieWriter() |
| 343 | + anim.save('unused.null', writer=writer) |
| 344 | + |
| 345 | + for f in frames_generated: |
| 346 | + assert f() is None |
0 commit comments