add ENOTDIR to from_file silent error handling

This commit is contained in:
abdullahmujahidali 2026-02-09 07:27:09 +05:00
parent d3b78fd18a
commit 560ea7fd40
3 changed files with 17 additions and 1 deletions

View file

@ -23,6 +23,8 @@ Unreleased
switching ``POST`` to ``GET``. This preserves the current behavior of switching ``POST`` to ``GET``. This preserves the current behavior of
``GET`` and ``POST`` redirects, and is also correct for frontend libraries ``GET`` and ``POST`` redirects, and is also correct for frontend libraries
such as HTMX. :issue:`5895` such as HTMX. :issue:`5895`
- ``Config.from_file()`` with ``silent=True`` handles ``ENOTDIR`` errors,
matching the behavior of ``Config.from_pyfile()``. :issue:`5912`
Version 3.1.2 Version 3.1.2

View file

@ -293,7 +293,7 @@ class Config(dict): # type: ignore[type-arg]
with open(filename, "r" if text else "rb") as f: with open(filename, "r" if text else "rb") as f:
obj = load(f) obj = load(f)
except OSError as e: 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 return False
e.strerror = f"Unable to load configuration file ({e.strerror})" e.strerror = f"Unable to load configuration file ({e.strerror})"

View file

@ -195,6 +195,20 @@ def test_config_missing_file():
assert not app.config.from_file("missing.json", load=json.load, silent=True) assert not app.config.from_file("missing.json", load=json.load, silent=True)
def test_config_from_file_enotdir(tmp_path):
not_a_dir = tmp_path / "not_a_dir"
not_a_dir.write_text("")
app = flask.Flask(__name__)
app.config.root_path = str(tmp_path)
with pytest.raises(OSError):
app.config.from_file("not_a_dir/config.json", load=json.load)
assert not app.config.from_file(
"not_a_dir/config.json", load=json.load, silent=True
)
def test_custom_config_class(): def test_custom_config_class():
class Config(flask.Config): class Config(flask.Config):
pass pass