Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit da91154

Browse files
committed
Further simplify pgf tmpdir cleanup.
By carefully tearing down components of LatexManager in the right order (first the subprocess), we avoid the need for a global registry of things that need to be cleared at process termination.
1 parent e5404d8 commit da91154

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``backend_pgf.TmpDirCleaner``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... is deprecated, with no replacement.

lib/matplotlib/backends/backend_pgf.py

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import shutil
1212
import subprocess
1313
import sys
14-
import tempfile
1514
from tempfile import TemporaryDirectory
1615
import weakref
1716

@@ -200,7 +199,6 @@ class LatexManager:
200199
determining the metrics of text elements. The LaTeX environment can be
201200
modified by setting fonts and/or a custom preamble in `.rcParams`.
202201
"""
203-
_unclean_instances = weakref.WeakSet()
204202

205203
@staticmethod
206204
def _build_latex_header():
@@ -237,12 +235,6 @@ def _get_cached_or_new(cls):
237235
def _get_cached_or_new_impl(cls, header): # Helper for _get_cached_or_new.
238236
return cls()
239237

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-
246238
def _stdin_writeln(self, s):
247239
if self.latex is None:
248240
self._setup_latex_process()
@@ -268,13 +260,10 @@ def _expect_prompt(self):
268260
return self._expect("\n*")
269261

270262
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)
278267

279268
# test the LaTeX setup to ensure a clean startup of the subprocess
280269
self.texcommand = mpl.rcParams["pgf.texsystem"]
@@ -303,11 +292,21 @@ def __init__(self):
303292
self.str_cache = {} # cache for strings already processed
304293

305294
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.
307299
self.latex = subprocess.Popen(
308300
[self.texcommand, "-halt-on-error"],
309301
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
310302
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)
311310
# write header with 'pgf_backend_query_start' token
312311
self._stdin_writeln(self._build_latex_header())
313312
# read all lines until our 'pgf_backend_query_start' token appears
@@ -318,23 +317,6 @@ def _setup_latex_process(self):
318317
def latex_stdin_utf8(self):
319318
return self.latex.stdin
320319

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-
338320
def get_width_height_descent(self, text, prop):
339321
"""
340322
Get the width, total height and descent for a text typeset by the
@@ -766,16 +748,25 @@ class GraphicsContextPgf(GraphicsContextBase):
766748
pass
767749

768750

751+
@cbook.deprecated("3.4")
769752
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
771759

772760
@staticmethod
761+
@cbook.deprecated("3.4")
773762
def add(tmpdir):
774-
TmpDirCleaner.remaining_tmpdirs.add(tmpdir)
763+
TmpDirCleaner._remaining_tmpdirs.add(tmpdir)
775764

776765
@staticmethod
766+
@cbook.deprecated("3.4")
767+
@atexit.register
777768
def cleanup_remaining_tmpdirs():
778-
for tmpdir in TmpDirCleaner.remaining_tmpdirs:
769+
for tmpdir in TmpDirCleaner._remaining_tmpdirs:
779770
error_message = "error deleting tmp directory {}".format(tmpdir)
780771
shutil.rmtree(
781772
tmpdir,
@@ -930,14 +921,6 @@ class _BackendPgf(_Backend):
930921
FigureCanvas = FigureCanvasPgf
931922

932923

933-
def _cleanup_all():
934-
LatexManager._cleanup_remaining_instances()
935-
TmpDirCleaner.cleanup_remaining_tmpdirs()
936-
937-
938-
atexit.register(_cleanup_all)
939-
940-
941924
class PdfPages:
942925
"""
943926
A multi-page PDF file using the pgf backend

0 commit comments

Comments
 (0)