Skip to content
Myr

Refactor r.iter_lines to retrieve more than one byte at a time.

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

Fixes universal line ending handling, but takes away ability to specyfy custom line endings.

authored by Chase Sterling · committed by Chase Sterling · parent e3288d5 · Browse files · 1 file+13 added−22 removed
This commit

State

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

Diff stats

1 file+13 added−22 removed

  • other:+13 added−22 removed

1 file+13 added−22 removed

modified:requests/models.py+13 added-22 removed
7 unmodified lines
8"""910import os11import urllib12import zlib1213from urlparse import urlparse, urlunparse, urljoin, urlsplit14from datetime import datetime15
609 unmodified lines
625626 return gen627628630 def iter_lines(self, newlines=None, decode_unicode=None):629 def iter_lines(self, chunk_size=10 * 1024, decode_unicode=None):630 """Iterates over the response data, one line at a time. This631 avoids reading the content at once into memory for large632 responses.634635 :param newlines: a collection of bytes to seperate lines with.633 """634638 if newlines is None:639 newlines = ('\r', '\n', '\r\n')635 pending = None636 for chunk in self.iter_content(chunk_size, decode_unicode=decode_unicode):637 if pending is not None:638 chunk = pending + chunk639 lines = chunk.splitlines()640 for line in lines[:-1]:641 yield line642 # Save the last part of the chunk for next iteration, to keep full line together643 pending = lines[-1]644641 chunk = []645 # Yield the last line646 if pending is not None:647 yield pending648643 for c in self.iter_content(1, decode_unicode=decode_unicode):644 if not c:645 break649647 if c in newlines:648 yield ''.join(chunk)649 chunk = []650 else:651 chunk.append(c)652653 # Yield the remainder, in case the response654 # did not terminate with a newline655 if chunk:656 yield ''.join(chunk)657658650 @property651 def content(self):652 """Content of the response, in bytes or unicode653 (if available).
52 unmodified lines