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

Skip to content

Commit 5faa579

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR #20123: Ensure that Matplotlib is importable even if there's no HOME.
1 parent 418cdfe commit 5faa579

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

lib/matplotlib/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,14 @@ def _get_xdg_cache_dir():
408408
return os.environ.get('XDG_CACHE_HOME') or str(Path.home() / ".cache")
409409

410410

411-
def _get_config_or_cache_dir(xdg_base):
411+
def _get_config_or_cache_dir(xdg_base_getter):
412412
configdir = os.environ.get('MPLCONFIGDIR')
413413
if configdir:
414414
configdir = Path(configdir).resolve()
415-
elif sys.platform.startswith(('linux', 'freebsd')) and xdg_base:
416-
configdir = Path(xdg_base, "matplotlib")
415+
elif sys.platform.startswith(('linux', 'freebsd')):
416+
# Only call _xdg_base_getter here so that MPLCONFIGDIR is tried first,
417+
# as _xdg_base_getter can throw.
418+
configdir = Path(xdg_base_getter(), "matplotlib")
417419
else:
418420
configdir = Path.home() / ".matplotlib"
419421
try:
@@ -454,7 +456,7 @@ def get_configdir():
454456
4. Else, create a temporary directory, and use it as the configuration
455457
directory.
456458
"""
457-
return _get_config_or_cache_dir(_get_xdg_config_dir())
459+
return _get_config_or_cache_dir(_get_xdg_config_dir)
458460

459461

460462
@_logged_cached('CACHEDIR=%s')
@@ -465,7 +467,7 @@ def get_cachedir():
465467
The procedure used to find the directory is the same as for
466468
_get_config_dir, except using ``$XDG_CACHE_HOME``/``$HOME/.cache`` instead.
467469
"""
468-
return _get_config_or_cache_dir(_get_xdg_cache_dir())
470+
return _get_config_or_cache_dir(_get_xdg_cache_dir)
469471

470472

471473
@_logged_cached('matplotlib data path: %s')

lib/matplotlib/font_manager.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,18 @@
129129

130130

131131
# OS Font paths
132+
try:
133+
_HOME = Path.home()
134+
except Exception: # Exceptions thrown by home() are not specified...
135+
_HOME = Path(os.devnull) # Just an arbitrary path with no children.
132136
MSFolders = \
133137
r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
134138
MSFontDirectories = [
135139
r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts',
136140
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Fonts']
137141
MSUserFontDirectories = [
138-
str(Path.home() / 'AppData/Local/Microsoft/Windows/Fonts'),
139-
str(Path.home() / 'AppData/Roaming/Microsoft/Windows/Fonts'),
142+
str(_HOME / 'AppData/Local/Microsoft/Windows/Fonts'),
143+
str(_HOME / 'AppData/Roaming/Microsoft/Windows/Fonts'),
140144
]
141145
X11FontDirectories = [
142146
# an old standard installation point
@@ -149,9 +153,9 @@
149153
# common application, not really useful
150154
"/usr/lib/openoffice/share/fonts/truetype/",
151155
# user fonts
152-
str((Path(os.environ.get('XDG_DATA_HOME') or Path.home() / ".local/share"))
156+
str((Path(os.environ.get('XDG_DATA_HOME') or _HOME / ".local/share"))
153157
/ "fonts"),
154-
str(Path.home() / ".fonts"),
158+
str(_HOME / ".fonts"),
155159
]
156160
OSXFontDirectories = [
157161
"/Library/Fonts/",
@@ -160,7 +164,7 @@
160164
# fonts installed via MacPorts
161165
"/opt/local/share/fonts",
162166
# user fonts
163-
str(Path.home() / "Library/Fonts"),
167+
str(_HOME / "Library/Fonts"),
164168
]
165169

166170

lib/matplotlib/tests/test_matplotlib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ def test_tmpconfigdir_warning(tmpdir):
2525
os.chmod(tmpdir, mode)
2626

2727

28+
def test_importable_with_no_home(tmpdir):
29+
subprocess.run(
30+
[sys.executable, "-c",
31+
"import pathlib; pathlib.Path.home = lambda *args: 1/0; "
32+
"import matplotlib.pyplot"],
33+
env={**os.environ, "MPLCONFIGDIR": str(tmpdir)}, check=True)
34+
35+
2836
def test_use_doc_standard_backends():
2937
"""
3038
Test that the standard backends mentioned in the docstring of

0 commit comments

Comments
 (0)