8000 Warn if a temporary config/cache dir must be created. · matplotlib/matplotlib@694d69e · GitHub
[go: up one dir, main page]

Skip to content

Commit 694d69e

Browse files
committed
Warn if a temporary config/cache dir must be created.
Paying the cost of regen'ing the font cache on every import seems a bit silly when one can just set MPLCONFIGDIR to a persistent directory. Moreover the tmpdir approach is brittle against forking; this is fixable but somewhat complex to do.
1 parent 439a122 commit 694d69e

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

lib/matplotlib/__init__.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -431,16 +431,6 @@ def get_home():
431431
return None
432432

433433

434-
def _create_tmp_config_or_cache_dir():
435-
"""
436-
If the config or cache directory cannot be created, create a temporary one.
437-
"""
438-
configdir = os.environ['MPLCONFIGDIR'] = (
439-
tempfile.mkdtemp(prefix='matplotlib-'))
440-
atexit.register(shutil.rmtree, configdir)
441-
return configdir
442-
443-
444434
def _get_xdg_config_dir():
445435
"""
446436
Return the XDG configuration directory, according to the `XDG
@@ -474,7 +464,19 @@ def _get_config_or_cache_dir(xdg_base):
474464
else:
475465
if os.access(str(configdir), os.W_OK) and configdir.is_dir():
476466
return str(configdir)
477-
return _create_tmp_config_or_cache_dir()
467+
# If the config or cache directory cannot be created or is not a writable
468+
# directory, create a temporary one.
469+
tmpdir = os.environ["MPLCONFIGDIR"] = \
470+
tempfile.mkdtemp(prefix="matplotlib-")
471+
atexit.register(shutil.rmtree, tmpdir)
472+
_log.warning(
473+
"Matplotlib created a temporary config/cache directory at %s because "
474+
"the default path (%s) is not a writable directory; it is highly "
475+
"recommended to set the MPLCONFIGDIR environment variable to a "
476+
"writable directory, in particular to speed up the import of "
477+
"Matplotlib and to 8000 better support multiprocessing.",
478+
configdir, tmpdir)
479+
return tmpdir
478480

479481

480482
@_logged_cached('CONFIGDIR=%s')

lib/matplotlib/tests/test_matplotlib.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
1+
import os
2+
import subprocess
3+
import sys
4+
5+
import pytest
6+
17
import matplotlib
2-
import matplotlib.rcsetup
8+
9+
10+
@pytest.mark.skipif(
11+
os.name == "nt", reason="chmod() doesn't work as is on Windows")
12+
def test_tmpconfigdir_warning(tmpdir):
13+
"""Test that a warning is emitted if a temporary configdir must be used."""
14+
mode = os.stat(tmpdir).st_mode
15+
try:
16+
os.chmod(tmpdir, 0)
17+
proc = subprocess.run(
18+
[sys.executable, "-c", "import matplotlib"],
19+
env={**os.environ, "MPLCONFIGDIR": str(tmpdir)},
20+
stderr=subprocess.PIPE, universal_newlines=True, check=True)
21+
assert "set the MPLCONFIGDIR" in proc.stderr
22+
finally:
23+
os.chmod(tmpdir, mode)
324

425

526
def test_use_doc_standard_backends():

0 commit comments

Comments
 (0)
0