8000 Merge pull request #15969 from anntzer/restart-pgf · matplotlib/matplotlib@3c67db3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c67db3

Browse files
authored
Merge pull request #15969 from anntzer/restart-pgf
Restart pgf's latex instance after bad latex inputs.
2 parents e5283e0 + 4e12ccc commit 3c67db3

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/matplotlib/backends/backend_pgf.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ def _cleanup_remaining_instances():
232232
latex_manager._cleanup()
233233

234234
def _stdin_writeln(self, s):
235+
if self.latex is None:
236+
self._setup_latex_process()
235237
self.latex.stdin.write(s)
236238
self.latex.stdin.write("\n")
237239
self.latex.stdin.flush()
@@ -245,6 +247,8 @@ def _expect(self, s):
245247
if chars[-len(s):] == s:
246248
break
247249
if not c:
250+
self.latex.kill()
251+
self.latex = None
248252
raise LatexError("LaTeX process halted", "".join(chars))
249253
return "".join(chars)
250254

@@ -281,6 +285,10 @@ def __init__(self):
281285
raise LatexError("LaTeX returned an error, probably missing font "
282286
"or error in preamble:\n%s" % stdout)
283287

288+
self.latex = None # Will be set up on first use.
289+
self.str_cache = {} # cache for strings already processed
290+
291+
def _setup_latex_process(self):
284292
# open LaTeX process for real work
285293
self.latex = subprocess.Popen(
286294
[self.texcommand, "-halt-on-error"],
@@ -292,9 +300,6 @@ def __init__(self):
292300
self._expect("*pgf_backend_query_start")
293301
self._expect_prompt()
294302

295-
# cache for strings already processed
296-
self.str_cache = {}
297-
298303
@cbook.deprecated("3.3")
299304
def latex_stdin_utf8(self):
300305
return self.latex.stdin
@@ -408,10 +413,10 @@ def __init__(self, figure, fh, dummy=False):
408413
else:
409414
# if fh does not belong to a filename, deactivate draw_image
410415
if not hasattr(fh, 'name') or not os.path.exists(fh.name):
411-
cbook._warn_external("streamed pgf-code does not support "
412-
"raster graphics, consider using the "
413-
"pgf-to-pdf option", UserWarning)
414-
self.__dict__["draw_image"] = lambda *args, **kwargs: None
416+
self.__dict__["draw_image"] = \
417+
lambda *args, **kwargs: cbook._warn_external(
418+
"streamed pgf-code does not support raster graphics, "
419+
"consider using the pgf-to-pdf option")
415420

416421
@cbook.deprecated("3.2")
417422
def latexManager(self):

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from io import BytesIO
12
import os
23
from pathlib import Path
34
import shutil
@@ -276,3 +277,15 @@ def test_pdf_pages_lualatex():
276277
pdf.savefig(fig)
277278

278279
assert pdf.get_pagecount() == 2
280+
281+
282+
@needs_xelatex
283+
def test_tex_restart_after_error():
284+
fig = plt.figure()
285+
fig.suptitle(r"\oops")
286+
with pytest.raises(ValueError):
287+
fig.savefig(BytesIO(), format="pgf")
288+
289+
fig = plt.figure() # start from scratch
290+
fig.suptitle(r"this is ok")
291+
fig.savefig(BytesIO(), format="pgf")

0 commit comments

Comments
 (0)
0