Fixed #13222 -- Repeated iteration of HttpResponse

Thanks teepark for the report and grahamd for his insights.
This commit is contained in:
Aymeric Augustin 2012-10-22 22:52:36 +02:00
parent e70170c2cb
commit 6a64822bf4
2 changed files with 9 additions and 2 deletions

View File

@ -260,9 +260,9 @@ class HttpResponse(HttpResponseBase):
else: else:
self._container = [value] self._container = [value]
self._base_content_is_iter = False self._base_content_is_iter = False
self._iterator = iter(self._container)
def __iter__(self): def __iter__(self):
self._iterator = iter(self._container)
return self return self
def __next__(self): def __next__(self):
@ -284,7 +284,7 @@ class HttpResponse(HttpResponseBase):
def tell(self): def tell(self):
if self._base_content_is_iter: if self._base_content_is_iter:
raise Exception("This %s instance cannot tell its position" % self.__class__.__name__) 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): class StreamingHttpResponse(HttpResponseBase):

View File

@ -330,6 +330,13 @@ class HttpResponseTests(unittest.TestCase):
self.assertRaises(UnicodeEncodeError, self.assertRaises(UnicodeEncodeError,
getattr, r, 'content') 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): def test_file_interface(self):
r = HttpResponse() r = HttpResponse()
r.write(b"hello") r.write(b"hello")