Skip to content

Commit 4e46319

Browse files
committed
Added tests for PEP 519/pathlib, these should fail
1 parent 5f553bc commit 4e46319

File tree

5 files changed

+305
-2
lines changed

5 files changed

+305
-2
lines changed

lib/matplotlib/tests/test_animation.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010
from numpy.testing import assert_equal
1111
from nose.tools import assert_false, assert_true
12+
from nose.plugins.skip import SkipTest
1213
import matplotlib as mpl
1314
from matplotlib import pyplot as plt
1415
from matplotlib import animation
@@ -107,6 +108,12 @@ def test_save_animation_smoketest():
107108
for writer, extension in six.iteritems(WRITER_OUTPUT):
108109
yield check_save_animation, writer, extension
109110

111+
for writer, extension in six.iteritems(WRITER_OUTPUT):
112+
yield check_save_animation_pep_519, writer, extension
113+
114+
for writer, extension in six.iteritems(WRITER_OUTPUT):
115+
yield check_save_animation_pathlib, writer, extension
116+
110117

111118
@cleanup
112119
def check_save_animation(writer, extension='mp4'):
@@ -144,6 +151,91 @@ def animate(i):
144151
pass
145152

146153

154+
@cleanup
155+
def check_save_animation_pep_519(writer, extension='mp4'):
156+
class FakeFSPathClass(object):
157+
def __init__(self, path):
158+
self._path = path
159+
160+
def __fspath__(self):
161+
return self._path
162+
163+
if not animation.writers.is_available(writer):
164+
skip("writer '%s' not available on this system"
165+
% writer)
166+
fig, ax = plt.subplots()
167+
line, = ax.plot([], [])
168+
169+
ax.set_xlim(0, 10)
170+
ax.set_ylim(-1, 1)
171+
172+
def init():
173+
line.set_data([], [])
174+
return line,
175+
176+
def animate(i):
177+
x = np.linspace(0, 10, 100)
178+
y = np.sin(x + i)
179+
line.set_data(x, y)
180+
return line,
181+
182+
# Use NamedTemporaryFile: will be automatically deleted
183+
F = tempfile.NamedTemporaryFile(suffix='.' + extension)
184+
F.close()
185+
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5)
186+
try:
187+
anim.save(FakeFSPathClass(F.name), fps=30, writer=writer, bitrate=500)
188+
except UnicodeDecodeError:
189+
xfail("There can be errors in the numpy import stack, "
190+
"see issues #1891 and #2679")
191+
finally:
192+
try:
193+
os.remove(F.name)
194+
except Exception:
195+
pass
196+
197+
198+
@cleanup
199+
def check_save_animation_pathlib(writer, extension='mp4'):
200+
try:
201+
from pathlib import Path
202+
except ImportError:
203+
raise SkipTest("pathlib not installed")
204+
205+
if not animation.writers.is_available(writer):
206+
skip("writer '%s' not available on this system" % writer)
207+
fig, ax = plt.subplots()
208+
line, = ax.plot([], [])
209+
210+
ax.set_xlim(0, 10)
211+
ax.set_ylim(-1, 1)
212+
213+
def init():
214+
line.set_data([], [])
215+
return line,
216+
217+
def animate(i):
218+
x = np.linspace(0, 10, 100)
219+
y = np.sin(x + i)
220+
line.set_data(x, y)
221+
return line,
222+
223+
# Use NamedTemporaryFile: will be automatically deleted
224+
F = tempfile.NamedTemporaryFile(suffix='.' + extension)
225+
F.close()
226+
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5)
227+
try:
228+
anim.save(Path(F.name), fps=30, writer=writer, bitrate=500)
229+
except UnicodeDecodeError:
230+
xfail("There can be errors in the numpy import stack, "
231+
"see issues #1891 and #2679")
232+
finally:
233+
try:
234+
os.remove(F.name)
235+
except Exception:
236+
pass
237+
238+
147239
@cleanup
148240
def test_no_length_frames():
149241
fig, ax = plt.subplots()

lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from matplotlib import pyplot as plt
1515
from matplotlib.testing.decorators import (image_comparison, knownfailureif,
1616
cleanup)
17+
from nose.plugins.skip import SkipTest
1718

