Fixed #25576 -- Added IOBase methods required by TextIOWrapper to HttpResponse.
This commit is contained in:
parent
1745aa000a
commit
05248a1009
|
@ -263,6 +263,12 @@ class HttpResponseBase(six.Iterator):
|
||||||
# These methods partially implement a stream-like object interface.
|
# These methods partially implement a stream-like object interface.
|
||||||
# See https://docs.python.org/library/io.html#io.IOBase
|
# See https://docs.python.org/library/io.html#io.IOBase
|
||||||
|
|
||||||
|
def readable(self):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def seekable(self):
|
||||||
|
return False
|
||||||
|
|
||||||
def writable(self):
|
def writable(self):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
@ -807,6 +807,20 @@ Methods
|
||||||
Returns the value of :attr:`HttpResponse.content`. This method makes
|
Returns the value of :attr:`HttpResponse.content`. This method makes
|
||||||
an :class:`HttpResponse` instance a stream-like object.
|
an :class:`HttpResponse` instance a stream-like object.
|
||||||
|
|
||||||
|
.. method:: HttpResponse.readable()
|
||||||
|
|
||||||
|
.. versionadded:: 1.10
|
||||||
|
|
||||||
|
Always ``False``. This method makes an :class:`HttpResponse` instance a
|
||||||
|
stream-like object.
|
||||||
|
|
||||||
|
.. method:: HttpResponse.seekable()
|
||||||
|
|
||||||
|
.. versionadded:: 1.10
|
||||||
|
|
||||||
|
Always ``False``. This method makes an :class:`HttpResponse` instance a
|
||||||
|
stream-like object.
|
||||||
|
|
||||||
.. method:: HttpResponse.writable()
|
.. method:: HttpResponse.writable()
|
||||||
|
|
||||||
Always ``True``. This method makes an :class:`HttpResponse` instance a
|
Always ``True``. This method makes an :class:`HttpResponse` instance a
|
||||||
|
|
|
@ -174,6 +174,11 @@ Requests and Responses
|
||||||
|
|
||||||
* Added ``request.user`` to the debug view.
|
* Added ``request.user`` to the debug view.
|
||||||
|
|
||||||
|
* Added :class:`~django.http.HttpResponse` methods
|
||||||
|
:meth:`~django.http.HttpResponse.readable()` and
|
||||||
|
:meth:`~django.http.HttpResponse.seekable()` to make an instance a
|
||||||
|
stream-like object and allow wrapping it with :py:class:`io.TextIOWrapper`.
|
||||||
|
|
||||||
Serialization
|
Serialization
|
||||||
^^^^^^^^^^^^^
|
^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import io
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.http.response import HttpResponseBase
|
from django.http.response import HttpResponseBase
|
||||||
|
@ -112,3 +114,10 @@ class HttpResponseTests(SimpleTestCase):
|
||||||
response = HttpResponse(content="Café :)".encode(UTF8), status=201)
|
response = HttpResponse(content="Café :)".encode(UTF8), status=201)
|
||||||
expected = '<HttpResponse status_code=201, "text/html; charset=utf-8">'
|
expected = '<HttpResponse status_code=201, "text/html; charset=utf-8">'
|
||||||
self.assertEqual(repr(response), expected)
|
self.assertEqual(repr(response), expected)
|
||||||
|
|
||||||
|
def test_wrap_textiowrapper(self):
|
||||||
|
content = "Café :)"
|
||||||
|
r = HttpResponse()
|
||||||
|
with io.TextIOWrapper(r, UTF8) as buf:
|
||||||
|
buf.write(content)
|
||||||
|
self.assertEqual(r.content, content.encode(UTF8))
|
||||||
|
|
Loading…
Reference in New Issue