Fixed #30294 -- Allowed HttpResponse to accept memoryview content.
This commit is contained in:
parent
879cc3da62
commit
9aa56cb0d5
|
@ -229,7 +229,7 @@ class HttpResponseBase:
|
|||
# Handle string types -- we can't rely on force_bytes here because:
|
||||
# - Python attempts str conversion first
|
||||
# - when self._charset != 'utf-8' it re-encodes the content
|
||||
if isinstance(value, bytes):
|
||||
if isinstance(value, (bytes, memoryview)):
|
||||
return bytes(value)
|
||||
if isinstance(value, str):
|
||||
return bytes(value.encode(self.charset))
|
||||
|
|
|
@ -631,13 +631,18 @@ Usage
|
|||
Passing strings
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
Typical usage is to pass the contents of the page, as a string or bytestring,
|
||||
to the :class:`HttpResponse` constructor::
|
||||
Typical usage is to pass the contents of the page, as a string, bytestring,
|
||||
or :class:`memoryview`, to the :class:`HttpResponse` constructor::
|
||||
|
||||
>>> from django.http import HttpResponse
|
||||
>>> response = HttpResponse("Here's the text of the Web page.")
|
||||
>>> response = HttpResponse("Text only, please.", content_type="text/plain")
|
||||
>>> response = HttpResponse(b'Bytestrings are also accepted.')
|
||||
>>> response = HttpResponse(memoryview(b'Memoryview as well.'))
|
||||
|
||||
.. versionchanged:: 3.0
|
||||
|
||||
Support for :class:`memoryview` was added.
|
||||
|
||||
But if you want to add content incrementally, you can use ``response`` as a
|
||||
file-like object::
|
||||
|
@ -741,10 +746,10 @@ Methods
|
|||
Instantiates an ``HttpResponse`` object with the given page content and
|
||||
content type.
|
||||
|
||||
``content`` is most commonly an iterator, bytestring, or string. Other
|
||||
types will be converted to a bytestring by encoding their string
|
||||
representation. Iterators should return strings or bytestrings and those
|
||||
will be joined together to form the content of the response.
|
||||
``content`` is most commonly an iterator, bytestring, :class:`memoryview`,
|
||||
or string. Other types will be converted to a bytestring by encoding their
|
||||
string representation. Iterators should return strings or bytestrings and
|
||||
those will be joined together to form the content of the response.
|
||||
|
||||
``content_type`` is the MIME type optionally completed by a character set
|
||||
encoding and is used to fill the HTTP ``Content-Type`` header. If not
|
||||
|
@ -760,6 +765,10 @@ Methods
|
|||
given it will be extracted from ``content_type``, and if that
|
||||
is unsuccessful, the :setting:`DEFAULT_CHARSET` setting will be used.
|
||||
|
||||
.. versionchanged:: 3.0
|
||||
|
||||
Support for :class:`memoryview` ``content`` was added.
|
||||
|
||||
.. method:: HttpResponse.__setitem__(header, value)
|
||||
|
||||
Sets the given header name to the given value. Both ``header`` and
|
||||
|
|
|
@ -188,7 +188,8 @@ Models
|
|||
Requests and Responses
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
* ...
|
||||
* Allowed :class:`~django.http.HttpResponse` to be initialized with
|
||||
:class:`memoryview` content.
|
||||
|
||||
Serialization
|
||||
~~~~~~~~~~~~~
|
||||
|
|
|
@ -366,6 +366,10 @@ class HttpResponseTests(unittest.TestCase):
|
|||
r.content = 12345
|
||||
self.assertEqual(r.content, b'12345')
|
||||
|
||||
def test_memoryview_content(self):
|
||||
r = HttpResponse(memoryview(b'memoryview'))
|
||||
self.assertEqual(r.content, b'memoryview')
|
||||
|
||||
def test_iter_content(self):
|
||||
r = HttpResponse(['abc', 'def', 'ghi'])
|
||||
self.assertEqual(r.content, b'abcdefghi')
|
||||
|
|
Loading…
Reference in New Issue