From f225130ee52f8b93b9e4a51c1ee779f544fcca51 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 12 Sep 2023 19:08:30 +1000 Subject: [PATCH] Only search for Ghostscript Windows binary when needed --- src/PIL/EpsImagePlugin.py | 48 +++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/PIL/EpsImagePlugin.py b/src/PIL/EpsImagePlugin.py index b96ce9603..84177aaaa 100644 --- a/src/PIL/EpsImagePlugin.py +++ b/src/PIL/EpsImagePlugin.py @@ -38,7 +38,14 @@ split = re.compile(r"^%%([^:]*):[ \t]*(.*)[ \t]*$") field = re.compile(r"^%[%!\w]([^:]*)[ \t]*$") gs_windows_binary = None -if sys.platform.startswith("win"): + + +def _get_windows_binary(): + global gs_windows_binary + if gs_windows_binary is not None: + return gs_windows_binary + if not sys.platform.startswith("win"): + return import shutil for binary in ("gswin32c", "gswin64c", "gs"): @@ -47,19 +54,19 @@ if sys.platform.startswith("win"): break else: gs_windows_binary = False + return gs_windows_binary def has_ghostscript(): - if gs_windows_binary: + gs_windows_binary = _get_windows_binary() + if gs_windows_binary is not None: + return gs_windows_binary is not False + try: + subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL) return True - if not sys.platform.startswith("win"): - try: - subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL) - return True - except OSError: - # No Ghostscript - pass - return False + except OSError: + # No Ghostscript + return False def Ghostscript(tile, size, fp, scale=1, transparency=False): @@ -132,17 +139,18 @@ def Ghostscript(tile, size, fp, scale=1, transparency=False): "showpage", ] - if gs_windows_binary is not None: - if not gs_windows_binary: - try: - os.unlink(outfile) - if infile_temp: - os.unlink(infile_temp) - except OSError: - pass + gs_windows_binary = _get_windows_binary() + if gs_windows_binary is False: + try: + os.unlink(outfile) + if infile_temp: + os.unlink(infile_temp) + except OSError: + pass - msg = "Unable to locate Ghostscript on paths" - raise OSError(msg) + msg = "Unable to locate Ghostscript on paths" + raise OSError(msg) + elif gs_windows_binary is not None: command[0] = gs_windows_binary # push data through Ghostscript