mirror of
https://github.com/django/django.git
synced 2025-10-24 22:26:08 +00:00
Fixed #13222 -- Made HttpResponse iterable once
response.content can be accessed many times as desired, and always returns the same result. iter(response) works only once and consumes the iterator.
This commit is contained in:
@@ -283,7 +283,8 @@ class HttpResponse(HttpResponseBase):
|
||||
'deprecated. Use `StreamingHttpResponse` instead '
|
||||
'if you need the streaming behavior.',
|
||||
PendingDeprecationWarning, stacklevel=2)
|
||||
self._iterator = iter(self._container)
|
||||
if not hasattr(self, '_iterator'):
|
||||
self._iterator = iter(self._container)
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
@@ -303,7 +304,7 @@ class HttpResponse(HttpResponseBase):
|
||||
|
||||
def tell(self):
|
||||
self._consume_content()
|
||||
return sum(len(chunk) for chunk in self)
|
||||
return len(self.content)
|
||||
|
||||
|
||||
class StreamingHttpResponse(HttpResponseBase):
|
||||
|
||||
@@ -596,7 +596,11 @@ class TransactionTestCase(SimpleTestCase):
|
||||
msg_prefix + "Couldn't retrieve content: Response code was %d"
|
||||
" (expected %d)" % (response.status_code, status_code))
|
||||
text = force_text(text, encoding=response._charset)
|
||||
content = b''.join(response).decode(response._charset)
|
||||
if response.streaming:
|
||||
content = b''.join(response.streaming_content)
|
||||
else:
|
||||
content = response.content
|
||||
content = content.decode(response._charset)
|
||||
# Avoid ResourceWarning about unclosed files.
|
||||
response.close()
|
||||
if html:
|
||||
|
||||
Reference in New Issue
Block a user