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

Skip to content

Make ExecutableUnavailableError #14776

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

Merged
merged 4 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@ def wrapper():
_ExecInfo = namedtuple("_ExecInfo", "executable version")


class ExecutableNotFoundError(FileNotFoundError):
"""
Error raised when an executable that Matplotlib optionally
depends on can't be found.
"""
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the "pass" here.

Also the docstring would typically be formatted

"""
Error raised...
"""

(basically either it fits in one line and we write

"""A one line docstring."""

or it doesn't and we write

"""
A multiline
docstring.
"""

or even

"""
One line, but just too long to have opening and closing quotes on the same line.
"""



@functools.lru_cache()
def _get_executable_info(name):
"""
Expand All @@ -307,7 +315,7 @@ def _get_executable_info(name):

Raises
------
FileNotFoundError
ExecutableNotFoundError
If the executable is not found or older than the oldest version
supported by Matplotlib.
ValueError
Expand All @@ -319,25 +327,27 @@ def impl(args, regex, min_ver=None, ignore_exit_code=False):
# Search for a regex match in the output; if the match succeeds, the
# first group of the match is the version.
# Return an _ExecInfo if the executable exists, and has a version of
# at least min_ver (if set); else, raise FileNotFoundError.
# at least min_ver (if set); else, raise ExecutableNotFoundError.
try:
output = subprocess.check_output(
args, stderr=subprocess.STDOUT, universal_newlines=True)
except subprocess.CalledProcessError as _cpe:
if ignore_exit_code:
output = _cpe.output
else:
raise _cpe
raise ExecutableNotFoundError(str(_cpe)) from _cpe
except FileNotFoundError as _fnf:
raise ExecutableNotFoundError(str(_fnf)) from _fnf
match = re.search(regex, output)
if match:
version = LooseVersion(match.group(1))
if min_ver is not None and version < min_ver:
raise FileNotFoundError(
raise ExecutableNotFoundError(
f"You have {args[0]} version {version} but the minimum "
f"version supported by Matplotlib is {min_ver}.")
return _ExecInfo(args[0], version)
else:
raise FileNotFoundError(
raise ExecutableNotFoundError(
f"Failed to determine the version of {args[0]} from "
f"{' '.join(args)}, which output {output}")

Expand All @@ -350,9 +360,10 @@ def impl(args, regex, min_ver=None, ignore_exit_code=False):
for e in execs:
try:
return impl([e, "--version"], "(.*)", "9")
except FileNotFoundError:
except ExecutableNotFoundError:
pass
raise FileNotFoundError("Failed to find a Ghostscript installation")
message = "Failed to find a Ghostscript installation"
raise ExecutableNotFoundError(message)
elif name == "inkscape":
return impl(["inkscape", "-V"], "^Inkscape ([^ ]*)")
elif name == "magick":
Expand Down Expand Up @@ -380,7 +391,7 @@ def impl(args, regex, min_ver=None, ignore_exit_code=False):
else:
path = "convert"
if path is None:
raise FileNotFoundError(
raise ExecutableNotFoundError(
"Failed to find an ImageMagick installation")
return impl([path, "--version"], r"^Version: ImageMagick (\S*)")
elif name == "pdftops":
Expand All @@ -389,7 +400,7 @@ def impl(args, regex, min_ver=None, ignore_exit_code=False):
if info and not ("3.0" <= info.version
# poppler version numbers.
or "0.9" <= info.version <= "1.0"):
raise FileNotFoundError(
raise ExecutableNotFoundError(
f"You have pdftops version {info.version} but the minimum "
f"version supported by Matplotlib is 3.0.")
return info
Expand Down Expand Up @@ -478,14 +489,14 @@ def checkdep_ps_distiller(s):
return False
try:
_get_executable_info("gs")
except FileNotFoundError:
except ExecutableNotFoundError:
_log.warning(
"Setting rcParams['ps.usedistiller'] requires ghostscript.")
return False
if s == "xpdf":
try:
_get_executable_info("pdftops")
except FileNotFoundError:
except ExecutableNotFoundError:
_log.warning(
"Setting rcParams['ps.usedistiller'] to 'xpdf' requires xpdf.")
return False
Expand All @@ -500,12 +511,12 @@ def checkdep_usetex(s):
return False
try:
_get_executable_info("dvipng")
except FileNotFoundError:
except ExecutableNotFoundError:
_log.warning("usetex mode requires dvipng.")
return False
try:
_get_executable_info("gs")
except FileNotFoundError:
except ExecutableNotFoundError:
_log.warning("usetex mode requires ghostscript.")
return False
return True
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,8 @@ def bin_path(cls):
def isAvailable(cls):
try:
return super().isAvailable()
except FileNotFoundError: # May be raised by get_executable_info.
except mpl.ExecutableNotFoundError:
# May be raised by get_executable_info.
return False


Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def cairo_convert(pdffile, pngfile, dpi):
return cairo_convert
try:
gs_info = mpl._get_executable_info("gs")
except FileNotFoundError:
except mpl.ExecutableNotFoundError:
pass
else:
def gs_convert(pdffile, pngfile, dpi):
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,14 @@ def validate_ps_distiller(s):
elif s in ('ghostscript', 'xpdf'):
try:
mpl._get_executable_info("gs")
except FileNotFoundError:
except mpl.ExecutableNotFoundError:
_log.warning("Setting rcParams['ps.usedistiller'] requires "
"ghostscript.")
return None
if s == "xpdf":
try:
mpl._get_executable_info("pdftops")
except FileNotFoundError:
except mpl.ExecutableNotFoundError:
_log.warning("Setting rcParams['ps.usedistiller'] to 'xpdf' "
"requires xpdf.")
return None
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/testing/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ def __call__(self, orig, dest):
def _update_converter():
try:
mpl._get_executable_info("gs")
except FileNotFoundError:
except mpl.ExecutableNotFoundError:
pass
else:
converter['pdf'] = converter['eps'] = _GSConverter()
try:
mpl._get_executable_info("inkscape")
except FileNotFoundError:
except mpl.ExecutableNotFoundError:
pass
else:
converter['svg'] = _SVGConverter()
Expand Down