8000 Add PEP 519 support by aragilar · Pull Request #6772 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Add PEP 519 support #6772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added tests for PEP 519/pathlib, these should fail
  • Loading branch information
aragilar committed Jun 13, 2016
commit 94e8e543c362acc689c171ad2cc103f18f005044
96 changes: 96 additions & 0 deletions lib/matplotlib/tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import tempfile
import numpy as np
from nose import with_setup
from nose.plugins.skip import SkipTest
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.testing.noseclasses import KnownFailureTest
Expand All @@ -27,6 +29,12 @@ def test_save_animation_smoketest():
for writer, extension in six.iteritems(WRITER_OUTPUT):
yield check_save_animation, writer, extension

for writer, extension in six.iteritems(WRITER_OUTPUT):
yield check_save_animation_pep_519, writer, extension

for writer, extension in six.iteritems(WRITER_OUTPUT):
yield check_save_animation_pathlib, writer, extension


@cleanup
def check_save_animation(writer, extension='mp4'):
Expand Down Expand Up @@ -66,6 +74,94 @@ def animate(i):
pass


@cleanup
def check_save_animation_pep_519(writer, extension='mp4'):
class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

if not animation.writers.is_available(writer):
raise KnownFailureTest("writer '%s' not available on this system"
% writer)
fig, ax = plt.subplots()
line, = ax.plot([], [])

ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)

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,

# Use NamedTemporaryFile: will be automatically deleted
F = tempfile.NamedTemporaryFile(suffix='.' + extension)
F.close()
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5)
try:
anim.save(FakeFSPathClass(F.name), fps=30, writer=writer, bitrate=500)
except UnicodeDecodeError:
raise KnownFailureTest("There can be errors in the numpy " +
"import stack, " +
"see issues #1891 and #2679")
finally:
try:
os.remove(F.name)
except Exception:
pass


@cleanup
def check_save_animation_pathlib(writer, extension='mp4'):
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")

if not animation.writers.is_available(writer):
raise KnownFailureTest("writer '%s' not available on this system"
% writer)
fig, ax = plt.subplots()
line, = ax.plot([], [])

ax.set_xlim(0, 10)
ax.set_ylim(-1, 1)

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,

# Use NamedTemporaryFile: will be automatically deleted
F = tempfile.NamedTemporaryFile(suffix='.' + extension)
F.close()
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5)
try:
anim.save(Path(F.name), fps=30, writer=writer, bitrate=500)
except UnicodeDecodeError:
raise KnownFailureTest("There can be errors in the numpy " +
"import stack, " +
"see issues #1891 and #2679")
finally:
try:
os.remove(F.name)
except Exception:
pass


@cleanup
def test_no_length_frames():
fig, ax = plt.subplots()
Expand Down
35 changes: 35 additions & 0 deletions lib/matplotlib/tests/test_backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from matplotlib import pyplot as plt
from matplotlib.testing.decorators import (image_comparison, knownfailureif,
cleanup)
from nose.plugins.skip import SkipTest

if 'TRAVIS' not in os.environ:
@image_comparison(baseline_images=['pdf_use14corefonts'],
Expand Down Expand Up @@ -132,3 +133,37 @@ def test_grayscale_alpha():
ax.imshow(dd, interpolation='none', cmap='gray_r')
ax.set_xticks([])
ax.set_yticks([])


@cleanup
def test_pdfpages_accept_pep_519():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path
with NamedTemporaryFile(suffix='.pdf') as tmpfile:
with PdfPages(FakeFSPathClass(tmpfile.name)) as 10000 pdf:
fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
pdf.savefig(fig)


@cleanup
def test_savefig_accept_pathlib():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile import NamedTemporaryFile

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
with NamedTemporaryFile(suffix='.pdf') as tmpfile:
with PdfPages(Path(tmpfile.name)) as pdf:
fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
pdf.savefig(fig)
31 changes: 31 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
assert_array_almost_equal)
from nose.tools import (assert_equal, assert_not_equal, raises, assert_true,
assert_raises)
from nose.plugins.skip import SkipTest
from matplotlib.testing.decorators import cleanup

