New bytes/unicode separation.

Response.content for bytes.
New Response.text for unicode.
This commit is contained in:
Kenneth Reitz
2012-01-21 01:24:21 -05:00
parent 6d071f4331
commit 49a38ac89a
+26 -28
View File
@@ -528,7 +528,7 @@ class Request(object):
if prefetch:
# Save the response.
self.response.content
if self.config.get('danger_mode'):
self.response.raise_for_status()
@@ -598,7 +598,7 @@ class Response(object):
return True
def iter_content(self, chunk_size=10 * 1024, decode_unicode=None):
def iter_content(self, chunk_size=10 * 1024, decode_unicode=False):
"""Iterates over the response data. This avoids reading the content
at once into memory for large responses. The chunk size is the number
of bytes it should read into memory. This is not necessarily the
@@ -616,7 +616,7 @@ class Response(object):
break
yield chunk
self._content_consumed = True
def generate_chunked():
resp = self.raw._original_response
fp = resp.fp
@@ -652,9 +652,6 @@ class Response(object):
elif 'deflate' in self.headers.get('content-encoding', ''):
gen = stream_decompress(gen, mode='deflate')
if decode_unicode is None:
decode_unicode = self.config.get('decode_unicode')
if decode_unicode:
gen = stream_decode_response_unicode(gen, self)
@@ -692,9 +689,7 @@ class Response(object):
@property
def content(self):
"""Content of the response, in bytes or unicode
(if available).
"""
"""Content of the response, in bytes."""
if self._content is None:
# Read the contents.
@@ -707,26 +702,29 @@ class Response(object):
except AttributeError:
self._content = None
content = self._content
# Decode unicode content.
if self.config.get('decode_unicode'):
# Try charset from content-type
if self.encoding:
try:
content = unicode(content, self.encoding)
except UnicodeError:
pass
# Fall back:
try:
content = unicode(content, self.encoding, errors='replace')
except TypeError:
pass
self._content_consumed = True
return self._content
@property
def text(self):
"""Content of the response, in unicode."""
# Try charset from content-type
content = u''
try:
content = unicode(self.content, self.encoding)
except UnicodeError:
pass
# Try to fall back:
try:
content = unicode(content, self.encoding, errors='replace')
except TypeError:
pass
return content