Merge pull request #2957 from IgnasiBosch/2943-bytesio-partial-content
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #2943: Allow bytesio partial content
authored by David Lord · committed by GitHub · parents 4f3dbb3, 366f3f4 · Browse files · 3 files·+37 added−9 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+37 added−9 removed
- tests:+14 added−0 removed
- other:+23 added−9 removed
Provenance
- Commit
4f32c6d- Tree
8458f33- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
4f3dbb3- Parent 2
366f3f4
3 files changed+37 added−9 removed
modified:flask/helpers.py+12 added-2 removed
6 unmodified lines7 8 :copyright: © 2010 by the Pallets team.9 :license: BSD, see LICENSE for more details.10 """11-11+import io12 import os13 import socket14 import sys15 import pkgutil494 unmodified lines510 Filenames are encoded with ASCII instead of Latin-1 for broader511 compatibility with WSGI servers.512 513 .. versionchanged:: 1.1514- Filenames may be a `PathLike` object.514+ Filename may be a :class:`~os.PathLike` object.515 516+ .. versionadded:: 1.1517+ Partial content supports :class:`~io.BytesIO`.518+519 :param filename_or_fp: the filename of the file to send.520 This is relative to the :attr:`~Flask.root_path`521 if a relative path is specified.522 Alternatively a file object might be provided in79 unmodified lines602 file = open(filename, 'rb')603 mtime = os.path.getmtime(filename)604 fsize = os.path.getsize(filename)605 headers['Content-Length'] = fsize606+ elif isinstance(file, io.BytesIO):607+ try:608+ fsize = file.getbuffer().nbytes609+ except AttributeError:610+ # Python 2 doesn't have getbuffer611+ fsize = len(file.getvalue())612+ headers['Content-Length'] = fsize613 data = wrap_file(request.environ, file)614 615 rv = current_app.response_class(data, mimetype=mimetype, headers=headers,616 direct_passthrough=True)451 unmodified linesmodified:tests/test_helpers.py+14 added
8 unmodified lines9 :license: BSD, see LICENSE for more details.10 """11 12 import datetime13+import io14 import os15 import uuid16 17 import pytest590 unmodified lines608 datetime.datetime(1999, 1, 1))})609 assert rv.status_code == 200610 rv.close()611 612+ def test_send_file_range_request_bytesio(self, app, client):613+ @app.route('/')614+ def index():615+ file = io.BytesIO(b'somethingsomething')616+ return flask.send_file(617+ file, attachment_filename='filename', conditional=True618+ )619+620+ rv = client.get('/', headers={'Range': 'bytes=4-15'})621+ assert rv.status_code == 206622+ assert rv.data == b'somethingsomething'[4:16]623+ rv.close()624+625 @pytest.mark.skipif(626 not callable(getattr(Range, 'to_content_range_header', None)),627 reason="not implemented within werkzeug"628 )376 unmodified linesmodified:CHANGES.rst+11 added-7 removed
8 unmodified lines9 10 Unreleased11 12 - :meth:`flask.RequestContext.copy` includes the current session13- object in the request context copy. This prevents ``flask.session``14- pointing to an out-of-date object. (`#2935`)15-- Using built-in RequestContext, unprintable Unicode characters in Host16- header will result in a HTTP 400 response and not HTTP 500 as previously.17- (`#2994`)18-- :func:`send_file` supports ``PathLike`` objects as describe in19- PEP 0519, to support ``pathlib`` in Python 3. (`#3059`_)13+ object in the request context copy. This prevents ``session``14+ pointing to an out-of-date object. (`#2935`_)15+- Using built-in RequestContext, unprintable Unicode characters in16+ Host header will result in a HTTP 400 response and not HTTP 500 as17+ previously. (`#2994`_)18+- :func:`send_file` supports :class:`~os.PathLike` objects as19+ 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 24 .. _#2935: https://github.com/pallets/flask/issues/293525+.. _#2957: https://github.com/pallets/flask/issues/295726 .. _#2994: https://github.com/pallets/flask/pull/299427 .. _#3059: https://github.com/pallets/flask/pull/305928 29 931 unmodified lines