Skip to content

Commit 0d225fd

Browse files
authored
use env config path for system config path when no system config path (#430)
* use env config path for system config path when no system config path * update test for system config path on windows * handle env == system in config path same as jupyter_path * make sure we don't duplicate paths when env == sys (Windows)
1 parent 2a2807b commit 0d225fd

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

jupyter_core/paths.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,26 +338,29 @@ def jupyter_path(*subdirs: str) -> list[str]:
338338
return paths
339339

340340

341+
ENV_CONFIG_PATH: list[str] = [str(Path(sys.prefix, "etc", "jupyter"))]
342+
341343
if use_platform_dirs():
342344
if os.name == "nt" and not _use_programdata:
343345
# default PROGRAMDATA is not safe by default on Windows
344-
SYSTEM_CONFIG_PATH = []
346+
# use ENV to avoid an empty list, since some may assume this is non-empty
347+
SYSTEM_CONFIG_PATH = ENV_CONFIG_PATH[:]
345348
else:
346349
SYSTEM_CONFIG_PATH = platformdirs.site_config_dir(
347350
APPNAME, appauthor=False, multipath=True
348351
).split(os.pathsep)
349352
elif os.name == "nt":
350353
# PROGRAMDATA is not defined by default on XP, and not safe by default
354+
# but make sure it's not empty
351355
if _win_programdata:
352356
SYSTEM_CONFIG_PATH = [str(Path(_win_programdata, "jupyter"))]
353357
else:
354-
SYSTEM_CONFIG_PATH = []
358+
SYSTEM_CONFIG_PATH = ENV_CONFIG_PATH[:]
355359
else:
356360
SYSTEM_CONFIG_PATH = [
357361
"/usr/local/etc/jupyter",
358362
"/etc/jupyter",
359363
]
360-
ENV_CONFIG_PATH: list[str] = [str(Path(sys.prefix, "etc", "jupyter"))]
361364

362365

363366
def jupyter_config_path() -> list[str]:
@@ -408,7 +411,13 @@ def jupyter_config_path() -> list[str]:
408411
if userdir not in user:
409412
user.append(userdir)
410413

411-
env = [p for p in ENV_CONFIG_PATH if p not in SYSTEM_CONFIG_PATH]
414+
# Windows usually doesn't have a 'system' prefix,
415+
# so 'system' and 'env' are the same
416+
# make sure that env can still be preferred in this case
417+
if ENV_CONFIG_PATH == SYSTEM_CONFIG_PATH:
418+
env = ENV_CONFIG_PATH
419+
else:
420+
env = [p for p in ENV_CONFIG_PATH if p not in SYSTEM_CONFIG_PATH]
412421

413422
if prefer_environment_over_user():
414423
paths.extend(env)
@@ -418,7 +427,8 @@ def jupyter_config_path() -> list[str]:
418427
paths.extend(env)
419428

420429
# Finally, system path
421-
paths.extend(SYSTEM_CONFIG_PATH)
430+
if ENV_CONFIG_PATH != SYSTEM_CONFIG_PATH:
431+
paths.extend(SYSTEM_CONFIG_PATH)
422432
return paths
423433

424434

tests/test_paths.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,14 @@ def test_jupyter_config_path_env():
400400
assert path[:2] == [pjoin("foo", "bar"), pjoin("bar", "baz")]
401401

402402

403+
def test_jupyter_config_path_duplicate_sys_env():
404+
# make sure we don't repeat duplicate system/env paths
405+
with patch("jupyter_core.paths.SYSTEM_CONFIG_PATH", paths.ENV_CONFIG_PATH):
406+
config_path = jupyter_config_path()
407+
for path in paths.ENV_CONFIG_PATH:
408+
assert config_path.count(path) == 1
409+
410+
403411
def test_prefer_environment_over_user():
404412
with prefer_env:
405413
assert prefer_environment_over_user() is True
@@ -621,7 +629,7 @@ def test_windows_programdata(request, tmp_path, use_programdata, use_platformdir
621629
# SIM300 (yoda conditions) gets false positives
622630
# when the 'variable' we are testing looks like a constant
623631
if use_programdata in {"0", None}:
624-
assert paths.SYSTEM_CONFIG_PATH == []
632+
assert paths.SYSTEM_CONFIG_PATH == paths.ENV_CONFIG_PATH
625633
assert paths.SYSTEM_JUPYTER_PATH == [str(Path(sys.prefix, "share", "jupyter"))] # noqa:SIM300
626634
# use_programdata is True
627635
elif use_platformdirs == "1":

0 commit comments

Comments
 (0)