Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2560 -- Add close() support to HttpResponse iterators. Thanks,…

… Ivan

Sagalaev.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3791 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c3d7aad6d0911bb84120d7c8d41923c895784b21 1 parent dc39762
Malcolm Tredinnick authored September 22, 2006
7  django/core/handlers/modpython.py
@@ -155,8 +155,11 @@ def populate_apache_request(http_response, mod_python_req):
155 155
     for c in http_response.cookies.values():
156 156
         mod_python_req.headers_out.add('Set-Cookie', c.output(header=''))
157 157
     mod_python_req.status = http_response.status_code
158  
-    for chunk in http_response.iterator:
159  
-        mod_python_req.write(chunk)
  158
+    try:
  159
+        for chunk in http_response:
  160
+            mod_python_req.write(chunk)
  161
+    finally:
  162
+        http_response.close()
160 163
 
161 164
 def handler(req):
162 165
     # mod_python hooks into this function.
2  django/core/handlers/wsgi.py
@@ -163,4 +163,4 @@ def __call__(self, environ, start_response):
163 163
         for c in response.cookies.values():
164 164
             response_headers.append(('Set-Cookie', c.output(header='')))
165 165
         start_response(status, response_headers)
166  
-        return response.iterator
  166
+        return response
31  django/http/__init__.py
@@ -161,10 +161,10 @@ def __init__(self, content='', mimetype=None):
161 161
         if not mimetype:
162 162
             mimetype = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, settings.DEFAULT_CHARSET)
163 163
         if hasattr(content, '__iter__'):
164  
-            self._iterator = content
  164
+            self._container = content
165 165
             self._is_string = False
166 166
         else:
167  
-            self._iterator = [content]
  167
+            self._container = [content]
168 168
             self._is_string = True
169 169
         self.headers = {'Content-Type': mimetype}
170 170
         self.cookies = SimpleCookie()
@@ -213,32 +213,37 @@ def delete_cookie(self, key, path='/', domain=None):
213 213
         self.cookies[key]['max-age'] = 0
214 214
 
215 215
     def _get_content(self):
216  
-        content = ''.join(self._iterator)
  216
+        content = ''.join(self._container)
217 217
         if isinstance(content, unicode):
218 218
             content = content.encode(self._charset)
219 219
         return content
220 220
 
221 221
     def _set_content(self, value):
222  
-        self._iterator = [value]
  222
+        self._container = [value]
223 223
         self._is_string = True
224 224
 
225 225
     content = property(_get_content, _set_content)
226 226
 
227  
-    def _get_iterator(self):
228  
-        "Output iterator. Converts data into client charset if necessary."
229  
-        for chunk in self._iterator:
230  
-            if isinstance(chunk, unicode):
231  
-                chunk = chunk.encode(self._charset)
232  
-            yield chunk
  227
+    def __iter__(self):
  228
+        self._iterator = self._container.__iter__()
  229
+        return self
233 230
 
234  
-    iterator = property(_get_iterator)
  231
+    def next(self):
  232
+        chunk = self._iterator.next()
  233
+        if isinstance(chunk, unicode):
  234
+            chunk = chunk.encode(self._charset)
  235
+        return chunk
  236
+
  237
+    def close(self):
  238
+        if hasattr(self._container, 'close'):
  239
+            self._container.close()
235 240
 
236 241
     # The remaining methods partially implement the file-like object interface.
237 242
     # See http://docs.python.org/lib/bltin-file-objects.html
238 243
     def write(self, content):
239 244
         if not self._is_string:
240 245
             raise Exception, "This %s instance is not writable" % self.__class__
241  
-        self._iterator.append(content)
  246
+        self._container.append(content)
242 247
 
243 248
     def flush(self):
244 249
         pass
@@ -246,7 +251,7 @@ def flush(self):
246 251
     def tell(self):
247 252
         if not self._is_string:
248 253
             raise Exception, "This %s instance cannot tell its position" % self.__class__
249  
-        return sum([len(chunk) for chunk in self._iterator])
  254
+        return sum([len(chunk) for chunk in self._container])
250 255
 
251 256
 class HttpResponseRedirect(HttpResponse):
252 257
     def __init__(self, redirect_to):

0 notes on commit c3d7aad

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