Skip to content

Commit 942e865

Browse files
committed
Use pathlib in texmanager.
Only a few changes are needed. Note that make_png returns an absolute path so path joining is not even needed here. Also drop an unrelated but outdated comment re: background handling, which is obsolete since 8940c66 (look for `if hack: ...`).
1 parent cae6696 commit 942e865

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

lib/matplotlib/texmanager.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import functools
2424
import hashlib
2525
import logging
26-
import os
2726
from pathlib import Path
2827
import subprocess
2928
from tempfile import TemporaryDirectory
@@ -63,7 +62,7 @@ class TexManager:
6362
Repeated calls to this constructor always return the same instance.
6463
"""
6564

66-
_texcache = os.path.join(mpl.get_cachedir(), 'tex.cache')
65+
_cache_dir = Path(mpl.get_cachedir(), 'tex.cache')
6766
_grey_arrayd = {}
6867

6968
_font_families = ('serif', 'sans-serif', 'cursive', 'monospace')
@@ -109,7 +108,7 @@ class TexManager:
109108

110109
@functools.lru_cache # Always return the same instance.
111110
def __new__(cls):
112-
Path(cls._texcache).mkdir(parents=True, exist_ok=True)
111+
cls._cache_dir.mkdir(parents=True, exist_ok=True)
113112
return object.__new__(cls)
114113

115114
@classmethod
@@ -176,14 +175,14 @@ def get_basefile(cls, tex, fontsize, dpi=None):
176175
src.encode('utf-8'),
177176
usedforsecurity=False
178177
).hexdigest()
179-
filepath = Path(cls._texcache)
178+
filepath = cls._cache_dir
180179

181180
num_letters, num_levels = 2, 2
182181
for i in range(0, num_letters*num_levels, num_letters):
183-
filepath = filepath / Path(filehash[i:i+2])
182+
filepath = filepath / filehash[i:i+2]
184183

185184
filepath.mkdir(parents=True, exist_ok=True)
186-
return os.path.join(filepath, filehash)
185+
return str(filepath / filehash)
187186

188187
@classmethod
189188
def get_font_preamble(cls):
@@ -253,7 +252,7 @@ def _run_checked_subprocess(cls, command, tex, *, cwd=None):
253252
_log.debug(cbook._pformat_subprocess(command))
254253
try:
255254
report = subprocess.check_output(
256-
command, cwd=cwd if cwd is not None else cls._texcache,
255+
command, cwd=cwd if cwd is not None else cls._cache_dir,
257256
stderr=subprocess.STDOUT)
258257
except FileNotFoundError as exc:
259258
raise RuntimeError(
@@ -312,7 +311,6 @@ def make_png(cls, tex, fontsize, dpi):
312311
Return the file name.
313312
"""
314313
pngfile = Path(cls.get_basefile(tex, fontsize)).with_suffix(".png")
315-
# see get_rgba for a discussion of the background
316314
if not pngfile.exists():
317315
dvifile = cls.make_dvi(tex, fontsize)
318316
with TemporaryDirectory(dir=pngfile.parent) as tmpdir:
@@ -338,7 +336,7 @@ def get_grey(cls, tex, fontsize=None, dpi=None):
338336
alpha = cls._grey_arrayd.get(key)
339337
if alpha is None:
340338
pngfile = cls.make_png(tex, fontsize, dpi)
341-
rgba = mpl.image.imread(os.path.join(cls._texcache, pngfile))
339+
rgba = mpl.image.imread(pngfile)
342340
cls._grey_arrayd[key] = alpha = rgba[:, :, -1]
343341
return alpha
344342

0 commit comments

Comments
 (0)