From: Ori Livneh Date: Tue, 20 Dec 2011 07:01:37 +0000 (-0500) Subject: fix bug that causes iter_lines to truncate content X-Git-Tag: v0.8.7~10^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f214b91d770ef194f9fad7041a1356ff27097ae5;p=services%2Fpython-requests.git fix bug that causes iter_lines to truncate content Currently, if the response does not terminate with a newline, iter_lines truncates the trailing remainder. --- diff --git a/requests/models.py b/requests/models.py index e84c8f1..88a002a 100644 --- a/requests/models.py +++ b/requests/models.py @@ -597,7 +597,7 @@ class Response(object): def generate(): chunk = [] - while 1: + while True: c = self.raw.read(1) if not c: break @@ -608,6 +608,11 @@ class Response(object): else: chunk.append(c) + # Yield the remainder, in case the response + # did not terminate with a newline + if chunk: + yield ''.join(chunk) + self._content_consumed = True gen = generate() diff --git a/test_requests.py b/test_requests.py index 829d2a9..ebcb596 100755 --- a/test_requests.py +++ b/test_requests.py @@ -3,6 +3,7 @@ from __future__ import with_statement +import StringIO import time import os import unittest @@ -603,5 +604,21 @@ class RequestsTestSuite(unittest.TestCase): self.assertEqual(i, len_lines) + # Test 'dangling' fragment in responses that do not terminate in + # a newline. + quote = ( + '''Why will he not upon our fair request\n''' + '''Untent his person and share the air with us?''' + ) + + # Make a request and monkey-patch its contents + r = requests.get(httpbin('get')) + r.raw = StringIO.StringIO(quote) + + # Make sure iter_lines doesn't chop the trailing bit + lines = '\n'.join(r.iter_lines()) + self.assertEqual(lines, quote) + + if __name__ == '__main__': unittest.main()