Skip to content
Myr

raise a ValueError if no encoding was sent and chardet is unavailable

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

authored by Shivaram Lingamneni · committed by Shivaram Lingamneni · parent 57ccd7e · Browse files · 1 file+8 added−12 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+8 added−12 removed

  • other:+8 added−12 removed

1 file+8 added−12 removed

modified:requests/models.py+8 added-12 removed
35 unmodified lines
3637# Import chardet if it is available.38try:39 import chardet40 # hush pyflakes41 chardet42except ImportError:41 pass43 chardet = None4445REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)46CONTENT_CHUNK_SIZE = 10 * 102447
733 unmodified lines
781782 self._content_consumed = True783 return self._content784783 def _detected_encoding(self):784 try:785 detected = chardet.detect(self.content) or {}786 return detected.get('encoding')787788 # Trust that chardet isn't available or something went terribly wrong.789 except Exception:790 pass791785 @property786 def text(self):787 """Content of the response, in unicode.788
4 unmodified lines
793 # Try charset from content-type794 content = None795 encoding = self.encoding796804 # Fallback to auto-detected encoding if chardet is available.797 # Fallback to auto-detected encoding.798 if self.encoding is None:806 encoding = self._detected_encoding()799 if chardet is not None:800 encoding = chardet.detect(self.content)['encoding']801 else:802 raise ValueError("Can't detect page encoding.")803804 # Decode unicode from given encoding.805 try:806 content = str(self.content, encoding, errors='replace')
31 unmodified lines