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

Skip to content

Ensure that Matplotlib is importable even if there's no HOME. #20123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,14 @@ def _get_xdg_cache_dir():
return os.environ.get('XDG_CACHE_HOME') or str(Path.home() / ".cache")


def _get_config_or_cache_dir(xdg_base):
def _get_config_or_cache_dir(xdg_base_getter):
configdir = os.environ.get('MPLCONFIGDIR')
if configdir:
configdir = Path(configdir).resolve()
elif sys.platform.startswith(('linux', 'freebsd')) and xdg_base:
configdir = Path(xdg_base, "matplotlib")
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")
else:
configdir = Path.home() / ".matplotlib"
try:
Expand Down Expand Up @@ -470,7 +472,7 @@ def get_configdir():
4. Else, create a temporary directory, and use it as the configuration
directory.
"""
return _get_config_or_cache_dir(_get_xdg_config_dir())
return _get_config_or_cache_dir(_get_xdg_config_dir)


@_logged_cached('CACHEDIR=%s')
Expand All @@ -481,7 +483,7 @@ def get_cachedir():
The procedure used to find the directory is the same as for
_get_config_dir, except using ``$XDG_CACHE_HOME``/``$HOME/.cache`` instead.
"""
return _get_config_or_cache_dir(_get_xdg_cache_dir())
return _get_config_or_cache_dir(_get_xdg_cache_dir)


@_logged_cached('matplotlib data path: %s')
Expand Down
14 changes: 9 additions & 5 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,18 @@


# OS Font paths
try:
_HOME = Path.home()
except Exception: # Exceptions thrown by home() are not specified...
_HOME = Path(os.devnull) # Just an arbitrary path with no children.
MSFolders = \
r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
MSFontDirectories = [
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts',
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts']
MSUserFontDirectories = [
str(Path.home() / 'AppData/Local/Microsoft/Windows/Fonts'),
str(Path.home() / 'AppData/Roaming/Microsoft/Windows/Fonts'),
str(_HOME / 'AppData/Local/Microsoft/Windows/Fonts'),
str(_HOME / 'AppData/Roaming/Microsoft/Windows/Fonts'),
]
X11FontDirectories = [
# an old standard installation point
Expand All @@ -149,9 +153,9 @@
# common application, not really useful
"/usr/lib/openoffice/share/fonts/truetype/",
# user fonts
str((Path(os.environ.get('XDG_DATA_HOME') or Path.home() / ".local/share"))
str((Path(os.environ.get('XDG_DATA_HOME') or _HOME / ".local/share"))
/ "fonts"),
str(Path.home() / ".fonts"),
str(_HOME / ".fonts"),
]
OSXFontDirectories = [
"/Library/Fonts/",
Expand All @@ -160,7 +164,7 @@
# fonts installed via MacPorts
"/opt/local/share/fonts",
# user fonts
str(Path.home() / "Library/Fonts"),
str(_HOME / "Library/Fonts"),
]


Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def test_tmpconfigdir_warning(tmpdir):
os.chmod(tmpdir, mode)


def test_importable_with_no_home(tmpdir):
subprocess.run(
[sys.executable, "-c",
"import pathlib; pathlib.Path.home = lambda *args: 1/0; "
"import matplotlib.pyplot"],
env={**os.environ, "MPLCONFIGDIR": str(tmpdir)}, check=True)


def test_use_doc_standard_backends():
"""
Test that the standard backends mentioned in the docstring of
Expand Down