Merge pull request #3174 from icreatedanaccount/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Allow mode='rt' on open_resource() helper
authored by David Lord · committed by GitHub · parents 05a4e15, 6dbcbde · Browse files · 5 files·+24 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+24 added−2 removed
- tests:+15 added−0 removed
- other:+9 added−2 removed
Provenance
- Commit
8ef56c8- Tree
a244c9d- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
05a4e15- Parent 2
6dbcbde
5 files changed+24 added−2 removed
modified:docs/conf.py+2 added
16 unmodified lines17 "sphinx.ext.autodoc",18 "sphinx.ext.intersphinx",19 "sphinxcontrib.log_cabinet",20 "pallets_sphinx_themes",21+ "sphinx_issues",22 ]23 intersphinx_mapping = {24 "python": ("https://docs.python.org/3/", None),25 "werkzeug": ("http://werkzeug.pocoo.org/docs/", None),3 unmodified lines29 "sqlalchemy": ("https://docs.sqlalchemy.org/", None),30 "wtforms": ("https://wtforms.readthedocs.io/en/stable/", None),31 "blinker": ("https://pythonhosted.org/blinker/", None),32 }33+issues_github_path = "pallets/flask"34 35 # HTML -----------------------------------------------------------------36 37 html_theme = "flask"70 unmodified linesmodified:docs/requirements.txt+1 added
1 Sphinx~=1.8.02 Pallets-Sphinx-Themes~=1.1.03 sphinxcontrib-log-cabinet~=1.0.04+sphinx-issues~=1.2.0modified:flask/helpers.py+4 added-2 removed
1051 unmodified lines1052 do_something_with(contents)1053 1054 :param resource: the name of the resource. To access resources within1055 subfolders use forward slashes as separator.1056- :param mode: resource file opening mode, default is 'rb'.1056+ :param mode: Open file in this mode. Only reading is supported,1057+ valid values are "r" (or "rt") and "rb".1058 """1058- if mode not in ("r", "rb"):1059+ if mode not in {"r", "rt", "rb"}:1060 raise ValueError("Resources can only be opened for reading")1061+1062 return open(os.path.join(self.root_path, resource), mode)1063 1064 1065 def total_seconds(td):38 unmodified linesmodified:tests/test_helpers.py+15 added
1012 unmodified lines1013 with pytest.raises(NotFound):1014 print(flask.safe_join(*args))1015 1016 1017+1018 class TestHelpers(object):1019 @pytest.mark.parametrize(1020 "debug, expected_flag, expected_default_flag",1021 [38 unmodified lines1060 rv = flask.helpers.make_response("Hello")1061 assert rv.status_code == 2001062 assert rv.data == b"Hello"1063 assert rv.mimetype == "text/html"1064+1065+ @pytest.mark.parametrize("mode", ("r", "rb", "rt"))1066+ def test_open_resource(self, mode):1067+ app = flask.Flask(__name__)1068+1069+ with app.open_resource("static/index.html", mode) as f:1070+ assert "<h1>Hello World!</h1>" in str(f.read())1071+1072+ @pytest.mark.parametrize("mode", ("w", "x", "a", "r+"))1073+ def test_open_resource_exceptions(self, mode):1074+ app = flask.Flask(__name__)1075+1076+ with pytest.raises(ValueError):1077+ app.open_resource("static/index.html", mode)modified:CHANGES.rst+2 added
18 unmodified lines19 described in PEP 0519, to support :mod:`pathlib` in Python 3.20 (`#3059`_)21 - :func:`send_file` supports :class:`~io.BytesIO` partial content.22 (`#2957`_)23+- :func:`open_resource` accepts the "rt" file mode. This still does24+ the same thing as "r". (:issue:`3163`)25 26 .. _#2935: https://github.com/pallets/flask/issues/293527 .. _#2957: https://github.com/pallets/flask/issues/295728 .. _#2994: https://github.com/pallets/flask/pull/2994946 unmodified lines