Add ENOTDIR handling to Config.from_file with silent=True

This commit is contained in:
04cb 2026-03-07 13:41:46 +08:00
parent 3a9d54f3da
commit 14c8008543
2 changed files with 18 additions and 1 deletions

View file

@ -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})"

View file

@ -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