Skip to content
Myr

Merge pull request #831 from slingamn/permissive_itercontent_2

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

permissive implementation of iter_content

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 1175866, 77cf995 · Browse files · 3 files+27 added−4 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+27 added−4 removed

  • tests:+18 added−0 removed
  • other:+9 added−4 removed

3 files+27 added−4 removed

modified:requests/models.py+3 added-4 removed
29 unmodified lines
30 URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL)31from .utils import (32 get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri,33 stream_decode_response_unicode, get_netrc_auth, get_environ_proxies,34 to_key_val_list, DEFAULT_CA_BUNDLE_PATH, parse_header_links)34 to_key_val_list, DEFAULT_CA_BUNDLE_PATH, parse_header_links, iter_slices)35from .compat import (36 cookielib, urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes,37 StringIO, is_py2, chardet, json, builtin_str, numeric_types)38
690 unmodified lines
729 of bytes it should read into memory. This is not necessarily the730 length of each item returned as decoding can take place.731 """732 if self._content_consumed:733 raise RuntimeError(734 'The content for this response was already consumed'735 )733 # simulate reading small chunks of the content734 return iter_slices(self._content, chunk_size)735736 def generate():737 while 1:738 chunk = self.raw.read(chunk_size)
146 unmodified lines
modified:requests/utils.py+6 added
358 unmodified lines
359 rv = decoder.decode('', final=True)360 if rv:361 yield rv362363def iter_slices(string, slice_length):364 """Iterate over slices of a string."""365 pos = 0366 while pos < len(string):367 yield string[pos:pos+slice_length]368 pos += slice_length369370def get_unicode_from_response(r):371 """Returns the requested content back in unicode.372
187 unmodified lines
modified:tests/test_requests.py+18 added
922 unmodified lines
923924 joined = lines[0] + '\n' + lines[1] + '\r\n' + lines[2]925 self.assertEqual(joined, quote)926927 def test_permissive_iter_content(self):928 """Test that iter_content and iter_lines work even after the body has been fetched."""929 r = get(httpbin('stream', '10'), prefetch=True)930 assert r._content_consumed931 # iter_lines should still work without crashing932 self.assertEqual(len(list(r.iter_lines())), 10)933934 # iter_content should return a one-item iterator over the whole content935 iter_content_list = list(r.iter_content(chunk_size=1))936 self.assertTrue(all(len(item) == 1 for item in iter_content_list))937 # when joined, it should be exactly the original content938 self.assertEqual(bytes().join(iter_content_list), r.content)939940 # test different chunk sizes:941 for chunk_size in range(2, 20):942 self.assertEqual(bytes().join(r.iter_content(chunk_size=chunk_size)), r.content)943944945 # def test_safe_mode(self):946947 # safe = requests.session(config=dict(safe_mode=True))948
196 unmodified lines