Skip to content
Myr

Use chardet for detection when no encoding is provided.

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

authored by Kenneth Reitz · committed by Kenneth Reitz · parent 47e96b8 · Browse files · 1 file+33 added−9 removed
This commit

State

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

Diff stats

1 file+33 added−9 removed

  • other:+33 added−9 removed

1 file+33 added−9 removed

modified:requests/models.py+33 added-9 removed
29 unmodified lines
30from .utils import (31 get_encoding_from_headers, stream_decode_response_unicode,32 stream_decompress, guess_filename, requote_path)3334# Import chardet if it is available.35try:36 import chardet37except ImportError:38 pass3940REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)4142
678 unmodified lines
721722723 @property724 def text(self):720 """Content of the response, in unicode."""725 """Content of the response, in unicode.726727 if Response.encoding is None and chardet module is available, encoding728 will be guessed.729 """730731 # Try charset from content-type732 content = None733 encoding = self.encoding734725 if self.encoding:726 try:727 content = unicode(self.content, self.encoding)728 except UnicodeError:729 pass735 # Fallback to auto-detected encoding if chardet is available.736 if self.encoding is None:737731 # Try to fall back:738 try:733 content = unicode(content, self.encoding, errors='replace')734 except TypeError:739 detected = chardet.detect(self.content) or {}740 encoding = detected.get('encoding')741742 # Trust that chardet isn't available or something went terribly wrong.743 except Exception:744 pass745746747 # Decode unicode from given encoding.748 try:749 content = unicode(self.content, encoding)750 except UnicodeError, TypeError:751 pass752753 # Try to fall back:754 try:755 content = unicode(content, encoding, errors='replace')756 except UnicodeError, TypeError:757 pass758759760761 return content762763764 def raise_for_status(self):
15 unmodified lines