Skip to content

Commit 40b51d5

Browse files
authored
Merge pull request #11566 from anntzer/morecleanups
Cleanups.
2 parents d762d6d + b1cde41 commit 40b51d5

File tree

11 files changed

+34
-66
lines changed

11 files changed

+34
-66
lines changed

doc/api/next_api_changes/2018-02-26-AL-removals.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ The following deprecated API elements have been removed:
4848

4949
The following API elements have been removed:
5050

51-
- ``matplotlib.sphinxext.sphinx_version``,
51+
- ``backend_cairo.HAS_CAIRO_CFFI``,
52+
- ``sphinxext.sphinx_version``,

doc/sphinxext/mock_gui_toolkits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
class MyCairoCffi(MagicMock):
6-
pass
6+
__name__ = "cairocffi"
77

88

99
class MyPyQt4(MagicMock):

lib/matplotlib/__init__.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,19 +1417,15 @@ def tk_window_focus():
14171417

14181418

14191419
def _init_tests():
1420-
try:
1420+
# CPython's faulthandler since v3.6 handles exceptions on Windows
1421+
# https://bugs.python.org/issue23848 but until v3.6.4 it was printing
1422+
# non-fatal exceptions https://bugs.python.org/issue30557
1423+
import platform
1424+
if not (sys.platform == 'win32' and
1425+
(3, 6) < sys.version_info < (3, 6, 4) and
1426+
platform.python_implementation() == 'CPython'):
14211427
import faulthandler
1422-
except ImportError:
1423-
pass
1424-
else:
1425-
# CPython's faulthandler since v3.6 handles exceptions on Windows
1426-
# https://bugs.python.org/issue23848 but until v3.6.4 it was
1427-
# printing non-fatal exceptions https://bugs.python.org/issue30557
1428-
import platform
1429-
if not (sys.platform == 'win32' and
1430-
(3, 6) < sys.version_info < (3, 6, 4) and
1431-
platform.python_implementation() == 'CPython'):
1432-
faulthandler.enable()
1428+
faulthandler.enable()
14331429

14341430
# The version of FreeType to install locally for running the
14351431
# tests. This must match the value in `setupext.py`

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
from PIL import Image
5555
from PIL import PILLOW_VERSION
5656
from distutils.version import LooseVersion
57-
if LooseVersion(PILLOW_VERSION) >= LooseVersion("3.4"):
57+
if LooseVersion(PILLOW_VERSION) >= "3.4":
5858
_has_pil = True
5959
else:
6060
_has_pil = False

lib/matplotlib/backends/_gtk3_compat.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import importlib
1515
import sys
1616

17-
1817
if "gi" in sys.modules:
1918
import gi
2019
elif "pgi" in sys.modules:
@@ -28,6 +27,16 @@
2827
except ImportError:
2928
raise ImportError("The Gtk3 backend requires PyGObject or pgi")
3029

30+
from .backend_cairo import cairo # noqa
31+
# The following combinations are allowed:
32+
# gi + pycairo
33+
# gi + cairocffi
34+
# pgi + cairocffi
35+
# (pgi doesn't work with pycairo)
36+
# We always try to import cairocffi first so if a check below fails it means
37+
# that cairocffi was unavailable to start with.
38+
if gi.__name__ == "pgi" and cairo.__name__ == "cairo":
39+
raise ImportError("pgi and pycairo are not compatible")
3140

