From 80994eaeac7e2079325c8f6cba2e3bd4e84d9324 Mon Sep 17 00:00:00 2001 From: Logan Pageler Date: Thu, 21 Aug 2025 20:24:58 -0700 Subject: [PATCH 1/2] Added handleing for undetermined home directory --- lib/matplotlib/__init__.py | 39 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index dc3555fd7969..95e422b4fa74 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -532,19 +532,30 @@ def _get_config_or_cache_dir(xdg_base_getter): elif sys.platform.startswith(('linux', 'freebsd')): # Only call _xdg_base_getter here so that MPLCONFIGDIR is tried first, # as _xdg_base_getter can throw. - configdir = Path(xdg_base_getter(), "matplotlib") + try: + configdir = Path(xdg_base_getter(), "matplotlib") + except RuntimeError: + pass else: - configdir = Path.home() / ".matplotlib" - # Resolve the path to handle potential issues with inaccessible symlinks. - configdir = configdir.resolve() - try: - configdir.mkdir(parents=True, exist_ok=True) - except OSError as exc: - _log.warning("mkdir -p failed for path %s: %s", configdir, exc) + try: + configdir = Path.home() / ".matplotlib" + except RuntimeError: + pass + + if configdir: + # Resolve the path to handle potential issues with inaccessible symlinks. + configdir = configdir.resolve() + try: + configdir.mkdir(parents=True, exist_ok=True) + except OSError as exc: + _log.warning("mkdir -p failed for path %s: %s", configdir, exc) + else: + if os.access(str(configdir), os.W_OK) and configdir.is_dir(): + return str(configdir) + _log.warning("%s is not a writable directory", configdir) + issue_msg = "the default path ({configdir})" else: - if os.access(str(configdir), os.W_OK) and configdir.is_dir(): - return str(configdir) - _log.warning("%s is not a writable directory", configdir) + issue_msg = "resolving the home directory" # If the config or cache directory cannot be created or is not a writable # directory, create a temporary one. try: @@ -552,17 +563,17 @@ def _get_config_or_cache_dir(xdg_base_getter): except OSError as exc: raise OSError( f"Matplotlib requires access to a writable cache directory, but there " - f"was an issue with the default path ({configdir}), and a temporary " + f"was an issue with {issue_msg}, and a temporary " f"directory could not be created; set the MPLCONFIGDIR environment " f"variable to a writable directory") from exc os.environ["MPLCONFIGDIR"] = tmpdir atexit.register(shutil.rmtree, tmpdir) _log.warning( "Matplotlib created a temporary cache directory at %s because there was " - "an issue with the default path (%s); it is highly recommended to set the " + "an issue with %s; 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.", - tmpdir, configdir) + tmpdir, issue_msg) return tmpdir From 01e1163d78efb3ef4d192f93483baad0e907bfdd Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 22 Aug 2025 11:27:27 +0200 Subject: [PATCH 2/2] Add comment on the cause of the RuntimeError --- lib/matplotlib/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/__init__.py b/lib/matplotlib/__init__.py index 95e422b4fa74..d1e575107f49 100644 --- a/lib/matplotlib/__init__.py +++ b/lib/matplotlib/__init__.py @@ -534,12 +534,12 @@ def _get_config_or_cache_dir(xdg_base_getter): # as _xdg_base_getter can throw. try: configdir = Path(xdg_base_getter(), "matplotlib") - except RuntimeError: + except RuntimeError: # raised if Path.home() is not available pass else: try: configdir = Path.home() / ".matplotlib" - except RuntimeError: + except RuntimeError: # raised if Path.home() is not available pass if configdir: