Skip to content
Myr

Merge pull request #2478 from plaes/iter-lines

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

Document Response.iter_lines() deficiencies

authored by Cory Benfield · committed by Cory Benfield · parents c53aa08, 4497a26 · Browse files · 3 files+32 added−0 removed
This commit

State

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

Diff stats

3 files+32 added−0 removed

  • other:+32 added−0 removed

3 files+32 added−0 removed

modified:docs/user/advanced.rst+13 added
397 unmodified lines
398 # filter out keep-alive new lines399 if line:400 print(json.loads(line))401402.. warning::403404 :class:`~requests.Response.iter_lines()` is not reentrant safe.405 Calling this method multiple times causes some of the received data406 being lost. In case you need to call it from multiple places, use407 the resulting iterator object instead::408409 lines = r.iter_lines()410 # Save the first line for later or just skip it411 first_line = next(lines)412 for line in lines:413 print(line)414415.. _proxies:416417Proxies418-------
393 unmodified lines
modified:requests/models.py+2 added
686 unmodified lines
687 def iter_lines(self, chunk_size=ITER_CHUNK_SIZE, decode_unicode=None, delimiter=None):688 """Iterates over the response data, one line at a time. When689 stream=True is set on the request, this avoids reading the690 content at once into memory for large responses.691692 .. note:: This method is not reentrant safe.693 """694695 pending = None696
145 unmodified lines
modified:test_requests.py+17 added
1050 unmodified lines
1051 assert r.status_code == 2001052 assert 'application/json' in r.request.headers['Content-Type']1053 assert {'life': 42} == r.json()['json']10541055 def test_response_iter_lines(self):1056 r = requests.get(httpbin('stream/4'), stream=True)1057 assert r.status_code == 20010581059 it = r.iter_lines()1060 next(it)1061 assert len(list(it)) == 310621063 @pytest.mark.xfail1064 def test_response_iter_lines_reentrant(self):1065 """Response.iter_lines() is not reentrant safe"""1066 r = requests.get(httpbin('stream/4'), stream=True)1067 assert r.status_code == 20010681069 next(r.iter_lines())1070 assert len(list(r.iter_lines())) == 3107110721073class TestContentEncodingDetection(unittest.TestCase):10741075 def test_none(self):1076 encodings = requests.utils.get_encodings_from_content('')
563 unmodified lines