mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
Merge pull request #1937 from jaraco/master
Improved decoding support for Response.iter_content and iter_lines
This commit is contained in:
+11
-7
@@ -616,11 +616,10 @@ class Response(object):
|
||||
large responses. The chunk size is the number of bytes it should
|
||||
read into memory. This is not necessarily the length of each item
|
||||
returned as decoding can take place.
|
||||
"""
|
||||
if self._content_consumed:
|
||||
# simulate reading small chunks of the content
|
||||
return iter_slices(self._content, chunk_size)
|
||||
|
||||
If decode_unicode is True, content will be decoded using the best
|
||||
available encoding based on the response.
|
||||
"""
|
||||
def generate():
|
||||
try:
|
||||
# Special case for urllib3.
|
||||
@@ -641,12 +640,17 @@ class Response(object):
|
||||
|
||||
self._content_consumed = True
|
||||
|
||||
gen = generate()
|
||||
# simulate reading small chunks of the content
|
||||
reused_chunks = iter_slices(self._content, chunk_size)
|
||||
|
||||
stream_chunks = generate()
|
||||
|
||||
chunks = reused_chunks if self._content_consumed else stream_chunks
|
||||
|
||||
if decode_unicode:
|
||||
gen = stream_decode_response_unicode(gen, self)
|
||||
chunks = stream_decode_response_unicode(chunks, self)
|
||||
|
||||
return gen
|
||||
return chunks
|
||||
|
||||
def iter_lines(self, chunk_size=ITER_CHUNK_SIZE, decode_unicode=None):
|
||||
"""Iterates over the response data, one line at a time. When
|
||||
|
||||
@@ -10,6 +10,7 @@ import pickle
|
||||
import unittest
|
||||
import collections
|
||||
|
||||
import io
|
||||
import requests
|
||||
import pytest
|
||||
from requests.adapters import HTTPAdapter
|
||||
@@ -705,6 +706,26 @@ class RequestsTestCase(unittest.TestCase):
|
||||
assert next(iter(r))
|
||||
io.close()
|
||||
|
||||
def test_response_decode_unicode(self):
|
||||
"""
|
||||
When called with decode_unicode, Response.iter_content should always
|
||||
return unicode.
|
||||
"""
|
||||
r = requests.Response()
|
||||
r._content_consumed = True
|
||||
r._content = b'the content'
|
||||
r.encoding = 'ascii'
|
||||
|
||||
chunks = r.iter_content(decode_unicode=True)
|
||||
assert all(isinstance(chunk, str) for chunk in chunks)
|
||||
|
||||
# also for streaming
|
||||
r = requests.Response()
|
||||
r.raw = io.BytesIO(b'the content')
|
||||
r.encoding = 'ascii'
|
||||
chunks = r.iter_content(decode_unicode=True)
|
||||
assert all(isinstance(chunk, str) for chunk in chunks)
|
||||
|
||||
def test_request_and_response_are_pickleable(self):
|
||||
r = requests.get(httpbin('get'))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user