@@ -532,37 +532,48 @@ def _get_config_or_cache_dir(xdg_base_getter):
532
532
elif sys .platform .startswith (('linux' , 'freebsd' )):
533
533
# Only call _xdg_base_getter here so that MPLCONFIGDIR is tried first,
534
534
# 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
536
539
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})"
544
557
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"
548
559
# If the config or cache directory cannot be created or is not a writable
549
560
# directory, create a temporary one.
550
561
try :
551
562
tmpdir = tempfile .mkdtemp (prefix = "matplotlib-" )
552
563
except OSError as exc :
553
564
raise OSError (
554
565
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 "
556
567
f"directory could not be created; set the MPLCONFIGDIR environment "
557
568
f"variable to a writable directory" ) from exc
558
569
os .environ ["MPLCONFIGDIR" ] = tmpdir
559
570
atexit .register (shutil .rmtree , tmpdir )
560
571
_log .warning (
561
572
"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 "
563
574
"MPLCONFIGDIR environment variable to a writable directory, in particular to "
564
575
"speed up the import of Matplotlib and to better support multiprocessing." ,
565
- tmpdir , configdir )
576
+ tmpdir , issue_msg )
566
577
return tmpdir
567
578
568
579
0 commit comments