Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 095a60c

Browse filesBrowse 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 db7cd7e commit 095a60c
Copy full SHA for 095a60c

File tree

Expand file treeCollapse file tree

2 files changed

+30
-11
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+30
-11
lines changed

‎lib/matplotlib/__init__.py

Copy file name to clipboardExpand all lines: lib/matplotlib/__init__.py
+12-11Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,16 +519,6 @@ def get_home():
519519
return None
520520

521521

522-
def _create_tmp_config_or_cache_dir():
523-
"""
524-
If the config or cache directory cannot be created, create a temporary one.
525-
"""
526-
configdir = os.environ['MPLCONFIGDIR'] = (
527-
tempfile.mkdtemp(prefix='matplotlib-'))
528-
atexit.register(shutil.rmtree, configdir)
529-
return configdir
530-
531-
532522
def _get_xdg_config_dir():
533523
"""
534524
Return the XDG configuration directory, according to the `XDG
@@ -562,7 +552,18 @@ def _get_config_or_cache_dir(xdg_base):
562552
else:
563553
if os.access(str(configdir), os.W_OK) and configdir.is_dir():
564554
return str(configdir)
565-
return _create_tmp_config_or_cache_dir()
555+
# If the config or cache directory cannot be created or is not a writable
556+
# directory, create a temporary one.
557+
tmpdir = os.environ["MPLCONFIGDIR"] = \
558+
tempfile.mkdtemp(prefix="matplotlib-")
559+
atexit.register(shutil.rmtree, tmpdir)
560+
_log.warning(
561+
"Matplotlib created a temporary config/cache directory at %s because "
562+
"the default path (%s) is not a writable directory; it is recommended "
563+
"to set the MPLCONFIGDIR environment variable to a writable "
564+
"directory, in particular to speed up the import of Matplotlib.",
565+
configdir, tmpdir)
566+
return tmpdir
566567

567568

568569
@_logged_cached('CONFIGDIR=%s')

‎lib/matplotlib/tests/test_matplotlib.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_matplotlib.py
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
import os
2+
import subprocess
3+
import sys
4+
15
import matplotlib
26
import matplotlib.rcsetup
37

48

9+
def test_tmpconfigdir_warning(tmpdir):
10+
"""Test that a warning is emitted if a temporary configdir must be used."""
11+
mode = os.stat(tmpdir).st_mode
12+
try:
13+
os.chmod(tmpdir, 0)
14+
proc = subprocess.run(
15+
[sys.executable, "-c", "import matplotlib"],
16+
env={**os.environ, "MPLCONFIGDIR": str(tmpdir)},
17+
stderr=subprocess.PIPE, universal_newlines=True, check=True)
18+
assert "it is recommended to set the MPLCONFIGDIR" in proc.stderr
19+
finally:
20+
os.chmod(tmpdir, mode)
21+
22+
523
def test_use_doc_standard_backends():
624
"""
725
Test that the standard backends mentioned in the docstring of

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.