Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISO-8859-1 fallback for reason decoding #3554

Merged
merged 2 commits into from Sep 5, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion requests/models.py
Expand Up @@ -848,7 +848,10 @@ def raise_for_status(self):

http_error_msg = ''
if isinstance(self.reason, bytes):
reason = self.reason.decode('utf-8', 'ignore')
try:
reason = self.reason.decode('utf-8')
except UnicodeDecodeError:
reason = self.reason.decode('iso-8859-1')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add comments to the start of this block explaining why we're doing this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! Comment added.

else:
reason = self.reason

Expand Down
12 changes: 12 additions & 0 deletions tests/test_requests.py
Expand Up @@ -1022,6 +1022,18 @@ def test_response_reason_unicode(self):
r.encoding = None
assert not r.ok # old behaviour - crashes here

def test_response_reason_unicode_fallback(self):
# check raise_status falls back to ISO-8859-1
r = requests.Response()
r.url = 'some url'
reason = u'Komponenttia ei löydy'
r.reason = reason.encode('latin-1')
r.status_code = 500
r.encoding = None
with pytest.raises(requests.exceptions.HTTPError) as e:
r.raise_for_status()
assert reason in str(e)

def test_response_chunk_size_type(self):
"""Ensure that chunk_size is passed as None or an integer, otherwise
raise a TypeError.
Expand Down