1819
if 'TRAVIS' not in os.environ:
1920
@image_comparison(baseline_images=['pdf_use14corefonts'],
@@ -132,3 +133,39 @@ def test_grayscale_alpha():
132133
ax.imshow(dd, interpolation='none', cmap='gray_r')
133134
ax.set_xticks([])
134135
ax.set_yticks([])
136+
137+
138+
@cleanup
139+
def test_pdfpages_accept_pep_519():
140+
from tempfile import NamedTemporaryFile
141+
142+
class FakeFSPathClass(object):
143+
def __init__(self, path):
144+
self._path = path
145+
146+
def __fspath__(self):
147+
return self._path
148+
tmpfile = NamedTemporaryFile(suffix='.pdf')
149+
tmpfile.close()
150+
with PdfPages(FakeFSPathClass(tmpfile.name)) as pdf:
151+
fig, ax = plt.subplots()
152+
ax.plot([1, 2], [3, 4])
153+
pdf.savefig(fig)
154+
155+
156+
@cleanup
157+
def test_savefig_accept_pathlib():
158+
try:
159+
from pathlib import Path
160+
except ImportError:
161+
raise SkipTest("pathlib not installed")
162+
from tempfile import NamedTemporaryFile
163+
164+
fig, ax = plt.subplots()
165+
ax.plot([1, 2], [3, 4])
166+
tmpfile = NamedTemporaryFile(suffix='.pdf')
167+
tmpfile.close()
168+
with PdfPages(Path(tmpfile.name)) as pdf:
169+
fig, ax = plt.subplots()
170+
ax.plot([1, 2], [3, 4])
171+
pdf.savefig(fig)

lib/matplotlib/tests/test_cbook.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
assert_array_almost_equal)
1414
from nose.tools import (assert_equal, assert_not_equal, raises, assert_true,
1515
assert_raises)
16+
from nose.plugins.skip import SkipTest
17+
from matplotlib.testing.decorators import cleanup
1618

1719
import matplotlib.cbook as cbook
1820
import matplotlib.colors as mcolors
@@ -523,3 +525,34 @@ def test_flatiter():
523525

524526
assert 0 == next(it)
525527
assert 1 == next(it)
528+
529+
530+
@cleanup
531+
def test_to_filehandle_accept_pep_519():
532+
from tempfile import NamedTemporaryFile
533+
534+
class FakeFSPathClass(object):
535+
def __init__(self, path):
536+
self._path = path
537+
538+
def __fspath__(self):
539+
return self._path
540+
541+
tmpfile = NamedTemporaryFile(delete=False)
542+
tmpfile.close()
543+
pep519_path = FakeFSPathClass(tmpfile.name)
544+
cbook.to_filehandle(pep519_path)
545+
546+
547+
@cleanup
548+
def test_to_filehandle_accept_pathlib():
549+
try:
550+
from pathlib import Path
551+
except ImportError:
552+
raise SkipTest("pathlib not installed")
553+
from tempfile import NamedTemporaryFile
554+
555+
tmpfile = NamedTemporaryFile(delete=False)
556+
tmpfile.close()
557+
path = Path(tmpfile.name)
558+
cbook.to_filehandle(path)

lib/matplotlib/tests/test_figure.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from six.moves import xrange
66

77
from nose.tools import assert_equal, assert_true
8+
from nose.plugins.skip import SkipTest
9+
810
from matplotlib import rcParams
911
from matplotlib.testing.decorators import image_comparison, cleanup
1012
from matplotlib.axes import Axes
@@ -205,6 +207,111 @@ def test_figaspect():
205207
assert h / w == 1
206208

207209

