Django

Code

Changeset 3791

Show
Ignore:
Timestamp:
09/22/06 07:32:00 (2 years ago)
Author:
mtredinnick
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/handlers/modpython.py

    r3414 r3791  
    156156        mod_python_req.headers_out.add('Set-Cookie', c.output(header='')) 
    157157    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() 
    160163 
    161164def handler(req): 
  • django/trunk/django/core/handlers/wsgi.py

    r3411 r3791  
    164164            response_headers.append(('Set-Cookie', c.output(header=''))) 
    165165        start_response(status, response_headers) 
    166         return response.iterator 
     166        return response 
  • django/trunk/django/http/__init__.py

    r3545 r3791  
    162162            mimetype = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, settings.DEFAULT_CHARSET) 
    163163        if hasattr(content, '__iter__'): 
    164             self._iterator = content 
     164            self._container = content 
    165165            self._is_string = False 
    166166        else: 
    167             self._iterator = [content] 
     167            self._container = [content] 
    168168            self._is_string = True 
    169169        self.headers = {'Content-Type': mimetype} 
     
    214214 
    215215    def _get_content(self): 
    216         content = ''.join(self._iterator) 
     216        content = ''.join(self._container) 
    217217        if isinstance(content, unicode): 
    218218            content = content.encode(self._charset) 
     
    220220 
    221221    def _set_content(self, value): 
    222         self._iterator = [value] 
     222        self._container = [value] 
    223223        self._is_string = True 
    224224 
    225225    content = property(_get_content, _set_content) 
    226226 
    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 
    233  
    234     iterator = property(_get_iterator) 
     227    def __iter__(self): 
     228        self._iterator = self._container.__iter__() 
     229        return self 
     230 
     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() 
    235240 
    236241    # The remaining methods partially implement the file-like object interface. 
     
    239244        if not self._is_string: 
    240245            raise Exception, "This %s instance is not writable" % self.__class__ 
    241         self._iterator.append(content) 
     246        self._container.append(content) 
    242247 
    243248    def flush(self): 
     
    247252        if not self._is_string: 
    248253            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]) 
    250255 
    251256class HttpResponseRedirect(HttpResponse):