Skip to content
Myr

Considerably simplify and fix the iter_lines() function

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

1. Do not use rstrip() as this would also throw away meaningful whitespaces 2. Avoid assuming what striplines() considers a line break terminator, the list of those is quite long in Unicode. 3. Add tests for the above edge cases.

authored by Jakub Wieczorek · committed by Jakub Wieczorek · parent 6bb0529 · Browse files · 2 files+21 added−25 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+21 added−25 removed

  • tests:+11 added−7 removed
  • other:+10 added−18 removed

2 files+21 added−25 removed

modified:requests/models.py+10 added-18 removed
692 unmodified lines
693 gen = stream_decode_response_unicode(gen, self)694695 return gen696697 def iter_lines(self, chunk_size=10 * 1024, decode_unicode=None):697 def iter_lines(self, decode_unicode=None):698 """Iterates over the response data, one line at a time. This699 avoids reading the content at once into memory for large700 responses.701 """702703 #TODO: why rstrip by default703 pending = None704706 for chunk in self.iter_content(chunk_size, decode_unicode=decode_unicode):705 for chunk in self.iter_content(chunk_size=10 * 1024, decode_unicode=decode_unicode):706707 if pending is not None:708 chunk = pending + chunk710 lines = chunk.splitlines(True)709 lines = chunk.splitlines()710712 for line in lines[:-1]:713 yield line.rstrip()714715 # Save the last part of the chunk for next iteration, to keep full line together716 # lines may be empty for the last chunk of a chunked response717718 if lines:719 pending = lines[-1]720 #if pending is a complete line, give it baack721 if pending[-1] == '\n':722 yield pending.rstrip()723 pending = None711 # An incomplete line.712 if lines[-1].endswith(chunk[-1]):713 pending = lines.pop()714 else:715 pending = None716727 # Yield the last line717 for line in lines:718 yield line719720 if pending is not None:729 yield pending.rstrip()721 yield pending722723 @property724 def content(self):725 """Content of the response, in bytes."""
63 unmodified lines
modified:tests/test_requests.py+11 added-7 removed
724 unmodified lines
725 len_lines = len(lines)726727 self.assertEqual(i, len_lines)728729 # Test 'dangling' fragment in responses that do not terminate in730 # a newline.729 # Tests that trailing whitespaces within lines do not get stripped.730 # Tests that a trailing non-terminated line does not get stripped.731 quote = (732 '''Why will he not upon our fair request\n'''733 '''Untent his person and share the air with us?'''732 '''Agamemnon \n'''733 '''\tWhy will he not upon our fair request\r\n'''734 '''\tUntent his person and share the air with us?'''735 )736737 # Make a request and monkey-patch its contents738 r = get(httpbin('get'))739 r.raw = StringIO(quote)740740 # Make sure iter_lines doesn't chop the trailing bit741 lines = '\n'.join(r.iter_lines())742 self.assertEqual(lines, quote)741 lines = list(r.iter_lines())742 len_lines = len(lines)743 self.assertEqual(len_lines, 3)744745 joined = lines[0] + '\n' + lines[1] + '\r\n' + lines[2]746 self.assertEqual(joined, quote)747748 def test_safe_mode(self):749750 safe = requests.session(config=dict(safe_mode=True))751
56 unmodified lines