8000 Merge pull request #15933 from anntzer/warntmpconfigdir · matplotlib/matplotlib@7f808e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f808e7

Browse files
authored
Merge pull request #15933 from anntzer/warntmpconfigdir
Warn if a temporary config/cache dir must be created.
2 parents 080fdc9 + 694d69e commit 7f808e7

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 base
@@ -475,7 +465,19 @@ def _get_config_or_cache_dir(xdg_base):
475465
else:
476466
if os.access(str(configdir), os.W_OK) and configdir.is_dir():
477467
return str(configdir)
478-
return _create_tmp_config_or_cache_dir()
468+
# If the config or cache directory cannot be created or is not a writable
469+
# directory, create a temporary one.
470+
tmpdir = os.environ["MPLCONFIGDIR"] = \
471+
tempfile.mkdtemp(prefix="matplotlib-")
472+
atexit.register(shutil.rmtree, tmpdir)
473+
_log.warning(
474+
"Matplotlib created a temporary config/cache directory at %s because "
475+
"the default path (%s) is not a writable directory; it is highly "
476+
"recommended to set the MPLCONFIGDIR environment variable to a "
477+
"writable directory, in particular to speed up the import of "
478+
"Matplotlib and to better support multiprocessing.",
479+
configdir, tmpdir)
480+
return tmpdir
479481

480482

481483
@_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