diff options
| author | Michael Kelly <mkelly@mozilla.com> | 2014-04-14 11:58:59 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-11-03 12:29:19 -0500 |
| commit | ebc8e79cf3bdd42a99e91d6e679248d07097d3db (patch) | |
| tree | 4c4a59eb2a00372efc7ac0794e5fd6f12a635a39 /django | |
| parent | f7969b0920c403118656f6bfec58d6454d79ef1a (diff) | |
Fixed #18523 -- Added stream-like API to HttpResponse.
Added getvalue() to HttpResponse to return the content of the response,
along with a few other methods to partially match io.IOBase.
Thanks Claude Paroz for the suggestion and Nick Sanford for review.
Diffstat (limited to 'django')
| -rw-r--r-- | django/http/response.py | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/django/http/response.py b/django/http/response.py index 0cc14d1346..9e8280a307 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -112,6 +112,7 @@ class HttpResponseBase(six.Iterator): # historical behavior of request_finished. self._handler_class = None self.cookies = SimpleCookie() + self.closed = False if status is not None: self.status_code = status if reason is not None: @@ -313,16 +314,26 @@ class HttpResponseBase(six.Iterator): closable.close() except Exception: pass + self.closed = True signals.request_finished.send(sender=self._handler_class) def write(self, content): - raise Exception("This %s instance is not writable" % self.__class__.__name__) + raise IOError("This %s instance is not writable" % self.__class__.__name__) def flush(self): pass def tell(self): - raise Exception("This %s instance cannot tell its position" % self.__class__.__name__) + raise IOError("This %s instance cannot tell its position" % self.__class__.__name__) + + # These methods partially implement a stream-like object interface. + # See https://docs.python.org/library/io.html#io.IOBase + + def writable(self): + return False + + def writelines(self, lines): + raise IOError("This %s instance is not writable" % self.__class__.__name__) class HttpResponse(HttpResponseBase): @@ -373,6 +384,16 @@ class HttpResponse(HttpResponseBase): def tell(self): return len(self.content) + def getvalue(self): + return self.content + + def writable(self): + return True + + def writelines(self, lines): + for line in lines: + self.write(line) + class StreamingHttpResponse(HttpResponseBase): """ @@ -410,6 +431,9 @@ class StreamingHttpResponse(HttpResponseBase): def __iter__(self): return self.streaming_content + def getvalue(self): + return b''.join(self.streaming_content) + class HttpResponseRedirectBase(HttpResponse): allowed_schemes = ['http', 'https', 'ftp'] |
