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

Skip to content

Commit 6900a71

Browse files
mdboomtacaswell
authored andcommitted
Lazy import of private modules
1 parent 867c3dd commit 6900a71

File tree

11 files changed

+60
-12
lines changed

11 files changed

+60
-12
lines changed

doc/api/api_changes.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ subplotparams will collapse axes to zero width or height. This prevents
4646
``tight_layout`` from being executed. Similarly
4747
`.tight_layout.get_tight_layout_figure` will return None.
4848

49+
To improve import (startup) time, private modules are now imported lazily.
50+
These modules are no longer available at these locations:
51+
52+
- `matplotlib.backends.backend_agg._png`
53+
- `matplotlib.contour._contour`
54+
- `matplotlib.image._png`
55+
- `matplotlib.mathtext._png`
56+
- `matplotlib.testing.compare._png`
57+
- `matplotlib.texmanager._png`
58+
- `matplotlib.tri.triangulation._tri`
59+
- `matplotlib.tri.triangulation._qhull`
60+
- `matplotlib.tri.tricontour._tri`
61+
- `matplotlib.tri.trifinder._tri`
62+
4963
API Changes for 3.0.0
5064
=====================
5165

lib/matplotlib/backends/backend_agg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
from matplotlib import colors as mcolors
3939

4040
from matplotlib.backends._backend_agg import RendererAgg as _RendererAgg
41-
from matplotlib import _png
4241

4342
from matplotlib.backend_bases import _has_pil
4443

