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

Skip to content

Commit 186b3b3

Browse files
authored
Merge pull request #10801 from anntzer/fix-undefined-name
Fix undefined name. Add animation tests.
2 parents f69408f + b114131 commit 186b3b3

File tree

7 files changed

+83
-36
lines changed

7 files changed

+83
-36
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ install:
6363
- activate test-environment
6464
- echo %PYTHON_VERSION% %TARGET_ARCH%
6565
# pytest-cov>=2.3.1 due to https://github.com/pytest-dev/pytest-cov/issues/124
66-
- pip install -q "pytest!=3.3.0,>=3.2.0" "pytest-cov>=2.3.1" pytest-rerunfailures pytest-timeout pytest-xdist
66+
- pip install -q "pytest>=3.4" "pytest-cov>=2.3.1" pytest-rerunfailures pytest-timeout pytest-xdist
6767

6868
# Apply patch to `subprocess` on Python versions > 2 and < 3.6.3
6969
# https://github.com/matplotlib/matplotlib/issues/9176

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ addons:
1818
paths:
1919
- result_images.tar.bz2
2020
apt:
21+
sources:
22+
- sourceline: ppa:jonathonf/ffmpeg-3
2123
packages:
2224
- cm-super
2325
- dvipng
26+
- ffmpeg
2427
- gdb
2528
- gir1.2-gtk-3.0
2629
- graphviz
2730
- inkscape
28-
- libav-tools
2931
- libcairo2
3032
- libgeos-dev
3133
- libgirepository-1.0.1
@@ -52,7 +54,7 @@ env:
5254
- NUMPY=numpy
5355
- PANDAS=
5456
- PYPARSING=pyparsing
55-
- PYTEST='pytest!=3.3.0,>=3.2.0'
57+
- PYTEST='pytest>=3.4'
5658
- PYTEST_COV=pytest-cov
5759
- PYTEST_PEP8=
5860
- SPHINX=sphinx
@@ -74,7 +76,7 @@ matrix:
7476
- NUMPY=numpy==1.10.0
7577
- PANDAS='pandas<0.21.0'
7678
- PYPARSING=pyparsing==2.0.1
77-
- PYTEST=pytest==3.1.0
79+
- PYTEST=pytest==3.4
7880
- PYTEST_COV=pytest-cov==2.3.1
7981
- SPHINX=sphinx==1.3
8082
- python: 3.5

doc/devel/contributing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ environment is set up properly::
151151

152152
.. note::
153153

154-
**Additional dependencies for testing**: pytest_ (version 3.1 or later),
154+
**Additional dependencies for testing**: pytest_ (version 3.4 or later),
155155
Ghostscript_, Inkscape_
156156

157157
.. seealso::

doc/devel/testing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ local FreeType build
2525

2626
The following software is required to run the tests:
2727

28-
- pytest_ (>=3.1)
28+
- pytest_ (>=3.4)
2929
- Ghostscript_ (to render PDF files)
3030
- Inkscape_ (to render SVG files)
3131

lib/matplotlib/animation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,7 @@ def _args(self):
685685
'-vframes', str(self._frame_counter)] + self.output_args
686686

687687

