Skip to content
Myr

fix bug that causes iter_lines to truncate content

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Currently, if the response does not terminate with a newline, iter_lines truncates the trailing remainder.

authored by Ori Livneh · committed by Ori Livneh · parent f80fbad · Browse files · 2 files+23 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+23 added−1 removed

  • other:+23 added−1 removed

2 files+23 added−1 removed

modified:requests/models.py+6 added-1 removed
595 unmodified lines
596597 def generate():598 chunk = []599600 while 1:600 while True:601 c = self.raw.read(1)602 if not c:603 break604
2 unmodified lines
607 chunk = []608 else:609 chunk.append(c)610611 # Yield the remainder, in case the response612 # did not terminate with a newline613 if chunk:614 yield ''.join(chunk)615616 self._content_consumed = True617618 gen = generate()619
75 unmodified lines
modified:test_requests.py+17 added
1 unmodified line
2# -*- coding: utf-8 -*-34from __future__ import with_statement56import StringIO7import time8import os9import unittest10
592 unmodified lines
603 len_lines = len([l for l in r.iter_lines()])604605 self.assertEqual(i, len_lines)606607 # Test 'dangling' fragment in responses that do not terminate in608 # a newline.609 quote = (610 '''Why will he not upon our fair request\n'''611 '''Untent his person and share the air with us?'''612 )613614 # Make a request and monkey-patch its contents615 r = requests.get(httpbin('get'))616 r.raw = StringIO.StringIO(quote)617618 # Make sure iter_lines doesn't chop the trailing bit619 lines = '\n'.join(r.iter_lines())620 self.assertEqual(lines, quote)621622623if __name__ == '__main__':624 unittest.main()