8000 Merge pull request #20123 from anntzer/hl · matplotlib/matplotlib@3628661 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3628661

Browse files
authored
Merge pull request #20123 from anntzer/hl
Ensure that Matplotlib is importable even if there's no HOME.
2 parents a324f51 + a5effc9 commit 3628661

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
@@ -424,12 +424,14 @@ def _get_xdg_cache_dir():
424424
return os.environ.get('XDG_CACHE_HOME') or str(Path.home() / ".cache")
425425

426426

427-
def _get_config_or_cache_dir(xdg_base):
427+
def _get_config_or_cache_dir(xdg_base_getter):
428428
configdir = os.environ.get('MPLCONFIGDIR')
429429
if configdir:
430430
configdir = Path(configdir).resolve()
431-
elif sys.platform.startswith(('linux', 'freebsd')) and xdg_base:
432-
configdir = Path(xdg_base, "matplotlib")
431+
elif sys.platform.startswith(('linux', 'freebsd')):
432+
# Only call _xdg_base_getter here so that MPLCONFIGDIR is tried first,
433+
# as _xdg_base_getter can throw.
434+
configdir = Path(xdg_base_getter(), "matplotlib")
433435
else:
434436
configdir = Path.home() / ".matplotlib"
435437
try:
@@ -470,7 +472,7 @@ def get_configdir():
470472
4. Else, create a temporary directory, and use it as the configuration
471473
directory.
472474
"""
473-
return _get_config_or_cache_dir(_get_xdg_config_dir())
475+
return _get_config_or_cache_dir(_get_xdg_config_dir)
474476

475477

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

486488

487489
@_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)
0