688-
# Base class of avconv information. AVConv has identical arguments to
689-
# FFMpeg
688+
# Base class of avconv information. AVConv has identical arguments to FFMpeg.
690689
class AVConvBase(FFMpegBase):
691690
'''Mixin class for avconv output.
692691
@@ -1340,7 +1339,8 @@ def to_html5_video(self, embed_limit=None):
13401339
# Now open and base64 encode.
13411340
vid64 = base64.encodebytes(path.read_bytes())
13421341

1343-
if len(vid64) >= embed_limit:
1342+
vid_len = len(vid64)
1343+
if vid_len >= embed_limit:
13441344
_log.warning(
13451345
"Animation movie is %s bytes, exceeding the limit of %s. "
13461346
"If you're sure you want a large animation embedded, set "

lib/matplotlib/tests/test_animation.py

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
from pathlib import Path
13
import sys
24
import tempfile
35

@@ -38,29 +40,35 @@ def finish(self):
3840
pass
3941

4042

41-
def test_null_movie_writer():
42-
# Test running an animation with NullMovieWriter.
43-
44-
fig = plt.figure()
43+
def make_animation(**kwargs):
44+
fig, ax = plt.subplots()
45+
line, = ax.plot([])
4546

4647
def init():
4748
pass
4849

4950
def animate(i):
50-
pass
51+
line.set_data([0, 1], [0, i])
52+
return line,
53+
54+
return animation.FuncAnimation(fig, animate, **kwargs)
55+
56+
57+
def test_null_movie_writer():
58+
# Test running an animation with NullMovieWriter.
5159

5260
num_frames = 5
61+
anim = make_animation(frames=num_frames)
62+
5363
filename = "unused.null"
5464
dpi = 50
5565
savefig_kwargs = dict(foo=0)
56-
57-
anim = animation.FuncAnimation(fig, animate, init_func=init,
58-
frames=num_frames)
5966
writer = NullMovieWriter()
67+
6068
anim.save(filename, dpi=dpi, writer=writer,
6169
savefig_kwargs=savefig_kwargs)
6270

63-
assert writer.fig == fig
71+
assert writer.fig == plt.figure(1) # The figure used by make_animation.
6472
assert writer.outfile == filename
6573
assert writer.dpi == dpi
6674
assert writer.args == ()
@@ -174,23 +182,8 @@ def animate(i):
174182

175183

176184
def test_no_length_frames():
177-
fig, ax = plt.subplots()
178-
line, = ax.plot([], [])
179-
180-
def init():
181-
line.set_data([], [])
182-
return line,
183-
184-
def animate(i):
185-
x = np.linspace(0, 10, 100)
186-
y = np.sin(x + i)
187-
line.set_data(x, y)
188-
return line,
189-
190-
anim = animation.FuncAnimation(fig, animate, init_func=init,
191-
frames=iter(range(5)))
192-
writer = NullMovieWriter()
193-
anim.save('unused.null', writer=writer)
185+
(make_animation(frames=iter(range(5)))
186+
.save('unused.null', writer=NullMovieWriter()))
194187

195188

196189
def test_movie_writer_registry():
@@ -215,3 +208,55 @@ def test_movie_writer_registry():
215208
assert not animation.writers._dirty
216209
assert animation.writers.is_available("ffmpeg")
217210
mpl.rcParams['animation.ffmpeg_path'] = ffmpeg_path
211+
212+
213+
@pytest.mark.skipif(
214+
not animation.writers.is_available(mpl.rcParams["animation.writer"]),
215+
reason="animation writer not installed")
216+
@pytest.mark.parametrize("method_name", ["to_html5_video", "to_jshtml"])
217+
def test_embed_limit(method_name, caplog):
218+
with mpl.rc_context({"animation.embed_limit": 1e-6}): # ~1 byte.
219+
getattr(make_animation(frames=1), method_name)()
220+
assert len(caplog.records) == 1
221+
record, = caplog.records
222+
assert (record.name == "matplotlib.animation"
223+
and record.levelname == "WARNING")
224+
225+
226+
@pytest.mark.skipif(
227+
not animation.writers.is_available(mpl.rcParams["animation.writer"]),
228+
reason="animation writer not installed")
229+
@pytest.mark.parametrize(
230+
"method_name",
231+
["to_html5_video",
232+
pytest.mark.xfail("to_jshtml")]) # Needs to be fixed.
233+
def test_cleanup_temporaries(method_name, tmpdir):
234+
with tmpdir.as_cwd():
235+
getattr(make_animation(frames=1), method_name)()
236+
assert list(Path(str(tmpdir)).iterdir()) == []
237+
238+
239+
# Currently, this fails with a ValueError after we try to communicate() twice
240+
# with the Popen.
241+
@pytest.mark.xfail
242+
@pytest.mark.skipif(os.name != "posix", reason="requires a POSIX OS")
243+
def test_failing_ffmpeg(tmpdir, monkeypatch):
244+
"""
245+
Test that we correctly raise an OSError when ffmpeg fails.
246+
247+
To do so, mock ffmpeg using a simple executable shell script that
248+
succeeds when called with no arguments (so that it gets registered by
249+
`isAvailable`), but fails otherwise, and add it to the $PATH.
250+
"""
251+
try:
252+
with tmpdir.as_cwd():
253+
monkeypatch.setenv("PATH", ".:" + os.environ["PATH"])
254+
exe_path = Path(tmpdir, "ffmpeg")
255+
exe_path.write_text("#!/bin/sh\n"
256+
"[[ $@ -eq 0 ]]\n")
257+
os.chmod(str(exe_path), 0o755)
258+
animation.writers.reset_available_writers()
259+
with pytest.raises(OSError):
260+
make_animation().save("test.mpeg")
261+
finally:
262+
animation.writers.reset_available_writers()

setupext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ def get_namespace_packages(self):
739739

740740
class Tests(OptionalPackage):
741741
name = "tests"
742-
pytest_min_version = '3.1'
742+
pytest_min_version = '3.4'
743743
default_config = False
744744

745745
def check(self):

0 commit comments

Comments
 (0)