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 0cb2ae1

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 0cb2ae1
Copy full SHA for 0cb2ae1

File tree

Expand file treeCollapse file tree

2 files changed

+35
-12
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-12
lines changed

‎lib/matplotlib/__init__.py

Copy file name to clipboardExpand all lines: lib/matplotlib/__init__.py
+13-11Lines changed: 13 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,19 @@ 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 highly "
563+
"recommended to set the MPLCONFIGDIR environment variable to a "
564+
"writable directory, in particular to speed up the import of "
565+
"Matplotlib and to better support multiprocessing.",
566+
configdir, tmpdir)
567+
return tmpdir
566568

567569

568570
@_logged_cached('CONFIGDIR=%s')

‎lib/matplotlib/tests/test_matplotlib.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_matplotlib.py
+22-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.