Skip to content
Myr

adding asserted_encoding check on None type encoding to match text() behavior (#3362)

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

authored by Nate Prewitt · committed by Ian Cordasco · parent 36453b9 · Browse files · 2 files+19 added−5 removed
This commit

State

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

Diff stats

2 files+19 added−5 removed

  • tests:+7 added−0 removed
  • other:+12 added−5 removed

2 files+19 added−5 removed

modified:requests/utils.py+12 added-5 removed
356 unmodified lines
357358359def stream_decode_response_unicode(iterator, r):360 """Stream decodes a iterator."""361 encoding = r.encoding362362 if r.encoding is None:363 for item in iterator:364 yield item365 return363 if encoding is None:364 encoding = r.apparent_encoding365366 try:367 decoder = codecs.getincrementaldecoder(r.encoding)(errors='replace')367 decoder = codecs.getincrementaldecoder(encoding)(errors='replace')368 except (LookupError, TypeError):369 # A LookupError is raised if the encoding was not found which could370 # indicate a misspelling or similar mistake.371 #372 # A TypeError can be raised if encoding is None373 raise UnicodeError("Unable to decode contents with encoding %s." % encoding)374375 for chunk in iterator:376 rv = decoder.decode(chunk)377 if rv:378 yield rv
370 unmodified lines
modified:tests/test_requests.py+7 added
978 unmodified lines
979 r.encoding = 'ascii'980 chunks = r.iter_content(decode_unicode=True)981 assert all(isinstance(chunk, str) for chunk in chunks)982983 # check for encoding value of None984 r = requests.Response()985 r.raw = io.BytesIO(b'the content')986 r.encoding = None987 chunks = r.iter_content(decode_unicode=True)988 assert all(isinstance(chunk, str) for chunk in chunks)989990 def test_response_chunk_size_int(self):991 """Ensure that chunk_size is passed as an integer, otherwise992 raise a TypeError.993 """
725 unmodified lines