Ticket #2560: 2560.1.patch

File 2560.1.patch, 3.8 KB (added by Ivan Sagalaev <Maniac@…>, 9 years ago)

Patch

  • django/http/__init__.py

     
    161161        if not mimetype:
    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}
    170170        self.cookies = SimpleCookie()
     
    213213        self.cookies[key]['max-age'] = 0
    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)
    219219        return content
    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
     227    def __iter__(self):
     228        self._iterator = self._container.__iter__()
     229        return self
    233230
    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
    235236
     237    def close(self):
     238        if hasattr(self._container, 'close'):
     239            self._container.close()
     240
    236241    # The remaining methods partially implement the file-like object interface.
    237242    # See http://docs.python.org/lib/bltin-file-objects.html
    238243    def write(self, content):
    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):
    244249        pass
     
    246251    def tell(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):
    252257    def __init__(self, redirect_to):
  • django/core/handlers/wsgi.py

     
    163163        for c in response.cookies.values():
    164164            response_headers.append(('Set-Cookie', c.output(header='')))
    165165        start_response(status, response_headers)
    166         return response.iterator
     166        return response # response is a WSGI-compilant iterator
  • django/core/handlers/modpython.py

     
    155155    for c in http_response.cookies.values():
    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):
    162165    # mod_python hooks into this function.
Back to Top