Skip to content
Myr

fix iteration on null responses in safe_mode

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

authored by Ori Livneh · committed by Ori Livneh · parent a7223f8 · Browse files · 2 files+33 added−19 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+33 added−19 removed

  • other:+33 added−19 removed

2 files+33 added−19 removed

modified:requests/models.py+22 added-19 removed
557 unmodified lines
558 'The content for this response was already consumed'559 )560561 def generate():562 # self.raw can be None if we're in safe_mode and the request failed563 if self.raw is not None:562 while 1:563 chunk = self.raw.read(chunk_size)564 if not chunk:565 break566 yield chunk564 while 1:565 chunk = self.raw.read(chunk_size)566 if not chunk:567 break568 yield chunk569 self._content_consumed = True570571 gen = generate()572
25 unmodified lines
598 'The content for this response was already consumed'599 )600601 def generate():602 if self.raw is not None:600 chunk = []603 chunk = []604602 while 1:603 c = self.raw.read(1)604 if not c:605 break605 while 1:606 c = self.raw.read(1)607 if not c:608 break609607 if c in newlines:610 if c in newlines:611 yield ''.join(chunk)612 chunk = []613 else:614 chunk.append(c)615616 # Yield the remainder, in case the response617 # did not terminate with a newline618 if chunk:619 yield ''.join(chunk)609 chunk = []610 else:611 chunk.append(c)620613 # Yield the remainder, in case the response614 # did not terminate with a newline615 if chunk:616 yield ''.join(chunk)617621 self._content_consumed = True622623 gen = generate()624
75 unmodified lines
modified:test_requests.py+11 added
617 unmodified lines
618 # Make sure iter_lines doesn't chop the trailing bit619 lines = '\n'.join(r.iter_lines())620 self.assertEqual(lines, quote)621622 def test_null_response(self):623624 # Safe mode creates empty responses for failed requests.625626 # Iterating on these responses should produce empty sequences627 r = requests.get('http://_/', config=dict(safe_mode=True))628 self.assertEquals(list(r.iter_lines()), [])629630 r = requests.get('http://_/', config=dict(safe_mode=True))631 self.assertEquals(list(r.iter_content()), [])632633 def test_timeout(self):634635 # When not in safe mode, should raise Timeout exception636 with self.assertRaises(requests.exceptions.Timeout):
10 unmodified lines