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

Skip to content

Commit bb41673

Browse files
committed
Fix error generation for missing pgf.texsystem.
If pgf.texsystem isn't installed, then self.latex will not be created *at all* by _setup_latex_process, and we thus cannot access self.latex.args[0], but must instead read the rc value again. To make this clearer, move the error handling into _setup_latex_process, instead of having it outside. No test because that'd require setting up a test env where pgf.texsystem is explicitly *missing* (or go through lots of mocks), which seems not worth it.
1 parent 469b96b commit bb41673

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -241,24 +241,14 @@ def __init__(self):
241241
self._finalize_tmpdir = weakref.finalize(self, self._tmpdir.cleanup)
242242

243243
# test the LaTeX setup to ensure a clean startup of the subprocess
244-
try:
245-
self._setup_latex_process(expect_reply=False)
246-
except FileNotFoundError as err:
247-
raise RuntimeError(
248-
f"{self.latex.args[0]!r} not found. Install it or change "
249-
f"rcParams['pgf.texsystem'] to an available TeX "
250-
f"implementation.") from err
251-
except OSError as err:
252-
raise RuntimeError(
253-
f"Error starting process {self.latex.args[0]!r}") from err
244+
self._setup_latex_process(expect_reply=False)
254245
stdout, stderr = self.latex.communicate("\n\\makeatletter\\@@end\n")
255246
if self.latex.returncode != 0:
256247
raise LatexError(
257248
f"LaTeX errored (probably missing font or error in preamble) "
258249
f"while processing the following input:\n"
259250
f"{self._build_latex_header()}",
260251
stdout)
261-
262252
self.latex = None # Will be set up on first use.
263253
# Per-instance cache.
264254
self._get_box_metrics = functools.lru_cache(self._get_box_metrics)
@@ -268,10 +258,19 @@ def _setup_latex_process(self, *, expect_reply=True):
268258
# Windows, we must ensure that the subprocess has quit before being
269259
# able to delete the tmpdir in which it runs; in order to do so, we
270260
# must first `kill()` it, and then `communicate()` with it.
271-
self.latex = subprocess.Popen(
272-
[mpl.rcParams["pgf.texsystem"], "-halt-on-error"],
273-
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
274-
encoding="utf-8", cwd=self.tmpdir)
261+
try:
262+
self.latex = subprocess.Popen(
263+
[mpl.rcParams["pgf.texsystem"], "-halt-on-error"],
264+
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
265+
encoding="utf-8", cwd=self.tmpdir)
266+
except FileNotFoundError as err:
267+
raise RuntimeError(
268+
f"{rcParams['pgf.texsystem']!r} not found; install it or change "
269+
f"rcParams['pgf.texsystem'] to an available TeX implementation"
270+
) from err
271+
except OSError as err:
272+
raise RuntimeError(
273+
f"Error starting process {self.latex.args[0]!r}") from err
275274

276275
def finalize_latex(latex):
277276
latex.kill()

0 commit comments

Comments
 (0)