From: Kenneth Reitz Date: Wed, 14 Dec 2011 15:37:40 +0000 (-0500) Subject: iter_lines! X-Git-Tag: v0.8.5~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6fb26744466e38ec701d75ebf3ce3fe1cc5469ca;p=services%2Fpython-requests.git iter_lines! --- diff --git a/requests/models.py b/requests/models.py index 567d1f6..78af22c 100644 --- a/requests/models.py +++ b/requests/models.py @@ -572,6 +572,52 @@ class Response(object): return gen + def iter_lines(self, newlines=None, decode_unicode=None): + """Iterates over the response data, one line at a time. This + avoids reading the content at once into memory for large + responses. + + :param newlines: a collection of bytes to seperate lines with. + """ + + if newlines is None: + newlines = ('\r', '\n', '\r\n') + + if self._content_consumed: + raise RuntimeError( + 'The content for this response was already consumed' + ) + + def generate(): + chunk = [] + + while 1: + c = self.raw.read(1) + if not c: + break + + if c in newlines: + yield ''.join(chunk) + chunk = [] + else: + chunk.append(c) + + self._content_consumed = True + + gen = generate() + + if 'gzip' in self.headers.get('content-encoding', ''): + gen = stream_decode_gzip(gen) + + if decode_unicode is None: + decode_unicode = self.config.get('decode_unicode') + + if decode_unicode: + gen = stream_decode_response_unicode(gen, self) + + return gen + + @property def content(self): """Content of the response, in bytes or unicode