diff --git a/doc/api/next_api_changes/2018-02-26-AL-removals.rst b/doc/api/next_api_changes/2018-02-26-AL-removals.rst index d7ae9530f1d1..cef1b3932613 100644 --- a/doc/api/next_api_changes/2018-02-26-AL-removals.rst +++ b/doc/api/next_api_changes/2018-02-26-AL-removals.rst @@ -48,4 +48,5 @@ The following deprecated API elements have been removed: The following API elements have been removed: -- ``matplotlib.sphinxext.sphinx_version``, +- ``backend_cairo.HAS_CAIRO_CFFI``, +- ``sphinxext.sphinx_version``, diff --git a/doc/sphinxext/mock_gui_toolkits.py b/doc/sphinxext/mock_gui_toolkits.py index 097a3409b167..bb76c68da301 100644 --- a/doc/sphinxext/mock_gui_toolkits.py +++ b/doc/sphinxext/mock_gui_toolkits.py @@ -3,7 +3,7 @@ class MyCairoCffi(MagicMock): - pass + __name__ = "cairocffi" class MyPyQt4(MagicMock): diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 52205228271e..57b69ed30cd7 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -1417,19 +1417,15 @@ def tk_window_focus(): def _init_tests(): - try: + # CPython's faulthandler since v3.6 handles exceptions on Windows + # https://bugs.python.org/issue23848 but until v3.6.4 it was printing + # non-fatal exceptions https://bugs.python.org/issue30557 + import platform + if not (sys.platform == 'win32' and + (3, 6) < sys.version_info < (3, 6, 4) and + platform.python_implementation() == 'CPython'): import faulthandler - except ImportError: - pass - else: - # CPython's faulthandler since v3.6 handles exceptions on Windows - # https://bugs.python.org/issue23848 but until v3.6.4 it was - # printing non-fatal exceptions https://bugs.python.org/issue30557 - import platform - if not (sys.platform == 'win32' and - (3, 6) < sys.version_info < (3, 6, 4) and - platform.python_implementation() == 'CPython'): - faulthandler.enable() + faulthandler.enable() # The version of FreeType to install locally for running the # tests. This must match the value in `setupext.py` diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 2cfb02457e4e..a194b985b587 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -54,7 +54,7 @@ from PIL import Image from PIL import PILLOW_VERSION from distutils.version import LooseVersion - if LooseVersion(PILLOW_VERSION) >= LooseVersion("3.4"): + if LooseVersion(PILLOW_VERSION) >= "3.4": _has_pil = True else: _has_pil = False diff --git a/lib/matplotlib/backends/_gtk3_compat.py b/lib/matplotlib/backends/_gtk3_compat.py index e134ccdf078c..5670e5375497 100644 --- a/lib/matplotlib/backends/_gtk3_compat.py +++ b/lib/matplotlib/backends/_gtk3_compat.py @@ -14,7 +14,6 @@ import importlib import sys - if "gi" in sys.modules: import gi elif "pgi" in sys.modules: @@ -28,6 +27,16 @@ except ImportError: raise ImportError("The Gtk3 backend requires PyGObject or pgi") +from .backend_cairo import cairo # noqa +# The following combinations are allowed: +# gi + pycairo +# gi + cairocffi +# pgi + cairocffi +# (pgi doesn't work with pycairo) +# We always try to import cairocffi first so if a check below fails it means +# that cairocffi was unavailable to start with. +if gi.__name__ == "pgi" and cairo.__name__ == "cairo": + raise ImportError("pgi and pycairo are not compatible") gi.require_version("Gtk", "3.0") globals().update( diff --git a/lib/matplotlib/backends/backend_cairo.py b/lib/matplotlib/backends/backend_cairo.py index 258237d741f4..7631c5d2b89b 100644 --- a/lib/matplotlib/backends/backend_cairo.py +++ b/lib/matplotlib/backends/backend_cairo.py @@ -24,14 +24,11 @@ raise ImportError("cairo backend requires that cairocffi or pycairo " "is installed") else: - HAS_CAIRO_CFFI = False if cairo.version_info < (1, 11, 0): # Introduced create_for_data for Py3. raise ImportError( "cairo {} is installed; cairo>=1.11.0 is required" .format(cairo.version)) -else: - HAS_CAIRO_CFFI = True backend_version = cairo.version @@ -65,7 +62,7 @@ def _premultiplied_argb32_to_unmultiplied_rgba8888(buf): return rgba -if HAS_CAIRO_CFFI: +if cairo.__name__ == "cairocffi": # Convert a pycairo context to a cairocffi one. def _to_context(ctx): if not isinstance(ctx, cairo.Context): @@ -177,7 +174,8 @@ def _append_paths_fast(ctx, paths, transforms, clip=None): cairo.cairo.cairo_append_path(ctx._pointer, ptr) -_append_paths = _append_paths_fast if HAS_CAIRO_CFFI else _append_paths_slow +_append_paths = (_append_paths_fast if cairo.__name__ == "cairocffi" + else _append_paths_slow) def _append_path(ctx, path, transform, clip=None): diff --git a/lib/matplotlib/backends/backend_gtk3agg.py b/lib/matplotlib/backends/backend_gtk3agg.py index d982d98e2369..66f7e151a48a 100644 --- a/lib/matplotlib/backends/backend_gtk3agg.py +++ b/lib/matplotlib/backends/backend_gtk3agg.py @@ -1,5 +1,4 @@ import sys -import warnings import numpy as np @@ -9,16 +8,6 @@ from .backend_gtk3 import Gtk, _BackendGTK3 from matplotlib import transforms -# The following combinations are allowed: -# gi + pycairo -# gi + cairocffi -# pgi + cairocffi -# (pgi doesn't work with pycairo) -# We always try to import cairocffi first so if a check below fails it means -# that cairocffi was unavailable to start with. -if gi.__name__ == "pgi" and cairo.__name__ == "cairo": - raise ImportError("pgi and pycairo are not compatible") - class FigureCanvasGTK3Agg(backend_gtk3.FigureCanvasGTK3, backend_agg.FigureCanvasAgg): diff --git a/lib/matplotlib/backends/backend_gtk3cairo.py b/lib/matplotlib/backends/backend_gtk3cairo.py index dcbf02eb5024..1d7416826e09 100644 --- a/lib/matplotlib/backends/backend_gtk3cairo.py +++ b/lib/matplotlib/backends/backend_gtk3cairo.py @@ -1,21 +1,9 @@ from . import backend_cairo, backend_gtk3 from ._gtk3_compat import gi -from .backend_cairo import cairo from .backend_gtk3 import Gtk, _BackendGTK3 from matplotlib.backend_bases import cursors -# The following combinations are allowed: -# gi + pycairo -# gi + cairocffi -# pgi + cairocffi -# (pgi doesn't work with pycairo) -# We always try to import cairocffi first so if a check below fails it means -# that cairocffi was unavailable to start with. -if gi.__name__ == "pgi" and cairo.__name__ == "cairo": - raise ImportError("pgi and pycairo are not compatible") - - class RendererGTK3Cairo(backend_cairo.RendererCairo): def set_context(self, ctx): self.gc.ctx = backend_cairo._to_context(ctx) diff --git a/lib/matplotlib/image.py b/lib/matplotlib/image.py index 5e25d7aa2531..a41428a5f345 100644 --- a/lib/matplotlib/image.py +++ b/lib/matplotlib/image.py @@ -1331,15 +1331,6 @@ def imread(fname, format=None): .. _Pillow documentation: http://pillow.readthedocs.io/en/latest/ """ - def pilread(fname): - """try to load the image with PIL or return None""" - try: - from PIL import Image - except ImportError: - return None - with Image.open(fname) as image: - return pil_to_array(image) - handlers = {'png': _png.read_png, } if format is None: if isinstance(fname, str): @@ -1358,13 +1349,15 @@ def pilread(fname): else: ext = format - if ext not in handlers: - im = pilread(fname) - if im is None: + if ext not in handlers: # Try to load the image with PIL. + try: + from PIL import Image + except ImportError: raise ValueError('Only know how to handle extensions: %s; ' 'with Pillow installed matplotlib can handle ' 'more images' % list(handlers)) - return im + with Image.open(fname) as image: + return pil_to_array(image) handler = handlers[ext] diff --git a/lib/matplotlib/tests/test_image.py b/lib/matplotlib/tests/test_image.py index 85d5761cc7de..3897bacd50e8 100644 --- a/lib/matplotlib/tests/test_image.py +++ b/lib/matplotlib/tests/test_image.py @@ -115,7 +115,7 @@ def test_image_python_io(): def test_imread_pil_uint16(): img = plt.imread(os.path.join(os.path.dirname(__file__), 'baseline_images', 'test_image', 'uint16.tif')) - assert (img.dtype == np.uint16) + assert img.dtype == np.uint16 assert np.sum(img) == 134184960 diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index ea4ba461442c..6109d326f4a0 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -13,13 +13,6 @@ from matplotlib.cbook.deprecation import MatplotlibDeprecationWarning -try: - from mpl_toolkits.natgrid import _natgrid - HAS_NATGRID = True -except ImportError: - HAS_NATGRID = False - - ''' A lot of mlab.py has been deprecated in Matplotlib 2.2 and is scheduled for removal in the future. The tests that use deprecated methods have a block @@ -2174,8 +2167,9 @@ def get_z(x, y): np.ma.getmask(correct_zi_masked)) -@pytest.mark.xfail(not HAS_NATGRID, reason='natgrid not installed') def test_griddata_nn(): + pytest.importorskip('mpl_toolkits.natgrid') + # z is a linear function of x and y. def get_z(x, y): return 3.0*x - y