Skip to content
Myr

Merge pull request #4992 from AntoineMath/file_mode

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by GitHub · parents 4c288bc, b10b6d4 · Browse files · 4 files+23 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+23 added−5 removed

  • tests:+13 added−2 removed
  • other:+10 added−3 removed

4 files+23 added−5 removed

modified:src/flask/config.py+8 added-3 removed
232 unmodified lines
233 self,234 filename: str,235 load: t.Callable[[t.IO[t.Any]], t.Mapping],236 silent: bool = False,237 text: bool = True,238 ) -> bool:239 """Update the values in the config from a file that is loaded240 using the ``load`` parameter. The loaded data is passed to the241 :meth:`from_mapping` method.
2 unmodified lines
244245 import json246 app.config.from_file("config.json", load=json.load)247247 import toml248 app.config.from_file("config.toml", load=toml.load)248 import tomllib249 app.config.from_file("config.toml", load=tomllib.load, text=False)250251 :param filename: The path to the data file. This can be an252 absolute path or relative to the config root path.253 :param load: A callable that takes a file handle and returns a254 mapping of loaded data from the file.255 :type load: ``Callable[[Reader], Mapping]`` where ``Reader``256 implements a ``read`` method.257 :param silent: Ignore the file if it doesn't exist.258 :param text: Open the file in text or binary mode.259 :return: ``True`` if the file was loaded successfully.260261 .. versionchanged:: 2.3262 The ``text`` parameter was added.263264 .. versionadded:: 2.0265 """266 filename = os.path.join(self.root_path, filename)267268 try:264 with open(filename) as f:269 with open(filename, "r" if text else "rb") as f:270 obj = load(f)271 except OSError as e:272 if silent and e.errno in (errno.ENOENT, errno.EISDIR):273 return False
70 unmodified lines
added:tests/static/config.toml+2 added
modified:tests/test_config.py+11 added-2 removed
4 unmodified lines
5import pytest67import flask899# config keys used for the TestConfig10TEST_KEY = "foo"11SECRET_KEY = "config"12
15 unmodified lines
28 app.config.from_object(__name__)29 common_object_test(app)303133def test_config_from_file():32def test_config_from_file_json():33 app = flask.Flask(__name__)34 current_dir = os.path.dirname(os.path.abspath(__file__))35 app.config.from_file(os.path.join(current_dir, "static", "config.json"), json.load)36 common_object_test(app)373839def test_config_from_file_toml():40 tomllib = pytest.importorskip("tomllib", reason="tomllib added in 3.11")41 app = flask.Flask(__name__)42 current_dir = os.path.dirname(os.path.abspath(__file__))43 app.config.from_file(44 os.path.join(current_dir, "static", "config.toml"), tomllib.load, text=False45 )46 common_object_test(app)474849def test_from_prefixed_env(monkeypatch):50 monkeypatch.setenv("FLASK_STRING", "value")51 monkeypatch.setenv("FLASK_BOOL", "true")52 monkeypatch.setenv("FLASK_INT", "1")
206 unmodified lines
modified:CHANGES.rst+2 added
31 unmodified lines
32 :issue:`4993`33- Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.34 :pr:`4947`35- Ensure subdomains are applied with nested blueprints. :issue:`4834`36- ``config.from_file`` can use ``text=False`` to indicate that the parser wants a37 binary file instead. :issue:`4989`383940Version 2.2.341-------------
1415 unmodified lines