Skip to content

Commit 2f3b2ad

Browse files
authored
Merge pull request matplotlib#25842 from meeseeksmachine/auto-backport-of-pr-25840-on-v3.7.x
Backport PR matplotlib#25840 on branch v3.7.x (Emit explanatory exception when no temporary cachedir can be created.)
2 parents c0cc7a6 + 791dc71 commit 2f3b2ad

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

lib/matplotlib/__init__.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,21 @@ def _get_config_or_cache_dir(xdg_base_getter):
518518
return str(configdir)
519519
# If the config or cache directory cannot be created or is not a writable
520520
# directory, create a temporary one.
521-
tmpdir = os.environ["MPLCONFIGDIR"] = \
522-
tempfile.mkdtemp(prefix="matplotlib-")
521+
try:
522+
tmpdir = tempfile.mkdtemp(prefix="matplotlib-")
523+
except OSError as exc:
524+
raise OSError(
525+
f"Matplotlib requires access to a writable cache directory, but the "
526+
f"default path ({configdir}) is not a writable directory, and a temporary "
527+
f"directory could not be created; set the MPLCONFIGDIR environment "
528+
f"variable to a writable directory") from exc
529+
os.environ["MPLCONFIGDIR"] = tmpdir
523530
atexit.register(shutil.rmtree, tmpdir)
524531
_log.warning(
525-
"Matplotlib created a temporary config/cache directory at %s because "
526-
"the default path (%s) is not a writable directory; it is highly "
527-
"recommended to set the MPLCONFIGDIR environment variable to a "
528-
"writable directory, in particular to speed up the import of "
529-
"Matplotlib and to better support multiprocessing.",
532+
"Matplotlib created a temporary cache directory at %s because the default path "
533+
"(%s) is not a writable directory; it is highly recommended to set the "
534+
"MPLCONFIGDIR environment variable to a writable directory, in particular to "
535+
"speed up the import of Matplotlib and to better support multiprocessing.",
530536
tmpdir, configdir)
531537
return tmpdir
532538

0 commit comments

Comments
 (0)