3241
gi.require_version("Gtk", "3.0")
3342
globals().update(

lib/matplotlib/backends/backend_cairo.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@
2424
raise ImportError("cairo backend requires that cairocffi or pycairo "
2525
"is installed")
2626
else:
27-
HAS_CAIRO_CFFI = False
2827
if cairo.version_info < (1, 11, 0):
2928
# Introduced create_for_data for Py3.
3029
raise ImportError(
3130
"cairo {} is installed; cairo>=1.11.0 is required"
3231
.format(cairo.version))
33-
else:
34-
HAS_CAIRO_CFFI = True
3532

3633
backend_version = cairo.version
3734

@@ -65,7 +62,7 @@ def _premultiplied_argb32_to_unmultiplied_rgba8888(buf):
6562
return rgba
6663

6764

68-
if HAS_CAIRO_CFFI:
65+
if cairo.__name__ == "cairocffi":
6966
# Convert a pycairo context to a cairocffi one.
7067
def _to_context(ctx):
7168
if not isinstance(ctx, cairo.Context):
@@ -177,7 +174,8 @@ def _append_paths_fast(ctx, paths, transforms, clip=None):
177174
cairo.cairo.cairo_append_path(ctx._pointer, ptr)
178175

179176

180-
_append_paths = _append_paths_fast if HAS_CAIRO_CFFI else _append_paths_slow
177+
_append_paths = (_append_paths_fast if cairo.__name__ == "cairocffi"
178+
else _append_paths_slow)
181179

182180

183181
def _append_path(ctx, path, transform, clip=None):

lib/matplotlib/backends/backend_gtk3agg.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
import warnings
32

43
import numpy as np
54

@@ -9,16 +8,6 @@
98
from .backend_gtk3 import Gtk, _BackendGTK3
109
from matplotlib import transforms
1110

12-
# The following combinations are allowed:
13-
# gi + pycairo
14-
# gi + cairocffi
15-
# pgi + cairocffi
16-
# (pgi doesn't work with pycairo)
17-
# We always try to import cairocffi first so if a check below fails it means
18-
# that cairocffi was unavailable to start with.
19-
if gi.__name__ == "pgi" and cairo.__name__ == "cairo":
20-
raise ImportError("pgi and pycairo are not compatible")
21-
2211

2312
class FigureCanvasGTK3Agg(backend_gtk3.FigureCanvasGTK3,
2413
backend_agg.FigureCanvasAgg):

lib/matplotlib/backends/backend_gtk3cairo.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
from . import backend_cairo, backend_gtk3
22
from ._gtk3_compat import gi
3-
from .backend_cairo import cairo
43
from .backend_gtk3 import Gtk, _BackendGTK3
54
from matplotlib.backend_bases import cursors
65

76

8-
# The following combinations are allowed:
9-
# gi + pycairo
10-
# gi + cairocffi
11-
# pgi + cairocffi
12-
# (pgi doesn't work with pycairo)
13-
# We always try to import cairocffi first so if a check below fails it means
14-
# that cairocffi was unavailable to start with.
15-
if gi.__name__ == "pgi" and cairo.__name__ == "cairo":
16-
raise ImportError("pgi and pycairo are not compatible")
17-
18-
197
class RendererGTK3Cairo(backend_cairo.RendererCairo):
208
def set_context(self, ctx):
219
self.gc.ctx = backend_cairo._to_context(ctx)

lib/matplotlib/image.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,15 +1331,6 @@ def imread(fname, format=None):
13311331
.. _Pillow documentation: http://pillow.readthedocs.io/en/latest/
13321332
"""
13331333

1334-
def pilread(fname):
1335-
"""try to load the image with PIL or return None"""
1336-
try:
1337-
from PIL import Image
1338-
except ImportError:
1339-
return None
1340-
with Image.open(fname) as image:
1341-
return pil_to_array(image)
1342-
13431334
handlers = {'png': _png.read_png, }
13441335
if format is None:
13451336
if isinstance(fname, str):
@@ -1358,13 +1349,15 @@ def pilread(fname):
13581349
else:
13591350
ext = format
13601351

1361-
if ext not in handlers:
1362-
im = pilread(fname)
1363-
if im is None:
1352+
if ext not in handlers: # Try to load the image with PIL.
1353+
try:
1354+
from PIL import Image
1355+
except ImportError:
13641356
raise ValueError('Only know how to handle extensions: %s; '
13651357
'with Pillow installed matplotlib can handle '
13661358
'more images' % list(handlers))
1367-
return im
1359+
with Image.open(fname) as image:
1360+
return pil_to_array(image)
13681361

13691362
handler = handlers[ext]
13701363

lib/matplotlib/tests/test_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_image_python_io():
115115
def test_imread_pil_uint16():
116116
img = plt.imread(os.path.join(os.path.dirname(__file__),
117117
'baseline_images', 'test_image', 'uint16.tif'))
118-
assert (img.dtype == np.uint16)
118+
assert img.dtype == np.uint16
119119
assert np.sum(img) == 134184960
120120

121121

lib/matplotlib/tests/test_mlab.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@
1313
from matplotlib.cbook.deprecation import MatplotlibDeprecationWarning
1414

1515

16-
try:
17-
from mpl_toolkits.natgrid import _natgrid
18-
HAS_NATGRID = True
19-
except ImportError:
20-
HAS_NATGRID = False
21-
22-
2316
'''
2417
A lot of mlab.py has been deprecated in Matplotlib 2.2 and is scheduled for
2518
removal in the future. The tests that use deprecated methods have a block
@@ -2174,8 +2167,9 @@ def get_z(x, y):
21742167
np.ma.getmask(correct_zi_masked))
21752168

21762169

2177-
@pytest.mark.xfail(not HAS_NATGRID, reason='natgrid not installed')
21782170
def test_griddata_nn():
2171+
pytest.importorskip('mpl_toolkits.natgrid')
2172+
21792173
# z is a linear function of x and y.
21802174
def get_z(x, y):
21812175
return 3.0*x - y

0 commit comments

Comments
 (0)