From cd10158789a762d825e80cbd00ca612eacaae61f Mon Sep 17 00:00:00 2001 From: Benjamin Moody Date: Fri, 10 Nov 2023 15:09:24 -0500 Subject: [PATCH] Document that Response.iter_lines is broken. This method is broken in a couple of significant ways: - If delimiter is None, and if one chunk happens to end with \r and the following chunk begins with \n, this method will yield a bogus blank line at that point. - If delimiter is a string, and if a chunk happens to end with that string, this method will yield a bogus blank line at that point. This is broken both with and without decode_unicode=True, and it is broken both with and without stream=True. (Setting stream=True makes the problem worse, because then the chunk boundaries are partially dependent on server or network I/O chunking.) Unless and until this behavior is fixed, this method should not be used. --- src/requests/models.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/requests/models.py b/src/requests/models.py index 44556394ec..6ccb76489c 100644 --- a/src/requests/models.py +++ b/src/requests/models.py @@ -852,9 +852,14 @@ def generate(): def iter_lines( self, chunk_size=ITER_CHUNK_SIZE, decode_unicode=False, delimiter=None ): - """Iterates over the response data, one line at a time. When - stream=True is set on the request, this avoids reading the - content at once into memory for large responses. + """Iterates over the response data, one line at a time. + + This method is broken and should not be used. It inserts fake + blank lines in unpredictable locations. Furthermore, if + stream=True is set on the request, the results may not be + reproducible. However, the behavior of this method is frozen + and cannot be fixed while preserving strict backward + compatibility. This behavior should be fixed in Requests 3.0. .. note:: This method is not reentrant safe. """