Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #1569 -- Made streaming HttpResponse unicode-aware

git-svn-id: http://code.djangoproject.com/svn/django/trunk@2675 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 63e7f205952f6759774fc1e3caaaa28a56ae9da8 1 parent a0485f5
Adrian Holovaty authored April 11, 2006

Showing 1 changed file with 15 additions and 6 deletions. Show diff stats Hide diff stats

  1. 21  django/utils/httpwrappers.py
21  django/utils/httpwrappers.py
@@ -151,10 +151,10 @@ def __init__(self, content='', mimetype=None):
151 151
         if not mimetype:
152 152
             mimetype = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, settings.DEFAULT_CHARSET)
153 153
         if hasattr(content, '__iter__'):
154  
-            self.iterator = content
  154
+            self._iterator = content
155 155
             self._is_string = False
156 156
         else:
157  
-            self.iterator = [content]
  157
+            self._iterator = [content]
158 158
             self._is_string = True
159 159
         self.headers = {'Content-Type': mimetype}
160 160
         self.cookies = SimpleCookie()
@@ -200,23 +200,32 @@ def delete_cookie(self, key):
200 200
             pass
201 201
 
202 202
     def _get_content(self):
203  
-        content = ''.join(self.iterator)
  203
+        content = ''.join(self._iterator)
204 204
         if isinstance(content, unicode):
205 205
             content = content.encode(self._charset)
206 206
         return content
207 207
 
208 208
     def _set_content(self, value):
209  
-        self.iterator = [value]
  209
+        self._iterator = [value]
210 210
         self._is_string = True
211 211
 
212 212
     content = property(_get_content, _set_content)
213 213
 
  214
+    def _get_iterator(self):
  215
+        "Output iterator. Converts data into client charset if necessary."
  216
+        for chunk in self._iterator:
  217
+            if isinstance(chunk, unicode):
  218
+                chunk = chunk.encode(self._charset)
  219
+            yield chunk
  220
+
  221
+    iterator = property(_get_iterator)
  222
+
214 223
     # The remaining methods partially implement the file-like object interface.
215 224
     # See http://docs.python.org/lib/bltin-file-objects.html
216 225
     def write(self, content):
217 226
         if not self._is_string:
218 227
             raise Exception, "This %s instance is not writable" % self.__class__
219  
-        self.iterator.append(content)
  228
+        self._iterator.append(content)
220 229
 
221 230
     def flush(self):
222 231
         pass
@@ -224,7 +233,7 @@ def flush(self):
224 233
     def tell(self):
225 234
         if not self._is_string:
226 235
             raise Exception, "This %s instance cannot tell its position" % self.__class__
227  
-        return sum([len(chunk) for chunk in self.iterator])
  236
+        return sum([len(chunk) for chunk in self._iterator])
228 237
 
229 238
 class HttpResponseRedirect(HttpResponse):
230 239
     def __init__(self, redirect_to):

0 notes on commit 63e7f20

Please sign in to comment.
Something went wrong with that request. Please try again.