forked from orbit-oss/flask
clean up open_resource and tests
This commit is contained in:
parent
ad709be46e
commit
6f703a564c
3 changed files with 13 additions and 21 deletions
|
|
@ -20,6 +20,8 @@ Unreleased
|
||||||
(`#3059`_)
|
(`#3059`_)
|
||||||
- :func:`send_file` supports :class:`~io.BytesIO` partial content.
|
- :func:`send_file` supports :class:`~io.BytesIO` partial content.
|
||||||
(`#2957`_)
|
(`#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
|
.. _#2935: https://github.com/pallets/flask/issues/2935
|
||||||
.. _#2957: https://github.com/pallets/flask/issues/2957
|
.. _#2957: https://github.com/pallets/flask/issues/2957
|
||||||
|
|
|
||||||
|
|
@ -1053,12 +1053,12 @@ class _PackageBoundObject(object):
|
||||||
|
|
||||||
:param resource: the name of the resource. To access resources within
|
:param resource: the name of the resource. To access resources within
|
||||||
subfolders use forward slashes as separator.
|
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':
|
if mode not in {"r", "rt", "rb"}:
|
||||||
mode = 'r'
|
|
||||||
if mode not in ("r", "rb"):
|
|
||||||
raise ValueError("Resources can only be opened for reading")
|
raise ValueError("Resources can only be opened for reading")
|
||||||
|
|
||||||
return open(os.path.join(self.root_path, resource), mode)
|
return open(os.path.join(self.root_path, resource), mode)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1062,26 +1062,16 @@ class TestHelpers(object):
|
||||||
assert rv.data == b"Hello"
|
assert rv.data == b"Hello"
|
||||||
assert rv.mimetype == "text/html"
|
assert rv.mimetype == "text/html"
|
||||||
|
|
||||||
@pytest.mark.parametrize('mode', [
|
@pytest.mark.parametrize("mode", ("r", "rb", "rt"))
|
||||||
'r',
|
|
||||||
'rb',
|
|
||||||
'rt'
|
|
||||||
])
|
|
||||||
def test_open_resource(self, mode):
|
def test_open_resource(self, mode):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
with app.open_resource('static/index.html', mode) as f:
|
|
||||||
assert '<h1>Hello World!</h1>' in str(f.read())
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('mode', [
|
with app.open_resource("static/index.html", mode) as f:
|
||||||
'w',
|
assert "<h1>Hello World!</h1>" in str(f.read())
|
||||||
'x',
|
|
||||||
'a',
|
@pytest.mark.parametrize("mode", ("w", "x", "a", "r+"))
|
||||||
'b',
|
|
||||||
't',
|
|
||||||
'+'
|
|
||||||
])
|
|
||||||
def test_open_resource_exceptions(self, mode):
|
def test_open_resource_exceptions(self, mode):
|
||||||
app = flask.Flask(__name__)
|
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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue