Repository: matplotlib/matplotlib
File: lib/matplotlib/__init__.py (Python)

Code before:


def _get_xdg_cache_dir():
    """
    Return the XDG cache directory, according to the XDG base directory spec:

    https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
    """
    return os.environ.get('XDG_CACHE_HOME') or str(Path.home() / ".cache")


def _get_config_or_cache_dir(xdg_base_getter):
    configdir = os.environ.get('MPLCONFIGDIR')
    if configdir:
        configdir = Path(configdir)
    elif sys.platform.startswith(('linux', 'freebsd')):
        # Only call _xdg_base_getter here so that MPLCONFIGDIR is tried first,
        # as _xdg_base_getter can throw.
        try:
            configdir = Path(xdg_base_getter(), "matplotlib")
        except RuntimeError:
            pass
    else:
        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:
        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:
        tmpdir = tempfile.mkdtemp(prefix="matplotlib-")
    except OSError as exc:
        raise OSError(
            f"Matplotlib requires access to a writable cache directory, but there "
            f"was an issue with {issue_msg}, and a temporary "
            f"directory could not be created; set the MPLCONFIGDIR environment "

Review comment (suggestion):
```suggestion
        except RuntimeError:  # raised if Path.home() is not available
```

Code after:


def _get_xdg_cache_dir():
    """
    Return the XDG cache directory, according to the XDG base directory spec:

    https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
    """
    return os.environ.get('XDG_CACHE_HOME') or str(Path.home() / ".cache")


def _get_config_or_cache_dir(xdg_base_getter):
    configdir = os.environ.get('MPLCONFIGDIR')
    if configdir:
        configdir = Path(configdir)
    elif sys.platform.startswith(('linux', 'freebsd')):
        # Only call _xdg_base_getter here so that MPLCONFIGDIR is tried first,
        # as _xdg_base_getter can throw.
        try:
            configdir = Path(xdg_base_getter(), "matplotlib")
        except RuntimeError:  # raised if Path.home() is not available
            pass
    else:
        try:
            configdir = Path.home() / ".matplotlib"
        except RuntimeError:  # raised if Path.home() is not available
            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:
        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:
        tmpdir = tempfile.mkdtemp(prefix="matplotlib-")
    except OSError as exc:
        raise OSError(
            f"Matplotlib requires access to a writable cache directory, but there "
            f"was an issue with {issue_msg}, and a temporary "
            f"directory could not be created; set the MPLCONFIGDIR environment "