From ecaed490ea65bd59ef4ca64860f8592eefa59797 Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Sun, 16 Aug 2026 02:46:03 -0700 Subject: [PATCH] fix: Raise a clear error when the settings file does not exist Signed-off-by: Sai Asish Y --- RELEASE.md | 3 +++ pelican/settings.py | 4 ++++ pelican/tests/test_settings.py | 7 +++++++ 3 files changed, 14 insertions(+) create mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 00000000..5d2005eb --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,3 @@ +Release type: patch + +Raise a clear error when the specified settings file does not exist diff --git a/pelican/settings.py b/pelican/settings.py index 45957fd0..2a4e94a3 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -17,6 +17,8 @@ from pelican.paginator import PaginationRule def load_source(name: str, path: str) -> ModuleType: spec = importlib.util.spec_from_file_location(name, path) + if spec is None or spec.loader is None: + raise ImportError(f"Cannot import settings from {path}") mod = importlib.util.module_from_spec(spec) sys.modules[name] = mod spec.loader.exec_module(mod) @@ -242,6 +244,8 @@ def get_settings_from_module(module: ModuleType | None = None) -> Settings: def get_settings_from_file(path: str) -> Settings: """Loads settings from a file path, returning a dict.""" + if not os.path.isfile(path): + raise FileNotFoundError(f"Settings file not found: {path}") name, ext = os.path.splitext(os.path.basename(path)) module = load_source(name, path) return get_settings_from_module(module) diff --git a/pelican/tests/test_settings.py b/pelican/tests/test_settings.py index 54167e63..a3df4112 100644 --- a/pelican/tests/test_settings.py +++ b/pelican/tests/test_settings.py @@ -184,6 +184,13 @@ class TestSettingsConfiguration(unittest.TestCase): locale.setlocale(locale.LC_TIME, "") self.assertEqual(lc_time, locale.getlocale(locale.LC_TIME)) + def test_read_settings_missing_file(self): + missing = join(self.PATH, "no_such_conf.py") + with self.assertRaises(FileNotFoundError): + read_settings(missing) + with self.assertRaises(FileNotFoundError): + read_settings(join(self.PATH, "no_such_conf")) + def test_invalid_settings_throw_exception(self): # Test that the path name is valid