Skip to content

Commit 70b32cd

Browse files
anntzerQuLogic
authored andcommitted
Restart pgf's latex instance after bad latex inputs.
... and also move the warning about pgf-to-stream not supporting raster images to when actually outputting a raster image (otherwise the warning is spurious, and affects e.g. test_tex_restart_after_error).
1 parent e72ed82 commit 70b32cd

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
@@ -252,6 +252,8 @@ def _cleanup_remaining_instances():
252252
latex_manager._cleanup()
253253

254254
def _stdin_writeln(self, s):
255+
if self.latex is None:
256+
self._setup_latex_process()
255257
self.latex_stdin_utf8.write(s)
256258
self.latex_stdin_utf8.write("\n")
257259
self.latex_stdin_utf8.flush()
@@ -265,6 +267,8 @@ def _expect(self, s):
265267
if buf[-len(exp):] == exp:
266268
break
267269
if not len(b):
270+
self.latex.kill()
271+
self.latex = None
268272
raise LatexError("LaTeX process halted", buf.decode("utf8"))
269273
return buf.decode("utf8")
270274

@@ -301,6 +305,10 @@ def __init__(self):
301305
raise LatexError("LaTeX returned an error, probably missing font "
302306
"or error in preamble:\n%s" % stdout)
303307

308+
self.latex = None # Will be set up on first use.
309+
self.str_cache = {} # cache for strings already processed
310+
311+
def _setup_latex_process(self):
304312
# open LaTeX process for real work
305313
latex = subprocess.Popen([self.texcommand, "-halt-on-error"],
306314
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
@@ -313,9 +321,6 @@ def __init__(self):
313321
self._expect("*pgf_backend_query_start")
314322
self._expect_prompt()
315323

316-
# cache for strings already processed
317-
self.str_cache = {}
318-
319324
def _cleanup(self):
320325
if not self._os_path.isdir(self.tmpdir):
321326
return
@@ -427,10 +432,10 @@ def __init__(self, figure, fh, dummy=False):
427432
else:
428433
# if fh does not belong to a filename, deactivate draw_image
429434
if not hasattr(fh, 'name') or not os.path.exists(fh.name):
430-
cbook._warn_external("streamed pgf-code does not support "
431-
"raster graphics, consider using the "
432-
"pgf-to-pdf option", UserWarning)
433-
self.__dict__["draw_image"] = lambda *args, **kwargs: None
435+
self.__dict__["draw_image"] = \
436+
lambda *args, **kwargs: cbook._warn_external(
437+
"streamed pgf-code does not support raster graphics, "
438+
"consider using the pgf-to-pdf option")
434439

435440
@cbook.deprecated("3.2")
436441
@property

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
@@ -269,3 +270,15 @@ def test_pdf_pages_lualatex():
269270
pdf.savefig(fig)
270271

271272
assert pdf.get_pagecount() == 2
273+
274+
275+
@needs_xelatex
276+
def test_tex_restart_after_error():
277+
fig = plt.figure()
278+
fig.suptitle(r"\oops")
279+
with pytest.raises(ValueError):
280+
fig.savefig(BytesIO(), format="pgf")
281+
282+
fig = plt.figure() # start from scratch
283+
fig.suptitle(r"this is ok")
284+
fig.savefig(BytesIO(), format="pgf")

0 commit comments

Comments
 (0)