11
11
import shutil
12
12
import subprocess
13
13
import sys
14
- import tempfile
15
14
from tempfile import TemporaryDirectory
16
15
import weakref
17
16
@@ -200,7 +199,6 @@ class LatexManager:
200
199
determining the metrics of text elements. The LaTeX environment can be
201
200
modified by setting fonts and/or a custom preamble in `.rcParams`.
202
201
"""
203
- _unclean_instances = weakref .WeakSet ()
204
202
205
203
@staticmethod
206
204
def _build_latex_header ():
@@ -237,12 +235,6 @@ def _get_cached_or_new(cls):
237
235
def _get_cached_or_new_impl (cls , header ): # Helper for _get_cached_or_new.
238
236
return cls ()
239
237
240
- @staticmethod
241
- def _cleanup_remaining_instances ():
242
- unclean_instances = list (LatexManager ._unclean_instances )
243
- for latex_manager in unclean_instances :
244
- latex_manager ._cleanup ()
245
-
246
238
def _stdin_writeln (self , s ):
247
239
if self .latex is None :
248
240
self ._setup_latex_process ()
@@ -268,13 +260,10 @@ def _expect_prompt(self):
268
260
return self ._expect ("\n *" )
269
261
270
262
def __init__ (self ):
271
- # store references for __del__
272
- self ._os_path = os .path
273
- self ._shutil = shutil
274
-
275
- # create a tmp directory for running latex, remember to cleanup
276
- self .tmpdir = tempfile .mkdtemp (prefix = "mpl_pgf_lm_" )
277
- LatexManager ._unclean_instances .add (self )
263
+ # create a tmp directory for running latex, register it for deletion
264
+ self ._tmpdir = TemporaryDirectory ()
265
+ self .tmpdir = self ._tmpdir .name
266
+ self ._finalize_tmpdir = weakref .finalize (self , self ._tmpdir .cleanup )
278
267
279
268
# test the LaTeX setup to ensure a clean startup of the subprocess
280
269
self .texcommand = mpl .rcParams ["pgf.texsystem" ]
@@ -303,11 +292,21 @@ def __init__(self):
303
292
self .str_cache = {} # cache for strings already processed
304
293
305
294
def _setup_latex_process (self ):
306
- # open LaTeX process for real work
295
+ # Open LaTeX process for real work; register it for deletion. On
296
+ # Windows, we must ensure that the subprocess has quit before being
297
+ # able to delete the tmpdir in which it runs; in order to do so, we
298
+ # must first `kill()` it, and then `communicate()` with it.
307
299
self .latex = subprocess .Popen (
308
300
[self .texcommand , "-halt-on-error" ],
309
301
stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
310
302
encoding = "utf-8" , cwd = self .tmpdir )
303
+
304
+ def finalize_latex (latex ):
305
+ latex .kill ()
306
+ latex .communicate ()
307
+
308
+ self ._finalize_latex = weakref .finalize (
309
+ self , finalize_latex , self .latex )
311
310
# write header with 'pgf_backend_query_start' token
312
311
self ._stdin_writeln (self ._build_latex_header ())
313
312
# read all lines until our 'pgf_backend_query_start' token appears
@@ -318,23 +317,6 @@ def _setup_latex_process(self):
318
317
def latex_stdin_utf8 (self ):
319
318
return self .latex .stdin
320
319
321
- def _cleanup (self ):
322
- if not self ._os_path .isdir (self .tmpdir ):
323
- return
324
- try :
325
- self .latex .communicate ()
326
- except Exception :
327
- pass
328
- try :
329
- self ._shutil .rmtree (self .tmpdir )
330
- LatexManager ._unclean_instances .discard (self )
331
- except Exception :
332
- sys .stderr .write ("error deleting tmp directory %s\n " % self .tmpdir )
333
-
334
- def __del__ (self ):
335
- _log .debug ("deleting LatexManager" )
336
- self ._cleanup ()
337
-
338
320
def get_width_height_descent (self , text , prop ):
339
321
"""
340
322
Get the width, total height and descent for a text typeset by the
@@ -766,16 +748,25 @@ class GraphicsContextPgf(GraphicsContextBase):
766
748
pass
767
749
768
750
751
+ @cbook .deprecated ("3.4" )
769
752
class TmpDirCleaner :
770
- remaining_tmpdirs = set ()
753
+ _remaining_tmpdirs = set ()
754
+
755
+ @cbook ._classproperty
756
+ @cbook .deprecated ("3.4" )
757
+ def remaining_tmpdirs (cls ):
758
+ return cls ._remaining_tmpdirs
771
759
772
760
@staticmethod
761
+ @cbook .deprecated ("3.4" )
773
762
def add (tmpdir ):
774
- TmpDirCleaner .remaining_tmpdirs .add (tmpdir )
763
+ TmpDirCleaner ._remaining_tmpdirs .add (tmpdir )
775
764
776
765
@staticmethod
766
+ @cbook .deprecated ("3.4" )
767
+ @atexit .register
777
768
def cleanup_remaining_tmpdirs ():
778
- for tmpdir in TmpDirCleaner .remaining_tmpdirs :
769
+ for tmpdir in TmpDirCleaner ._remaining_tmpdirs :
779
770
error_message = "error deleting tmp directory {}" .format (tmpdir )
780
771
shutil .rmtree (
781
772
tmpdir ,
@@ -930,14 +921,6 @@ class _BackendPgf(_Backend):
930
921
FigureCanvas = FigureCanvasPgf
931
922
932
923
933
- def _cleanup_all ():
934
- LatexManager ._cleanup_remaining_instances ()
935
- TmpDirCleaner .cleanup_remaining_tmpdirs ()
936
-
937
-
938
- atexit .register (_cleanup_all )
939
-
940
-
941
924
class PdfPages :
942
925
"""
943
926
A multi-page PDF file using the pgf backend
0 commit comments