210+
@cleanup
211+
def test_savefig_accept_pep_519_png():
212+
from tempfile import NamedTemporaryFile
213+
214+
class FakeFSPathClass(object):
215+
def __init__(self, path):
216+
self._path = path
217+
218+
def __fspath__(self):
219+
return self._path
220+
221+
fig, ax = plt.subplots()
222+
ax.plot([1, 2], [3, 4])
223+
224+
tmpfile = NamedTemporaryFile(suffix='.png')
225+
tmpfile.close()
226+
pep519_path = FakeFSPathClass(tmpfile.name)
227+
fig.savefig(pep519_path)
228+
229+
230+
@cleanup
231+
def test_savefig_accept_pathlib_png():
232+
try:
233+
from pathlib import Path
234+
except ImportError:
235+
raise SkipTest("pathlib not installed")
236+
from tempfile import NamedTemporaryFile
237+
238+
fig, ax = plt.subplots()
239+
ax.plot([1, 2], [3, 4])
240+
tmpfile = NamedTemporaryFile(suffix='.png')
241+
tmpfile.close()
242+
path = Path(tmpfile.name)
243+
fig.savefig(path)
244+
245+
246+
@cleanup
247+
def test_savefig_accept_pep_519_svg():
248+
from tempfile import NamedTemporaryFile
249+
250+
class FakeFSPathClass(object):
251+
def __init__(self, path):
252+
self._path = path
253+
254+
def __fspath__(self):
255+
return self._path
256+
257+
fig, ax = plt.subplots()
258+
ax.plot([1, 2], [3, 4])
259+
tmpfile = NamedTemporaryFile(suffix='.svg')
260+
tmpfile.close()
261+
pep519_path = FakeFSPathClass(tmpfile.name)
262+
fig.savefig(pep519_path)
263+
264+
265+
@cleanup
266+
def test_savefig_accept_pathlib_svg():
267+
try:
268+
from pathlib import Path
269+
except ImportError:
270+
raise SkipTest("pathlib not installed")
271+
from tempfile import NamedTemporaryFile
272+
273+
fig, ax = plt.subplots()
274+
ax.plot([1, 2], [3, 4])
275+
tmpfile = NamedTemporaryFile(suffix='.svg')
276+
tmpfile.close()
277+
path = Path(tmpfile.name)
278+
fig.savefig(path)
279+
280+
281+
@cleanup
282+
def test_savefig_accept_pep_519_pdf():
283+
from tempfile import NamedTemporaryFile
284+
285+
class FakeFSPathClass(object):
286+
def __init__(self, path):
287+
self._path = path
288+
289+
def __fspath__(self):
290+
return self._path
291+
292+
fig, ax = plt.subplots()
293+
ax.plot([1, 2], [3, 4])
294+
tmpfile = NamedTemporaryFile(suffix='.pdf')
295+
tmpfile.close()
296+
pep519_path = FakeFSPathClass(tmpfile.name)
297+
fig.savefig(pep519_path)
298+
299+
300+
@cleanup
301+
def test_savefig_accept_pathlib_pdf():
302+
try:
303+
from pathlib import Path
304+
except ImportError:
305+
raise SkipTest("pathlib not installed")
306+
from tempfile import NamedTemporaryFile
307+
308+
fig, ax = plt.subplots()
309+
ax.plot([1, 2], [3, 4])
310+
tmpfile = NamedTemporaryFile(suffix='.pdf')
311+
tmpfile.close()
312+
path = Path(tmpfile.name)
313+
fig.savefig(path)
314+
208315
if __name__ == "__main__":
209316
import nose
210317
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

lib/matplotlib/tests/test_image.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77

88
from nose.plugins.attrib import attr
9+
from nose.plugins.skip import SkipTest
910

1011
import numpy as np
1112

@@ -752,5 +753,38 @@ def test_imshow_endianess():
752753
ax2.imshow(Z.astype('>f8'), **kwargs)
753754

754755

755-
if __name__ == '__main__':
756-
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
756+
@cleanup
757+
def test_imsave_accept_pep_519():
758+
from tempfile import NamedTemporaryFile
759+
760+
class FakeFSPathClass(object):
761+
def __init__(self, path):
762+
self._path = path
763+
764+
def __fspath__(self):
765+
return self._path
766+
767+
a = np.array([[1, 2], [3, 4]])
768+
tmpfile = NamedTemporaryFile(suffix='.pdf')
769+
tmpfile.close()
770+
pep519_path = FakeFSPathClass(tmpfile.name)
771+
plt.imsave(pep519_path, a)
772+
773+
774+
@cleanup
775+
def test_imsave_accept_pathlib():
776+
try:
777+
from pathlib import Path
778+
except ImportError:
779+
raise SkipTest("pathlib not installed")
780+
from tempfile import NamedTemporaryFile
781+
782+
a = np.array([[1, 2], [3, 4]])
783+
tmpfile = NamedTemporaryFile(suffix='.pdf')
784+
tmpfile.close()
785+
path = Path(tmpfile.name)
786+
plt.imsave(path, a)
787+
788+
789+
if __name__=='__main__':
790+
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

0 commit comments

Comments
 (0)