From 14c80085434894d5a26a4e1cee7f4f4f3b97d9ce Mon Sep 17 00:00:00 2001 From: 04cb <0x04cb@gmail.com> Date: Sat, 7 Mar 2026 13:41:46 +0800 Subject: [PATCH] Add ENOTDIR handling to Config.from_file with silent=True --- src/flask/config.py | 2 +- tests/test_config.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/flask/config.py b/src/flask/config.py index 34ef1a57..9e37f2bd 100644 --- a/src/flask/config.py +++ b/src/flask/config.py @@ -293,7 +293,7 @@ class Config(dict): # type: ignore[type-arg] with open(filename, "r" if text else "rb") as f: obj = load(f) except OSError as e: - if silent and e.errno in (errno.ENOENT, errno.EISDIR): + if silent and e.errno in (errno.ENOENT, errno.EISDIR, errno.ENOTDIR): return False e.strerror = f"Unable to load configuration file ({e.strerror})" diff --git a/tests/test_config.py b/tests/test_config.py index e5b1906e..084fd809 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -248,3 +248,20 @@ def test_from_pyfile_weird_encoding(tmp_path, encoding): app.config.from_pyfile(os.fspath(f)) value = app.config["TEST_VALUE"] assert value == "föö" + + +def test_config_from_file_enotdir_silent(tmp_path): + """Test that from_file with silent=True handles ENOTDIR error. + When a path component is a regular file instead of a directory, + from_file should return False instead of raising OSError. + """ + # Create a file where a directory is expected + config_file = tmp_path / "not_a_dir" + config_file.write_text("This is a file, not a directory") + + # Try to load a config file through the non-directory path + app = flask.Flask(__name__) + result = app.config.from_file( + os.path.join(config_file, "config.json"), load=json.load, silent=True + ) + assert result is False