Skip to content
Myr

clean up open_resource and tests

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

authored by David Lord · committed by David Lord · parent ad709be · Browse files · 3 files+13 added−21 removed
This commit

State

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

Diff stats

3 files+13 added−21 removed

  • tests:+7 added−17 removed
  • other:+6 added−4 removed

3 files+13 added−21 removed

modified:flask/helpers.py+4 added-4 removed
1051 unmodified lines
1052 do_something_with(contents)10531054 :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 == 'rt':1059 mode = 'r'1060 if mode not in ("r", "rb"):1059 if mode not in {"r", "rt", "rb"}:1060 raise ValueError("Resources can only be opened for reading")10611062 return open(os.path.join(self.root_path, resource), mode)106310641065def total_seconds(td):
38 unmodified lines
modified:tests/test_helpers.py+7 added-17 removed
1060 unmodified lines
1061 assert rv.status_code == 2001062 assert rv.data == b"Hello"1063 assert rv.mimetype == "text/html"10641065 @pytest.mark.parametrize('mode', [1066 'r',1067 'rb',1068 'rt'1069 ])1065 @pytest.mark.parametrize("mode", ("r", "rb", "rt"))1066 def test_open_resource(self, mode):1067 app = flask.Flask(__name__)1072 with app.open_resource('static/index.html', mode) as f:1073 assert '<h1>Hello World!</h1>' in str(f.read())10681075 @pytest.mark.parametrize('mode', [1076 'w',1077 'x',1078 'a',1079 'b',1080 't',1081 '+'1082 ])1069 with app.open_resource("static/index.html", mode) as f:1070 assert "<h1>Hello World!</h1>" in str(f.read())10711072 @pytest.mark.parametrize("mode", ("w", "x", "a", "r+"))1073 def test_open_resource_exceptions(self, mode):1074 app = flask.Flask(__name__)1085 with pytest.raises(ValueError):1086 app.open_resource('static/index.html', mode)10751076 with pytest.raises(ValueError):1077 app.open_resource("static/index.html", mode)
modified:CHANGES.rst+2 added
18 unmodified lines
19 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`)2526.. _#2935: https://github.com/pallets/flask/issues/293527.. _#2957: https://github.com/pallets/flask/issues/295728.. _#2994: https://github.com/pallets/flask/pull/2994
946 unmodified lines