diff --git a/django/http/response.py b/django/http/response.py index 3365cdbdca..ed5c14eef8 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -263,6 +263,12 @@ class HttpResponseBase(six.Iterator): # These methods partially implement a stream-like object interface. # See https://docs.python.org/library/io.html#io.IOBase + def readable(self): + return False + + def seekable(self): + return False + def writable(self): return False diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt index 9360095e19..2e43506a88 100644 --- a/docs/ref/request-response.txt +++ b/docs/ref/request-response.txt @@ -807,6 +807,20 @@ Methods Returns the value of :attr:`HttpResponse.content`. This method makes 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() Always ``True``. This method makes an :class:`HttpResponse` instance a diff --git a/docs/releases/1.10.txt b/docs/releases/1.10.txt index 46cc18f467..ebfae18f24 100644 --- a/docs/releases/1.10.txt +++ b/docs/releases/1.10.txt @@ -174,6 +174,11 @@ Requests and Responses * 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 ^^^^^^^^^^^^^ diff --git a/tests/responses/tests.py b/tests/responses/tests.py index a34228ffa7..6d0588daa7 100644 --- a/tests/responses/tests.py +++ b/tests/responses/tests.py @@ -2,6 +2,8 @@ from __future__ import unicode_literals +import io + from django.conf import settings from django.http import HttpResponse from django.http.response import HttpResponseBase @@ -112,3 +114,10 @@ class HttpResponseTests(SimpleTestCase): response = HttpResponse(content="Café :)".encode(UTF8), status=201) 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))