From 05248a1009c05985b1c227a1a48b871c3068cb12 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Tue, 20 Oct 2015 14:23:58 -0700 Subject: [PATCH] Fixed #25576 -- Added IOBase methods required by TextIOWrapper to HttpResponse. --- django/http/response.py | 6 ++++++ docs/ref/request-response.txt | 14 ++++++++++++++ docs/releases/1.10.txt | 5 +++++ tests/responses/tests.py | 9 +++++++++ 4 files changed, 34 insertions(+) diff --git a/django/http/response.py b/django/http/response.py index 3365cdbdca8..ed5c14eef8f 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 9360095e196..2e43506a885 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 46cc18f467f..ebfae18f24c 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 a34228ffa7c..6d0588daa78 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))