mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
adding asserted_encoding check on None type encoding to match text() behavior (#3362)
This commit is contained in:
committed by
Ian Cordasco
parent
36453b95b1
commit
92fe51c0af
+12
-5
@@ -358,13 +358,20 @@ def get_encoding_from_headers(headers):
|
||||
|
||||
def stream_decode_response_unicode(iterator, r):
|
||||
"""Stream decodes a iterator."""
|
||||
encoding = r.encoding
|
||||
|
||||
if r.encoding is None:
|
||||
for item in iterator:
|
||||
yield item
|
||||
return
|
||||
if encoding is None:
|
||||
encoding = r.apparent_encoding
|
||||
|
||||
try:
|
||||
decoder = codecs.getincrementaldecoder(encoding)(errors='replace')
|
||||
except (LookupError, TypeError):
|
||||
# A LookupError is raised if the encoding was not found which could
|
||||
# indicate a misspelling or similar mistake.
|
||||
#
|
||||
# A TypeError can be raised if encoding is None
|
||||
raise UnicodeError("Unable to decode contents with encoding %s." % encoding)
|
||||
|
||||
decoder = codecs.getincrementaldecoder(r.encoding)(errors='replace')
|
||||
for chunk in iterator:
|
||||
rv = decoder.decode(chunk)
|
||||
if rv:
|
||||
|
||||
@@ -980,6 +980,13 @@ class TestRequests:
|
||||
chunks = r.iter_content(decode_unicode=True)
|
||||
assert all(isinstance(chunk, str) for chunk in chunks)
|
||||
|
||||
# check for encoding value of None
|
||||
r = requests.Response()
|
||||
r.raw = io.BytesIO(b'the content')
|
||||
r.encoding = None
|
||||
chunks = r.iter_content(decode_unicode=True)
|
||||
assert all(isinstance(chunk, str) for chunk in chunks)
|
||||
|
||||
def test_response_chunk_size_int(self):
|
||||
"""Ensure that chunk_size is passed as an integer, otherwise
|
||||
raise a TypeError.
|
||||
|
||||
Reference in New Issue
Block a user