Merge branch 'iter_lines-fix' of https://github.com/fawek/requests into develop
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 98cbde1, 228c5e6 · 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
Provenance
- Commit
c7f165a- Tree
a606264- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
98cbde1- Parent 2
228c5e6
2 files changed+21 added−25 removed
modified:requests/models.py+10 added-18 removed
702 unmodified lines703 gen = stream_decode_response_unicode(gen, self)704 705 return gen706 707- def iter_lines(self, chunk_size=10 * 1024, decode_unicode=None):707+ def iter_lines(self, decode_unicode=None):708 """Iterates over the response data, one line at a time. This709 avoids reading the content at once into memory for large710 responses.711 """712 713- #TODO: why rstrip by default713 pending = None714 716- for chunk in self.iter_content(chunk_size, decode_unicode=decode_unicode):715+ for chunk in self.iter_content(chunk_size=10 * 1024, decode_unicode=decode_unicode):716 717 if pending is not None:718 chunk = pending + chunk720- lines = chunk.splitlines(True)719+ lines = chunk.splitlines()720 722- for line in lines[:-1]:723- yield line.rstrip()724-725- # Save the last part of the chunk for next iteration, to keep full line together726- # lines may be empty for the last chunk of a chunked response727-728- if lines:729- pending = lines[-1]730- #if pending is a complete line, give it baack731- if pending[-1] == '\n':732- yield pending.rstrip()733- pending = None721+ # An incomplete line.722+ if lines[-1].endswith(chunk[-1]):723+ pending = lines.pop()724 else:725 pending = None726 737- # Yield the last line727+ for line in lines:728+ yield line729+730 if pending is not None:739- yield pending.rstrip()731+ yield pending732 733 @property734 def content(self):735 """Content of the response, in bytes."""63 unmodified linesmodified:tests/test_requests.py+11 added-7 removed
724 unmodified lines725 len_lines = len(lines)726 727 self.assertEqual(i, len_lines)728 729- # 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 )736 737 # Make a request and monkey-patch its contents738 r = get(httpbin('get'))739 r.raw = StringIO(quote)740 740- # 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)744 745+ joined = lines[0] + '\n' + lines[1] + '\r\n' + lines[2]746+ self.assertEqual(joined, quote)747+748 def test_safe_mode(self):749 750 safe = requests.session(config=dict(safe_mode=True))751 56 unmodified lines