New bytes/unicode separation.
authorKenneth Reitz <me@kennethreitz.com>
Sat, 21 Jan 2012 06:24:21 +0000 (01:24 -0500)
committerKenneth Reitz <me@kennethreitz.com>
Sat, 21 Jan 2012 06:24:21 +0000 (01:24 -0500)
Response.content for bytes.
New Response.text for unicode.

requests/models.py

index f407751..5da4d7e 100644 (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
+        self._content_consumed = True
+        return self._content
 
-        # Decode unicode content.
-        if self.config.get('decode_unicode'):
 
-            # Try charset from content-type
+    @property
+    def text(self):
+        """Content of the response, in unicode."""
 
-            if self.encoding:
-                try:
-                    content = unicode(content, self.encoding)
-                except UnicodeError:
-                    pass
+        # Try charset from content-type
 
-            # Fall back:
-            try:
-                content = unicode(content, self.encoding, errors='replace')
-            except TypeError:
-                pass
+        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
 
-        self._content_consumed = True
         return content