Skip to content

Commit cab5463

Browse files
committed
Delay checking for existence of postscript distillers.
Currently, if one has `ps.usedistiller` set in their matplotlibrc, matplotlib will immediately check for the presence of ghostscript (and possibly pstopdf) at import time and run them in subprocesses to check their version, even though they may not be needed at all (if the user is not going to save a postscript image); this can quite slow down import time. Compare e.g. with `text.usetex` which requires the presence of a `tex` install, but doesn't check for its presence until it is actually needed. Delay the checking for the presence of the distillers until we actually need them. For consistency with the old behavior, don't error out if they are missing, but just emit a warning.
1 parent 7d10cb6 commit cab5463

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -981,9 +981,11 @@ def print_figure_impl(fh):
981981
with open(tmpfile, 'w', encoding='latin-1') as fh:
982982
print_figure_impl(fh)
983983
if mpl.rcParams['ps.usedistiller'] == 'ghostscript':
984-
gs_distill(tmpfile, is_eps, ptype=papertype, bbox=bbox)
984+
_try_distill(gs_distill,
985+
tmpfile, is_eps, ptype=papertype, bbox=bbox)
985986
elif mpl.rcParams['ps.usedistiller'] == 'xpdf':
986-
xpdf_distill(tmpfile, is_eps, ptype=papertype, bbox=bbox)
987+
_try_distill(xpdf_distill,
988+
tmpfile, is_eps, ptype=papertype, bbox=bbox)
987989
_move_path_to_path_or_stream(tmpfile, outfile)
988990

989991
else:
@@ -1141,10 +1143,12 @@ def write(self, *args, **kwargs):
11411143

11421144
if (mpl.rcParams['ps.usedistiller'] == 'ghostscript'
11431145
or mpl.rcParams['text.usetex']):
1144-
gs_distill(tmpfile, is_eps, ptype=papertype, bbox=bbox,
1145-
rotated=psfrag_rotated)
1146+
_try_distill(gs_distill,
1147+
tmpfile, is_eps, ptype=papertype, bbox=bbox,
1148+
rotated=psfrag_rotated)
11461149
elif mpl.rcParams['ps.usedistiller'] == 'xpdf':
1147-
xpdf_distill(tmpfile, is_eps, ptype=papertype, bbox=bbox,
1150+
_try_distill(xpdf_distill,
1151+
tmpfile, is_eps, ptype=papertype, bbox=bbox,
11481152
rotated=psfrag_rotated)
11491153

11501154
_move_path_to_path_or_stream(tmpfile, outfile)
@@ -1198,6 +1202,13 @@ def convert_psfrags(tmpfile, psfrags, font_preamble, custom_preamble,
11981202
return psfrag_rotated
11991203

12001204

1205+
def _try_distill(func, *args, **kwargs):
1206+
try:
1207+
func(*args, **kwargs)
1208+
except mpl.ExecutableNotFoundError as exc:
1209+
_log.warning("%s. Distillation step skipped.", exc)
1210+
1211+
12011212
def gs_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
12021213
"""
12031214
Use ghostscript's pswrite or epswrite device to distill a file.
@@ -1239,6 +1250,9 @@ def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False):
12391250
operators. This distiller is preferred, generating high-level postscript
12401251
output that treats text as text.
12411252
"""
1253+
mpl._get_executable_info("gs")
1254+
mpl._get_executable_info("pdftops")
1255+
12421256
pdffile = tmpfile + '.pdf'
12431257
psfile = tmpfile + '.ps'
12441258

lib/matplotlib/rcsetup.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -486,24 +486,8 @@ def validate_ps_distiller(s):
486486
s = s.lower()
487487
if s in ('none', None, 'false', False):
488488
return None
489-
elif s in ('ghostscript', 'xpdf'):
490-
try:
491-
mpl._get_executable_info("gs")
492-
except mpl.ExecutableNotFoundError:
493-
_log.warning("Setting rcParams['ps.usedistiller'] requires "
494-
"ghostscript.")
495-
return None
496-
if s == "xpdf":
497-
try:
498-
mpl._get_executable_info("pdftops")
499-
except mpl.ExecutableNotFoundError:
500-
_log.warning("Setting rcParams['ps.usedistiller'] to 'xpdf' "
501-
"requires xpdf.")
502-
return None
503-
return s
504489
else:
505-
raise ValueError('matplotlibrc ps.usedistiller must either be none, '
506-
'ghostscript or xpdf')
490+
return ValidateInStrings('ps.usedistiller', ['ghostscript', 'xpdf'])(s)
507491

508492

509493
# A validator dedicated to the named line styles, based on the items in

0 commit comments

Comments
 (0)