import matplotlib.cbook as cbook
import matplotlib.colors as mcolors
Expand Down Expand Up @@ -499,3 +501,32 @@ class dummy():
base_set = mapping[ref(objs[0])]
for o in objs[1:]:
assert mapping[ref(o)] is base_set


@cleanup
def test_to_filehandle_accept_pep_519():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

with NamedTemporaryFile() as tmpfile:
pep519_path = FakeFSPathClass(tmpfile.name)
cbook.to_filehandle(pep519_path)


@cleanup
def test_to_filehandle_accept_pathlib():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile import NamedTemporaryFile

with NamedTemporaryFile() as tmpfile:
path = Path(tmpfile.name)
cbook.to_filehandle(path)
99 changes: 99 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from matplotlib.externals.six.moves import xrange

from nose.tools import assert_equal, assert_true
from nose.plugins.skip import SkipTest
from matplotlib.testing.decorators import image_comparison, cleanup
from matplotlib.axes import Axes
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -203,6 +204,104 @@ def test_figaspect():
assert h / w == 1


@cleanup
def test_savefig_accept_pep_519_png():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
with NamedTemporaryFile(suffix='.png') as tmpfile:
pep519_path = FakeFSPathClass(tmpfile.name)
fig.savefig(pep519_path)


@cleanup
def test_savefig_accept_pathlib_png():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile im A3D4 port NamedTemporaryFile

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
with NamedTemporaryFile(suffix='.png') as tmpfile:
path = Path(tmpfile.name)
fig.savefig(path)


@cleanup
def test_savefig_accept_pep_519_svg():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
with NamedTemporaryFile(suffix='.svg') as tmpfile:
pep519_path = FakeFSPathClass(tmpfile.name)
fig.savefig(pep519_path)


@cleanup
def test_savefig_accept_pathlib_svg():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile import NamedTemporaryFile

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
with NamedTemporaryFile(suffix='.svg') as tmpfile:
path = Path(tmpfile.name)
fig.savefig(path)


@cleanup
def test_savefig_accept_pep_519_pdf():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
with NamedTemporaryFile(suffix='.pdf') as tmpfile:
pep519_path = FakeFSPathClass(tmpfile.name)
fig.savefig(pep519_path)


@cleanup
def test_savefig_accept_pathlib_pdf():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile import NamedTemporaryFile

fig, ax = plt.subplots()
ax.plot([1, 2], [3, 4])
with NamedTemporaryFile(suffix='.pdf') as tmpfile:
path = Path(tmpfile.name)
fig.savefig(path)

if __name__ == "__main__":
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
32 changes: 32 additions & 0 deletions lib/matplotlib/tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os

from nose.plugins.attrib import attr
from nose.plugins.skip import SkipTest

import numpy as np

Expand Down Expand Up @@ -674,6 +675,37 @@ def test_mask_image():
ax2.imshow(A, interpolation='nearest')


@cleanup
def test_imsave_accept_pep_519():
from tempfile import NamedTemporaryFile

class FakeFSPathClass(object):
def __init__(self, path):
self._path = path

def __fspath__(self):
return self._path

a = np.array([[1, 2], [3, 4]])
with NamedTemporaryFile(suffix='.pdf') as tmpfile:
pep519_path = FakeFSPathClass(tmpfile.name)
plt.imsave(pep519_path, a)


@cleanup
def test_imsave_accept_pathlib():
try:
from pathlib import Path
except ImportError:
raise SkipTest("pathlib not installed")
from tempfile import NamedTemporaryFile

a = np.array([[1, 2], [3, 4]])
with NamedTemporaryFile(suffix='.pdf') as tmpfile:
path = Path(tmpfile.name)
plt.imsave(path, a)


if __name__=='__main__':
import nose
nose.runmodule(argv=['-s','--with-doctest'], exit=False)
0