Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #13222 -- Repeated iteration of HttpResponse

Thanks teepark for the report and grahamd for his insights.
  • Loading branch information...
commit 6a64822bf4632707212314a25a843c862bdb3874 1 parent e70170c
@aaugustin aaugustin authored
View
4 django/http/response.py
@@ -260,9 +260,9 @@ def content(self, value):
else:
self._container = [value]
self._base_content_is_iter = False
+ self._iterator = iter(self._container)
def __iter__(self):
- self._iterator = iter(self._container)
return self
def __next__(self):
@@ -284,7 +284,7 @@ def write(self, content):
def tell(self):
if self._base_content_is_iter:
raise Exception("This %s instance cannot tell its position" % self.__class__.__name__)
- return sum([len(chunk) for chunk in self])
+ return len(self.content)
class StreamingHttpResponse(HttpResponseBase):
View
7 tests/regressiontests/httpwrappers/tests.py
@@ -330,6 +330,13 @@ def test_iter_content(self):
self.assertRaises(UnicodeEncodeError,
getattr, r, 'content')
+ def test_iterator_isnt_rewound(self):
+ # Regression test for #13222
+ r = HttpResponse('abc')
+ i = iter(r)
+ self.assertEqual(list(i), [b'abc'])
+ self.assertEqual(list(i), [])
+
def test_file_interface(self):
r = HttpResponse()
r.write(b"hello")
Please sign in to comment.
Something went wrong with that request. Please try again.