From 72a3962f13a107695ad70d7904341cec0290c1e6 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Mon, 4 Sep 2023 13:26:34 -0500 Subject: [PATCH] Backport PR #26689: Fix error generation for missing pgf.texsystem. --- lib/matplotlib/backends/backend_pgf.py | 29 +++++++++++++------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index 734c188b0048..ccf4b800a614 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -241,16 +241,7 @@ def __init__(self): self._finalize_tmpdir = weakref.finalize(self, self._tmpdir.cleanup) # test the LaTeX setup to ensure a clean startup of the subprocess - try: - self._setup_latex_process(expect_reply=False) - except FileNotFoundError as err: - raise RuntimeError( - f"{self.latex.args[0]!r} not found. Install it or change " - f"rcParams['pgf.texsystem'] to an available TeX " - f"implementation.") from err - except OSError as err: - raise RuntimeError( - f"Error starting process {self.latex.args[0]!r}") from err + self._setup_latex_process(expect_reply=False) stdout, stderr = self.latex.communicate("\n\\makeatletter\\@@end\n") if self.latex.returncode != 0: raise LatexError( @@ -258,7 +249,6 @@ def __init__(self): f"while processing the following input:\n" f"{self._build_latex_header()}", stdout) - self.latex = None # Will be set up on first use. # Per-instance cache. self._get_box_metrics = functools.lru_cache(self._get_box_metrics) @@ -268,10 +258,19 @@ def _setup_latex_process(self, *, expect_reply=True): # Windows, we must ensure that the subprocess has quit before being # able to delete the tmpdir in which it runs; in order to do so, we # must first `kill()` it, and then `communicate()` with it. - self.latex = subprocess.Popen( - [mpl.rcParams["pgf.texsystem"], "-halt-on-error"], - stdin=subprocess.PIPE, stdout=subprocess.PIPE, - encoding="utf-8", cwd=self.tmpdir) + try: + self.latex = subprocess.Popen( + [mpl.rcParams["pgf.texsystem"], "-halt-on-error"], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, + encoding="utf-8", cwd=self.tmpdir) + except FileNotFoundError as err: + raise RuntimeError( + f"{mpl.rcParams['pgf.texsystem']!r} not found; install it or change " + f"rcParams['pgf.texsystem'] to an available TeX implementation" + ) from err + except OSError as err: + raise RuntimeError( + f"Error starting {mpl.rcParams['pgf.texsystem']!r}") from err def finalize_latex(latex): latex.kill()