diff --git a/requests/utils.py b/requests/utils.py index e37b9109..dfeb77d9 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -367,20 +367,13 @@ def get_encoding_from_headers(headers): def stream_decode_response_unicode(iterator, r): """Stream decodes a iterator.""" - encoding = r.encoding - 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) + if r.encoding is None: + for item in iterator: + yield item + return + decoder = codecs.getincrementaldecoder(r.encoding)(errors='replace') for chunk in iterator: rv = decoder.decode(chunk) if rv: diff --git a/tests/test_requests.py b/tests/test_requests.py index 3d959a4b..efbc7ed0 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -973,13 +973,6 @@ 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_reason_unicode(self): # check for unicode HTTP status r = requests.Response()