From 92fe51c0afd9239388d2c8bb17dc46babdf7881f Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Tue, 28 Jun 2016 13:22:00 -0600 Subject: [PATCH] adding asserted_encoding check on None type encoding to match text() behavior (#3362) --- requests/utils.py | 17 ++++++++++++----- tests/test_requests.py | 7 +++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/requests/utils.py b/requests/utils.py index 8d17b6b2..62d023fa 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -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: diff --git a/tests/test_requests.py b/tests/test_requests.py index d2a2714a..4393814f 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -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.