Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 65cbdef

Browse filesBrowse files
authored
Merge pull request #23444 from anntzer/pgflm
MNT: Deduplicate common parts in LatexManager.{__init__,_setup_latex_process}
2 parents a33a6fd + 28123a1 commit 65cbdef
Copy full SHA for 65cbdef

File tree

Expand file treeCollapse file tree

2 files changed

+20
-19
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+20
-19
lines changed
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The ``texcommand`` and ``latex_header`` attributes of ``backend_pgf.LatexManager``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... are deprecated.

‎lib/matplotlib/backends/backend_pgf.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_pgf.py
+17-19Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -287,43 +287,41 @@ def __init__(self):
287287
self._finalize_tmpdir = weakref.finalize(self, self._tmpdir.cleanup)
288288

289289
# test the LaTeX setup to ensure a clean startup of the subprocess
290-
self.texcommand = mpl.rcParams["pgf.texsystem"]
291-
self.latex_header = LatexManager._build_latex_header()
292-
latex_end = "\n\\makeatletter\n\\@@end\n"
293290
try:
294-
latex = subprocess.Popen(
295-
[self.texcommand, "-halt-on-error"],
296-
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
297-
encoding="utf-8", cwd=self.tmpdir)
291+
self._setup_latex_process(expect_reply=False)
298292
except FileNotFoundError as err:
299293
raise RuntimeError(
300-
f"{self.texcommand} not found. Install it or change "
294+
f"{self.latex.args[0]!r} not found. Install it or change "
301295
f"rcParams['pgf.texsystem'] to an available TeX "
302296
f"implementation.") from err
303297
except OSError as err:
304-
raise RuntimeError("Error starting process %r" %
305-
self.texcommand) from err
306-
test_input = self.latex_header + latex_end
307-
stdout, stderr = latex.communicate(test_input)
308-
if latex.returncode != 0:
298+
raise RuntimeError(
299+
f"Error starting process {self.latex.args[0]!r}") from err
300+
stdout, stderr = self.latex.communicate("\n\\makeatletter\\@@end\n")
301+
if self.latex.returncode != 0:
309302
raise LatexError(
310303
f"LaTeX errored (probably missing font or error in preamble) "
311-
f"while processing the following input:\n{test_input}",
304+
f"while processing the following input:\n"
305+
f"{self._build_latex_header()}",
312306
stdout)
313307

314308
self.latex = None # Will be set up on first use.
315309
# Per-instance cache.
316310
self._get_box_metrics = functools.lru_cache()(self._get_box_metrics)
317311

318312
str_cache = _api.deprecated("3.5")(property(lambda self: {}))
313+
texcommand = _api.deprecated("3.6")(
314+
property(lambda self: mpl.rcParams["pgf.texsystem"]))
315+
latex_header = _api.deprecated("3.6")(
316+
property(lambda self: self._build_latex_header()))
319317

320-
def _setup_latex_process(self):
318+
def _setup_latex_process(self, *, expect_reply=True):
321319
# Open LaTeX process for real work; register it for deletion. On
322320
# Windows, we must ensure that the subprocess has quit before being
323321
# able to delete the tmpdir in which it runs; in order to do so, we
324322
# must first `kill()` it, and then `communicate()` with it.
325323
self.latex = subprocess.Popen(
326-
[self.texcommand, "-halt-on-error"],
324+
[mpl.rcParams["pgf.texsystem"], "-halt-on-error"],
327325
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
328326
encoding="utf-8", cwd=self.tmpdir)
329327

@@ -335,9 +333,9 @@ def finalize_latex(latex):
335333
self, finalize_latex, self.latex)
336334
# write header with 'pgf_backend_query_start' token
337335
self._stdin_writeln(self._build_latex_header())
338-
# read all lines until our 'pgf_backend_query_start' token appears
339-
self._expect("*pgf_backend_query_start")
340-
self._expect_prompt()
336+
if expect_reply: # read until 'pgf_backend_query_start' token appears
337+
self._expect("*pgf_backend_query_start")
338+
self._expect_prompt()
341339

342340
def get_width_height_descent(self, text, prop):
343341
"""

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.