@@ -499,6 +498,7 @@ def print_png(self, filename_or_obj, *args,
499498
If the 'pnginfo' key is present, it completely overrides
500499
*metadata*, including the default 'Software' key.
501500
"""
501+
from matplotlib import _png
502502

503503
if metadata is None:
504504
metadata = {}

lib/matplotlib/contour.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from numpy import ma
99

1010
import matplotlib as mpl
11-
import matplotlib._contour as _contour
1211
import matplotlib.path as mpath
1312
import matplotlib.ticker as ticker
1413
import matplotlib.cm as cm
@@ -1451,6 +1450,8 @@ def _process_args(self, *args, **kwargs):
14511450
self._mins = args[0]._mins
14521451
self._maxs = args[0]._maxs
14531452
else:
1453+
import matplotlib._contour as _contour
1454+
14541455
self._corner_mask = kwargs.pop('corner_mask', None)
14551456
if self._corner_mask is None:
14561457
self._corner_mask = mpl.rcParams['contour.corner_mask']

lib/matplotlib/image.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
# For clarity, names from _image are given explicitly in this module:
2424
import matplotlib._image as _image
25-
import matplotlib._png as _png
2625

2726
# For user convenience, the names from _image are also imported into
2827
# the image namespace:
@@ -635,6 +634,7 @@ def contains(self, mouseevent):
635634

636635
def write_png(self, fname):
637636
"""Write the image to png file with fname"""
637+
from matplotlib import _png
638638
im = self.to_rgba(self._A[::-1] if self.origin == 'lower' else self._A,
639639
bytes=True, norm=True)
640640
_png.write_png(im, fname)
@@ -1359,7 +1359,11 @@ def imread(fname, format=None):
13591359
.. _Pillow documentation: http://pillow.readthedocs.io/en/latest/
13601360
"""
13611361

1362-
handlers = {'png': _png.read_png, }
1362+
def read_png(*args, **kwargs):
1363+
from matplotlib import _png
1364+
return _png.read_png(*args, **kwargs)
1365+
1366+
handlers = {'png': read_png, }
13631367
if format is None:
13641368
if isinstance(fname, str):
13651369
parsed = urllib.parse.urlparse(fname)

lib/matplotlib/mathtext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
ParserElement.enablePackrat()
3434

35-
from matplotlib import _png, cbook, colors as mcolors, get_data_path, rcParams
35+
from matplotlib import cbook, colors as mcolors, get_data_path, rcParams
3636
from matplotlib.afm import AFM
3737
from matplotlib.cbook import get_realpath_and_stat
3838
from matplotlib.ft2font import FT2Image, KERNING_DEFAULT, LOAD_NO_HINTING
@@ -3450,6 +3450,7 @@ def to_png(self, filename, texstr, color='black', dpi=120, fontsize=14):
34503450
Returns the offset of the baseline from the bottom of the
34513451
image in pixels.
34523452
"""
3453+
from matplotlib import _png
34533454
rgba, depth = self.to_rgba(
34543455
texstr, color=color, dpi=dpi, fontsize=fontsize)
34553456
_png.write_png(rgba, filename)

lib/matplotlib/testing/compare.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import matplotlib as mpl
1919
from matplotlib.testing.exceptions import ImageComparisonFailure
20-
from matplotlib import _png, cbook
20+
from matplotlib import cbook
2121

2222
__all__ = ['compare_float', 'compare_images', 'comparable_formats']
2323

@@ -418,6 +418,8 @@ def compare_images(expected, actual, tol, in_decorator=False):
418418
compare_images(img1, img2, 0.001)
419419
420420
"""
421+
from matplotlib import _png
422+
421423
if not os.path.exists(actual):
422424
raise Exception("Output image %s does not exist." % actual)
423425

@@ -488,6 +490,7 @@ def save_diff_image(expected, actual, output):
488490
File path to save difference image to.
489491
'''
490492
# Drop alpha channels, similarly to compare_images.
493+
from matplotlib import _png
491494
expected_image = _png.read_png(expected)[..., :3]
492495
actual_image = _png.read_png(actual)[..., :3]
493496
actual_image, expected_image = crop_to_same(

lib/matplotlib/tests/test_basic.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import builtins
2+
import subprocess
3+
import sys
24

35
import matplotlib
6+
from matplotlib.cbook import dedent
47

58

69
def test_simple():
@@ -30,3 +33,24 @@ def test_override_builtins():
3033
overridden = True
3134

3235
assert not overridden
36+
37+
38+
def test_lazy_imports():
39+
source = dedent("""
40+
import sys
41+
42+
import matplotlib.figure
43+
import matplotlib.backend_bases
44+
import matplotlib.pyplot
45+
46+
assert 'matplotlib._png' not in sys.modules
47+
assert 'matplotlib._tri' not in sys.modules
48+
assert 'matplotlib._qhull' not in sys.modules
49+
assert 'matplotlib._contour' not in sys.modules
50+
""")
51+
52+
subprocess.check_call([
53+
sys.executable,
54+
'-c',
55+
source
56+
])

lib/matplotlib/texmanager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import numpy as np
4242

4343
import matplotlib as mpl
44-
from matplotlib import _png, cbook, dviread, rcParams
44+
from matplotlib import cbook, dviread, rcParams
4545

4646
_log = logging.getLogger(__name__)
4747

@@ -397,6 +397,7 @@ def make_png(self, tex, fontsize, dpi):
397397

398398
def get_grey(self, tex, fontsize=None, dpi=None):
399399
"""Return the alpha channel."""
400+
from matplotlib import _png
400401
key = tex, self.get_font_config(), fontsize, dpi
401402
alpha = self.grey_arrayd.get(key)
402403
if alpha is None:

lib/matplotlib/tri/triangulation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import numpy as np
22

3-
import matplotlib._tri as _tri
4-
import matplotlib._qhull as _qhull
5-
63

74
class Triangulation(object):
85
"""
@@ -39,6 +36,8 @@ class Triangulation(object):
3936
triangles formed from colinear points, or overlapping triangles.
4037
"""
4138
def __init__(self, x, y, triangles=None, mask=None):
39+
from matplotlib import _qhull
40+
4241
self.x = np.asarray(x, dtype=np.float64)
4342
self.y = np.asarray(y, dtype=np.float64)
4443
if self.x.shape != self.y.shape or self.x.ndim != 1:
@@ -106,6 +105,7 @@ def get_cpp_triangulation(self):
106105
Return the underlying C++ Triangulation object, creating it
107106
if necessary.
108107
"""
108+
from matplotlib import _tri
109109
if self._cpp_triangulation is None:
110110
self._cpp_triangulation = _tri.Triangulation(
111111
self.x, self.y, self.triangles, self.mask, self._edges,

lib/matplotlib/tri/tricontour.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from matplotlib.contour import ContourSet
44
from matplotlib.tri.triangulation import Triangulation
5-
import matplotlib._tri as _tri
65

76

87
class TriContourSet(ContourSet):
@@ -44,6 +43,7 @@ def _process_args(self, *args, **kwargs):
4443
if self.levels is None:
4544
self.levels = args[0].levels
4645
else:
46+
from matplotlib import _tri
4747
tri, z = self._contour_args(args, kwargs)
4848
C = _tri.TriContourGenerator(tri.get_cpp_triangulation(), z)
4949
self._mins = [tri.x.min(), tri.y.min()]

lib/matplotlib/tri/trifinder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import numpy as np
22

33
from matplotlib.tri import Triangulation
4-
import matplotlib._tri as _tri
54

65

76
class TriFinder(object):
@@ -35,6 +34,7 @@ class TrapezoidMapTriFinder(TriFinder):
3534
this should not be relied upon.
3635
"""
3736
def __init__(self, triangulation):
37+
from matplotlib import _tri
3838
TriFinder.__init__(self, triangulation)
3939
self._cpp_trifinder = _tri.TrapezoidMapTriFinder(
4040
triangulation.get_cpp_triangulation())

0 commit comments

Comments
 (0)