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
Provenance
- Commit
2ad4d8d- Tree
bddf2fb- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
4c288bc- Parent 2
b10b6d4
4 files changed+23 added−5 removed
modified:src/flask/config.py+8 added-3 removed
232 unmodified lines233 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 lines244 245 import json246 app.config.from_file("config.json", load=json.load)247 247- 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)250 251 :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.260 261+ .. versionchanged:: 2.3262+ The ``text`` parameter was added.263+264 .. versionadded:: 2.0265 """266 filename = os.path.join(self.root_path, filename)267 268 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 False70 unmodified linesadded:tests/static/config.toml+2 added
12TEST_KEY="foo"SECRET_KEY="config"modified:tests/test_config.py+11 added-2 removed
4 unmodified lines5 import pytest6 7 import flask8 9-9 # config keys used for the TestConfig10 TEST_KEY = "foo"11 SECRET_KEY = "config"12 15 unmodified lines28 app.config.from_object(__name__)29 common_object_test(app)30 31 33-def test_config_from_file():32+def 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)37 38 39+def 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)47+48+49 def 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 linesmodified:CHANGES.rst+2 added
31 unmodified lines32 :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`38 39 40 Version 2.2.341 -------------1415 unmodified lines