From ad709be46e660b76263a50bfecdb390f0863a90c Mon Sep 17 00:00:00 2001 From: icreatedanaccount Date: Mon, 6 May 2019 10:56:35 -0400 Subject: [PATCH 1/3] Allow mode='rt' on open_resource() helper --- flask/helpers.py | 2 ++ tests/test_helpers.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/flask/helpers.py b/flask/helpers.py index 19cb4c99..78f4b37c 100644 --- a/flask/helpers.py +++ b/flask/helpers.py @@ -1055,6 +1055,8 @@ class _PackageBoundObject(object): subfolders use forward slashes as separator. :param mode: resource file opening mode, default is 'rb'. """ + if mode == 'rt': + mode = 'r' if mode not in ("r", "rb"): raise ValueError("Resources can only be opened for reading") return open(os.path.join(self.root_path, resource), mode) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index d65a5f5e..e7730da0 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1014,6 +1014,7 @@ class TestSafeJoin(object): print(flask.safe_join(*args)) + class TestHelpers(object): @pytest.mark.parametrize( "debug, expected_flag, expected_default_flag", @@ -1060,3 +1061,27 @@ class TestHelpers(object): assert rv.status_code == 200 assert rv.data == b"Hello" assert rv.mimetype == "text/html" + + @pytest.mark.parametrize('mode', [ + 'r', + 'rb', + 'rt' + ]) + def test_open_resource(self, mode): + app = flask.Flask(__name__) + with app.open_resource('static/index.html', mode) as f: + assert '

Hello World!

' in str(f.read()) + + @pytest.mark.parametrize('mode', [ + 'w', + 'x', + 'a', + 'b', + 't', + '+' + ]) + def test_open_resource_exceptions(self, mode): + app = flask.Flask(__name__) + with pytest.raises(ValueError): + app.open_resource('static/index.html', mode) + From 6f703a564c97b034eb0e98419595b7fa32d3562a Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 17 May 2019 13:20:31 -0700 Subject: [PATCH 2/3] clean up open_resource and tests --- CHANGES.rst | 2 ++ flask/helpers.py | 8 ++++---- tests/test_helpers.py | 24 +++++++----------------- 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index a383f3f4..930fd7e8 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -20,6 +20,8 @@ Unreleased (`#3059`_) - :func:`send_file` supports :class:`~io.BytesIO` partial content. (`#2957`_) +- :func:`open_resource` accepts the "rt" file mode. This still does + the same thing as "r". (:issue:`3163`) .. _#2935: https://github.com/pallets/flask/issues/2935 .. _#2957: https://github.com/pallets/flask/issues/2957 diff --git a/flask/helpers.py b/flask/helpers.py index 78f4b37c..c5e85a02 100644 --- a/flask/helpers.py +++ b/flask/helpers.py @@ -1053,12 +1053,12 @@ class _PackageBoundObject(object): :param resource: the name of the resource. To access resources within subfolders use forward slashes as separator. - :param mode: resource file opening mode, default is 'rb'. + :param mode: Open file in this mode. Only reading is supported, + valid values are "r" (or "rt") and "rb". """ - if mode == 'rt': - mode = 'r' - if mode not in ("r", "rb"): + if mode not in {"r", "rt", "rb"}: raise ValueError("Resources can only be opened for reading") + return open(os.path.join(self.root_path, resource), mode) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index e7730da0..cddc7517 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1062,26 +1062,16 @@ class TestHelpers(object): assert rv.data == b"Hello" assert rv.mimetype == "text/html" - @pytest.mark.parametrize('mode', [ - 'r', - 'rb', - 'rt' - ]) + @pytest.mark.parametrize("mode", ("r", "rb", "rt")) def test_open_resource(self, mode): app = flask.Flask(__name__) - with app.open_resource('static/index.html', mode) as f: - assert '

Hello World!

' in str(f.read()) - @pytest.mark.parametrize('mode', [ - 'w', - 'x', - 'a', - 'b', - 't', - '+' - ]) + with app.open_resource("static/index.html", mode) as f: + assert "

Hello World!

" in str(f.read()) + + @pytest.mark.parametrize("mode", ("w", "x", "a", "r+")) def test_open_resource_exceptions(self, mode): app = flask.Flask(__name__) - with pytest.raises(ValueError): - app.open_resource('static/index.html', mode) + with pytest.raises(ValueError): + app.open_resource("static/index.html", mode) From 6dbcbdee3553683215d9e50b59834cb9af3aebd3 Mon Sep 17 00:00:00 2001 From: David Lord Date: Fri, 17 May 2019 13:26:54 -0700 Subject: [PATCH 3/3] add sphinx-issues --- docs/conf.py | 2 ++ docs/requirements.txt | 1 + 2 files changed, 3 insertions(+) diff --git a/docs/conf.py b/docs/conf.py index f494ea8d..e263d01a 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -18,6 +18,7 @@ extensions = [ "sphinx.ext.intersphinx", "sphinxcontrib.log_cabinet", "pallets_sphinx_themes", + "sphinx_issues", ] intersphinx_mapping = { "python": ("https://docs.python.org/3/", None), @@ -29,6 +30,7 @@ intersphinx_mapping = { "wtforms": ("https://wtforms.readthedocs.io/en/stable/", None), "blinker": ("https://pythonhosted.org/blinker/", None), } +issues_github_path = "pallets/flask" # HTML ----------------------------------------------------------------- diff --git a/docs/requirements.txt b/docs/requirements.txt index e667deaa..6443d592 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,3 +1,4 @@ Sphinx~=1.8.0 Pallets-Sphinx-Themes~=1.1.0 sphinxcontrib-log-cabinet~=1.0.0 +sphinx-issues~=1.2.0