@@ -286,24 +286,14 @@ def __init__(self):
286286 self ._finalize_tmpdir = weakref .finalize (self , self ._tmpdir .cleanup )
287287
288288 # test the LaTeX setup to ensure a clean startup of the subprocess
289- try :
290- self ._setup_latex_process (expect_reply = False )
291- except FileNotFoundError as err :
292- raise RuntimeError (
293- f"{ self .latex .args [0 ]!r} not found. Install it or change "
294- f"rcParams['pgf.texsystem'] to an available TeX "
295- f"implementation." ) from err
296- except OSError as err :
297- raise RuntimeError (
298- f"Error starting process { self .latex .args [0 ]!r} " ) from err
289+ self ._setup_latex_process (expect_reply = False )
299290 stdout , stderr = self .latex .communicate ("\n \\ makeatletter\\ @@end\n " )
300291 if self .latex .returncode != 0 :
301292 raise LatexError (
302293 f"LaTeX errored (probably missing font or error in preamble) "
303294 f"while processing the following input:\n "
304295 f"{ self ._build_latex_header ()} " ,
305296 stdout )
306-
307297 self .latex = None # Will be set up on first use.
308298 # Per-instance cache.
309299 self ._get_box_metrics = functools .lru_cache ()(self ._get_box_metrics )
@@ -317,15 +307,28 @@ def _setup_latex_process(self, *, expect_reply=True):
317307 # Open LaTeX process for real work; register it for deletion. On
318308 # Windows, we must ensure that the subprocess has quit before being
319309 # able to delete the tmpdir in which it runs; in order to do so, we
320- # must first `kill()` it, and then `communicate()` with it.
321- self .latex = subprocess .Popen (
322- [mpl .rcParams ["pgf.texsystem" ], "-halt-on-error" ],
323- stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
324- encoding = "utf-8" , cwd = self .tmpdir )
310+ # must first `kill()` it, and then `communicate()` with or `wait()` on
311+ # it.
312+ try :
313+ self .latex = subprocess .Popen (
314+ [mpl .rcParams ["pgf.texsystem" ], "-halt-on-error" ],
315+ stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
316+ encoding = "utf-8" , cwd = self .tmpdir )
317+ except FileNotFoundError as err :
318+ raise RuntimeError (
319+ f"{ mpl .rcParams ['pgf.texsystem' ]!r} not found; install it or change "
320+ f"rcParams['pgf.texsystem'] to an available TeX implementation"
321+ ) from err
322+ except OSError as err :
323+ raise RuntimeError (
324+ f"Error starting { mpl .rcParams ['pgf.texsystem' ]!r} " ) from err
325325
326326 def finalize_latex (latex ):
327327 latex .kill ()
328- latex .communicate ()
328+ try :
329+ latex .communicate ()
330+ except RuntimeError :
331+ latex .wait ()
329332
330333 self ._finalize_latex = weakref .finalize (
331334 self , finalize_latex , self .latex )
0 commit comments