Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5054100

Browse files
Added handling for undetermined home directory (matplotlib#30454)
* Added handleing for undetermined home directory * Add comment on the cause of the RuntimeError --------- Co-authored-by: Tim Hoffmann <[email protected]>
1 parent 2eccf17 commit 5054100

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

lib/matplotlib/__init__.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -532,37 +532,48 @@ def _get_config_or_cache_dir(xdg_base_getter):
532532
elif sys.platform.startswith(('linux', 'freebsd')):
533533
# Only call _xdg_base_getter here so that MPLCONFIGDIR is tried first,
534534
# as _xdg_base_getter can throw.
535-
configdir = Path(xdg_base_getter(), "matplotlib")
535+
try:
536+
configdir = Path(xdg_base_getter(), "matplotlib")
537+
except RuntimeError: # raised if Path.home() is not available
538+
pass
536539
else:
537-
configdir = Path.home() / ".matplotlib"
538-
# Resolve the path to handle potential issues with inaccessible symlinks.
539-
configdir = configdir.resolve()
540-
try:
541-
configdir.mkdir(parents=True, exist_ok=True)
542-
except OSError as exc:
543-
_log.warning("mkdir -p failed for path %s: %s", configdir, exc)
540+
try:
541+
configdir = Path.home() / ".matplotlib"
542+
except RuntimeError: # raised if Path.home() is not available
543+
pass
544+
545+
if configdir:
546+
# Resolve the path to handle potential issues with inaccessible symlinks.
547+
configdir = configdir.resolve()
548+
try:
549+
configdir.mkdir(parents=True, exist_ok=True)
550+
except OSError as exc:
551+
_log.warning("mkdir -p failed for path %s: %s", configdir, exc)
552+
else:
553+
if os.access(str(configdir), os.W_OK) and configdir.is_dir():
554+
return str(configdir)
555+
_log.warning("%s is not a writable directory", configdir)
556+
issue_msg = "the default path ({configdir})"
544557
else:
545-
if os.access(str(configdir), os.W_OK) and configdir.is_dir():
546-
return str(configdir)
547-
_log.warning("%s is not a writable directory", configdir)
558+
issue_msg = "resolving the home directory"
548559
# If the config or cache directory cannot be created or is not a writable
549560
# directory, create a temporary one.
550561
try:
551562
tmpdir = tempfile.mkdtemp(prefix="matplotlib-")
552563
except OSError as exc:
553564
raise OSError(
554565
f"Matplotlib requires access to a writable cache directory, but there "
555-
f"was an issue with the default path ({configdir}), and a temporary "
566+
f"was an issue with {issue_msg}, and a temporary "
556567
f"directory could not be created; set the MPLCONFIGDIR environment "
557568
f"variable to a writable directory") from exc
558569
os.environ["MPLCONFIGDIR"] = tmpdir
559570
atexit.register(shutil.rmtree, tmpdir)
560571
_log.warning(
561572
"Matplotlib created a temporary cache directory at %s because there was "
562-
"an issue with the default path (%s); it is highly recommended to set the "
573+
"an issue with %s; it is highly recommended to set the "
563574
"MPLCONFIGDIR environment variable to a writable directory, in particular to "
564575
"speed up the import of Matplotlib and to better support multiprocessing.",
565-
tmpdir, configdir)
576+
tmpdir, issue_msg)
566577
return tmpdir
567578

568579

0 commit comments

Comments
 (0)