Skip to content

Commit

Permalink
_detected_encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Reitz committed Mar 9, 2012
1 parent 9451ae2 commit 253cba3
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ def generate_chunked():

return gen

def iter_lines(self, chunk_size=10 * 1024, decode_unicode=True):
def iter_lines(self, chunk_size=10 * 1024, decode_unicode=None):
"""Iterates over the response data, one line at a time. This
avoids reading the content at once into memory for large
responses.
Expand Down Expand Up @@ -756,6 +756,16 @@ def content(self):
self._content_consumed = True
return self._content

def _detected_encoding(self):
try:
detected = chardet.detect(self.content) or {}
return detected.get('encoding')

# Trust that chardet isn't available or something went terribly wrong.
except Exception:
pass


@property
def text(self):
"""Content of the response, in unicode.
Expand All @@ -770,13 +780,7 @@ def text(self):

# Fallback to auto-detected encoding if chardet is available.
if self.encoding is None:
try:
detected = chardet.detect(self.content) or {}
encoding = detected.get('encoding')

# Trust that chardet isn't available or something went terribly wrong.
except Exception:
pass
encoding = self._detected_encoding()

# Decode unicode from given encoding.
try:
Expand Down

0 comments on commit 253cba3

Please sign in to comment.