Skip to content

Warn if a temporary config/cache dir must be created. #15933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,16 +431,6 @@ def get_home():
return None


def _create_tmp_config_or_cache_dir():
"""
If the config or cache directory cannot be created, create a temporary one.
"""
configdir = os.environ['MPLCONFIGDIR'] = (
tempfile.mkdtemp(prefix='matplotlib-'))
atexit.register(shutil.rmtree, configdir)
return configdir


def _get_xdg_config_dir():
"""
Return the XDG configuration directory, according to the `XDG
Expand Down Expand Up @@ -474,7 +464,19 @@ def _get_config_or_cache_dir(xdg_base):
else:
if os.access(str(configdir), os.W_OK) and configdir.is_dir():
return str(configdir)
return _create_tmp_config_or_cache_dir()
# If the config or cache directory cannot be created or is not a writable
# directory, create a temporary one.
tmpdir = os.environ["MPLCONFIGDIR"] = \
tempfile.mkdtemp(prefix="matplotlib-")
atexit.register(shutil.rmtree, tmpdir)
_log.warning(
"Matplotlib created a temporary config/cache directory at %s because "
"the default path (%s) is not a writable directory; it is highly "
"recommended to set the MPLCONFIGDIR environment variable to a "
"writable directory, in particular to speed up the import of "
"Matplotlib and to better support multiprocessing.",
configdir, tmpdir)
return tmpdir


@_logged_cached('CONFIGDIR=%s')
Expand Down
23 changes: 22 additions & 1 deletion lib/matplotlib/tests/test_matplotlib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import os
import subprocess
import sys

import pytest

import matplotlib
import matplotlib.rcsetup


@pytest.mark.skipif(
os.name == "nt", reason="chmod() doesn't work as is on Windows")
def test_tmpconfigdir_warning(tmpdir):
"""Test that a warning is emitted if a temporary configdir must be used."""
mode = os.stat(tmpdir).st_mode
try:
os.chmod(tmpdir, 0)
proc = subprocess.run(
[sys.executable, "-c", "import matplotlib"],
env={**os.environ, "MPLCONFIGDIR": str(tmpdir)},
stderr=subprocess.PIPE, universal_newlines=True, check=True)
assert "set the MPLCONFIGDIR" in proc.stderr
finally:
os.chmod(tmpdir, mode)


def test_use_doc_standard_backends():
Expand Down